blob: 3e74802d1a3cddfec4e8d206436d05ce8a0591ea [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);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067
68 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
69 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000071 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000072
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++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000463 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000464 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 Madinaed095022005-04-13 06:12:04 +0000876
877 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
878 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
879 // this add into a shladd, try:
880 ConstantSDNode *CSD = NULL;
881 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
882 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
883
884 // ++FusedSHLADD; // Statistic
885 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
886 int shl_amt = CSD->getValue();
887 Tmp3 = SelectExpr(N.getOperand(1));
888
889 BuildMI(BB, IA64::SHLADD, 3, Result)
890 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
891 return Result; // early exit
892 }
893 }
894
895 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000896 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000897 if(DestType != MVT::f64) { // integer addition:
898 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
899 case 1: // adding a constant that's 14 bits
900 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
901 return Result; // early exit
902 } // fallthrough and emit a reg+reg ADD:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000903 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000904 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
905 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000906 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000907 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
908 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000909 return Result;
910 }
911
912 case ISD::MUL: {
913 Tmp1 = SelectExpr(N.getOperand(0));
914 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina4826a072005-04-06 09:55:17 +0000915
916 if(DestType != MVT::f64) { // TODO: speed!
917 // boring old integer multiply with xma
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000918 unsigned TempFR1=MakeReg(MVT::f64);
919 unsigned TempFR2=MakeReg(MVT::f64);
920 unsigned TempFR3=MakeReg(MVT::f64);
921 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
922 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
923 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
924 .addReg(IA64::F0);
925 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
926 }
927 else // floating point multiply
928 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
929 return Result;
930 }
931
932 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +0000933 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
934 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
935 // into an fms, do so:
936 // ++FusedFP; // Statistic
937 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
938 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
939 Tmp3 = SelectExpr(N.getOperand(1));
940 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
941 return Result; // early exit
942 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000943 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000944 if(DestType != MVT::f64) { // integer subtraction:
945 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
946 case 1: // subtracting *from* an 8 bit constant:
947 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
948 return Result; // early exit
949 } // fallthrough and emit a reg+reg SUB:
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000950 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +0000951 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
952 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000953 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000954 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +0000955 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000956 return Result;
957 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000958
959 case ISD::FABS: {
960 Tmp1 = SelectExpr(N.getOperand(0));
961 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
962 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
963 return Result;
964 }
965
966 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000967 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +0000968
969 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
970 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
971 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
972 } else {
973 Tmp1 = SelectExpr(N.getOperand(0));
974 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
975 }
976
Duraid Madinaa7ee8b82005-04-02 05:18:38 +0000977 return Result;
978 }
979
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000980 case ISD::AND: {
981 switch (N.getValueType()) {
982 default: assert(0 && "Cannot AND this type!");
983 case MVT::i1: { // if a bool, we emit a pseudocode AND
984 unsigned pA = SelectExpr(N.getOperand(0));
985 unsigned pB = SelectExpr(N.getOperand(1));
986
987/* our pseudocode for AND is:
988 *
989(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
990 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
991 ;;
992(pB) cmp.ne pTemp,p0 = r0,r0
993 ;;
994(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
995
996*/
997 unsigned pTemp = MakeReg(MVT::i1);
998
999 unsigned bogusTemp1 = MakeReg(MVT::i1);
1000 unsigned bogusTemp2 = MakeReg(MVT::i1);
1001 unsigned bogusTemp3 = MakeReg(MVT::i1);
1002 unsigned bogusTemp4 = MakeReg(MVT::i1);
1003
1004 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
1005 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
1006 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
1007 .addReg(IA64::r0).addReg(IA64::r0);
1008 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
1009 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
1010 BuildMI(BB, IA64::TPCMPNE, 3, Result)
1011 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
1012 break;
1013 }
Duraid Madinac02780e2005-04-13 04:50:54 +00001014
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001015 // if not a bool, we just AND away:
1016 case MVT::i8:
1017 case MVT::i16:
1018 case MVT::i32:
1019 case MVT::i64: {
1020 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001021 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1022 case 1: // ANDing a constant that is 2^n-1 for some n
1023 switch (Tmp3) {
1024 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1025 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1026 break;
1027 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1028 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1029 break;
1030 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1031 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1032 break;
1033 default: // otherwise, use dep.z to paste zeros
1034 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1035 .addImm(0).addImm(Tmp3);
1036 break;
1037 }
1038 return Result; // early exit
1039 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001040 Tmp2 = SelectExpr(N.getOperand(1));
1041 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001042 }
1043 }
1044 return Result;
1045 }
1046
1047 case ISD::OR: {
1048 switch (N.getValueType()) {
1049 default: assert(0 && "Cannot OR this type!");
1050 case MVT::i1: { // if a bool, we emit a pseudocode OR
1051 unsigned pA = SelectExpr(N.getOperand(0));
1052 unsigned pB = SelectExpr(N.getOperand(1));
1053
1054 unsigned pTemp1 = MakeReg(MVT::i1);
1055
1056/* our pseudocode for OR is:
1057 *
1058
1059pC = pA OR pB
1060-------------
1061
1062(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1063 ;;
1064(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
1065
1066*/
1067 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
1068 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
1069 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
1070 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
1071 break;
1072 }
1073 // if not a bool, we just OR away:
1074 case MVT::i8:
1075 case MVT::i16:
1076 case MVT::i32:
1077 case MVT::i64: {
1078 Tmp1 = SelectExpr(N.getOperand(0));
1079 Tmp2 = SelectExpr(N.getOperand(1));
1080 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1081 break;
1082 }
1083 }
1084 return Result;
1085 }
1086
1087 case ISD::XOR: {
1088 switch (N.getValueType()) {
1089 default: assert(0 && "Cannot XOR this type!");
1090 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1091 unsigned pY = SelectExpr(N.getOperand(0));
1092 unsigned pZ = SelectExpr(N.getOperand(1));
1093
1094/* one possible routine for XOR is:
1095
1096 // Compute px = py ^ pz
1097 // using sum of products: px = (py & !pz) | (pz & !py)
1098 // Uses 5 instructions in 3 cycles.
1099 // cycle 1
1100(pz) cmp.eq.unc px = r0, r0 // px = pz
1101(py) cmp.eq.unc pt = r0, r0 // pt = py
1102 ;;
1103 // cycle 2
1104(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1105(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1106 ;;
1107 } { .mmi
1108 // cycle 3
1109(pt) cmp.eq.or px = r0, r0 // px = px | pt
1110
1111*** Another, which we use here, requires one scratch GR. it is:
1112
1113 mov rt = 0 // initialize rt off critical path
1114 ;;
1115
1116 // cycle 1
1117(pz) cmp.eq.unc px = r0, r0 // px = pz
1118(pz) mov rt = 1 // rt = pz
1119 ;;
1120 // cycle 2
1121(py) cmp.ne px = 1, rt // if (py) px = !pz
1122
1123.. these routines kindly provided by Jim Hull
1124*/
1125 unsigned rt = MakeReg(MVT::i64);
1126
1127 // these two temporaries will never actually appear,
1128 // due to the two-address form of some of the instructions below
1129 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1130 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1131
1132 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1133 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
1134 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
1135 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
1136 .addReg(bogoGR).addImm(1).addReg(pZ);
1137 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
1138 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
1139 break;
1140 }
1141 // if not a bool, we just XOR away:
1142 case MVT::i8:
1143 case MVT::i16:
1144 case MVT::i32:
1145 case MVT::i64: {
1146 Tmp1 = SelectExpr(N.getOperand(0));
1147 Tmp2 = SelectExpr(N.getOperand(1));
1148 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1149 break;
1150 }
1151 }
1152 return Result;
1153 }
1154
1155 case ISD::SHL: {
1156 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001157 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1158 Tmp2 = CN->getValue();
1159 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1160 } else {
1161 Tmp2 = SelectExpr(N.getOperand(1));
1162 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1163 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001164 return Result;
1165 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001166
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001167 case ISD::SRL: {
1168 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001169 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1170 Tmp2 = CN->getValue();
1171 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1172 } else {
1173 Tmp2 = SelectExpr(N.getOperand(1));
1174 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1175 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001176 return Result;
1177 }
Duraid Madinaf55e4032005-04-07 12:33:38 +00001178
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001179 case ISD::SRA: {
1180 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001181 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1182 Tmp2 = CN->getValue();
1183 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1184 } else {
1185 Tmp2 = SelectExpr(N.getOperand(1));
1186 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1187 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001188 return Result;
1189 }
1190
1191 case ISD::SDIV:
1192 case ISD::UDIV:
1193 case ISD::SREM:
1194 case ISD::UREM: {
1195
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 Tmp2 = SelectExpr(N.getOperand(1));
1198
1199 bool isFP=false;
1200
1201 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1202 isFP=true;
1203
1204 bool isModulus=false; // is it a division or a modulus?
1205 bool isSigned=false;
1206
1207 switch(N.getOpcode()) {
1208 case ISD::SDIV: isModulus=false; isSigned=true; break;
1209 case ISD::UDIV: isModulus=false; isSigned=false; break;
1210 case ISD::SREM: isModulus=true; isSigned=true; break;
1211 case ISD::UREM: isModulus=true; isSigned=false; break;
1212 }
1213
Duraid Madina4826a072005-04-06 09:55:17 +00001214 if(!isModulus && !isFP) { // if this is an integer divide,
1215 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
1216 case 1: // division by a constant that's a power of 2
1217 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina6dcceb52005-04-08 10:01:48 +00001218 if(isSigned) { // argument could be negative, so emit some code:
1219 unsigned divAmt=Tmp3;
1220 unsigned tempGR1=MakeReg(MVT::i64);
1221 unsigned tempGR2=MakeReg(MVT::i64);
1222 unsigned tempGR3=MakeReg(MVT::i64);
1223 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1224 .addReg(Tmp1).addImm(divAmt-1);
1225 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1226 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1227 BuildMI(BB, IA64::ADD, 2, tempGR3)
1228 .addReg(Tmp1).addReg(tempGR2);
1229 BuildMI(BB, IA64::SHRS, 2, Result)
1230 .addReg(tempGR3).addImm(divAmt);
1231 }
1232 else // unsigned div-by-power-of-2 becomes a simple shift right:
Duraid Madina4826a072005-04-06 09:55:17 +00001233 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1234 return Result; // early exit
1235 }
1236 }
1237
Duraid Madinabeeaab22005-03-31 12:31:11 +00001238 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
1239 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001240 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1241 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1242 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1243 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1244 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1245 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1246 unsigned TmpF7=MakeReg(MVT::f64);
1247 unsigned TmpF8=MakeReg(MVT::f64);
1248 unsigned TmpF9=MakeReg(MVT::f64);
1249 unsigned TmpF10=MakeReg(MVT::f64);
1250 unsigned TmpF11=MakeReg(MVT::f64);
1251 unsigned TmpF12=MakeReg(MVT::f64);
1252 unsigned TmpF13=MakeReg(MVT::f64);
1253 unsigned TmpF14=MakeReg(MVT::f64);
1254 unsigned TmpF15=MakeReg(MVT::f64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001255
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001256 // OK, emit some code:
1257
1258 if(!isFP) {
1259 // first, load the inputs into FP regs.
1260 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1261 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
1262
1263 // next, convert the inputs to FP
1264 if(isSigned) {
1265 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1266 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
1267 } else {
1268 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1269 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
1270 }
1271
1272 } else { // this is an FP divide/remainder, so we 'leak' some temp
1273 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1274 TmpF3=Tmp1;
1275 TmpF4=Tmp2;
1276 }
1277
1278 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001279 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1280 BuildMI(BB, IA64::FRCPAS1, 4)
1281 .addReg(TmpF5, MachineOperand::Def)
1282 .addReg(TmpPR, MachineOperand::Def)
1283 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001284
Duraid Madinabeeaab22005-03-31 12:31:11 +00001285 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1286 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1287 // TPCMPNE below
1288 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1289 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
1290 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
1291 }
1292
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001293 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1294 // precision, don't need this much for f32/i32)
1295 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1296 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1297 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1298 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1299 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1300 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1301 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1302 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1303 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1304 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1305 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1306 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1307 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1308 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1309 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1310 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001311
1312 // FIXME: this is unfortunate :(
1313 // the story is that the dest reg of the fnma above and the fma below
1314 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1315 // be the same register, or this code breaks if the first argument is
1316 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001317 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1318 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1319
Duraid Madina6e02e682005-04-04 05:05:52 +00001320 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1321 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1322 }
1323
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001324 if(!isFP) {
1325 // round to an integer
1326 if(isSigned)
1327 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
1328 else
1329 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
1330 } else {
1331 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1332 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1333 // we really do need the above FMOV? ;)
1334 }
1335
1336 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001337 if(isFP) { // extra worrying about div-by-zero
1338 unsigned bogoResult=MakeReg(MVT::f64);
1339
1340 // we do a 'conditional fmov' (of the correct result, depending
1341 // on how the frcpa predicate turned out)
1342 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
1343 .addReg(TmpF12).addReg(TmpPR2);
1344 BuildMI(BB, IA64::CFMOV, 2, Result)
1345 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
1346 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001347 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001348 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001349 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001350 } else { // this is a modulus
1351 if(!isFP) {
1352 // answer = q * (-b) + a
1353 unsigned ModulusResult = MakeReg(MVT::f64);
1354 unsigned TmpF = MakeReg(MVT::f64);
1355 unsigned TmpI = MakeReg(MVT::i64);
Duraid Madina6e02e682005-04-04 05:05:52 +00001356
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001357 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1358 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1359 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1360 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1361 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
1362 } else { // FP modulus! The horror... the horror....
1363 assert(0 && "sorry, no FP modulus just yet!\n!\n");
1364 }
1365 }
1366
1367 return Result;
1368 }
1369
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001370 case ISD::SIGN_EXTEND_INREG: {
1371 Tmp1 = SelectExpr(N.getOperand(0));
1372 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1373 switch(MVN->getExtraValueType())
1374 {
1375 default:
1376 Node->dump();
1377 assert(0 && "don't know how to sign extend this type");
1378 break;
1379 case MVT::i8: Opc = IA64::SXT1; break;
1380 case MVT::i16: Opc = IA64::SXT2; break;
1381 case MVT::i32: Opc = IA64::SXT4; break;
1382 }
1383 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1384 return Result;
1385 }
1386
1387 case ISD::SETCC: {
1388 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001389
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001390 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1391 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001392
1393 if(ConstantSDNode *CSDN =
1394 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1395 // if we are comparing against a constant zero
1396 if(CSDN->getValue()==0)
1397 Tmp2 = IA64::r0; // then we can just compare against r0
1398 else
1399 Tmp2 = SelectExpr(N.getOperand(1));
1400 } else // not comparing against a constant
1401 Tmp2 = SelectExpr(N.getOperand(1));
1402
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001403 switch (SetCC->getCondition()) {
1404 default: assert(0 && "Unknown integer comparison!");
1405 case ISD::SETEQ:
1406 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1407 break;
1408 case ISD::SETGT:
1409 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1410 break;
1411 case ISD::SETGE:
1412 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1413 break;
1414 case ISD::SETLT:
1415 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1416 break;
1417 case ISD::SETLE:
1418 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1419 break;
1420 case ISD::SETNE:
1421 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1422 break;
1423 case ISD::SETULT:
1424 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1425 break;
1426 case ISD::SETUGT:
1427 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1428 break;
1429 case ISD::SETULE:
1430 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1431 break;
1432 case ISD::SETUGE:
1433 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1434 break;
1435 }
1436 }
1437 else { // if not integer, should be FP. FIXME: what about bools? ;)
1438 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1439 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001440
1441 if(ConstantFPSDNode *CFPSDN =
1442 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1443
1444 // if we are comparing against a constant +0.0 or +1.0
1445 if(CFPSDN->isExactlyValue(+0.0))
1446 Tmp2 = IA64::F0; // then we can just compare against f0
1447 else if(CFPSDN->isExactlyValue(+1.0))
1448 Tmp2 = IA64::F1; // or f1
1449 else
1450 Tmp2 = SelectExpr(N.getOperand(1));
1451 } else // not comparing against a constant
1452 Tmp2 = SelectExpr(N.getOperand(1));
1453
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001454 switch (SetCC->getCondition()) {
1455 default: assert(0 && "Unknown FP comparison!");
1456 case ISD::SETEQ:
1457 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1458 break;
1459 case ISD::SETGT:
1460 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1461 break;
1462 case ISD::SETGE:
1463 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1464 break;
1465 case ISD::SETLT:
1466 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1467 break;
1468 case ISD::SETLE:
1469 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1470 break;
1471 case ISD::SETNE:
1472 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1473 break;
1474 case ISD::SETULT:
1475 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1476 break;
1477 case ISD::SETUGT:
1478 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1479 break;
1480 case ISD::SETULE:
1481 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1482 break;
1483 case ISD::SETUGE:
1484 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1485 break;
1486 }
1487 }
1488 }
1489 else
1490 assert(0 && "this setcc not implemented yet");
1491
1492 return Result;
1493 }
1494
1495 case ISD::EXTLOAD:
1496 case ISD::ZEXTLOAD:
1497 case ISD::LOAD: {
1498 // Make sure we generate both values.
1499 if (Result != 1)
1500 ExprMap[N.getValue(1)] = 1; // Generate the token
1501 else
1502 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1503
1504 bool isBool=false;
1505
1506 if(opcode == ISD::LOAD) { // this is a LOAD
1507 switch (Node->getValueType(0)) {
1508 default: assert(0 && "Cannot load this type!");
1509 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1510 // FIXME: for now, we treat bool loads the same as i8 loads */
1511 case MVT::i8: Opc = IA64::LD1; break;
1512 case MVT::i16: Opc = IA64::LD2; break;
1513 case MVT::i32: Opc = IA64::LD4; break;
1514 case MVT::i64: Opc = IA64::LD8; break;
1515
1516 case MVT::f32: Opc = IA64::LDF4; break;
1517 case MVT::f64: Opc = IA64::LDF8; break;
1518 }
1519 } else { // this is an EXTLOAD or ZEXTLOAD
1520 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1521 switch (TypeBeingLoaded) {
1522 default: assert(0 && "Cannot extload/zextload this type!");
1523 // FIXME: bools?
1524 case MVT::i8: Opc = IA64::LD1; break;
1525 case MVT::i16: Opc = IA64::LD2; break;
1526 case MVT::i32: Opc = IA64::LD4; break;
1527 case MVT::f32: Opc = IA64::LDF4; break;
1528 }
1529 }
1530
1531 SDOperand Chain = N.getOperand(0);
1532 SDOperand Address = N.getOperand(1);
1533
1534 if(Address.getOpcode() == ISD::GlobalAddress) {
1535 Select(Chain);
1536 unsigned dummy = MakeReg(MVT::i64);
1537 unsigned dummy2 = MakeReg(MVT::i64);
1538 BuildMI(BB, IA64::ADD, 2, dummy)
1539 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1540 .addReg(IA64::r1);
1541 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1542 if(!isBool)
1543 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
1544 else { // emit a little pseudocode to load a bool (stored in one byte)
1545 // into a predicate register
1546 assert(Opc==IA64::LD1 && "problem loading a bool");
1547 unsigned dummy3 = MakeReg(MVT::i64);
1548 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1549 // we compare to 0. true? 0. false? 1.
1550 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1551 }
1552 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1553 Select(Chain);
1554 IA64Lowering.restoreGP(BB);
1555 unsigned dummy = MakeReg(MVT::i64);
1556 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
1557 .addReg(IA64::r1); // CPI+GP
1558 if(!isBool)
1559 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1560 else { // emit a little pseudocode to load a bool (stored in one byte)
1561 // into a predicate register
1562 assert(Opc==IA64::LD1 && "problem loading a bool");
1563 unsigned dummy3 = MakeReg(MVT::i64);
1564 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1565 // we compare to 0. true? 0. false? 1.
1566 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1567 }
1568 } else if(Address.getOpcode() == ISD::FrameIndex) {
1569 Select(Chain); // FIXME ? what about bools?
1570 unsigned dummy = MakeReg(MVT::i64);
1571 BuildMI(BB, IA64::MOV, 1, dummy)
1572 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1573 if(!isBool)
1574 BuildMI(BB, Opc, 1, Result).addReg(dummy);
1575 else { // emit a little pseudocode to load a bool (stored in one byte)
1576 // into a predicate register
1577 assert(Opc==IA64::LD1 && "problem loading a bool");
1578 unsigned dummy3 = MakeReg(MVT::i64);
1579 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1580 // we compare to 0. true? 0. false? 1.
1581 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
1582 }
1583 } else { // none of the above...
1584 Select(Chain);
1585 Tmp2 = SelectExpr(Address);
1586 if(!isBool)
1587 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1588 else { // emit a little pseudocode to load a bool (stored in one byte)
1589 // into a predicate register
1590 assert(Opc==IA64::LD1 && "problem loading a bool");
1591 unsigned dummy = MakeReg(MVT::i64);
1592 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1593 // we compare to 0. true? 0. false? 1.
1594 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1595 }
1596 }
1597
1598 return Result;
1599 }
1600
1601 case ISD::CopyFromReg: {
1602 if (Result == 1)
1603 Result = ExprMap[N.getValue(0)] =
1604 MakeReg(N.getValue(0).getValueType());
1605
1606 SDOperand Chain = N.getOperand(0);
1607
1608 Select(Chain);
1609 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1610
1611 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1612 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
1613 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
1614 // (r) Result =cmp.eq.unc(r0,r0)
1615 else
1616 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
1617 return Result;
1618 }
1619
1620 case ISD::CALL: {
1621 Select(N.getOperand(0));
1622
1623 // The chain for this call is now lowered.
1624 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
1625
1626 //grab the arguments
1627 std::vector<unsigned> argvregs;
1628
1629 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1630 argvregs.push_back(SelectExpr(N.getOperand(i)));
1631
1632 // see section 8.5.8 of "Itanium Software Conventions and
1633 // Runtime Architecture Guide to see some examples of what's going
1634 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
1635 // while FP args get mapped to F8->F15 as needed)
1636
1637 unsigned used_FPArgs=0; // how many FP Args have been used so far?
1638
1639 // in reg args
1640 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
1641 {
1642 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
1643 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
1644 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
1645 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
1646
1647 switch(N.getOperand(i+2).getValueType())
1648 {
1649 default: // XXX do we need to support MVT::i1 here?
1650 Node->dump();
1651 N.getOperand(i).Val->dump();
1652 std::cerr << "Type for " << i << " is: " <<
1653 N.getOperand(i+2).getValueType() << std::endl;
1654 assert(0 && "Unknown value type for call");
1655 case MVT::i64:
1656 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
1657 break;
1658 case MVT::f64:
1659 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
1660 .addReg(argvregs[i]);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001661 // FIXME: we don't need to do this _all_ the time:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001662 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
1663 break;
1664 }
1665 }
1666
1667 //in mem args
1668 for (int i = 8, e = argvregs.size(); i < e; ++i)
1669 {
1670 unsigned tempAddr = MakeReg(MVT::i64);
1671
1672 switch(N.getOperand(i+2).getValueType()) {
1673 default:
1674 Node->dump();
1675 N.getOperand(i).Val->dump();
1676 std::cerr << "Type for " << i << " is: " <<
1677 N.getOperand(i+2).getValueType() << "\n";
1678 assert(0 && "Unknown value type for call");
1679 case MVT::i1: // FIXME?
1680 case MVT::i8:
1681 case MVT::i16:
1682 case MVT::i32:
1683 case MVT::i64:
1684 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1685 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1686 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
1687 break;
1688 case MVT::f32:
1689 case MVT::f64:
1690 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
1691 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
1692 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
1693 break;
1694 }
1695 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001696
1697 /* XXX we want to re-enable direct branches! crippling them now
1698 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001699 //build the right kind of call
1700 if (GlobalAddressSDNode *GASD =
1701 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
1702 {
1703 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
1704 IA64Lowering.restoreGP_SP_RP(BB);
1705 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001706 ^^^^^^^^^^^^^ we want this code one day XXX */
1707 if (ExternalSymbolSDNode *ESSDN =
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001708 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00001709 { // FIXME : currently need this case for correctness, to avoid
1710 // "non-pic code with imm relocation against dynamic symbol" errors
1711 BuildMI(BB, IA64::BRCALL, 1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001712 .addExternalSymbol(ESSDN->getSymbol(), true);
1713 IA64Lowering.restoreGP_SP_RP(BB);
1714 }
1715 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001716 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00001717
1718 unsigned targetEntryPoint=MakeReg(MVT::i64);
1719 unsigned targetGPAddr=MakeReg(MVT::i64);
1720 unsigned currentGP=MakeReg(MVT::i64);
1721
1722 // b6 is a scratch branch register, we load the target entry point
1723 // from the base of the function descriptor
1724 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
1725 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
1726
1727 // save the current GP:
1728 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
1729
1730 /* TODO: we need to make sure doing this never, ever loads a
1731 * bogus value into r1 (GP). */
1732 // load the target GP (which is at mem[functiondescriptor+8])
1733 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
1734 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
1735 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
1736
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001737 // and then jump: (well, call)
1738 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001739 // and finally restore the old GP
1740 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
1741 IA64Lowering.restoreSP_RP(BB);
1742 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001743
1744 switch (Node->getValueType(0)) {
1745 default: assert(0 && "Unknown value type for call result!");
1746 case MVT::Other: return 1;
1747 case MVT::i1:
1748 BuildMI(BB, IA64::CMPNE, 2, Result)
1749 .addReg(IA64::r8).addReg(IA64::r0);
1750 break;
1751 case MVT::i8:
1752 case MVT::i16:
1753 case MVT::i32:
1754 case MVT::i64:
1755 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
1756 break;
1757 case MVT::f64:
1758 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
1759 break;
1760 }
1761 return Result+N.ResNo;
1762 }
1763
1764 } // <- uhhh XXX
1765 return 0;
1766}
1767
1768void ISel::Select(SDOperand N) {
1769 unsigned Tmp1, Tmp2, Opc;
1770 unsigned opcode = N.getOpcode();
1771
Nate Begeman85fdeb22005-03-24 04:39:54 +00001772 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001773 return; // Already selected.
1774
1775 SDNode *Node = N.Val;
1776
1777 switch (Node->getOpcode()) {
1778 default:
1779 Node->dump(); std::cerr << "\n";
1780 assert(0 && "Node not handled yet!");
1781
1782 case ISD::EntryToken: return; // Noop
1783
1784 case ISD::TokenFactor: {
1785 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1786 Select(Node->getOperand(i));
1787 return;
1788 }
1789
1790 case ISD::CopyToReg: {
1791 Select(N.getOperand(0));
1792 Tmp1 = SelectExpr(N.getOperand(1));
1793 Tmp2 = cast<RegSDNode>(N)->getReg();
1794
1795 if (Tmp1 != Tmp2) {
1796 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
1797 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
1798 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
1799 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
1800 else
1801 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
1802 // XXX is this the right way 'round? ;)
1803 }
1804 return;
1805 }
1806
1807 case ISD::RET: {
1808
1809 /* what the heck is going on here:
1810
1811<_sabre_> ret with two operands is obvious: chain and value
1812<camel_> yep
1813<_sabre_> ret with 3 values happens when 'expansion' occurs
1814<_sabre_> e.g. i64 gets split into 2x i32
1815<camel_> oh right
1816<_sabre_> you don't have this case on ia64
1817<camel_> yep
1818<_sabre_> so the two returned values go into EAX/EDX on ia32
1819<camel_> ahhh *memories*
1820<_sabre_> :)
1821<camel_> ok, thanks :)
1822<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
1823<_sabre_> this is the first operand always
1824<_sabre_> these operand often define chains, they are the last operand
1825<_sabre_> they are printed as 'ch' if you do DAG.dump()
1826 */
1827
1828 switch (N.getNumOperands()) {
1829 default:
1830 assert(0 && "Unknown return instruction!");
1831 case 2:
1832 Select(N.getOperand(0));
1833 Tmp1 = SelectExpr(N.getOperand(1));
1834 switch (N.getOperand(1).getValueType()) {
1835 default: assert(0 && "All other types should have been promoted!!");
1836 // FIXME: do I need to add support for bools here?
1837 // (return '0' or '1' r8, basically...)
Duraid Madinaca494fd2005-04-12 14:54:44 +00001838 //
1839 // FIXME: need to round floats - 80 bits is bad, the tester
1840 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001841 case MVT::i64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001842 // we mark r8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001843 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
1844 break;
1845 case MVT::f64:
Duraid Madinaca494fd2005-04-12 14:54:44 +00001846 // we mark F8 as live on exit up above in LowerArguments()
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001847 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
1848 }
1849 break;
1850 case 1:
1851 Select(N.getOperand(0));
1852 break;
1853 }
1854 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
1855 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
1856 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
1857 return;
1858 }
1859
1860 case ISD::BR: {
1861 Select(N.getOperand(0));
1862 MachineBasicBlock *Dest =
1863 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1864 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
1865 // XXX HACK! we do _not_ need long branches all the time
1866 return;
1867 }
1868
1869 case ISD::ImplicitDef: {
1870 Select(N.getOperand(0));
1871 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
1872 return;
1873 }
1874
1875 case ISD::BRCOND: {
1876 MachineBasicBlock *Dest =
1877 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1878
1879 Select(N.getOperand(0));
1880 Tmp1 = SelectExpr(N.getOperand(1));
1881 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
1882 // XXX HACK! we do _not_ need long branches all the time
1883 return;
1884 }
1885
1886 case ISD::EXTLOAD:
1887 case ISD::ZEXTLOAD:
1888 case ISD::SEXTLOAD:
1889 case ISD::LOAD:
1890 case ISD::CALL:
1891 case ISD::CopyFromReg:
1892 case ISD::DYNAMIC_STACKALLOC:
1893 SelectExpr(N);
1894 return;
1895
1896 case ISD::TRUNCSTORE:
1897 case ISD::STORE: {
1898 Select(N.getOperand(0));
1899 Tmp1 = SelectExpr(N.getOperand(1)); // value
1900
1901 bool isBool=false;
1902
1903 if(opcode == ISD::STORE) {
1904 switch (N.getOperand(1).getValueType()) {
1905 default: assert(0 && "Cannot store this type!");
1906 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1907 // FIXME?: for now, we treat bool loads the same as i8 stores */
1908 case MVT::i8: Opc = IA64::ST1; break;
1909 case MVT::i16: Opc = IA64::ST2; break;
1910 case MVT::i32: Opc = IA64::ST4; break;
1911 case MVT::i64: Opc = IA64::ST8; break;
1912
1913 case MVT::f32: Opc = IA64::STF4; break;
1914 case MVT::f64: Opc = IA64::STF8; break;
1915 }
1916 } else { // truncstore
1917 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1918 default: assert(0 && "unknown type in truncstore");
1919 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
1920 //FIXME: DAG does not promote this load?
1921 case MVT::i8: Opc = IA64::ST1; break;
1922 case MVT::i16: Opc = IA64::ST2; break;
1923 case MVT::i32: Opc = IA64::ST4; break;
1924 case MVT::f32: Opc = IA64::STF4; break;
1925 }
1926 }
1927
1928 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
1929 unsigned dummy = MakeReg(MVT::i64);
1930 unsigned dummy2 = MakeReg(MVT::i64);
1931 BuildMI(BB, IA64::ADD, 2, dummy)
1932 .addGlobalAddress(cast<GlobalAddressSDNode>
1933 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
1934 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1935
1936 if(!isBool)
1937 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
1938 else { // we are storing a bool, so emit a little pseudocode
1939 // to store a predicate register as one byte
1940 assert(Opc==IA64::ST1);
1941 unsigned dummy3 = MakeReg(MVT::i64);
1942 unsigned dummy4 = MakeReg(MVT::i64);
1943 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001944 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001945 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1946 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
1947 }
1948 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
1949
1950 // FIXME? (what about bools?)
1951
1952 unsigned dummy = MakeReg(MVT::i64);
1953 BuildMI(BB, IA64::MOV, 1, dummy)
1954 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
1955 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
1956 } else { // otherwise
1957 Tmp2 = SelectExpr(N.getOperand(2)); //address
1958 if(!isBool)
1959 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
1960 else { // we are storing a bool, so emit a little pseudocode
1961 // to store a predicate register as one byte
1962 assert(Opc==IA64::ST1);
1963 unsigned dummy3 = MakeReg(MVT::i64);
1964 unsigned dummy4 = MakeReg(MVT::i64);
1965 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001966 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001967 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
1968 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
1969 }
1970 }
1971 return;
1972 }
1973
1974 case ISD::ADJCALLSTACKDOWN:
1975 case ISD::ADJCALLSTACKUP: {
1976 Select(N.getOperand(0));
1977 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1978
1979 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
1980 IA64::ADJUSTCALLSTACKUP;
1981 BuildMI(BB, Opc, 1).addImm(Tmp1);
1982 return;
1983 }
1984
1985 return;
1986 }
1987 assert(0 && "GAME OVER. INSERT COIN?");
1988}
1989
1990
1991/// createIA64PatternInstructionSelector - This pass converts an LLVM function
1992/// into a machine code representation using pattern matching and a machine
1993/// description file.
1994///
1995FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
1996 return new ISel(TM);
1997}
1998
1999