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