blob: 5a743996ae749c12d86d9af2527bfe6bd1ba0a84 [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
1307 case ISD::ZERO_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 zero extend this type");
1315 break;
1316 case MVT::i8: Opc = IA64::ZXT1; break;
1317 case MVT::i16: Opc = IA64::ZXT2; break;
1318 case MVT::i32: Opc = IA64::ZXT4; break;
1319 }
1320 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1321 return Result;
1322 }
1323
1324 case ISD::SIGN_EXTEND_INREG: {
1325 Tmp1 = SelectExpr(N.getOperand(0));
1326 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1327 switch(MVN->getExtraValueType())
1328 {
1329 default:
1330 Node->dump();
1331 assert(0 && "don't know how to sign extend this type");
1332 break;
1333 case MVT::i8: Opc = IA64::SXT1; break;
1334 case MVT::i16: Opc = IA64::SXT2; break;
1335 case MVT::i32: Opc = IA64::SXT4; break;
1336 }
1337 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1338 return Result;
1339 }
1340
1341 case ISD::SETCC: {
1342 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001343
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001344 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1345 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001346
1347 if(ConstantSDNode *CSDN =
1348 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1349 // if we are comparing against a constant zero
1350 if(CSDN->getValue()==0)
1351 Tmp2 = IA64::r0; // then we can just compare against r0
1352 else
1353 Tmp2 = SelectExpr(N.getOperand(1));
1354 } else // not comparing against a constant
1355 Tmp2 = SelectExpr(N.getOperand(1));
1356
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001357 switch (SetCC->getCondition()) {
1358 default: assert(0 && "Unknown integer comparison!");
1359 case ISD::SETEQ:
1360 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1361 break;
1362 case ISD::SETGT:
1363 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1364 break;
1365 case ISD::SETGE:
1366 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1367 break;
1368 case ISD::SETLT:
1369 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1370 break;
1371 case ISD::SETLE:
1372 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1373 break;
1374 case ISD::SETNE:
1375 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1376 break;
1377 case ISD::SETULT:
1378 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1379 break;
1380 case ISD::SETUGT:
1381 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1382 break;
1383 case ISD::SETULE:
1384 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1385 break;
1386 case ISD::SETUGE:
1387 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1388 break;
1389 }
1390 }
1391 else { // if not integer, should be FP. FIXME: what about bools? ;)
1392 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1393 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001394
1395 if(ConstantFPSDNode *CFPSDN =
1396 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1397
1398 // if we are comparing against a constant +0.0 or +1.0
1399 if(CFPSDN->isExactlyValue(+0.0))
1400 Tmp2 = IA64::F0; // then we can just compare against f0
1401 else if(CFPSDN->isExactlyValue(+1.0))
1402 Tmp2 = IA64::F1; // or f1
1403 else
1404 Tmp2 = SelectExpr(N.getOperand(1));
1405 } else // not comparing against a constant
1406 Tmp2 = SelectExpr(N.getOperand(1));
1407
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001408 switch (SetCC->getCondition()) {
1409 default: assert(0 && "Unknown FP comparison!");
1410 case ISD::SETEQ:
1411 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1412 break;
1413 case ISD::SETGT:
1414 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1415 break;
1416 case ISD::SETGE:
1417 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1418 break;
1419 case ISD::SETLT:
1420 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1421 break;
1422 case ISD::SETLE:
1423 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1424 break;
1425 case ISD::SETNE:
1426 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1427 break;
1428 case ISD::SETULT:
1429 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1430 break;
1431 case ISD::SETUGT:
1432 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1433 break;
1434 case ISD::SETULE:
1435 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1436 break;
1437 case ISD::SETUGE:
1438 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1439 break;
1440 }
1441 }
1442 }
1443 else
1444 assert(0 && "this setcc not implemented yet");
1445
1446 return Result;
1447 }
1448
1449 case ISD::EXTLOAD:
1450 case ISD::ZEXTLOAD:
1451 case ISD::LOAD: {
1452 // Make sure we generate both values.
1453 if (Result != 1)
1454 ExprMap[N.getValue(1)] = 1; // Generate the token
1455 else
1456 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1457
1458 bool isBool=false;
1459
1460 if(opcode == ISD::LOAD) { // this is a LOAD
1461 switch (Node->getValueType(0)) {
1462 default: assert(0 && "Cannot load this type!");
1463 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1464 // FIXME: for now, we treat bool loads the same as i8 loads */
1465 case MVT::i8: Opc = IA64::LD1; break;
1466 case MVT::i16: Opc = IA64::LD2; break;
1467 case MVT::i32: Opc = IA64::LD4; break;
1468 case MVT::i64: Opc = IA64::LD8; break;
1469
1470 case MVT::f32: Opc = IA64::LDF4; break;
1471 case MVT::f64: Opc = IA64::LDF8; break;
1472 }
1473 } else { // this is an EXTLOAD or ZEXTLOAD
1474 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1475 switch (TypeBeingLoaded) {
1476 default: assert(0 && "Cannot extload/zextload this type!");
1477 // FIXME: bools?
1478 case MVT::i8: Opc = IA64::LD1; break;
1479 case MVT::i16: Opc = IA64::LD2; break;
1480 case MVT::i32: Opc = IA64::LD4; break;
1481 case MVT::f32: Opc = IA64::LDF4; break;
1482 }
1483 }
1484
1485 SDOperand Chain = N.getOperand(0);
1486 SDOperand Address = N.getOperand(1);
1487
1488 if(Address.getOpcode() == ISD::GlobalAddress) {
1489 Select(Chain);
1490 unsigned dummy = MakeReg(MVT::i64);
1491 unsigned dummy2 = MakeReg(MVT::i64);
1492 BuildMI(BB, IA64::ADD, 2, dummy)
1493 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1494 .addReg(IA64::r1);
1495 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1496 if(!isBool)
1497 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1498 else { // emit a little pseudocode to load a bool (stored in one byte)
1499 // into a predicate register
1500 assert(Opc==IA64::LD1 && "problem loading a bool");
1501 unsigned dummy3 = MakeReg(MVT::i64);
1502 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1503 // we compare to 0. true? 0. false? 1.
1504 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1505 }
1506 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1507 Select(Chain);
1508 IA64Lowering.restoreGP(BB);
1509 unsigned dummy = MakeReg(MVT::i64);
1510 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1511 .addReg(IA64::r1); // CPI+GP
1512 if(!isBool)
1513 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1514 else { // emit a little pseudocode to load a bool (stored in one byte)
1515 // into a predicate register
1516 assert(Opc==IA64::LD1 && "problem loading a bool");
1517 unsigned dummy3 = MakeReg(MVT::i64);
1518 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1519 // we compare to 0. true? 0. false? 1.
1520 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1521 }
1522 } else if(Address.getOpcode() == ISD::FrameIndex) {
1523 Select(Chain); // FIXME ? what about bools?
1524 unsigned dummy = MakeReg(MVT::i64);
1525 BuildMI(BB, IA64::MOV, 1, dummy)
1526 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1527 if(!isBool)
1528 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1529 else { // emit a little pseudocode to load a bool (stored in one byte)
1530 // into a predicate register
1531 assert(Opc==IA64::LD1 && "problem loading a bool");
1532 unsigned dummy3 = MakeReg(MVT::i64);
1533 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1534 // we compare to 0. true? 0. false? 1.
1535 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1536 }
1537 } else { // none of the above...
1538 Select(Chain);
1539 Tmp2 = SelectExpr(Address);
1540 if(!isBool)
1541 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1542 else { // emit a little pseudocode to load a bool (stored in one byte)
1543 // into a predicate register
1544 assert(Opc==IA64::LD1 && "problem loading a bool");
1545 unsigned dummy = MakeReg(MVT::i64);
1546 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1547 // we compare to 0. true? 0. false? 1.
1548 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1549 }
1550 }
1551
1552 return Result;
1553 }
1554
1555 case ISD::CopyFromReg: {
1556 if (Result == 1)
1557 Result = ExprMap[N.getValue(0)] =
1558 MakeReg(N.getValue(0).getValueType());
1559
1560 SDOperand Chain = N.getOperand(0);
1561
1562 Select(Chain);
1563 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1564
1565 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1566 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1567 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1568 // (r) Result =cmp.eq.unc(r0,r0)
1569 else
1570 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1571 return Result;
1572 }
1573
1574 case ISD::CALL: {
1575 Select(N.getOperand(0));
1576
1577 // The chain for this call is now lowered.
1578 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1579
1580 //grab the arguments
1581 std::vector<unsigned> argvregs;
1582
1583 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1584 argvregs.push_back(SelectExpr(N.getOperand(i)));
1585
1586 // see section 8.5.8 of "Itanium Software Conventions and
1587 // Runtime Architecture Guide to see some examples of what's going
1588 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1589 // while FP args get mapped to F8->F15 as needed)
1590
1591 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1592
1593 // in reg args
1594 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1595 {
1596 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1597 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1598 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1599 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1600
1601 switch(N.getOperand(i+2).getValueType())
1602 {
1603 default: // XXX do we need to support MVT::i1 here?
1604 Node->dump();
1605 N.getOperand(i).Val->dump();
1606 std::cerr << "Type for " << i << " is: " <<
1607 N.getOperand(i+2).getValueType() << std::endl;
1608 assert(0 && "Unknown value type for call");
1609 case MVT::i64:
1610 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1611 break;
1612 case MVT::f64:
1613 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1614 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001615 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001616 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1617 break;
1618 }
1619 }
1620
1621 //in mem args
1622 for (int i = 8, e = argvregs.size(); i < e; ++i)
1623 {
1624 unsigned tempAddr = MakeReg(MVT::i64);
1625
1626 switch(N.getOperand(i+2).getValueType()) {
1627 default:
1628 Node->dump();
1629 N.getOperand(i).Val->dump();
1630 std::cerr << "Type for " << i << " is: " <<
1631 N.getOperand(i+2).getValueType() << "\n";
1632 assert(0 && "Unknown value type for call");
1633 case MVT::i1: // FIXME?
1634 case MVT::i8:
1635 case MVT::i16:
1636 case MVT::i32:
1637 case MVT::i64:
1638 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1639 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1640 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1641 break;
1642 case MVT::f32:
1643 case MVT::f64:
1644 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1645 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1646 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1647 break;
1648 }
1649 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001650
1651 /* XXX we want to re-enable direct branches! crippling them now
1652 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001653 //build the right kind of call
1654 if (GlobalAddressSDNode *GASD =
1655 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1656 {
1657 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1658 IA64Lowering.restoreGP_SP_RP(BB);
1659 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001660 ^^^^^^^^^^^^^ we want this code one day XXX */
1661 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001662 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001663 { // FIXME : currently need this case for correctness, to avoid
1664 // "non-pic code with imm relocation against dynamic symbol" errors
1665 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001666 .addExternalSymbol(ESSDN->getSymbol(), true);
1667 IA64Lowering.restoreGP_SP_RP(BB);
1668 }
1669 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001670 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001671
1672 unsigned targetEntryPoint=MakeReg(MVT::i64);
1673 unsigned targetGPAddr=MakeReg(MVT::i64);
1674 unsigned currentGP=MakeReg(MVT::i64);
1675
1676 // b6 is a scratch branch register, we load the target entry point
1677 // from the base of the function descriptor
1678 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1679 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1680
1681 // save the current GP:
1682 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1683
1684 /* TODO: we need to make sure doing this never, ever loads a
1685 * bogus value into r1 (GP). */
1686 // load the target GP (which is at mem[functiondescriptor+8])
1687 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1688 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1689 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1690
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001691 // and then jump: (well, call)
1692 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001693 // and finally restore the old GP
1694 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1695 IA64Lowering.restoreSP_RP(BB);
1696 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001697
1698 switch (Node->getValueType(0)) {
1699 default: assert(0 && "Unknown value type for call result!");
1700 case MVT::Other: return 1;
1701 case MVT::i1:
1702 BuildMI(BB, IA64::CMPNE, 2, Result)
1703 .addReg(IA64::r8).addReg(IA64::r0);
1704 break;
1705 case MVT::i8:
1706 case MVT::i16:
1707 case MVT::i32:
1708 case MVT::i64:
1709 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1710 break;
1711 case MVT::f64:
1712 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1713 break;
1714 }
1715 return Result+N.ResNo;
1716 }
1717
1718 } // <- uhhh XXX
1719 return 0;
1720}
1721
1722void ISel::Select(SDOperand N) {
1723 unsigned Tmp1, Tmp2, Opc;
1724 unsigned opcode = N.getOpcode();
1725
Nate Begeman85fdeb22005-03-24 04:39:54 +00001726 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001727 return; // Already selected.
1728
1729 SDNode *Node = N.Val;
1730
1731 switch (Node->getOpcode()) {
1732 default:
1733 Node->dump(); std::cerr << "\n";
1734 assert(0 && "Node not handled yet!");
1735
1736 case ISD::EntryToken: return; // Noop
1737
1738 case ISD::TokenFactor: {
1739 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1740 Select(Node->getOperand(i));
1741 return;
1742 }
1743
1744 case ISD::CopyToReg: {
1745 Select(N.getOperand(0));
1746 Tmp1 = SelectExpr(N.getOperand(1));
1747 Tmp2 = cast<RegSDNode>(N)->getReg();
1748
1749 if (Tmp1 != Tmp2) {
1750 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1751 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1752 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1753 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1754 else
1755 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1756 // XXX is this the right way 'round? ;)
1757 }
1758 return;
1759 }
1760
1761 case ISD::RET: {
1762
1763 /* what the heck is going on here:
1764
1765<_sabre_> ret with two operands is obvious: chain and value
1766<camel_> yep
1767<_sabre_> ret with 3 values happens when 'expansion' occurs
1768<_sabre_> e.g. i64 gets split into 2x i32
1769<camel_> oh right
1770<_sabre_> you don't have this case on ia64
1771<camel_> yep
1772<_sabre_> so the two returned values go into EAX/EDX on ia32
1773<camel_> ahhh *memories*
1774<_sabre_> :)
1775<camel_> ok, thanks :)
1776<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1777<_sabre_> this is the first operand always
1778<_sabre_> these operand often define chains, they are the last operand
1779<_sabre_> they are printed as 'ch' if you do DAG.dump()
1780 */
1781
1782 switch (N.getNumOperands()) {
1783 default:
1784 assert(0 && "Unknown return instruction!");
1785 case 2:
1786 Select(N.getOperand(0));
1787 Tmp1 = SelectExpr(N.getOperand(1));
1788 switch (N.getOperand(1).getValueType()) {
1789 default: assert(0 && "All other types should have been promoted!!");
1790 // FIXME: do I need to add support for bools here?
1791 // (return '0' or '1' r8, basically...)
Duraid Madinaca494fd2005-04-12 14:54:44 +00001792 //
1793 // FIXME: need to round floats - 80 bits is bad, the tester
1794 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001795 case MVT::i64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001796 // we mark r8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001797 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1798 break;
1799 case MVT::f64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001800 // we mark F8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001801 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1802 }
1803 break;
1804 case 1:
1805 Select(N.getOperand(0));
1806 break;
1807 }
1808 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1809 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1810 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1811 return;
1812 }
1813
1814 case ISD::BR: {
1815 Select(N.getOperand(0));
1816 MachineBasicBlock *Dest =
1817 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1818 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1819 // XXX HACK! we do _not_ need long branches all the time
1820 return;
1821 }
1822
1823 case ISD::ImplicitDef: {
1824 Select(N.getOperand(0));
1825 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1826 return;
1827 }
1828
1829 case ISD::BRCOND: {
1830 MachineBasicBlock *Dest =
1831 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1832
1833 Select(N.getOperand(0));
1834 Tmp1 = SelectExpr(N.getOperand(1));
1835 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1836 // XXX HACK! we do _not_ need long branches all the time
1837 return;
1838 }
1839
1840 case ISD::EXTLOAD:
1841 case ISD::ZEXTLOAD:
1842 case ISD::SEXTLOAD:
1843 case ISD::LOAD:
1844 case ISD::CALL:
1845 case ISD::CopyFromReg:
1846 case ISD::DYNAMIC_STACKALLOC:
1847 SelectExpr(N);
1848 return;
1849
1850 case ISD::TRUNCSTORE:
1851 case ISD::STORE: {
1852 Select(N.getOperand(0));
1853 Tmp1 = SelectExpr(N.getOperand(1)); // value
1854
1855 bool isBool=false;
1856
1857 if(opcode == ISD::STORE) {
1858 switch (N.getOperand(1).getValueType()) {
1859 default: assert(0 && "Cannot store this type!");
1860 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1861 // FIXME?: for now, we treat bool loads the same as i8 stores */
1862 case MVT::i8: Opc = IA64::ST1; break;
1863 case MVT::i16: Opc = IA64::ST2; break;
1864 case MVT::i32: Opc = IA64::ST4; break;
1865 case MVT::i64: Opc = IA64::ST8; break;
1866
1867 case MVT::f32: Opc = IA64::STF4; break;
1868 case MVT::f64: Opc = IA64::STF8; break;
1869 }
1870 } else { // truncstore
1871 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1872 default: assert(0 && "unknown type in truncstore");
1873 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1874 //FIXME: DAG does not promote this load?
1875 case MVT::i8: Opc = IA64::ST1; break;
1876 case MVT::i16: Opc = IA64::ST2; break;
1877 case MVT::i32: Opc = IA64::ST4; break;
1878 case MVT::f32: Opc = IA64::STF4; break;
1879 }
1880 }
1881
1882 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1883 unsigned dummy = MakeReg(MVT::i64);
1884 unsigned dummy2 = MakeReg(MVT::i64);
1885 BuildMI(BB, IA64::ADD, 2, dummy)
1886 .addGlobalAddress(cast<GlobalAddressSDNode>
1887 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1888 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1889
1890 if(!isBool)
1891 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1892 else { // we are storing a bool, so emit a little pseudocode
1893 // to store a predicate register as one byte
1894 assert(Opc==IA64::ST1);
1895 unsigned dummy3 = MakeReg(MVT::i64);
1896 unsigned dummy4 = MakeReg(MVT::i64);
1897 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001898 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001899 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1900 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1901 }
1902 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1903
1904 // FIXME? (what about bools?)
1905
1906 unsigned dummy = MakeReg(MVT::i64);
1907 BuildMI(BB, IA64::MOV, 1, dummy)
1908 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1909 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1910 } else { // otherwise
1911 Tmp2 = SelectExpr(N.getOperand(2)); //address
1912 if(!isBool)
1913 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1914 else { // we are storing a bool, so emit a little pseudocode
1915 // to store a predicate register as one byte
1916 assert(Opc==IA64::ST1);
1917 unsigned dummy3 = MakeReg(MVT::i64);
1918 unsigned dummy4 = MakeReg(MVT::i64);
1919 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001920 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001921 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1922 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1923 }
1924 }
1925 return;
1926 }
1927
1928 case ISD::ADJCALLSTACKDOWN:
1929 case ISD::ADJCALLSTACKUP: {
1930 Select(N.getOperand(0));
1931 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1932
1933 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1934 IA64::ADJUSTCALLSTACKUP;
1935 BuildMI(BB, Opc, 1).addImm(Tmp1);
1936 return;
1937 }
1938
1939 return;
1940 }
1941 assert(0 && "GAME OVER. INSERT COIN?");
1942}
1943
1944
1945/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1946/// into a machine code representation using pattern matching and a machine
1947/// description file.
1948///
1949FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1950 return new ISel(TM);
1951}
1952
1953