blob: c2623d020cf6f281be18e4cc024a602455fe2a72 [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
Chris Lattnerda4d4692005-04-09 03:22:37 +000058 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000059 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
60
61 setSetCCResultType(MVT::i1);
62 setShiftAmountType(MVT::i64);
63
64 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000065
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:
Duraid Madinaca494fd2005-04-12 14:54:44 +0000186//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
187 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000188 // floating point args go into f8..f15 as-needed, the increment
189 argVreg[count] = // is below..:
190 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
191 // FP args go into f8..f15 as needed: (hence the ++)
192 argPreg[count] = args_FP[used_FPArgs++];
193 argOpc[count] = IA64::FMOV;
194 argt = newroot = DAG.getCopyFromReg(argVreg[count],
195 getValueType(I->getType()), DAG.getRoot());
196 break;
197 case MVT::i1: // NOTE: as far as C abi stuff goes,
198 // bools are just boring old ints
199 case MVT::i8:
200 case MVT::i16:
201 case MVT::i32:
202 case MVT::i64:
Duraid Madinaca494fd2005-04-12 14:54:44 +0000203//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
204 MF.addLiveIn(args_int[count]); // mark this register as liveIn
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000205 argVreg[count] =
206 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
207 argPreg[count] = args_int[count];
208 argOpc[count] = IA64::MOV;
209 argt = newroot =
210 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
211 if ( getValueType(I->getType()) != MVT::i64)
212 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
213 newroot);
214 break;
215 }
216 } else { // more than 8 args go into the frame
217 // Create the frame index object for this incoming parameter...
Duraid Madinabeeaab22005-03-31 12:31:11 +0000218 ArgOffset = 16 + 8 * (count - 8);
219 int FI = MFI->CreateFixedObject(8, ArgOffset);
220
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000221 // Create the SelectionDAG nodes corresponding to a load
222 //from this parameter
223 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
224 argt = newroot = DAG.getLoad(getValueType(I->getType()),
225 DAG.getEntryNode(), FIN);
226 }
227 ++count;
228 DAG.setRoot(newroot.getValue(1));
229 ArgValues.push_back(argt);
230 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000231
232
233 // Create a vreg to hold the output of (what will become)
234 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000235 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
236 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
237 // we create a PSEUDO_ALLOC (pseudo)instruction for now
238
239 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
240
241 // hmm:
242 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
243 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
244 // ..hmm.
245
246 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
247
248 // hmm:
249 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
250 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
251 // ..hmm.
252
Duraid Madinabeeaab22005-03-31 12:31:11 +0000253 unsigned tempOffset=0;
254
255 // if this is a varargs function, we simply lower llvm.va_start by
256 // pointing to the first entry
257 if(F.isVarArg()) {
258 tempOffset=0;
259 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000260 }
261
Duraid Madinabeeaab22005-03-31 12:31:11 +0000262 // here we actually do the moving of args, and store them to the stack
263 // too if this is a varargs function:
264 for (int i = 0; i < count && i < 8; ++i) {
265 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
266 if(F.isVarArg()) {
267 // if this is a varargs function, we copy the input registers to the stack
268 int FI = MFI->CreateFixedObject(8, tempOffset);
269 tempOffset+=8; //XXX: is it safe to use r22 like this?
270 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
271 // FIXME: we should use st8.spill here, one day
272 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
273 }
274 }
275
Duraid Madinaca494fd2005-04-12 14:54:44 +0000276 // Finally, inform the code generator which regs we return values in.
277 // (see the ISD::RET: case down below)
278 switch (getValueType(F.getReturnType())) {
279 default: assert(0 && "i have no idea where to return this type!");
280 case MVT::isVoid: break;
281 case MVT::i1:
282 case MVT::i8:
283 case MVT::i16:
284 case MVT::i32:
285 case MVT::i64:
286 MF.addLiveOut(IA64::r8);
287 break;
288 case MVT::f32:
289 case MVT::f64:
290 MF.addLiveOut(IA64::F8);
291 break;
292 }
293
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000294 return ArgValues;
295}
296
297std::pair<SDOperand, SDOperand>
298IA64TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000299 const Type *RetTy, bool isVarArg,
300 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000301
302 MachineFunction &MF = DAG.getMachineFunction();
303
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000304 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000305 unsigned outRegsUsed = 0;
306
307 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000308 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000309 outRegsUsed = 8;
310 } else {
311 outRegsUsed = Args.size();
312 }
313
314 // FIXME? this WILL fail if we ever try to pass around an arg that
315 // consumes more than a single output slot (a 'real' double, int128
316 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
317 // registers we use. Hopefully, the assembler will notice.
318 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
319 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000320
321 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
322 DAG.getConstant(NumBytes, getPointerTy()));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000323
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000324 std::vector<SDOperand> args_to_use;
325 for (unsigned i = 0, e = Args.size(); i != e; ++i)
326 {
327 switch (getValueType(Args[i].second)) {
328 default: assert(0 && "unexpected argument type!");
329 case MVT::i1:
330 case MVT::i8:
331 case MVT::i16:
332 case MVT::i32:
333 //promote to 64-bits, sign/zero extending based on type
334 //of the argument
335 if(Args[i].second->isSigned())
336 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
337 Args[i].first);
338 else
339 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
340 Args[i].first);
341 break;
342 case MVT::f32:
343 //promote to 64-bits
344 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
345 case MVT::f64:
346 case MVT::i64:
347 break;
348 }
349 args_to_use.push_back(Args[i].first);
350 }
351
352 std::vector<MVT::ValueType> RetVals;
353 MVT::ValueType RetTyVT = getValueType(RetTy);
354 if (RetTyVT != MVT::isVoid)
355 RetVals.push_back(RetTyVT);
356 RetVals.push_back(MVT::Other);
357
358 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
359 Callee, args_to_use), 0);
360 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
361 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
362 DAG.getConstant(NumBytes, getPointerTy()));
363 return std::make_pair(TheCall, Chain);
364}
365
366std::pair<SDOperand, SDOperand>
367IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
368 // vastart just returns the address of the VarArgsFrameIndex slot.
369 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
370}
371
372std::pair<SDOperand,SDOperand> IA64TargetLowering::
373LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
374 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000375
376 MVT::ValueType ArgVT = getValueType(ArgTy);
377 SDOperand Result;
378 if (!isVANext) {
379 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
380 } else {
381 unsigned Amt;
382 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
383 Amt = 8;
384 else {
385 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
386 "Other types should have been promoted for varargs!");
387 Amt = 8;
388 }
389 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
390 DAG.getConstant(Amt, VAList.getValueType()));
391 }
392 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000393}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000394
395std::pair<SDOperand, SDOperand> IA64TargetLowering::
396LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
397 SelectionDAG &DAG) {
398
399 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000400 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000401}
402
403
404namespace {
405
406 //===--------------------------------------------------------------------===//
407 /// ISel - IA64 specific code to select IA64 machine instructions for
408 /// SelectionDAG operations.
409 ///
410 class ISel : public SelectionDAGISel {
411 /// IA64Lowering - This object fully describes how to lower LLVM code to an
412 /// IA64-specific SelectionDAG.
413 IA64TargetLowering IA64Lowering;
414
415 /// ExprMap - As shared expressions are codegen'd, we keep track of which
416 /// vreg the value is produced in, so we only emit one copy of each compiled
417 /// tree.
418 std::map<SDOperand, unsigned> ExprMap;
419 std::set<SDOperand> LoweredTokens;
420
421 public:
422 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {
423 }
424
425 /// InstructionSelectBasicBlock - This callback is invoked by
426 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
427 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
428
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000429 unsigned SelectExpr(SDOperand N);
430 void Select(SDOperand N);
431 };
432}
433
434/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
435/// when it has created a SelectionDAG for us to codegen.
436void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
437
438 // Codegen the basic block.
439 Select(DAG.getRoot());
440
441 // Clear state used for selection.
442 ExprMap.clear();
443 LoweredTokens.clear();
444}
445
Duraid Madina4826a072005-04-06 09:55:17 +0000446/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
447/// returns zero when the input is not exactly a power of two.
448static uint64_t ExactLog2(uint64_t Val) {
449 if (Val == 0 || (Val & (Val-1))) return 0;
450 unsigned Count = 0;
451 while (Val != 1) {
452 Val >>= 1;
453 ++Count;
454 }
455 return Count;
456}
457
458/// ponderIntegerDivisionBy - When handling integer divides, if the divide
459/// is by a constant such that we can efficiently codegen it, this
460/// function says what to do. Currently, it returns 0 if the division must
461/// become a genuine divide, and 1 if the division can be turned into a
462/// right shift.
463static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
464 unsigned& Imm) {
465 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
466 // a constant, give up.
467
468 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
469
470 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
471 return 1;
472 }
473
474 return 0; // fallthrough
475}
476
Duraid Madinaf55e4032005-04-07 12:33:38 +0000477static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
478 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
479 // constant, give up.
480 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
481
482 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
483 Imm = v & 0x3FFF; // 14 bits
484 return 1;
485 }
486 return 0; // fallthrough
487}
488
489static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
490 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
491 // constant, give up.
492 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
493
494 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
495 Imm = v & 0xFF; // 8 bits
496 return 1;
497 }
498 return 0; // fallthrough
499}
500
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000501unsigned ISel::SelectExpr(SDOperand N) {
502 unsigned Result;
503 unsigned Tmp1, Tmp2, Tmp3;
504 unsigned Opc = 0;
505 MVT::ValueType DestType = N.getValueType();
506
507 unsigned opcode = N.getOpcode();
508
509 SDNode *Node = N.Val;
510 SDOperand Op0, Op1;
511
512 if (Node->getOpcode() == ISD::CopyFromReg)
513 // Just use the specified register as our input.
514 return dyn_cast<RegSDNode>(Node)->getReg();
515
516 unsigned &Reg = ExprMap[N];
517 if (Reg) return Reg;
518
519 if (N.getOpcode() != ISD::CALL)
520 Reg = Result = (N.getValueType() != MVT::Other) ?
521 MakeReg(N.getValueType()) : 1;
522 else {
523 // If this is a call instruction, make sure to prepare ALL of the result
524 // values as well as the chain.
525 if (Node->getNumValues() == 1)
526 Reg = Result = 1; // Void call, just a chain.
527 else {
528 Result = MakeReg(Node->getValueType(0));
529 ExprMap[N.getValue(0)] = Result;
530 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
531 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
532 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
533 }
534 }
535
536 switch (N.getOpcode()) {
537 default:
538 Node->dump();
539 assert(0 && "Node not handled!\n");
540
541 case ISD::FrameIndex: {
542 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
543 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
544 return Result;
545 }
546
547 case ISD::ConstantPool: {
548 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
549 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
550 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
551 .addReg(IA64::r1);
552 return Result;
553 }
554
555 case ISD::ConstantFP: {
556 Tmp1 = Result; // Intermediate Register
557 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
558 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
559 Tmp1 = MakeReg(MVT::f64);
560
561 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
562 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
563 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
564 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
565 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
566 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
567 else
568 assert(0 && "Unexpected FP constant!");
569 if (Tmp1 != Result)
570 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
571 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
572 .addReg(IA64::F0);
573 return Result;
574 }
575
576 case ISD::DYNAMIC_STACKALLOC: {
577 // Generate both result values.
578 if (Result != 1)
579 ExprMap[N.getValue(1)] = 1; // Generate the token
580 else
581 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
582
583 // FIXME: We are currently ignoring the requested alignment for handling
584 // greater than the stack alignment. This will need to be revisited at some
585 // point. Align = N.getOperand(2);
586
587 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
588 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
589 std::cerr << "Cannot allocate stack object with greater alignment than"
590 << " the stack alignment yet!";
591 abort();
592 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000593
594/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000595 Select(N.getOperand(0));
596 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
597 {
598 if (CN->getValue() < 32000)
599 {
600 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
601 .addImm(-CN->getValue());
602 } else {
603 Tmp1 = SelectExpr(N.getOperand(1));
604 // Subtract size from stack pointer, thereby allocating some space.
605 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
606 }
607 } else {
608 Tmp1 = SelectExpr(N.getOperand(1));
609 // Subtract size from stack pointer, thereby allocating some space.
610 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
611 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000612*/
613 Select(N.getOperand(0));
614 Tmp1 = SelectExpr(N.getOperand(1));
615 // Subtract size from stack pointer, thereby allocating some space.
616 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000617 // Put a pointer to the space into the result register, by copying the
618 // stack pointer.
619 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
620 return Result;
621 }
622
623 case ISD::SELECT: {
624 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
625 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
626 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
627
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000628 unsigned bogoResult;
629
630 switch (N.getOperand(1).getValueType()) {
631 default: assert(0 &&
632 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
633 case MVT::i64:
634 bogoResult=MakeReg(MVT::i64);
635 break;
636 case MVT::f64:
637 bogoResult=MakeReg(MVT::f64);
638 break;
639 }
Duraid Madina69c8e202005-04-01 10:35:00 +0000640
641 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
642 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
643 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
644 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000645 return Result;
646 }
647
648 case ISD::Constant: {
649 unsigned depositPos=0;
650 unsigned depositLen=0;
651 switch (N.getValueType()) {
652 default: assert(0 && "Cannot use constants of this type!");
653 case MVT::i1: { // if a bool, we don't 'load' so much as generate
654 // the constant:
655 if(cast<ConstantSDNode>(N)->getValue()) // true:
656 BuildMI(BB, IA64::CMPEQ, 2, Result)
657 .addReg(IA64::r0).addReg(IA64::r0);
658 else // false:
659 BuildMI(BB, IA64::CMPNE, 2, Result)
660 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000661 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000662 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000663 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000664 }
665
666 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000667
668 if(immediate==0) { // if the constant is just zero,
669 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
670 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000671 }
672
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000673 if (immediate <= 8191 && immediate >= -8192) {
674 // if this constants fits in 14 bits, we use a mov the assembler will
675 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
676 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
677 return Result; // early exit
678 }
679
680 if (immediate <= 2097151 && immediate >= -2097152) {
681 // if this constants fits in 22 bits, we use a mov the assembler will
682 // turn into: "addl rDest=imm,r0"
683 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
684 return Result; // early exit
685 }
686
687 /* otherwise, our immediate is big, so we use movl */
688 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +0000689 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000690 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000691 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000692
693 case ISD::UNDEF: {
694 BuildMI(BB, IA64::IDEF, 0, Result);
695 return Result;
696 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000697
698 case ISD::GlobalAddress: {
699 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
700 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000701
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000702 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000703 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000704
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000705 return Result;
706 }
707
708 case ISD::ExternalSymbol: {
709 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +0000710// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
711 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000712 return Result;
713 }
714
715 case ISD::FP_EXTEND: {
716 Tmp1 = SelectExpr(N.getOperand(0));
717 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
718 return Result;
719 }
720
721 case ISD::ZERO_EXTEND: {
722 Tmp1 = SelectExpr(N.getOperand(0)); // value
723
724 switch (N.getOperand(0).getValueType()) {
725 default: assert(0 && "Cannot zero-extend this type!");
726 case MVT::i8: Opc = IA64::ZXT1; break;
727 case MVT::i16: Opc = IA64::ZXT2; break;
728 case MVT::i32: Opc = IA64::ZXT4; break;
729
730 // we handle bools differently! :
731 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
732 unsigned dummy = MakeReg(MVT::i64);
733 // first load zero:
734 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
735 // ...then conditionally (PR:Tmp1) add 1:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000736 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000737 .addImm(1).addReg(Tmp1);
738 return Result; // XXX early exit!
739 }
740 }
741
742 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
743 return Result;
744 }
745
746 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
747
748assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
749
750 Tmp1 = SelectExpr(N.getOperand(0)); // value
751
752 switch (N.getOperand(0).getValueType()) {
753 default: assert(0 && "Cannot sign-extend this type!");
754 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
755 Opc = IA64::SXT1; break;
756 // FIXME: for now, we treat bools the same as i8s
757 case MVT::i8: Opc = IA64::SXT1; break;
758 case MVT::i16: Opc = IA64::SXT2; break;
759 case MVT::i32: Opc = IA64::SXT4; break;
760 }
761
762 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
763 return Result;
764 }
765
766 case ISD::TRUNCATE: {
767 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
768 // of R0 appropriately.
769 switch (N.getOperand(0).getValueType()) {
770 default: assert(0 && "Unknown truncate!");
771 case MVT::i64: break;
772 }
773 Tmp1 = SelectExpr(N.getOperand(0));
774 unsigned depositPos, depositLen;
775
776 switch (N.getValueType()) {
777 default: assert(0 && "Unknown truncate!");
778 case MVT::i1: {
779 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
780 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
781 .addReg(IA64::r0);
782 return Result; // XXX early exit!
783 }
784 case MVT::i8: depositPos=0; depositLen=8; break;
785 case MVT::i16: depositPos=0; depositLen=16; break;
786 case MVT::i32: depositPos=0; depositLen=32; break;
787 }
788 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
789 .addImm(depositPos).addImm(depositLen);
790 return Result;
791 }
792
793/*
794 case ISD::FP_ROUND: {
795 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
796 "error: trying to FP_ROUND something other than f64 -> f32!\n");
797 Tmp1 = SelectExpr(N.getOperand(0));
798 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
799 // we add 0.0 using a single precision add to do rounding
800 return Result;
801 }
802*/
803
804// FIXME: the following 4 cases need cleaning
805 case ISD::SINT_TO_FP: {
806 Tmp1 = SelectExpr(N.getOperand(0));
807 Tmp2 = MakeReg(MVT::f64);
808 unsigned dummy = MakeReg(MVT::f64);
809 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
810 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
811 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
812 return Result;
813 }
814
815 case ISD::UINT_TO_FP: {
816 Tmp1 = SelectExpr(N.getOperand(0));
817 Tmp2 = MakeReg(MVT::f64);
818 unsigned dummy = MakeReg(MVT::f64);
819 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
820 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
821 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
822 return Result;
823 }
824
825 case ISD::FP_TO_SINT: {
826 Tmp1 = SelectExpr(N.getOperand(0));
827 Tmp2 = MakeReg(MVT::f64);
828 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
829 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
830 return Result;
831 }
832
833 case ISD::FP_TO_UINT: {
834 Tmp1 = SelectExpr(N.getOperand(0));
835 Tmp2 = MakeReg(MVT::f64);
836 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
837 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
838 return Result;
839 }
840
841 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +0000842 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
843 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
844 // into an fma, do so:
845 // ++FusedFP; // Statistic
846 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
847 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
848 Tmp3 = SelectExpr(N.getOperand(1));
849 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
850 return Result; // early exit
851 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000852 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000853 if(DestType != MVT::f64) { // integer addition:
854 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
855 case 1: // adding a constant that's 14 bits
856 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
857 return Result; // early exit
858 } // fallthrough and emit a reg+reg ADD:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000859 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000860 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
861 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000862 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000863 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
864 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000865 return Result;
866 }
867
868 case ISD::MUL: {
869 Tmp1 = SelectExpr(N.getOperand(0));
870 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina4826a072005-04-06 09:55:17 +0000871
872 if(DestType != MVT::f64) { // TODO: speed!
873 // boring old integer multiply with xma
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000874 unsigned TempFR1=MakeReg(MVT::f64);
875 unsigned TempFR2=MakeReg(MVT::f64);
876 unsigned TempFR3=MakeReg(MVT::f64);
877 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
878 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
879 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
880 .addReg(IA64::F0);
881 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
882 }
883 else // floating point multiply
884 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
885 return Result;
886 }
887
888 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +0000889 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
890 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
891 // into an fms, do so:
892 // ++FusedFP; // Statistic
893 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
894 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
895 Tmp3 = SelectExpr(N.getOperand(1));
896 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
897 return Result; // early exit
898 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000899 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000900 if(DestType != MVT::f64) { // integer subtraction:
901 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
902 case 1: // subtracting *from* an 8 bit constant:
903 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
904 return Result; // early exit
905 } // fallthrough and emit a reg+reg SUB:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000906 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000907 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
908 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000909 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000910 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +0000911 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000912 return Result;
913 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000914
915 case ISD::FABS: {
916 Tmp1 = SelectExpr(N.getOperand(0));
917 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
918 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
919 return Result;
920 }
921
922 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000923 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000924
925 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
926 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
927 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
928 } else {
929 Tmp1 = SelectExpr(N.getOperand(0));
930 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
931 }
932
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000933 return Result;
934 }
935
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000936 case ISD::AND: {
937 switch (N.getValueType()) {
938 default: assert(0 && "Cannot AND this type!");
939 case MVT::i1: { // if a bool, we emit a pseudocode AND
940 unsigned pA = SelectExpr(N.getOperand(0));
941 unsigned pB = SelectExpr(N.getOperand(1));
942
943/* our pseudocode for AND is:
944 *
945(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
946 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
947 ;;
948(pB) cmp.ne pTemp,p0 = r0,r0
949 ;;
950(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
951
952*/
953 unsigned pTemp = MakeReg(MVT::i1);
954
955 unsigned bogusTemp1 = MakeReg(MVT::i1);
956 unsigned bogusTemp2 = MakeReg(MVT::i1);
957 unsigned bogusTemp3 = MakeReg(MVT::i1);
958 unsigned bogusTemp4 = MakeReg(MVT::i1);
959
960 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
961 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
962 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
963 .addReg(IA64::r0).addReg(IA64::r0);
964 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
965 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
966 BuildMI(BB, IA64::TPCMPNE, 3, Result)
967 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
968 break;
969 }
970 // if not a bool, we just AND away:
971 case MVT::i8:
972 case MVT::i16:
973 case MVT::i32:
974 case MVT::i64: {
975 Tmp1 = SelectExpr(N.getOperand(0));
976 Tmp2 = SelectExpr(N.getOperand(1));
977 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
978 break;
979 }
980 }
981 return Result;
982 }
983
984 case ISD::OR: {
985 switch (N.getValueType()) {
986 default: assert(0 && "Cannot OR this type!");
987 case MVT::i1: { // if a bool, we emit a pseudocode OR
988 unsigned pA = SelectExpr(N.getOperand(0));
989 unsigned pB = SelectExpr(N.getOperand(1));
990
991 unsigned pTemp1 = MakeReg(MVT::i1);
992
993/* our pseudocode for OR is:
994 *
995
996pC = pA OR pB
997-------------
998
999(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1000 ;;
1001(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
1002
1003*/
1004 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
1005 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
1006 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
1007 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
1008 break;
1009 }
1010 // if not a bool, we just OR away:
1011 case MVT::i8:
1012 case MVT::i16:
1013 case MVT::i32:
1014 case MVT::i64: {
1015 Tmp1 = SelectExpr(N.getOperand(0));
1016 Tmp2 = SelectExpr(N.getOperand(1));
1017 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1018 break;
1019 }
1020 }
1021 return Result;
1022 }
1023
1024 case ISD::XOR: {
1025 switch (N.getValueType()) {
1026 default: assert(0 && "Cannot XOR this type!");
1027 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1028 unsigned pY = SelectExpr(N.getOperand(0));
1029 unsigned pZ = SelectExpr(N.getOperand(1));
1030
1031/* one possible routine for XOR is:
1032
1033 // Compute px = py ^ pz
1034 // using sum of products: px = (py & !pz) | (pz & !py)
1035 // Uses 5 instructions in 3 cycles.
1036 // cycle 1
1037(pz) cmp.eq.unc px = r0, r0 // px = pz
1038(py) cmp.eq.unc pt = r0, r0 // pt = py
1039 ;;
1040 // cycle 2
1041(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1042(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1043 ;;
1044 } { .mmi
1045 // cycle 3
1046(pt) cmp.eq.or px = r0, r0 // px = px | pt
1047
1048*** Another, which we use here, requires one scratch GR. it is:
1049
1050 mov rt = 0 // initialize rt off critical path
1051 ;;
1052
1053 // cycle 1
1054(pz) cmp.eq.unc px = r0, r0 // px = pz
1055(pz) mov rt = 1 // rt = pz
1056 ;;
1057 // cycle 2
1058(py) cmp.ne px = 1, rt // if (py) px = !pz
1059
1060.. these routines kindly provided by Jim Hull
1061*/
1062 unsigned rt = MakeReg(MVT::i64);
1063
1064 // these two temporaries will never actually appear,
1065 // due to the two-address form of some of the instructions below
1066 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1067 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1068
1069 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1070 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
1071 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
1072 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
1073 .addReg(bogoGR).addImm(1).addReg(pZ);
1074 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
1075 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
1076 break;
1077 }
1078 // if not a bool, we just XOR away:
1079 case MVT::i8:
1080 case MVT::i16:
1081 case MVT::i32:
1082 case MVT::i64: {
1083 Tmp1 = SelectExpr(N.getOperand(0));
1084 Tmp2 = SelectExpr(N.getOperand(1));
1085 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1086 break;
1087 }
1088 }
1089 return Result;
1090 }
1091
1092 case ISD::SHL: {
1093 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001094 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1095 Tmp2 = CN->getValue();
1096 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1097 } else {
1098 Tmp2 = SelectExpr(N.getOperand(1));
1099 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1100 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001101 return Result;
1102 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001103
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001104 case ISD::SRL: {
1105 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001106 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1107 Tmp2 = CN->getValue();
1108 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1109 } else {
1110 Tmp2 = SelectExpr(N.getOperand(1));
1111 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1112 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001113 return Result;
1114 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001115
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001116 case ISD::SRA: {
1117 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001118 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1119 Tmp2 = CN->getValue();
1120 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1121 } else {
1122 Tmp2 = SelectExpr(N.getOperand(1));
1123 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1124 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001125 return Result;
1126 }
1127
1128 case ISD::SDIV:
1129 case ISD::UDIV:
1130 case ISD::SREM:
1131 case ISD::UREM: {
1132
1133 Tmp1 = SelectExpr(N.getOperand(0));
1134 Tmp2 = SelectExpr(N.getOperand(1));
1135
1136 bool isFP=false;
1137
1138 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1139 isFP=true;
1140
1141 bool isModulus=false; // is it a division or a modulus?
1142 bool isSigned=false;
1143
1144 switch(N.getOpcode()) {
1145 case ISD::SDIV: isModulus=false; isSigned=true; break;
1146 case ISD::UDIV: isModulus=false; isSigned=false; break;
1147 case ISD::SREM: isModulus=true; isSigned=true; break;
1148 case ISD::UREM: isModulus=true; isSigned=false; break;
1149 }
1150
Duraid Madina4826a072005-04-06 09:55:17 +00001151 if(!isModulus && !isFP) { // if this is an integer divide,
1152 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
1153 case 1: // division by a constant that's a power of 2
1154 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina6dcceb52005-04-08 10:01:48 +00001155 if(isSigned) { // argument could be negative, so emit some code:
1156 unsigned divAmt=Tmp3;
1157 unsigned tempGR1=MakeReg(MVT::i64);
1158 unsigned tempGR2=MakeReg(MVT::i64);
1159 unsigned tempGR3=MakeReg(MVT::i64);
1160 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1161 .addReg(Tmp1).addImm(divAmt-1);
1162 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1163 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1164 BuildMI(BB, IA64::ADD, 2, tempGR3)
1165 .addReg(Tmp1).addReg(tempGR2);
1166 BuildMI(BB, IA64::SHRS, 2, Result)
1167 .addReg(tempGR3).addImm(divAmt);
1168 }
1169 else // unsigned div-by-power-of-2 becomes a simple shift right:
Duraid Madina4826a072005-04-06 09:55:17 +00001170 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1171 return Result; // early exit
1172 }
1173 }
1174
Duraid Madinabeeaab22005-03-31 12:31:11 +00001175 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
1176 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001177 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1178 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1179 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1180 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1181 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1182 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1183 unsigned TmpF7=MakeReg(MVT::f64);
1184 unsigned TmpF8=MakeReg(MVT::f64);
1185 unsigned TmpF9=MakeReg(MVT::f64);
1186 unsigned TmpF10=MakeReg(MVT::f64);
1187 unsigned TmpF11=MakeReg(MVT::f64);
1188 unsigned TmpF12=MakeReg(MVT::f64);
1189 unsigned TmpF13=MakeReg(MVT::f64);
1190 unsigned TmpF14=MakeReg(MVT::f64);
1191 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001192
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001193 // OK, emit some code:
1194
1195 if(!isFP) {
1196 // first, load the inputs into FP regs.
1197 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1198 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1199
1200 // next, convert the inputs to FP
1201 if(isSigned) {
1202 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1203 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1204 } else {
1205 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1206 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1207 }
1208
1209 } else { // this is an FP divide/remainder, so we 'leak' some temp
1210 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1211 TmpF3=Tmp1;
1212 TmpF4=Tmp2;
1213 }
1214
1215 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001216 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1217 BuildMI(BB, IA64::FRCPAS1, 4)
1218 .addReg(TmpF5, MachineOperand::Def)
1219 .addReg(TmpPR, MachineOperand::Def)
1220 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001221
Duraid Madinabeeaab22005-03-31 12:31:11 +00001222 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1223 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1224 // TPCMPNE below
1225 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1226 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1227 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1228 }
1229
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001230 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1231 // precision, don't need this much for f32/i32)
1232 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1233 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1234 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1235 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1236 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1237 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1238 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1239 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1240 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1241 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1242 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1243 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1244 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1245 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1246 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1247 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001248
1249 // FIXME: this is unfortunate :(
1250 // the story is that the dest reg of the fnma above and the fma below
1251 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1252 // be the same register, or this code breaks if the first argument is
1253 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001254 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1255 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1256
Duraid Madina6e02e682005-04-04 05:05:52 +00001257 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1258 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1259 }
1260
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001261 if(!isFP) {
1262 // round to an integer
1263 if(isSigned)
1264 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1265 else
1266 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1267 } else {
1268 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1269 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1270 // we really do need the above FMOV? ;)
1271 }
1272
1273 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001274 if(isFP) { // extra worrying about div-by-zero
1275 unsigned bogoResult=MakeReg(MVT::f64);
1276
1277 // we do a 'conditional fmov' (of the correct result, depending
1278 // on how the frcpa predicate turned out)
1279 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1280 .addReg(TmpF12).addReg(TmpPR2);
1281 BuildMI(BB, IA64::CFMOV, 2, Result)
1282 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1283 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001284 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001285 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001286 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001287 } else { // this is a modulus
1288 if(!isFP) {
1289 // answer = q * (-b) + a
1290 unsigned ModulusResult = MakeReg(MVT::f64);
1291 unsigned TmpF = MakeReg(MVT::f64);
1292 unsigned TmpI = MakeReg(MVT::i64);
Duraid Madina6e02e682005-04-04 05:05:52 +00001293
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001294 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1295 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1296 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1297 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1298 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1299 } else { // FP modulus! The horror... the horror....
1300 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1301 }
1302 }
1303
1304 return Result;
1305 }
1306
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001307 case ISD::SIGN_EXTEND_INREG: {
1308 Tmp1 = SelectExpr(N.getOperand(0));
1309 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1310 switch(MVN->getExtraValueType())
1311 {
1312 default:
1313 Node->dump();
1314 assert(0 && "don't know how to sign extend this type");
1315 break;
1316 case MVT::i8: Opc = IA64::SXT1; break;
1317 case MVT::i16: Opc = IA64::SXT2; break;
1318 case MVT::i32: Opc = IA64::SXT4; break;
1319 }
1320 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1321 return Result;
1322 }
1323
1324 case ISD::SETCC: {
1325 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001326
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001327 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1328 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001329
1330 if(ConstantSDNode *CSDN =
1331 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1332 // if we are comparing against a constant zero
1333 if(CSDN->getValue()==0)
1334 Tmp2 = IA64::r0; // then we can just compare against r0
1335 else
1336 Tmp2 = SelectExpr(N.getOperand(1));
1337 } else // not comparing against a constant
1338 Tmp2 = SelectExpr(N.getOperand(1));
1339
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001340 switch (SetCC->getCondition()) {
1341 default: assert(0 && "Unknown integer comparison!");
1342 case ISD::SETEQ:
1343 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1344 break;
1345 case ISD::SETGT:
1346 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1347 break;
1348 case ISD::SETGE:
1349 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1350 break;
1351 case ISD::SETLT:
1352 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1353 break;
1354 case ISD::SETLE:
1355 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1356 break;
1357 case ISD::SETNE:
1358 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1359 break;
1360 case ISD::SETULT:
1361 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1362 break;
1363 case ISD::SETUGT:
1364 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1365 break;
1366 case ISD::SETULE:
1367 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1368 break;
1369 case ISD::SETUGE:
1370 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1371 break;
1372 }
1373 }
1374 else { // if not integer, should be FP. FIXME: what about bools? ;)
1375 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1376 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001377
1378 if(ConstantFPSDNode *CFPSDN =
1379 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1380
1381 // if we are comparing against a constant +0.0 or +1.0
1382 if(CFPSDN->isExactlyValue(+0.0))
1383 Tmp2 = IA64::F0; // then we can just compare against f0
1384 else if(CFPSDN->isExactlyValue(+1.0))
1385 Tmp2 = IA64::F1; // or f1
1386 else
1387 Tmp2 = SelectExpr(N.getOperand(1));
1388 } else // not comparing against a constant
1389 Tmp2 = SelectExpr(N.getOperand(1));
1390
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001391 switch (SetCC->getCondition()) {
1392 default: assert(0 && "Unknown FP comparison!");
1393 case ISD::SETEQ:
1394 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1395 break;
1396 case ISD::SETGT:
1397 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1398 break;
1399 case ISD::SETGE:
1400 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1401 break;
1402 case ISD::SETLT:
1403 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1404 break;
1405 case ISD::SETLE:
1406 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1407 break;
1408 case ISD::SETNE:
1409 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1410 break;
1411 case ISD::SETULT:
1412 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1413 break;
1414 case ISD::SETUGT:
1415 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1416 break;
1417 case ISD::SETULE:
1418 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1419 break;
1420 case ISD::SETUGE:
1421 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1422 break;
1423 }
1424 }
1425 }
1426 else
1427 assert(0 && "this setcc not implemented yet");
1428
1429 return Result;
1430 }
1431
1432 case ISD::EXTLOAD:
1433 case ISD::ZEXTLOAD:
1434 case ISD::LOAD: {
1435 // Make sure we generate both values.
1436 if (Result != 1)
1437 ExprMap[N.getValue(1)] = 1; // Generate the token
1438 else
1439 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1440
1441 bool isBool=false;
1442
1443 if(opcode == ISD::LOAD) { // this is a LOAD
1444 switch (Node->getValueType(0)) {
1445 default: assert(0 && "Cannot load this type!");
1446 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1447 // FIXME: for now, we treat bool loads the same as i8 loads */
1448 case MVT::i8: Opc = IA64::LD1; break;
1449 case MVT::i16: Opc = IA64::LD2; break;
1450 case MVT::i32: Opc = IA64::LD4; break;
1451 case MVT::i64: Opc = IA64::LD8; break;
1452
1453 case MVT::f32: Opc = IA64::LDF4; break;
1454 case MVT::f64: Opc = IA64::LDF8; break;
1455 }
1456 } else { // this is an EXTLOAD or ZEXTLOAD
1457 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1458 switch (TypeBeingLoaded) {
1459 default: assert(0 && "Cannot extload/zextload this type!");
1460 // FIXME: bools?
1461 case MVT::i8: Opc = IA64::LD1; break;
1462 case MVT::i16: Opc = IA64::LD2; break;
1463 case MVT::i32: Opc = IA64::LD4; break;
1464 case MVT::f32: Opc = IA64::LDF4; break;
1465 }
1466 }
1467
1468 SDOperand Chain = N.getOperand(0);
1469 SDOperand Address = N.getOperand(1);
1470
1471 if(Address.getOpcode() == ISD::GlobalAddress) {
1472 Select(Chain);
1473 unsigned dummy = MakeReg(MVT::i64);
1474 unsigned dummy2 = MakeReg(MVT::i64);
1475 BuildMI(BB, IA64::ADD, 2, dummy)
1476 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1477 .addReg(IA64::r1);
1478 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1479 if(!isBool)
1480 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1481 else { // emit a little pseudocode to load a bool (stored in one byte)
1482 // into a predicate register
1483 assert(Opc==IA64::LD1 && "problem loading a bool");
1484 unsigned dummy3 = MakeReg(MVT::i64);
1485 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1486 // we compare to 0. true? 0. false? 1.
1487 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1488 }
1489 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1490 Select(Chain);
1491 IA64Lowering.restoreGP(BB);
1492 unsigned dummy = MakeReg(MVT::i64);
1493 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1494 .addReg(IA64::r1); // CPI+GP
1495 if(!isBool)
1496 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1497 else { // emit a little pseudocode to load a bool (stored in one byte)
1498 // into a predicate register
1499 assert(Opc==IA64::LD1 && "problem loading a bool");
1500 unsigned dummy3 = MakeReg(MVT::i64);
1501 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1502 // we compare to 0. true? 0. false? 1.
1503 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1504 }
1505 } else if(Address.getOpcode() == ISD::FrameIndex) {
1506 Select(Chain); // FIXME ? what about bools?
1507 unsigned dummy = MakeReg(MVT::i64);
1508 BuildMI(BB, IA64::MOV, 1, dummy)
1509 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1510 if(!isBool)
1511 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1512 else { // emit a little pseudocode to load a bool (stored in one byte)
1513 // into a predicate register
1514 assert(Opc==IA64::LD1 && "problem loading a bool");
1515 unsigned dummy3 = MakeReg(MVT::i64);
1516 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1517 // we compare to 0. true? 0. false? 1.
1518 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1519 }
1520 } else { // none of the above...
1521 Select(Chain);
1522 Tmp2 = SelectExpr(Address);
1523 if(!isBool)
1524 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1525 else { // emit a little pseudocode to load a bool (stored in one byte)
1526 // into a predicate register
1527 assert(Opc==IA64::LD1 && "problem loading a bool");
1528 unsigned dummy = MakeReg(MVT::i64);
1529 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1530 // we compare to 0. true? 0. false? 1.
1531 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1532 }
1533 }
1534
1535 return Result;
1536 }
1537
1538 case ISD::CopyFromReg: {
1539 if (Result == 1)
1540 Result = ExprMap[N.getValue(0)] =
1541 MakeReg(N.getValue(0).getValueType());
1542
1543 SDOperand Chain = N.getOperand(0);
1544
1545 Select(Chain);
1546 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1547
1548 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1549 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1550 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1551 // (r) Result =cmp.eq.unc(r0,r0)
1552 else
1553 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1554 return Result;
1555 }
1556
1557 case ISD::CALL: {
1558 Select(N.getOperand(0));
1559
1560 // The chain for this call is now lowered.
1561 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1562
1563 //grab the arguments
1564 std::vector<unsigned> argvregs;
1565
1566 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1567 argvregs.push_back(SelectExpr(N.getOperand(i)));
1568
1569 // see section 8.5.8 of "Itanium Software Conventions and
1570 // Runtime Architecture Guide to see some examples of what's going
1571 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1572 // while FP args get mapped to F8->F15 as needed)
1573
1574 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1575
1576 // in reg args
1577 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1578 {
1579 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1580 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1581 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1582 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1583
1584 switch(N.getOperand(i+2).getValueType())
1585 {
1586 default: // XXX do we need to support MVT::i1 here?
1587 Node->dump();
1588 N.getOperand(i).Val->dump();
1589 std::cerr << "Type for " << i << " is: " <<
1590 N.getOperand(i+2).getValueType() << std::endl;
1591 assert(0 && "Unknown value type for call");
1592 case MVT::i64:
1593 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1594 break;
1595 case MVT::f64:
1596 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1597 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001598 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001599 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1600 break;
1601 }
1602 }
1603
1604 //in mem args
1605 for (int i = 8, e = argvregs.size(); i < e; ++i)
1606 {
1607 unsigned tempAddr = MakeReg(MVT::i64);
1608
1609 switch(N.getOperand(i+2).getValueType()) {
1610 default:
1611 Node->dump();
1612 N.getOperand(i).Val->dump();
1613 std::cerr << "Type for " << i << " is: " <<
1614 N.getOperand(i+2).getValueType() << "\n";
1615 assert(0 && "Unknown value type for call");
1616 case MVT::i1: // FIXME?
1617 case MVT::i8:
1618 case MVT::i16:
1619 case MVT::i32:
1620 case MVT::i64:
1621 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1622 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1623 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1624 break;
1625 case MVT::f32:
1626 case MVT::f64:
1627 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1628 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1629 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1630 break;
1631 }
1632 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001633
1634 /* XXX we want to re-enable direct branches! crippling them now
1635 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001636 //build the right kind of call
1637 if (GlobalAddressSDNode *GASD =
1638 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1639 {
1640 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1641 IA64Lowering.restoreGP_SP_RP(BB);
1642 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001643 ^^^^^^^^^^^^^ we want this code one day XXX */
1644 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001645 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001646 { // FIXME : currently need this case for correctness, to avoid
1647 // "non-pic code with imm relocation against dynamic symbol" errors
1648 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001649 .addExternalSymbol(ESSDN->getSymbol(), true);
1650 IA64Lowering.restoreGP_SP_RP(BB);
1651 }
1652 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001653 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001654
1655 unsigned targetEntryPoint=MakeReg(MVT::i64);
1656 unsigned targetGPAddr=MakeReg(MVT::i64);
1657 unsigned currentGP=MakeReg(MVT::i64);
1658
1659 // b6 is a scratch branch register, we load the target entry point
1660 // from the base of the function descriptor
1661 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1662 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1663
1664 // save the current GP:
1665 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1666
1667 /* TODO: we need to make sure doing this never, ever loads a
1668 * bogus value into r1 (GP). */
1669 // load the target GP (which is at mem[functiondescriptor+8])
1670 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1671 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1672 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1673
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001674 // and then jump: (well, call)
1675 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001676 // and finally restore the old GP
1677 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1678 IA64Lowering.restoreSP_RP(BB);
1679 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001680
1681 switch (Node->getValueType(0)) {
1682 default: assert(0 && "Unknown value type for call result!");
1683 case MVT::Other: return 1;
1684 case MVT::i1:
1685 BuildMI(BB, IA64::CMPNE, 2, Result)
1686 .addReg(IA64::r8).addReg(IA64::r0);
1687 break;
1688 case MVT::i8:
1689 case MVT::i16:
1690 case MVT::i32:
1691 case MVT::i64:
1692 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1693 break;
1694 case MVT::f64:
1695 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1696 break;
1697 }
1698 return Result+N.ResNo;
1699 }
1700
1701 } // <- uhhh XXX
1702 return 0;
1703}
1704
1705void ISel::Select(SDOperand N) {
1706 unsigned Tmp1, Tmp2, Opc;
1707 unsigned opcode = N.getOpcode();
1708
Nate Begeman85fdeb22005-03-24 04:39:54 +00001709 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001710 return; // Already selected.
1711
1712 SDNode *Node = N.Val;
1713
1714 switch (Node->getOpcode()) {
1715 default:
1716 Node->dump(); std::cerr << "\n";
1717 assert(0 && "Node not handled yet!");
1718
1719 case ISD::EntryToken: return; // Noop
1720
1721 case ISD::TokenFactor: {
1722 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1723 Select(Node->getOperand(i));
1724 return;
1725 }
1726
1727 case ISD::CopyToReg: {
1728 Select(N.getOperand(0));
1729 Tmp1 = SelectExpr(N.getOperand(1));
1730 Tmp2 = cast<RegSDNode>(N)->getReg();
1731
1732 if (Tmp1 != Tmp2) {
1733 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1734 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1735 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1736 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1737 else
1738 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1739 // XXX is this the right way 'round? ;)
1740 }
1741 return;
1742 }
1743
1744 case ISD::RET: {
1745
1746 /* what the heck is going on here:
1747
1748<_sabre_> ret with two operands is obvious: chain and value
1749<camel_> yep
1750<_sabre_> ret with 3 values happens when 'expansion' occurs
1751<_sabre_> e.g. i64 gets split into 2x i32
1752<camel_> oh right
1753<_sabre_> you don't have this case on ia64
1754<camel_> yep
1755<_sabre_> so the two returned values go into EAX/EDX on ia32
1756<camel_> ahhh *memories*
1757<_sabre_> :)
1758<camel_> ok, thanks :)
1759<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1760<_sabre_> this is the first operand always
1761<_sabre_> these operand often define chains, they are the last operand
1762<_sabre_> they are printed as 'ch' if you do DAG.dump()
1763 */
1764
1765 switch (N.getNumOperands()) {
1766 default:
1767 assert(0 && "Unknown return instruction!");
1768 case 2:
1769 Select(N.getOperand(0));
1770 Tmp1 = SelectExpr(N.getOperand(1));
1771 switch (N.getOperand(1).getValueType()) {
1772 default: assert(0 && "All other types should have been promoted!!");
1773 // FIXME: do I need to add support for bools here?
1774 // (return '0' or '1' r8, basically...)
Duraid Madinaca494fd2005-04-12 14:54:44 +00001775 //
1776 // FIXME: need to round floats - 80 bits is bad, the tester
1777 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001778 case MVT::i64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001779 // we mark r8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001780 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1781 break;
1782 case MVT::f64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001783 // we mark F8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001784 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1785 }
1786 break;
1787 case 1:
1788 Select(N.getOperand(0));
1789 break;
1790 }
1791 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1792 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1793 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1794 return;
1795 }
1796
1797 case ISD::BR: {
1798 Select(N.getOperand(0));
1799 MachineBasicBlock *Dest =
1800 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1801 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1802 // XXX HACK! we do _not_ need long branches all the time
1803 return;
1804 }
1805
1806 case ISD::ImplicitDef: {
1807 Select(N.getOperand(0));
1808 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1809 return;
1810 }
1811
1812 case ISD::BRCOND: {
1813 MachineBasicBlock *Dest =
1814 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1815
1816 Select(N.getOperand(0));
1817 Tmp1 = SelectExpr(N.getOperand(1));
1818 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1819 // XXX HACK! we do _not_ need long branches all the time
1820 return;
1821 }
1822
1823 case ISD::EXTLOAD:
1824 case ISD::ZEXTLOAD:
1825 case ISD::SEXTLOAD:
1826 case ISD::LOAD:
1827 case ISD::CALL:
1828 case ISD::CopyFromReg:
1829 case ISD::DYNAMIC_STACKALLOC:
1830 SelectExpr(N);
1831 return;
1832
1833 case ISD::TRUNCSTORE:
1834 case ISD::STORE: {
1835 Select(N.getOperand(0));
1836 Tmp1 = SelectExpr(N.getOperand(1)); // value
1837
1838 bool isBool=false;
1839
1840 if(opcode == ISD::STORE) {
1841 switch (N.getOperand(1).getValueType()) {
1842 default: assert(0 && "Cannot store this type!");
1843 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1844 // FIXME?: for now, we treat bool loads the same as i8 stores */
1845 case MVT::i8: Opc = IA64::ST1; break;
1846 case MVT::i16: Opc = IA64::ST2; break;
1847 case MVT::i32: Opc = IA64::ST4; break;
1848 case MVT::i64: Opc = IA64::ST8; break;
1849
1850 case MVT::f32: Opc = IA64::STF4; break;
1851 case MVT::f64: Opc = IA64::STF8; break;
1852 }
1853 } else { // truncstore
1854 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1855 default: assert(0 && "unknown type in truncstore");
1856 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1857 //FIXME: DAG does not promote this load?
1858 case MVT::i8: Opc = IA64::ST1; break;
1859 case MVT::i16: Opc = IA64::ST2; break;
1860 case MVT::i32: Opc = IA64::ST4; break;
1861 case MVT::f32: Opc = IA64::STF4; break;
1862 }
1863 }
1864
1865 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1866 unsigned dummy = MakeReg(MVT::i64);
1867 unsigned dummy2 = MakeReg(MVT::i64);
1868 BuildMI(BB, IA64::ADD, 2, dummy)
1869 .addGlobalAddress(cast<GlobalAddressSDNode>
1870 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1871 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1872
1873 if(!isBool)
1874 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1875 else { // we are storing a bool, so emit a little pseudocode
1876 // to store a predicate register as one byte
1877 assert(Opc==IA64::ST1);
1878 unsigned dummy3 = MakeReg(MVT::i64);
1879 unsigned dummy4 = MakeReg(MVT::i64);
1880 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001881 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001882 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1883 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1884 }
1885 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1886
1887 // FIXME? (what about bools?)
1888
1889 unsigned dummy = MakeReg(MVT::i64);
1890 BuildMI(BB, IA64::MOV, 1, dummy)
1891 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1892 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1893 } else { // otherwise
1894 Tmp2 = SelectExpr(N.getOperand(2)); //address
1895 if(!isBool)
1896 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1897 else { // we are storing a bool, so emit a little pseudocode
1898 // to store a predicate register as one byte
1899 assert(Opc==IA64::ST1);
1900 unsigned dummy3 = MakeReg(MVT::i64);
1901 unsigned dummy4 = MakeReg(MVT::i64);
1902 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001903 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001904 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1905 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1906 }
1907 }
1908 return;
1909 }
1910
1911 case ISD::ADJCALLSTACKDOWN:
1912 case ISD::ADJCALLSTACKUP: {
1913 Select(N.getOperand(0));
1914 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1915
1916 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1917 IA64::ADJUSTCALLSTACKUP;
1918 BuildMI(BB, Opc, 1).addImm(Tmp1);
1919 return;
1920 }
1921
1922 return;
1923 }
1924 assert(0 && "GAME OVER. INSERT COIN?");
1925}
1926
1927
1928/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1929/// into a machine code representation using pattern matching and a machine
1930/// description file.
1931///
1932FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1933 return new ISel(TM);
1934}
1935
1936