blob: c501fa1c331a7938f8f166dd57de143ba4efbcda [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.
Duraid Madinac02780e2005-04-13 04:50:54 +0000448static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000449 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
Duraid Madinac02780e2005-04-13 04:50:54 +0000458/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
459/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
460static unsigned ExactLog2sub1(uint64_t Val) {
461 unsigned int n;
462 for(n=0; n<64; n++) {
463 if(Val==(uint64_t)((1<<n)-1))
464 return n;
465 }
466 return 666;
467}
468
Duraid Madina4826a072005-04-06 09:55:17 +0000469/// ponderIntegerDivisionBy - When handling integer divides, if the divide
470/// is by a constant such that we can efficiently codegen it, this
471/// function says what to do. Currently, it returns 0 if the division must
472/// become a genuine divide, and 1 if the division can be turned into a
473/// right shift.
474static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
475 unsigned& Imm) {
476 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
477 // a constant, give up.
478
479 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
480
481 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
482 return 1;
483 }
484
485 return 0; // fallthrough
486}
487
Duraid Madinac02780e2005-04-13 04:50:54 +0000488static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
489 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
490 // a constant, give up.
491
492 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
493
494 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
495 return 1; // say so
496 }
497
498 return 0; // fallthrough
499}
500
Duraid Madinaf55e4032005-04-07 12:33:38 +0000501static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
502 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
503 // constant, give up.
504 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
505
506 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
507 Imm = v & 0x3FFF; // 14 bits
508 return 1;
509 }
510 return 0; // fallthrough
511}
512
513static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
514 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
515 // constant, give up.
516 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
517
518 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
519 Imm = v & 0xFF; // 8 bits
520 return 1;
521 }
522 return 0; // fallthrough
523}
524
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000525unsigned ISel::SelectExpr(SDOperand N) {
526 unsigned Result;
527 unsigned Tmp1, Tmp2, Tmp3;
528 unsigned Opc = 0;
529 MVT::ValueType DestType = N.getValueType();
530
531 unsigned opcode = N.getOpcode();
532
533 SDNode *Node = N.Val;
534 SDOperand Op0, Op1;
535
536 if (Node->getOpcode() == ISD::CopyFromReg)
537 // Just use the specified register as our input.
538 return dyn_cast<RegSDNode>(Node)->getReg();
539
540 unsigned &Reg = ExprMap[N];
541 if (Reg) return Reg;
542
543 if (N.getOpcode() != ISD::CALL)
544 Reg = Result = (N.getValueType() != MVT::Other) ?
545 MakeReg(N.getValueType()) : 1;
546 else {
547 // If this is a call instruction, make sure to prepare ALL of the result
548 // values as well as the chain.
549 if (Node->getNumValues() == 1)
550 Reg = Result = 1; // Void call, just a chain.
551 else {
552 Result = MakeReg(Node->getValueType(0));
553 ExprMap[N.getValue(0)] = Result;
554 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
555 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
556 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
557 }
558 }
559
560 switch (N.getOpcode()) {
561 default:
562 Node->dump();
563 assert(0 && "Node not handled!\n");
564
565 case ISD::FrameIndex: {
566 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
567 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
568 return Result;
569 }
570
571 case ISD::ConstantPool: {
572 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
573 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
574 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
575 .addReg(IA64::r1);
576 return Result;
577 }
578
579 case ISD::ConstantFP: {
580 Tmp1 = Result; // Intermediate Register
581 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
582 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
583 Tmp1 = MakeReg(MVT::f64);
584
585 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
586 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
587 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
588 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
589 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
590 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
591 else
592 assert(0 && "Unexpected FP constant!");
593 if (Tmp1 != Result)
594 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
595 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
596 .addReg(IA64::F0);
597 return Result;
598 }
599
600 case ISD::DYNAMIC_STACKALLOC: {
601 // Generate both result values.
602 if (Result != 1)
603 ExprMap[N.getValue(1)] = 1; // Generate the token
604 else
605 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
606
607 // FIXME: We are currently ignoring the requested alignment for handling
608 // greater than the stack alignment. This will need to be revisited at some
609 // point. Align = N.getOperand(2);
610
611 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
612 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
613 std::cerr << "Cannot allocate stack object with greater alignment than"
614 << " the stack alignment yet!";
615 abort();
616 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000617
618/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000619 Select(N.getOperand(0));
620 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
621 {
622 if (CN->getValue() < 32000)
623 {
624 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
625 .addImm(-CN->getValue());
626 } else {
627 Tmp1 = SelectExpr(N.getOperand(1));
628 // Subtract size from stack pointer, thereby allocating some space.
629 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
630 }
631 } else {
632 Tmp1 = SelectExpr(N.getOperand(1));
633 // Subtract size from stack pointer, thereby allocating some space.
634 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
635 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000636*/
637 Select(N.getOperand(0));
638 Tmp1 = SelectExpr(N.getOperand(1));
639 // Subtract size from stack pointer, thereby allocating some space.
640 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000641 // Put a pointer to the space into the result register, by copying the
642 // stack pointer.
643 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
644 return Result;
645 }
646
647 case ISD::SELECT: {
648 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
649 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
650 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
651
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000652 unsigned bogoResult;
653
654 switch (N.getOperand(1).getValueType()) {
655 default: assert(0 &&
656 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
657 case MVT::i64:
658 bogoResult=MakeReg(MVT::i64);
659 break;
660 case MVT::f64:
661 bogoResult=MakeReg(MVT::f64);
662 break;
663 }
Duraid Madina69c8e202005-04-01 10:35:00 +0000664
665 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
666 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
667 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
668 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000669 return Result;
670 }
671
672 case ISD::Constant: {
673 unsigned depositPos=0;
674 unsigned depositLen=0;
675 switch (N.getValueType()) {
676 default: assert(0 && "Cannot use constants of this type!");
677 case MVT::i1: { // if a bool, we don't 'load' so much as generate
678 // the constant:
679 if(cast<ConstantSDNode>(N)->getValue()) // true:
680 BuildMI(BB, IA64::CMPEQ, 2, Result)
681 .addReg(IA64::r0).addReg(IA64::r0);
682 else // false:
683 BuildMI(BB, IA64::CMPNE, 2, Result)
684 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000685 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000686 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000687 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000688 }
689
690 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000691
692 if(immediate==0) { // if the constant is just zero,
693 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
694 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000695 }
696
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000697 if (immediate <= 8191 && immediate >= -8192) {
698 // if this constants fits in 14 bits, we use a mov the assembler will
699 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
700 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
701 return Result; // early exit
702 }
703
704 if (immediate <= 2097151 && immediate >= -2097152) {
705 // if this constants fits in 22 bits, we use a mov the assembler will
706 // turn into: "addl rDest=imm,r0"
707 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
708 return Result; // early exit
709 }
710
711 /* otherwise, our immediate is big, so we use movl */
712 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +0000713 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000714 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000715 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000716
717 case ISD::UNDEF: {
718 BuildMI(BB, IA64::IDEF, 0, Result);
719 return Result;
720 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000721
722 case ISD::GlobalAddress: {
723 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
724 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000725
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000726 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000727 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +0000728
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000729 return Result;
730 }
731
732 case ISD::ExternalSymbol: {
733 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +0000734// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
735 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000736 return Result;
737 }
738
739 case ISD::FP_EXTEND: {
740 Tmp1 = SelectExpr(N.getOperand(0));
741 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
742 return Result;
743 }
744
745 case ISD::ZERO_EXTEND: {
746 Tmp1 = SelectExpr(N.getOperand(0)); // value
747
748 switch (N.getOperand(0).getValueType()) {
749 default: assert(0 && "Cannot zero-extend this type!");
750 case MVT::i8: Opc = IA64::ZXT1; break;
751 case MVT::i16: Opc = IA64::ZXT2; break;
752 case MVT::i32: Opc = IA64::ZXT4; break;
753
754 // we handle bools differently! :
755 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
756 unsigned dummy = MakeReg(MVT::i64);
757 // first load zero:
758 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
759 // ...then conditionally (PR:Tmp1) add 1:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000760 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000761 .addImm(1).addReg(Tmp1);
762 return Result; // XXX early exit!
763 }
764 }
765
766 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
767 return Result;
768 }
769
770 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
771
772assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
773
774 Tmp1 = SelectExpr(N.getOperand(0)); // value
775
776 switch (N.getOperand(0).getValueType()) {
777 default: assert(0 && "Cannot sign-extend this type!");
778 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
779 Opc = IA64::SXT1; break;
780 // FIXME: for now, we treat bools the same as i8s
781 case MVT::i8: Opc = IA64::SXT1; break;
782 case MVT::i16: Opc = IA64::SXT2; break;
783 case MVT::i32: Opc = IA64::SXT4; break;
784 }
785
786 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
787 return Result;
788 }
789
790 case ISD::TRUNCATE: {
791 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
792 // of R0 appropriately.
793 switch (N.getOperand(0).getValueType()) {
794 default: assert(0 && "Unknown truncate!");
795 case MVT::i64: break;
796 }
797 Tmp1 = SelectExpr(N.getOperand(0));
798 unsigned depositPos, depositLen;
799
800 switch (N.getValueType()) {
801 default: assert(0 && "Unknown truncate!");
802 case MVT::i1: {
803 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
804 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
805 .addReg(IA64::r0);
806 return Result; // XXX early exit!
807 }
808 case MVT::i8: depositPos=0; depositLen=8; break;
809 case MVT::i16: depositPos=0; depositLen=16; break;
810 case MVT::i32: depositPos=0; depositLen=32; break;
811 }
812 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
813 .addImm(depositPos).addImm(depositLen);
814 return Result;
815 }
816
817/*
818 case ISD::FP_ROUND: {
819 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
820 "error: trying to FP_ROUND something other than f64 -> f32!\n");
821 Tmp1 = SelectExpr(N.getOperand(0));
822 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
823 // we add 0.0 using a single precision add to do rounding
824 return Result;
825 }
826*/
827
828// FIXME: the following 4 cases need cleaning
829 case ISD::SINT_TO_FP: {
830 Tmp1 = SelectExpr(N.getOperand(0));
831 Tmp2 = MakeReg(MVT::f64);
832 unsigned dummy = MakeReg(MVT::f64);
833 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
834 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
835 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
836 return Result;
837 }
838
839 case ISD::UINT_TO_FP: {
840 Tmp1 = SelectExpr(N.getOperand(0));
841 Tmp2 = MakeReg(MVT::f64);
842 unsigned dummy = MakeReg(MVT::f64);
843 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
844 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
845 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
846 return Result;
847 }
848
849 case ISD::FP_TO_SINT: {
850 Tmp1 = SelectExpr(N.getOperand(0));
851 Tmp2 = MakeReg(MVT::f64);
852 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
853 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
854 return Result;
855 }
856
857 case ISD::FP_TO_UINT: {
858 Tmp1 = SelectExpr(N.getOperand(0));
859 Tmp2 = MakeReg(MVT::f64);
860 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
861 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
862 return Result;
863 }
864
865 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +0000866 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
867 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
868 // into an fma, do so:
869 // ++FusedFP; // Statistic
870 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
871 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
872 Tmp3 = SelectExpr(N.getOperand(1));
873 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
874 return Result; // early exit
875 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000876 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000877 if(DestType != MVT::f64) { // integer addition:
878 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
879 case 1: // adding a constant that's 14 bits
880 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
881 return Result; // early exit
882 } // fallthrough and emit a reg+reg ADD:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000883 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000884 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
885 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000886 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000887 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
888 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000889 return Result;
890 }
891
892 case ISD::MUL: {
893 Tmp1 = SelectExpr(N.getOperand(0));
894 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina4826a072005-04-06 09:55:17 +0000895
896 if(DestType != MVT::f64) { // TODO: speed!
897 // boring old integer multiply with xma
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000898 unsigned TempFR1=MakeReg(MVT::f64);
899 unsigned TempFR2=MakeReg(MVT::f64);
900 unsigned TempFR3=MakeReg(MVT::f64);
901 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
902 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
903 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
904 .addReg(IA64::F0);
905 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
906 }
907 else // floating point multiply
908 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
909 return Result;
910 }
911
912 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +0000913 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
914 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
915 // into an fms, do so:
916 // ++FusedFP; // Statistic
917 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
918 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
919 Tmp3 = SelectExpr(N.getOperand(1));
920 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
921 return Result; // early exit
922 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000923 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000924 if(DestType != MVT::f64) { // integer subtraction:
925 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
926 case 1: // subtracting *from* an 8 bit constant:
927 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
928 return Result; // early exit
929 } // fallthrough and emit a reg+reg SUB:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000930 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000931 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
932 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000933 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000934 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +0000935 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000936 return Result;
937 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000938
939 case ISD::FABS: {
940 Tmp1 = SelectExpr(N.getOperand(0));
941 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
942 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
943 return Result;
944 }
945
946 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000947 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000948
949 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
950 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
951 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
952 } else {
953 Tmp1 = SelectExpr(N.getOperand(0));
954 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
955 }
956
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000957 return Result;
958 }
959
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000960 case ISD::AND: {
961 switch (N.getValueType()) {
962 default: assert(0 && "Cannot AND this type!");
963 case MVT::i1: { // if a bool, we emit a pseudocode AND
964 unsigned pA = SelectExpr(N.getOperand(0));
965 unsigned pB = SelectExpr(N.getOperand(1));
966
967/* our pseudocode for AND is:
968 *
969(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
970 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
971 ;;
972(pB) cmp.ne pTemp,p0 = r0,r0
973 ;;
974(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
975
976*/
977 unsigned pTemp = MakeReg(MVT::i1);
978
979 unsigned bogusTemp1 = MakeReg(MVT::i1);
980 unsigned bogusTemp2 = MakeReg(MVT::i1);
981 unsigned bogusTemp3 = MakeReg(MVT::i1);
982 unsigned bogusTemp4 = MakeReg(MVT::i1);
983
984 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
985 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
986 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
987 .addReg(IA64::r0).addReg(IA64::r0);
988 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
989 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
990 BuildMI(BB, IA64::TPCMPNE, 3, Result)
991 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
992 break;
993 }
Duraid Madinac02780e2005-04-13 04:50:54 +0000994
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000995 // if not a bool, we just AND away:
996 case MVT::i8:
997 case MVT::i16:
998 case MVT::i32:
999 case MVT::i64: {
1000 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001001 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1002 case 1: // ANDing a constant that is 2^n-1 for some n
1003 switch (Tmp3) {
1004 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1005 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1006 break;
1007 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1008 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1009 break;
1010 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1011 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1012 break;
1013 default: // otherwise, use dep.z to paste zeros
1014 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1015 .addImm(0).addImm(Tmp3);
1016 break;
1017 }
1018 return Result; // early exit
1019 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001020 Tmp2 = SelectExpr(N.getOperand(1));
1021 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001022 }
1023 }
1024 return Result;
1025 }
1026
1027 case ISD::OR: {
1028 switch (N.getValueType()) {
1029 default: assert(0 && "Cannot OR this type!");
1030 case MVT::i1: { // if a bool, we emit a pseudocode OR
1031 unsigned pA = SelectExpr(N.getOperand(0));
1032 unsigned pB = SelectExpr(N.getOperand(1));
1033
1034 unsigned pTemp1 = MakeReg(MVT::i1);
1035
1036/* our pseudocode for OR is:
1037 *
1038
1039pC = pA OR pB
1040-------------
1041
1042(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1043 ;;
1044(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
1045
1046*/
1047 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
1048 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
1049 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
1050 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
1051 break;
1052 }
1053 // if not a bool, we just OR away:
1054 case MVT::i8:
1055 case MVT::i16:
1056 case MVT::i32:
1057 case MVT::i64: {
1058 Tmp1 = SelectExpr(N.getOperand(0));
1059 Tmp2 = SelectExpr(N.getOperand(1));
1060 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1061 break;
1062 }
1063 }
1064 return Result;
1065 }
1066
1067 case ISD::XOR: {
1068 switch (N.getValueType()) {
1069 default: assert(0 && "Cannot XOR this type!");
1070 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1071 unsigned pY = SelectExpr(N.getOperand(0));
1072 unsigned pZ = SelectExpr(N.getOperand(1));
1073
1074/* one possible routine for XOR is:
1075
1076 // Compute px = py ^ pz
1077 // using sum of products: px = (py & !pz) | (pz & !py)
1078 // Uses 5 instructions in 3 cycles.
1079 // cycle 1
1080(pz) cmp.eq.unc px = r0, r0 // px = pz
1081(py) cmp.eq.unc pt = r0, r0 // pt = py
1082 ;;
1083 // cycle 2
1084(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1085(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1086 ;;
1087 } { .mmi
1088 // cycle 3
1089(pt) cmp.eq.or px = r0, r0 // px = px | pt
1090
1091*** Another, which we use here, requires one scratch GR. it is:
1092
1093 mov rt = 0 // initialize rt off critical path
1094 ;;
1095
1096 // cycle 1
1097(pz) cmp.eq.unc px = r0, r0 // px = pz
1098(pz) mov rt = 1 // rt = pz
1099 ;;
1100 // cycle 2
1101(py) cmp.ne px = 1, rt // if (py) px = !pz
1102
1103.. these routines kindly provided by Jim Hull
1104*/
1105 unsigned rt = MakeReg(MVT::i64);
1106
1107 // these two temporaries will never actually appear,
1108 // due to the two-address form of some of the instructions below
1109 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1110 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1111
1112 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1113 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
1114 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
1115 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
1116 .addReg(bogoGR).addImm(1).addReg(pZ);
1117 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
1118 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
1119 break;
1120 }
1121 // if not a bool, we just XOR away:
1122 case MVT::i8:
1123 case MVT::i16:
1124 case MVT::i32:
1125 case MVT::i64: {
1126 Tmp1 = SelectExpr(N.getOperand(0));
1127 Tmp2 = SelectExpr(N.getOperand(1));
1128 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1129 break;
1130 }
1131 }
1132 return Result;
1133 }
1134
1135 case ISD::SHL: {
1136 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001137 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1138 Tmp2 = CN->getValue();
1139 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1140 } else {
1141 Tmp2 = SelectExpr(N.getOperand(1));
1142 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1143 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001144 return Result;
1145 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 case ISD::SRL: {
1148 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001149 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1150 Tmp2 = CN->getValue();
1151 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1152 } else {
1153 Tmp2 = SelectExpr(N.getOperand(1));
1154 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1155 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001156 return Result;
1157 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001158
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001159 case ISD::SRA: {
1160 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001161 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1162 Tmp2 = CN->getValue();
1163 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1164 } else {
1165 Tmp2 = SelectExpr(N.getOperand(1));
1166 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1167 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001168 return Result;
1169 }
1170
1171 case ISD::SDIV:
1172 case ISD::UDIV:
1173 case ISD::SREM:
1174 case ISD::UREM: {
1175
1176 Tmp1 = SelectExpr(N.getOperand(0));
1177 Tmp2 = SelectExpr(N.getOperand(1));
1178
1179 bool isFP=false;
1180
1181 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1182 isFP=true;
1183
1184 bool isModulus=false; // is it a division or a modulus?
1185 bool isSigned=false;
1186
1187 switch(N.getOpcode()) {
1188 case ISD::SDIV: isModulus=false; isSigned=true; break;
1189 case ISD::UDIV: isModulus=false; isSigned=false; break;
1190 case ISD::SREM: isModulus=true; isSigned=true; break;
1191 case ISD::UREM: isModulus=true; isSigned=false; break;
1192 }
1193
Duraid Madina4826a072005-04-06 09:55:17 +00001194 if(!isModulus && !isFP) { // if this is an integer divide,
1195 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
1196 case 1: // division by a constant that's a power of 2
1197 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina6dcceb52005-04-08 10:01:48 +00001198 if(isSigned) { // argument could be negative, so emit some code:
1199 unsigned divAmt=Tmp3;
1200 unsigned tempGR1=MakeReg(MVT::i64);
1201 unsigned tempGR2=MakeReg(MVT::i64);
1202 unsigned tempGR3=MakeReg(MVT::i64);
1203 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1204 .addReg(Tmp1).addImm(divAmt-1);
1205 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1206 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1207 BuildMI(BB, IA64::ADD, 2, tempGR3)
1208 .addReg(Tmp1).addReg(tempGR2);
1209 BuildMI(BB, IA64::SHRS, 2, Result)
1210 .addReg(tempGR3).addImm(divAmt);
1211 }
1212 else // unsigned div-by-power-of-2 becomes a simple shift right:
Duraid Madina4826a072005-04-06 09:55:17 +00001213 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1214 return Result; // early exit
1215 }
1216 }
1217
Duraid Madinabeeaab22005-03-31 12:31:11 +00001218 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
1219 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001220 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1221 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1222 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1223 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1224 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1225 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1226 unsigned TmpF7=MakeReg(MVT::f64);
1227 unsigned TmpF8=MakeReg(MVT::f64);
1228 unsigned TmpF9=MakeReg(MVT::f64);
1229 unsigned TmpF10=MakeReg(MVT::f64);
1230 unsigned TmpF11=MakeReg(MVT::f64);
1231 unsigned TmpF12=MakeReg(MVT::f64);
1232 unsigned TmpF13=MakeReg(MVT::f64);
1233 unsigned TmpF14=MakeReg(MVT::f64);
1234 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001235
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001236 // OK, emit some code:
1237
1238 if(!isFP) {
1239 // first, load the inputs into FP regs.
1240 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1241 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1242
1243 // next, convert the inputs to FP
1244 if(isSigned) {
1245 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1246 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1247 } else {
1248 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1249 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1250 }
1251
1252 } else { // this is an FP divide/remainder, so we 'leak' some temp
1253 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1254 TmpF3=Tmp1;
1255 TmpF4=Tmp2;
1256 }
1257
1258 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001259 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1260 BuildMI(BB, IA64::FRCPAS1, 4)
1261 .addReg(TmpF5, MachineOperand::Def)
1262 .addReg(TmpPR, MachineOperand::Def)
1263 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001264
Duraid Madinabeeaab22005-03-31 12:31:11 +00001265 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1266 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1267 // TPCMPNE below
1268 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1269 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1270 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1271 }
1272
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001273 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1274 // precision, don't need this much for f32/i32)
1275 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1276 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1277 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1278 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1279 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1280 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1281 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1282 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1283 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1284 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1285 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1286 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1287 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1288 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1289 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1290 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001291
1292 // FIXME: this is unfortunate :(
1293 // the story is that the dest reg of the fnma above and the fma below
1294 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1295 // be the same register, or this code breaks if the first argument is
1296 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001297 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1298 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1299
Duraid Madina6e02e682005-04-04 05:05:52 +00001300 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1301 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1302 }
1303
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001304 if(!isFP) {
1305 // round to an integer
1306 if(isSigned)
1307 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1308 else
1309 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1310 } else {
1311 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1312 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1313 // we really do need the above FMOV? ;)
1314 }
1315
1316 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001317 if(isFP) { // extra worrying about div-by-zero
1318 unsigned bogoResult=MakeReg(MVT::f64);
1319
1320 // we do a 'conditional fmov' (of the correct result, depending
1321 // on how the frcpa predicate turned out)
1322 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1323 .addReg(TmpF12).addReg(TmpPR2);
1324 BuildMI(BB, IA64::CFMOV, 2, Result)
1325 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1326 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001327 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001328 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001329 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001330 } else { // this is a modulus
1331 if(!isFP) {
1332 // answer = q * (-b) + a
1333 unsigned ModulusResult = MakeReg(MVT::f64);
1334 unsigned TmpF = MakeReg(MVT::f64);
1335 unsigned TmpI = MakeReg(MVT::i64);
Duraid Madina6e02e682005-04-04 05:05:52 +00001336
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001337 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1338 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1339 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1340 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1341 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1342 } else { // FP modulus! The horror... the horror....
1343 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1344 }
1345 }
1346
1347 return Result;
1348 }
1349
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001350 case ISD::SIGN_EXTEND_INREG: {
1351 Tmp1 = SelectExpr(N.getOperand(0));
1352 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1353 switch(MVN->getExtraValueType())
1354 {
1355 default:
1356 Node->dump();
1357 assert(0 && "don't know how to sign extend this type");
1358 break;
1359 case MVT::i8: Opc = IA64::SXT1; break;
1360 case MVT::i16: Opc = IA64::SXT2; break;
1361 case MVT::i32: Opc = IA64::SXT4; break;
1362 }
1363 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1364 return Result;
1365 }
1366
1367 case ISD::SETCC: {
1368 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001369
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001370 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1371 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001372
1373 if(ConstantSDNode *CSDN =
1374 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1375 // if we are comparing against a constant zero
1376 if(CSDN->getValue()==0)
1377 Tmp2 = IA64::r0; // then we can just compare against r0
1378 else
1379 Tmp2 = SelectExpr(N.getOperand(1));
1380 } else // not comparing against a constant
1381 Tmp2 = SelectExpr(N.getOperand(1));
1382
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001383 switch (SetCC->getCondition()) {
1384 default: assert(0 && "Unknown integer comparison!");
1385 case ISD::SETEQ:
1386 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1387 break;
1388 case ISD::SETGT:
1389 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1390 break;
1391 case ISD::SETGE:
1392 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1393 break;
1394 case ISD::SETLT:
1395 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1396 break;
1397 case ISD::SETLE:
1398 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1399 break;
1400 case ISD::SETNE:
1401 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1402 break;
1403 case ISD::SETULT:
1404 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1405 break;
1406 case ISD::SETUGT:
1407 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1408 break;
1409 case ISD::SETULE:
1410 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1411 break;
1412 case ISD::SETUGE:
1413 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1414 break;
1415 }
1416 }
1417 else { // if not integer, should be FP. FIXME: what about bools? ;)
1418 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1419 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001420
1421 if(ConstantFPSDNode *CFPSDN =
1422 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1423
1424 // if we are comparing against a constant +0.0 or +1.0
1425 if(CFPSDN->isExactlyValue(+0.0))
1426 Tmp2 = IA64::F0; // then we can just compare against f0
1427 else if(CFPSDN->isExactlyValue(+1.0))
1428 Tmp2 = IA64::F1; // or f1
1429 else
1430 Tmp2 = SelectExpr(N.getOperand(1));
1431 } else // not comparing against a constant
1432 Tmp2 = SelectExpr(N.getOperand(1));
1433
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001434 switch (SetCC->getCondition()) {
1435 default: assert(0 && "Unknown FP comparison!");
1436 case ISD::SETEQ:
1437 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1438 break;
1439 case ISD::SETGT:
1440 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1441 break;
1442 case ISD::SETGE:
1443 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1444 break;
1445 case ISD::SETLT:
1446 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1447 break;
1448 case ISD::SETLE:
1449 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1450 break;
1451 case ISD::SETNE:
1452 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1453 break;
1454 case ISD::SETULT:
1455 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1456 break;
1457 case ISD::SETUGT:
1458 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1459 break;
1460 case ISD::SETULE:
1461 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1462 break;
1463 case ISD::SETUGE:
1464 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1465 break;
1466 }
1467 }
1468 }
1469 else
1470 assert(0 && "this setcc not implemented yet");
1471
1472 return Result;
1473 }
1474
1475 case ISD::EXTLOAD:
1476 case ISD::ZEXTLOAD:
1477 case ISD::LOAD: {
1478 // Make sure we generate both values.
1479 if (Result != 1)
1480 ExprMap[N.getValue(1)] = 1; // Generate the token
1481 else
1482 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1483
1484 bool isBool=false;
1485
1486 if(opcode == ISD::LOAD) { // this is a LOAD
1487 switch (Node->getValueType(0)) {
1488 default: assert(0 && "Cannot load this type!");
1489 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1490 // FIXME: for now, we treat bool loads the same as i8 loads */
1491 case MVT::i8: Opc = IA64::LD1; break;
1492 case MVT::i16: Opc = IA64::LD2; break;
1493 case MVT::i32: Opc = IA64::LD4; break;
1494 case MVT::i64: Opc = IA64::LD8; break;
1495
1496 case MVT::f32: Opc = IA64::LDF4; break;
1497 case MVT::f64: Opc = IA64::LDF8; break;
1498 }
1499 } else { // this is an EXTLOAD or ZEXTLOAD
1500 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1501 switch (TypeBeingLoaded) {
1502 default: assert(0 && "Cannot extload/zextload this type!");
1503 // FIXME: bools?
1504 case MVT::i8: Opc = IA64::LD1; break;
1505 case MVT::i16: Opc = IA64::LD2; break;
1506 case MVT::i32: Opc = IA64::LD4; break;
1507 case MVT::f32: Opc = IA64::LDF4; break;
1508 }
1509 }
1510
1511 SDOperand Chain = N.getOperand(0);
1512 SDOperand Address = N.getOperand(1);
1513
1514 if(Address.getOpcode() == ISD::GlobalAddress) {
1515 Select(Chain);
1516 unsigned dummy = MakeReg(MVT::i64);
1517 unsigned dummy2 = MakeReg(MVT::i64);
1518 BuildMI(BB, IA64::ADD, 2, dummy)
1519 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1520 .addReg(IA64::r1);
1521 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1522 if(!isBool)
1523 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1524 else { // emit a little pseudocode to load a bool (stored in one byte)
1525 // into a predicate register
1526 assert(Opc==IA64::LD1 && "problem loading a bool");
1527 unsigned dummy3 = MakeReg(MVT::i64);
1528 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1529 // we compare to 0. true? 0. false? 1.
1530 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1531 }
1532 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1533 Select(Chain);
1534 IA64Lowering.restoreGP(BB);
1535 unsigned dummy = MakeReg(MVT::i64);
1536 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1537 .addReg(IA64::r1); // CPI+GP
1538 if(!isBool)
1539 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1540 else { // emit a little pseudocode to load a bool (stored in one byte)
1541 // into a predicate register
1542 assert(Opc==IA64::LD1 && "problem loading a bool");
1543 unsigned dummy3 = MakeReg(MVT::i64);
1544 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1545 // we compare to 0. true? 0. false? 1.
1546 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1547 }
1548 } else if(Address.getOpcode() == ISD::FrameIndex) {
1549 Select(Chain); // FIXME ? what about bools?
1550 unsigned dummy = MakeReg(MVT::i64);
1551 BuildMI(BB, IA64::MOV, 1, dummy)
1552 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1553 if(!isBool)
1554 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1555 else { // emit a little pseudocode to load a bool (stored in one byte)
1556 // into a predicate register
1557 assert(Opc==IA64::LD1 && "problem loading a bool");
1558 unsigned dummy3 = MakeReg(MVT::i64);
1559 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1560 // we compare to 0. true? 0. false? 1.
1561 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1562 }
1563 } else { // none of the above...
1564 Select(Chain);
1565 Tmp2 = SelectExpr(Address);
1566 if(!isBool)
1567 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1568 else { // emit a little pseudocode to load a bool (stored in one byte)
1569 // into a predicate register
1570 assert(Opc==IA64::LD1 && "problem loading a bool");
1571 unsigned dummy = MakeReg(MVT::i64);
1572 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1573 // we compare to 0. true? 0. false? 1.
1574 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1575 }
1576 }
1577
1578 return Result;
1579 }
1580
1581 case ISD::CopyFromReg: {
1582 if (Result == 1)
1583 Result = ExprMap[N.getValue(0)] =
1584 MakeReg(N.getValue(0).getValueType());
1585
1586 SDOperand Chain = N.getOperand(0);
1587
1588 Select(Chain);
1589 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1590
1591 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1592 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1593 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1594 // (r) Result =cmp.eq.unc(r0,r0)
1595 else
1596 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1597 return Result;
1598 }
1599
1600 case ISD::CALL: {
1601 Select(N.getOperand(0));
1602
1603 // The chain for this call is now lowered.
1604 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1605
1606 //grab the arguments
1607 std::vector<unsigned> argvregs;
1608
1609 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1610 argvregs.push_back(SelectExpr(N.getOperand(i)));
1611
1612 // see section 8.5.8 of "Itanium Software Conventions and
1613 // Runtime Architecture Guide to see some examples of what's going
1614 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1615 // while FP args get mapped to F8->F15 as needed)
1616
1617 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1618
1619 // in reg args
1620 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1621 {
1622 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1623 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1624 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1625 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1626
1627 switch(N.getOperand(i+2).getValueType())
1628 {
1629 default: // XXX do we need to support MVT::i1 here?
1630 Node->dump();
1631 N.getOperand(i).Val->dump();
1632 std::cerr << "Type for " << i << " is: " <<
1633 N.getOperand(i+2).getValueType() << std::endl;
1634 assert(0 && "Unknown value type for call");
1635 case MVT::i64:
1636 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1637 break;
1638 case MVT::f64:
1639 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1640 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001641 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001642 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1643 break;
1644 }
1645 }
1646
1647 //in mem args
1648 for (int i = 8, e = argvregs.size(); i < e; ++i)
1649 {
1650 unsigned tempAddr = MakeReg(MVT::i64);
1651
1652 switch(N.getOperand(i+2).getValueType()) {
1653 default:
1654 Node->dump();
1655 N.getOperand(i).Val->dump();
1656 std::cerr << "Type for " << i << " is: " <<
1657 N.getOperand(i+2).getValueType() << "\n";
1658 assert(0 && "Unknown value type for call");
1659 case MVT::i1: // FIXME?
1660 case MVT::i8:
1661 case MVT::i16:
1662 case MVT::i32:
1663 case MVT::i64:
1664 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1665 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1666 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1667 break;
1668 case MVT::f32:
1669 case MVT::f64:
1670 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1671 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1672 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1673 break;
1674 }
1675 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001676
1677 /* XXX we want to re-enable direct branches! crippling them now
1678 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001679 //build the right kind of call
1680 if (GlobalAddressSDNode *GASD =
1681 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1682 {
1683 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1684 IA64Lowering.restoreGP_SP_RP(BB);
1685 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001686 ^^^^^^^^^^^^^ we want this code one day XXX */
1687 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001688 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001689 { // FIXME : currently need this case for correctness, to avoid
1690 // "non-pic code with imm relocation against dynamic symbol" errors
1691 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001692 .addExternalSymbol(ESSDN->getSymbol(), true);
1693 IA64Lowering.restoreGP_SP_RP(BB);
1694 }
1695 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001696 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001697
1698 unsigned targetEntryPoint=MakeReg(MVT::i64);
1699 unsigned targetGPAddr=MakeReg(MVT::i64);
1700 unsigned currentGP=MakeReg(MVT::i64);
1701
1702 // b6 is a scratch branch register, we load the target entry point
1703 // from the base of the function descriptor
1704 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1705 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1706
1707 // save the current GP:
1708 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1709
1710 /* TODO: we need to make sure doing this never, ever loads a
1711 * bogus value into r1 (GP). */
1712 // load the target GP (which is at mem[functiondescriptor+8])
1713 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1714 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1715 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1716
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001717 // and then jump: (well, call)
1718 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001719 // and finally restore the old GP
1720 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1721 IA64Lowering.restoreSP_RP(BB);
1722 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001723
1724 switch (Node->getValueType(0)) {
1725 default: assert(0 && "Unknown value type for call result!");
1726 case MVT::Other: return 1;
1727 case MVT::i1:
1728 BuildMI(BB, IA64::CMPNE, 2, Result)
1729 .addReg(IA64::r8).addReg(IA64::r0);
1730 break;
1731 case MVT::i8:
1732 case MVT::i16:
1733 case MVT::i32:
1734 case MVT::i64:
1735 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1736 break;
1737 case MVT::f64:
1738 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1739 break;
1740 }
1741 return Result+N.ResNo;
1742 }
1743
1744 } // <- uhhh XXX
1745 return 0;
1746}
1747
1748void ISel::Select(SDOperand N) {
1749 unsigned Tmp1, Tmp2, Opc;
1750 unsigned opcode = N.getOpcode();
1751
Nate Begeman85fdeb22005-03-24 04:39:54 +00001752 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001753 return; // Already selected.
1754
1755 SDNode *Node = N.Val;
1756
1757 switch (Node->getOpcode()) {
1758 default:
1759 Node->dump(); std::cerr << "\n";
1760 assert(0 && "Node not handled yet!");
1761
1762 case ISD::EntryToken: return; // Noop
1763
1764 case ISD::TokenFactor: {
1765 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1766 Select(Node->getOperand(i));
1767 return;
1768 }
1769
1770 case ISD::CopyToReg: {
1771 Select(N.getOperand(0));
1772 Tmp1 = SelectExpr(N.getOperand(1));
1773 Tmp2 = cast<RegSDNode>(N)->getReg();
1774
1775 if (Tmp1 != Tmp2) {
1776 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1777 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1778 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1779 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1780 else
1781 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1782 // XXX is this the right way 'round? ;)
1783 }
1784 return;
1785 }
1786
1787 case ISD::RET: {
1788
1789 /* what the heck is going on here:
1790
1791<_sabre_> ret with two operands is obvious: chain and value
1792<camel_> yep
1793<_sabre_> ret with 3 values happens when 'expansion' occurs
1794<_sabre_> e.g. i64 gets split into 2x i32
1795<camel_> oh right
1796<_sabre_> you don't have this case on ia64
1797<camel_> yep
1798<_sabre_> so the two returned values go into EAX/EDX on ia32
1799<camel_> ahhh *memories*
1800<_sabre_> :)
1801<camel_> ok, thanks :)
1802<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1803<_sabre_> this is the first operand always
1804<_sabre_> these operand often define chains, they are the last operand
1805<_sabre_> they are printed as 'ch' if you do DAG.dump()
1806 */
1807
1808 switch (N.getNumOperands()) {
1809 default:
1810 assert(0 && "Unknown return instruction!");
1811 case 2:
1812 Select(N.getOperand(0));
1813 Tmp1 = SelectExpr(N.getOperand(1));
1814 switch (N.getOperand(1).getValueType()) {
1815 default: assert(0 && "All other types should have been promoted!!");
1816 // FIXME: do I need to add support for bools here?
1817 // (return '0' or '1' r8, basically...)
Duraid Madinaca494fd2005-04-12 14:54:44 +00001818 //
1819 // FIXME: need to round floats - 80 bits is bad, the tester
1820 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001821 case MVT::i64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001822 // we mark r8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001823 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1824 break;
1825 case MVT::f64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001826 // we mark F8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001827 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1828 }
1829 break;
1830 case 1:
1831 Select(N.getOperand(0));
1832 break;
1833 }
1834 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1835 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1836 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1837 return;
1838 }
1839
1840 case ISD::BR: {
1841 Select(N.getOperand(0));
1842 MachineBasicBlock *Dest =
1843 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1844 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1845 // XXX HACK! we do _not_ need long branches all the time
1846 return;
1847 }
1848
1849 case ISD::ImplicitDef: {
1850 Select(N.getOperand(0));
1851 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1852 return;
1853 }
1854
1855 case ISD::BRCOND: {
1856 MachineBasicBlock *Dest =
1857 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1858
1859 Select(N.getOperand(0));
1860 Tmp1 = SelectExpr(N.getOperand(1));
1861 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1862 // XXX HACK! we do _not_ need long branches all the time
1863 return;
1864 }
1865
1866 case ISD::EXTLOAD:
1867 case ISD::ZEXTLOAD:
1868 case ISD::SEXTLOAD:
1869 case ISD::LOAD:
1870 case ISD::CALL:
1871 case ISD::CopyFromReg:
1872 case ISD::DYNAMIC_STACKALLOC:
1873 SelectExpr(N);
1874 return;
1875
1876 case ISD::TRUNCSTORE:
1877 case ISD::STORE: {
1878 Select(N.getOperand(0));
1879 Tmp1 = SelectExpr(N.getOperand(1)); // value
1880
1881 bool isBool=false;
1882
1883 if(opcode == ISD::STORE) {
1884 switch (N.getOperand(1).getValueType()) {
1885 default: assert(0 && "Cannot store this type!");
1886 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1887 // FIXME?: for now, we treat bool loads the same as i8 stores */
1888 case MVT::i8: Opc = IA64::ST1; break;
1889 case MVT::i16: Opc = IA64::ST2; break;
1890 case MVT::i32: Opc = IA64::ST4; break;
1891 case MVT::i64: Opc = IA64::ST8; break;
1892
1893 case MVT::f32: Opc = IA64::STF4; break;
1894 case MVT::f64: Opc = IA64::STF8; break;
1895 }
1896 } else { // truncstore
1897 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1898 default: assert(0 && "unknown type in truncstore");
1899 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1900 //FIXME: DAG does not promote this load?
1901 case MVT::i8: Opc = IA64::ST1; break;
1902 case MVT::i16: Opc = IA64::ST2; break;
1903 case MVT::i32: Opc = IA64::ST4; break;
1904 case MVT::f32: Opc = IA64::STF4; break;
1905 }
1906 }
1907
1908 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1909 unsigned dummy = MakeReg(MVT::i64);
1910 unsigned dummy2 = MakeReg(MVT::i64);
1911 BuildMI(BB, IA64::ADD, 2, dummy)
1912 .addGlobalAddress(cast<GlobalAddressSDNode>
1913 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1914 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1915
1916 if(!isBool)
1917 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1918 else { // we are storing a bool, so emit a little pseudocode
1919 // to store a predicate register as one byte
1920 assert(Opc==IA64::ST1);
1921 unsigned dummy3 = MakeReg(MVT::i64);
1922 unsigned dummy4 = MakeReg(MVT::i64);
1923 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001924 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001925 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1926 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1927 }
1928 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1929
1930 // FIXME? (what about bools?)
1931
1932 unsigned dummy = MakeReg(MVT::i64);
1933 BuildMI(BB, IA64::MOV, 1, dummy)
1934 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1935 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1936 } else { // otherwise
1937 Tmp2 = SelectExpr(N.getOperand(2)); //address
1938 if(!isBool)
1939 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1940 else { // we are storing a bool, so emit a little pseudocode
1941 // to store a predicate register as one byte
1942 assert(Opc==IA64::ST1);
1943 unsigned dummy3 = MakeReg(MVT::i64);
1944 unsigned dummy4 = MakeReg(MVT::i64);
1945 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001946 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001947 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1948 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1949 }
1950 }
1951 return;
1952 }
1953
1954 case ISD::ADJCALLSTACKDOWN:
1955 case ISD::ADJCALLSTACKUP: {
1956 Select(N.getOperand(0));
1957 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1958
1959 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1960 IA64::ADJUSTCALLSTACKUP;
1961 BuildMI(BB, Opc, 1).addImm(Tmp1);
1962 return;
1963 }
1964
1965 return;
1966 }
1967 assert(0 && "GAME OVER. INSERT COIN?");
1968}
1969
1970
1971/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1972/// into a machine code representation using pattern matching and a machine
1973/// description file.
1974///
1975FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1976 return new ISel(TM);
1977}
1978
1979