blob: 804ad9937ef6fa2321dd3a266a08028e471e673c [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// 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.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
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>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000060 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
61
Misha Brukman4633f1c2005-04-21 23:13:11 +000062 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000063 setShiftAmountType(MVT::i64);
64
65 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000066
67 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000068
69 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000073
74 setOperationAction(ISD::SREM , MVT::f32 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
76
77 setOperationAction(ISD::UREM , MVT::f32 , Expand);
78 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000079
Duraid Madina9b9d45f2005-03-17 18:17:03 +000080 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
81 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
82 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
83
Chris Lattner17234b72005-04-30 04:26:06 +000084 // We don't support sin/cos/sqrt
85 setOperationAction(ISD::FSIN , MVT::f64, Expand);
86 setOperationAction(ISD::FCOS , MVT::f64, Expand);
87 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
88 setOperationAction(ISD::FSIN , MVT::f32, Expand);
89 setOperationAction(ISD::FCOS , MVT::f32, Expand);
90 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
91
Andrew Lenharthb5884d32005-05-04 19:25:37 +000092 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000093 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
94 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000095
Duraid Madina9b9d45f2005-03-17 18:17:03 +000096 computeRegisterProperties();
97
98 addLegalFPImmediate(+0.0);
99 addLegalFPImmediate(+1.0);
100 addLegalFPImmediate(-0.0);
101 addLegalFPImmediate(-1.0);
102 }
103
104 /// LowerArguments - This hook must be implemented to indicate how we should
105 /// lower the arguments for the specified function, into the specified DAG.
106 virtual std::vector<SDOperand>
107 LowerArguments(Function &F, SelectionDAG &DAG);
108
109 /// LowerCallTo - This hook lowers an abstract call to a function into an
110 /// actual call.
111 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000112 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000113 bool isTailCall, SDOperand Callee, ArgListTy &Args,
114 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000115
116 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000117 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000118
119 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000120 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000121 const Type *ArgTy, SelectionDAG &DAG);
122
123 virtual std::pair<SDOperand, SDOperand>
124 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
125 SelectionDAG &DAG);
126
127 void restoreGP_SP_RP(MachineBasicBlock* BB)
128 {
129 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
130 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
131 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
132 }
133
Duraid Madinabeeaab22005-03-31 12:31:11 +0000134 void restoreSP_RP(MachineBasicBlock* BB)
135 {
136 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
137 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
138 }
139
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000140 void restoreRP(MachineBasicBlock* BB)
141 {
142 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
143 }
144
145 void restoreGP(MachineBasicBlock* BB)
146 {
147 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
148 }
149
150 };
151}
152
153
154std::vector<SDOperand>
155IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
156 std::vector<SDOperand> ArgValues;
157
158 //
159 // add beautiful description of IA64 stack frame format
160 // here (from intel 24535803.pdf most likely)
161 //
162 MachineFunction &MF = DAG.getMachineFunction();
163 MachineFrameInfo *MFI = MF.getFrameInfo();
164
165 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
166 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
168
169 MachineBasicBlock& BB = MF.front();
170
Misha Brukman4633f1c2005-04-21 23:13:11 +0000171 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000172 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000173
174 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000175 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000176
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000177 unsigned argVreg[8];
178 unsigned argPreg[8];
179 unsigned argOpc[8];
180
Duraid Madinabeeaab22005-03-31 12:31:11 +0000181 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000182
Duraid Madinabeeaab22005-03-31 12:31:11 +0000183 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000184 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000185
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000186 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000187 {
188 SDOperand newroot, argt;
189 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000190
191 switch (getValueType(I->getType())) {
192 default:
193 std::cerr << "ERROR in LowerArgs: unknown type "
194 << getValueType(I->getType()) << "\n";
195 abort();
196 case MVT::f32:
197 // fixme? (well, will need to for weird FP structy stuff,
198 // see intel ABI docs)
199 case MVT::f64:
200//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
201 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
202 // floating point args go into f8..f15 as-needed, the increment
203 argVreg[count] = // is below..:
204 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
205 // FP args go into f8..f15 as needed: (hence the ++)
206 argPreg[count] = args_FP[used_FPArgs++];
207 argOpc[count] = IA64::FMOV;
208 argt = newroot = DAG.getCopyFromReg(argVreg[count],
209 getValueType(I->getType()), DAG.getRoot());
210 break;
211 case MVT::i1: // NOTE: as far as C abi stuff goes,
212 // bools are just boring old ints
213 case MVT::i8:
214 case MVT::i16:
215 case MVT::i32:
216 case MVT::i64:
217//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
218 MF.addLiveIn(args_int[count]); // mark this register as liveIn
219 argVreg[count] =
220 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
221 argPreg[count] = args_int[count];
222 argOpc[count] = IA64::MOV;
223 argt = newroot =
224 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
225 if ( getValueType(I->getType()) != MVT::i64)
226 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
227 newroot);
228 break;
229 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000230 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000231 // Create the frame index object for this incoming parameter...
232 ArgOffset = 16 + 8 * (count - 8);
233 int FI = MFI->CreateFixedObject(8, ArgOffset);
234
235 // Create the SelectionDAG nodes corresponding to a load
236 //from this parameter
237 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
238 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000239 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000240 }
241 ++count;
242 DAG.setRoot(newroot.getValue(1));
243 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000245
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246
Duraid Madinabeeaab22005-03-31 12:31:11 +0000247 // Create a vreg to hold the output of (what will become)
248 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000249 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
250 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
251 // we create a PSEUDO_ALLOC (pseudo)instruction for now
252
253 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
254
255 // hmm:
256 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
257 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
258 // ..hmm.
259
260 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
261
262 // hmm:
263 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
264 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
265 // ..hmm.
266
Duraid Madinabeeaab22005-03-31 12:31:11 +0000267 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000268
Duraid Madinabeeaab22005-03-31 12:31:11 +0000269 // if this is a varargs function, we simply lower llvm.va_start by
270 // pointing to the first entry
271 if(F.isVarArg()) {
272 tempOffset=0;
273 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000274 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000275
Duraid Madinabeeaab22005-03-31 12:31:11 +0000276 // here we actually do the moving of args, and store them to the stack
277 // too if this is a varargs function:
278 for (int i = 0; i < count && i < 8; ++i) {
279 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
280 if(F.isVarArg()) {
281 // if this is a varargs function, we copy the input registers to the stack
282 int FI = MFI->CreateFixedObject(8, tempOffset);
283 tempOffset+=8; //XXX: is it safe to use r22 like this?
284 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
285 // FIXME: we should use st8.spill here, one day
286 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
287 }
288 }
289
Duraid Madinaca494fd2005-04-12 14:54:44 +0000290 // Finally, inform the code generator which regs we return values in.
291 // (see the ISD::RET: case down below)
292 switch (getValueType(F.getReturnType())) {
293 default: assert(0 && "i have no idea where to return this type!");
294 case MVT::isVoid: break;
295 case MVT::i1:
296 case MVT::i8:
297 case MVT::i16:
298 case MVT::i32:
299 case MVT::i64:
300 MF.addLiveOut(IA64::r8);
301 break;
302 case MVT::f32:
303 case MVT::f64:
304 MF.addLiveOut(IA64::F8);
305 break;
306 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000307
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000308 return ArgValues;
309}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000310
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000311std::pair<SDOperand, SDOperand>
312IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000313 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000314 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000315 SDOperand Callee, ArgListTy &Args,
316 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000317
318 MachineFunction &MF = DAG.getMachineFunction();
319
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000320 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000321 unsigned outRegsUsed = 0;
322
323 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000324 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000325 outRegsUsed = 8;
326 } else {
327 outRegsUsed = Args.size();
328 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000329
Duraid Madinabeeaab22005-03-31 12:31:11 +0000330 // FIXME? this WILL fail if we ever try to pass around an arg that
331 // consumes more than a single output slot (a 'real' double, int128
332 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
333 // registers we use. Hopefully, the assembler will notice.
334 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
335 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000336
Chris Lattner16cd04d2005-05-12 23:24:06 +0000337 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000338 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000339
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000340 std::vector<SDOperand> args_to_use;
341 for (unsigned i = 0, e = Args.size(); i != e; ++i)
342 {
343 switch (getValueType(Args[i].second)) {
344 default: assert(0 && "unexpected argument type!");
345 case MVT::i1:
346 case MVT::i8:
347 case MVT::i16:
348 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000349 //promote to 64-bits, sign/zero extending based on type
350 //of the argument
351 if(Args[i].second->isSigned())
352 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
353 Args[i].first);
354 else
355 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
356 Args[i].first);
357 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000358 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000359 //promote to 64-bits
360 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000361 case MVT::f64:
362 case MVT::i64:
363 break;
364 }
365 args_to_use.push_back(Args[i].first);
366 }
367
368 std::vector<MVT::ValueType> RetVals;
369 MVT::ValueType RetTyVT = getValueType(RetTy);
370 if (RetTyVT != MVT::isVoid)
371 RetVals.push_back(RetTyVT);
372 RetVals.push_back(MVT::Other);
373
374 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000375 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000376 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000377 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000378 DAG.getConstant(NumBytes, getPointerTy()));
379 return std::make_pair(TheCall, Chain);
380}
381
382std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000383IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
384 // vastart just stores the address of the VarArgsFrameIndex slot.
385 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
386 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
387 return std::make_pair(Result, Result);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000388}
389
390std::pair<SDOperand,SDOperand> IA64TargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000391LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000392 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000393
394 MVT::ValueType ArgVT = getValueType(ArgTy);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000395 SDOperand Val = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
396 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val, DAG.getSrcValue(NULL));
397 unsigned Amt;
398 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
399 Amt = 8;
400 else {
401 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
402 "Other types should have been promoted for varargs!");
403 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000404 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000405 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
406 DAG.getConstant(Amt, Val.getValueType()));
407 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
408 Val, VAList, DAG.getSrcValue(NULL));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000409 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000410}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000411
412std::pair<SDOperand, SDOperand> IA64TargetLowering::
413LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
414 SelectionDAG &DAG) {
415
416 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000417 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000418}
419
420
421namespace {
422
423 //===--------------------------------------------------------------------===//
424 /// ISel - IA64 specific code to select IA64 machine instructions for
425 /// SelectionDAG operations.
426 ///
427 class ISel : public SelectionDAGISel {
428 /// IA64Lowering - This object fully describes how to lower LLVM code to an
429 /// IA64-specific SelectionDAG.
430 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000431 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
432 // for sdiv and udiv until it is put into the future
433 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000434
435 /// ExprMap - As shared expressions are codegen'd, we keep track of which
436 /// vreg the value is produced in, so we only emit one copy of each compiled
437 /// tree.
438 std::map<SDOperand, unsigned> ExprMap;
439 std::set<SDOperand> LoweredTokens;
440
441 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000442 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
443 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000444
445 /// InstructionSelectBasicBlock - This callback is invoked by
446 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
447 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
448
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000449 unsigned SelectExpr(SDOperand N);
450 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000451 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
452 SDOperand BuildConstmulSequence(SDOperand N);
453
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000454 };
455}
456
457/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
458/// when it has created a SelectionDAG for us to codegen.
459void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
460
461 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000462 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000463 Select(DAG.getRoot());
464
465 // Clear state used for selection.
466 ExprMap.clear();
467 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000468 ISelDAG = 0;
469}
470
Duraid Madinab2322562005-04-26 07:23:02 +0000471// strip leading '0' characters from a string
472void munchLeadingZeros(std::string& inString) {
473 while(inString.c_str()[0]=='0') {
474 inString.erase(0, 1);
475 }
476}
477
478// strip trailing '0' characters from a string
479void munchTrailingZeros(std::string& inString) {
480 int curPos=inString.length()-1;
481
482 while(inString.c_str()[curPos]=='0') {
483 inString.erase(curPos, 1);
484 curPos--;
485 }
486}
487
488// return how many consecutive '0' characters are at the end of a string
489unsigned int countTrailingZeros(std::string& inString) {
490 int curPos=inString.length()-1;
491 unsigned int zeroCount=0;
492 // assert goes here
493 while(inString.c_str()[curPos--]=='0') {
494 zeroCount++;
495 }
496 return zeroCount;
497}
498
499// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
500// '0' and 'N' (-1) characters)
501void boothEncode(std::string inString, std::string& boothEncodedString) {
502
503 int curpos=0;
504 int replacements=0;
505 int lim=inString.size();
506
507 while(curpos<lim) {
508 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
509 int runlength=0;
510 std::string replaceString="N";
511
512 // find the run length
513 for(;inString[curpos+runlength]=='1';runlength++) ;
514
515 for(int i=0; i<runlength-1; i++)
516 replaceString+="0";
517 replaceString+="1";
518
519 if(runlength>1) {
520 inString.replace(curpos, runlength+1, replaceString);
521 curpos+=runlength-1;
522 } else
523 curpos++;
524 } else { // a zero, we just keep chugging along
525 curpos++;
526 }
527 }
528
529 // clean up (trim the string, reverse it and turn '1's into 'P's)
530 munchTrailingZeros(inString);
531 boothEncodedString="";
532
533 for(int i=inString.size()-1;i>=0;i--)
534 if(inString[i]=='1')
535 boothEncodedString+="P";
536 else
537 boothEncodedString+=inString[i];
538
539}
540
541struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
542 unsigned firstVal; // A
543 unsigned firstShift; // B
544 unsigned secondVal; // C
545 unsigned secondShift; // D
546 bool isSub;
547};
548
549/* this implements Lefevre's "pattern-based" constant multiplication,
550 * see "Multiplication by an Integer Constant", INRIA report 1999-06
551 *
552 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
553 * to get better booth encodings - this does help in practice
554 * TODO: weight shifts appropriately (most architectures can't
555 * fuse a shift and an add for arbitrary shift amounts) */
556unsigned lefevre(const std::string inString,
557 std::vector<struct shiftaddblob> &ops) {
558 std::string retstring;
559 std::string s = inString;
560 munchTrailingZeros(s);
561
562 int length=s.length()-1;
563
564 if(length==0) {
565 return(0);
566 }
567
568 std::vector<int> p,n;
569
570 for(int i=0; i<=length; i++) {
571 if (s.c_str()[length-i]=='P') {
572 p.push_back(i);
573 } else if (s.c_str()[length-i]=='N') {
574 n.push_back(i);
575 }
576 }
577
578 std::string t, u;
Duraid Madina4706c032005-04-26 09:42:50 +0000579 int c;
580 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000581 std::map<const int, int> w;
582
Duraid Madina85d5f602005-04-27 11:57:39 +0000583 for(unsigned i=0; i<p.size(); i++) {
584 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000585 w[p[i]-p[j]]++;
586 }
587 }
588
Duraid Madina85d5f602005-04-27 11:57:39 +0000589 for(unsigned i=1; i<n.size(); i++) {
590 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000591 w[n[i]-n[j]]++;
592 }
593 }
594
Duraid Madina85d5f602005-04-27 11:57:39 +0000595 for(unsigned i=0; i<p.size(); i++) {
596 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000597 w[-abs(p[i]-n[j])]++;
598 }
599 }
600
601 std::map<const int, int>::const_iterator ii;
602 std::vector<int> d;
603 std::multimap<int, int> sorted_by_value;
604
605 for(ii = w.begin(); ii!=w.end(); ii++)
606 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
607
608 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
609 it != sorted_by_value.end(); ++it) {
610 d.push_back((*it).second);
611 }
612
613 int int_W=0;
614 int int_d;
615
616 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
617 d.pop_back();
618 retstring=s; // hmmm
619 int x=0;
620 int z=abs(int_d)-1;
621
622 if(int_d>0) {
623
Duraid Madina85d5f602005-04-27 11:57:39 +0000624 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000625 if( ((base+z+1) < retstring.size()) &&
626 retstring.c_str()[base]=='P' &&
627 retstring.c_str()[base+z+1]=='P')
628 {
629 // match
630 x++;
631 retstring.replace(base, 1, "0");
632 retstring.replace(base+z+1, 1, "p");
633 }
634 }
635
Duraid Madina85d5f602005-04-27 11:57:39 +0000636 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000637 if( ((base+z+1) < retstring.size()) &&
638 retstring.c_str()[base]=='N' &&
639 retstring.c_str()[base+z+1]=='N')
640 {
641 // match
642 x++;
643 retstring.replace(base, 1, "0");
644 retstring.replace(base+z+1, 1, "n");
645 }
646 }
647
648 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000649 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000650 if( ((base+z+1) < retstring.size()) &&
651 ((retstring.c_str()[base]=='P' &&
652 retstring.c_str()[base+z+1]=='N') ||
653 (retstring.c_str()[base]=='N' &&
654 retstring.c_str()[base+z+1]=='P')) ) {
655 // match
656 x++;
657
658 if(retstring.c_str()[base]=='P') {
659 retstring.replace(base, 1, "0");
660 retstring.replace(base+z+1, 1, "p");
661 } else { // retstring[base]=='N'
662 retstring.replace(base, 1, "0");
663 retstring.replace(base+z+1, 1, "n");
664 }
665 }
666 }
667 }
668
669 if(x>int_W) {
670 int_W = x;
671 t = retstring;
672 c = int_d; // tofix
673 }
674
675 } d.pop_back(); // hmm
676
677 u = t;
678
Duraid Madina85d5f602005-04-27 11:57:39 +0000679 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000680 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
681 t.replace(i, 1, "0");
682 }
683
Duraid Madina85d5f602005-04-27 11:57:39 +0000684 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000685 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000686 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000687 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000688 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000689 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000690 u.replace(i, 1, "N");
691 }
692
693 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000694 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000695 c=-c;
696 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000697 f=false;
Duraid Madinab2322562005-04-26 07:23:02 +0000698
Duraid Madina8a3042c2005-05-09 13:18:34 +0000699 int pos=0;
700 while(u[pos]=='0')
701 pos++;
702
703 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000704
705 int g=0;
706 if(hit) {
707 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000708 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000709 bool isP=(u[p]=='P');
710 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000711
712 if(isP)
713 u.replace(p, 1, "N");
714 if(isN)
715 u.replace(p, 1, "P");
716 }
717 }
718
719 munchLeadingZeros(u);
720
721 int i = lefevre(u, ops);
722
723 shiftaddblob blob;
724
725 blob.firstVal=i; blob.firstShift=c;
726 blob.isSub=f;
727 blob.secondVal=i; blob.secondShift=0;
728
729 ops.push_back(blob);
730
731 i = ops.size();
732
733 munchLeadingZeros(t);
734
735 if(t.length()==0)
736 return i;
737
738 if(t.c_str()[0]!='P') {
739 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000740 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000741 bool isP=(t.c_str()[p]=='P');
742 bool isN=(t.c_str()[p]=='N');
743
744 if(isP)
745 t.replace(p, 1, "N");
746 if(isN)
747 t.replace(p, 1, "P");
748 }
749 }
750
751 int j = lefevre(t, ops);
752
753 int trail=countTrailingZeros(u);
754 blob.secondVal=i; blob.secondShift=trail;
755
756 trail=countTrailingZeros(t);
757 blob.firstVal=j; blob.firstShift=trail;
758
759 switch(g) {
760 case 0:
761 blob.isSub=false; // first + second
762 break;
763 case 1:
764 blob.isSub=true; // first - second
765 break;
766 case 2:
767 blob.isSub=true; // second - first
768 int tmpval, tmpshift;
769 tmpval=blob.firstVal;
770 tmpshift=blob.firstShift;
771 blob.firstVal=blob.secondVal;
772 blob.firstShift=blob.secondShift;
773 blob.secondVal=tmpval;
774 blob.secondShift=tmpshift;
775 break;
776 //assert
777 }
778
779 ops.push_back(blob);
780 return ops.size();
781}
782
783SDOperand ISel::BuildConstmulSequence(SDOperand N) {
784 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
785 // in particular, *3 is nicer as *2+1, not *4-1
786 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
787
788 bool flippedSign;
789 unsigned preliminaryShift=0;
790
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000791 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000792
793 // first, we make the constant to multiply by positive
794 if(constant<0) {
795 constant=-constant;
796 flippedSign=true;
797 } else {
798 flippedSign=false;
799 }
800
801 // next, we make it odd.
802 for(; (constant%2==0); preliminaryShift++)
803 constant>>=1;
804
805 //OK, we have a positive, odd number of 64 bits or less. Convert it
806 //to a binary string, constantString[0] is the LSB
807 char constantString[65];
808 for(int i=0; i<64; i++)
809 constantString[i]='0'+((constant>>i)&0x1);
810 constantString[64]=0;
811
812 // now, Booth encode it
813 std::string boothEncodedString;
814 boothEncode(constantString, boothEncodedString);
815
816 std::vector<struct shiftaddblob> ops;
817 // do the transformation, filling out 'ops'
818 lefevre(boothEncodedString, ops);
819
Duraid Madinae75a24a2005-05-15 14:44:13 +0000820 assert(ops.size() < 80 && "constmul code has gone haywire\n");
821 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Duraid Madinab2322562005-04-26 07:23:02 +0000822
823 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000824 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000825 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
826 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
827 results[ops[i].firstVal-1];
828 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
829 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
830 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
831 results[ops[i].secondVal-1];
832 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
833 if(ops[i].isSub)
834 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
835 else
836 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
837 }
838
839 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000840 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000841 if(preliminaryShift) {
842 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000843 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Duraid Madinab2322562005-04-26 07:23:02 +0000844 results[ops.size()-1], finalshift);
845 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000846 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000847 }
848
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000849 SDOperand finalresult;
850 if(flippedSign) { // if we were multiplying by a negative constant:
851 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
852 // subtract the result from 0 to flip its sign
853 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
854 } else { // there was no preliminary multiply by -1 required
855 finalresult = shiftedresult;
856 }
857
Duraid Madinab2322562005-04-26 07:23:02 +0000858 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000859}
860
Duraid Madina4826a072005-04-06 09:55:17 +0000861/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
862/// returns zero when the input is not exactly a power of two.
Duraid Madinac02780e2005-04-13 04:50:54 +0000863static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000864 if (Val == 0 || (Val & (Val-1))) return 0;
865 unsigned Count = 0;
866 while (Val != 1) {
867 Val >>= 1;
868 ++Count;
869 }
870 return Count;
871}
872
Duraid Madinac02780e2005-04-13 04:50:54 +0000873/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
874/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
875static unsigned ExactLog2sub1(uint64_t Val) {
876 unsigned int n;
877 for(n=0; n<64; n++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000878 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000879 return n;
880 }
881 return 666;
882}
883
Duraid Madina4826a072005-04-06 09:55:17 +0000884/// ponderIntegerDivisionBy - When handling integer divides, if the divide
885/// is by a constant such that we can efficiently codegen it, this
886/// function says what to do. Currently, it returns 0 if the division must
887/// become a genuine divide, and 1 if the division can be turned into a
888/// right shift.
889static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
890 unsigned& Imm) {
891 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
892 // a constant, give up.
893
894 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
895
Misha Brukman4633f1c2005-04-21 23:13:11 +0000896 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
Duraid Madina4826a072005-04-06 09:55:17 +0000897 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000898 }
899
Duraid Madina4826a072005-04-06 09:55:17 +0000900 return 0; // fallthrough
901}
902
Duraid Madinac02780e2005-04-13 04:50:54 +0000903static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
904 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
905 // a constant, give up.
906
907 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
908
909 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
910 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000911 }
912
Duraid Madinac02780e2005-04-13 04:50:54 +0000913 return 0; // fallthrough
914}
915
Duraid Madinaf55e4032005-04-07 12:33:38 +0000916static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
917 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
918 // constant, give up.
919 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
920
921 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
922 Imm = v & 0x3FFF; // 14 bits
923 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000924 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000925 return 0; // fallthrough
926}
927
928static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
929 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
930 // constant, give up.
931 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
932
933 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
934 Imm = v & 0xFF; // 8 bits
935 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000936 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000937 return 0; // fallthrough
938}
939
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000940unsigned ISel::SelectExpr(SDOperand N) {
941 unsigned Result;
942 unsigned Tmp1, Tmp2, Tmp3;
943 unsigned Opc = 0;
944 MVT::ValueType DestType = N.getValueType();
945
946 unsigned opcode = N.getOpcode();
947
948 SDNode *Node = N.Val;
949 SDOperand Op0, Op1;
950
951 if (Node->getOpcode() == ISD::CopyFromReg)
952 // Just use the specified register as our input.
953 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000955 unsigned &Reg = ExprMap[N];
956 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000957
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000958 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000959 Reg = Result = (N.getValueType() != MVT::Other) ?
960 MakeReg(N.getValueType()) : 1;
961 else {
962 // If this is a call instruction, make sure to prepare ALL of the result
963 // values as well as the chain.
964 if (Node->getNumValues() == 1)
965 Reg = Result = 1; // Void call, just a chain.
966 else {
967 Result = MakeReg(Node->getValueType(0));
968 ExprMap[N.getValue(0)] = Result;
969 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
970 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
971 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
972 }
973 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000974
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000975 switch (N.getOpcode()) {
976 default:
977 Node->dump();
978 assert(0 && "Node not handled!\n");
979
980 case ISD::FrameIndex: {
981 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
982 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
983 return Result;
984 }
985
986 case ISD::ConstantPool: {
987 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
988 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
989 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
990 .addReg(IA64::r1);
991 return Result;
992 }
993
994 case ISD::ConstantFP: {
995 Tmp1 = Result; // Intermediate Register
996 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
997 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
998 Tmp1 = MakeReg(MVT::f64);
999
1000 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1001 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1002 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
1003 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1004 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1005 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
1006 else
1007 assert(0 && "Unexpected FP constant!");
1008 if (Tmp1 != Result)
1009 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
1010 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001011 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001012 return Result;
1013 }
1014
1015 case ISD::DYNAMIC_STACKALLOC: {
1016 // Generate both result values.
1017 if (Result != 1)
1018 ExprMap[N.getValue(1)] = 1; // Generate the token
1019 else
1020 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1021
1022 // FIXME: We are currently ignoring the requested alignment for handling
1023 // greater than the stack alignment. This will need to be revisited at some
1024 // point. Align = N.getOperand(2);
1025
1026 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1027 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1028 std::cerr << "Cannot allocate stack object with greater alignment than"
1029 << " the stack alignment yet!";
1030 abort();
1031 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001032
1033/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001034 Select(N.getOperand(0));
1035 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1036 {
1037 if (CN->getValue() < 32000)
1038 {
1039 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001040 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001041 } else {
1042 Tmp1 = SelectExpr(N.getOperand(1));
1043 // Subtract size from stack pointer, thereby allocating some space.
1044 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1045 }
1046 } else {
1047 Tmp1 = SelectExpr(N.getOperand(1));
1048 // Subtract size from stack pointer, thereby allocating some space.
1049 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1050 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001051*/
1052 Select(N.getOperand(0));
1053 Tmp1 = SelectExpr(N.getOperand(1));
1054 // Subtract size from stack pointer, thereby allocating some space.
1055 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001056 // Put a pointer to the space into the result register, by copying the
1057 // stack pointer.
1058 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1059 return Result;
1060 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001061
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001062 case ISD::SELECT: {
1063 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1064 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1065 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1066
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001067 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001068
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001069 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001070 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001071 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1072 // for i1, we load the condition into an integer register, then
1073 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1074 // of them will go through, since the integer register will hold
1075 // either 0 or 1)
1076 case MVT::i1: {
1077 bogoResult=MakeReg(MVT::i1);
1078
1079 // load the condition into an integer register
1080 unsigned condReg=MakeReg(MVT::i64);
1081 unsigned dummy=MakeReg(MVT::i64);
1082 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1083 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1084 .addImm(1).addReg(Tmp1);
1085
1086 // initialize Result (bool) to false (hence UNC) and if
1087 // the select condition (condReg) is false (0), copy Tmp3
1088 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1089 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1090
1091 // now, if the selection condition is true, write 1 to the
1092 // result if Tmp2 is 1
1093 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1094 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1095 break;
1096 }
1097 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1098 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001099 case MVT::i64:
1100 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001101 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1102 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1103 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001104 break;
1105 case MVT::f64:
1106 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001107 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1108 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1109 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001110 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001111 }
Duraid Madina4bd708d2005-05-02 06:41:13 +00001112
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001113 return Result;
1114 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001115
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001116 case ISD::Constant: {
1117 unsigned depositPos=0;
1118 unsigned depositLen=0;
1119 switch (N.getValueType()) {
1120 default: assert(0 && "Cannot use constants of this type!");
1121 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001122 // the constant:
1123 if(cast<ConstantSDNode>(N)->getValue()) // true:
1124 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1125 else // false:
1126 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1127 return Result; // early exit
1128 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001129 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001130 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001131
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001132 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001133
1134 if(immediate==0) { // if the constant is just zero,
1135 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1136 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001137 }
1138
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001139 if (immediate <= 8191 && immediate >= -8192) {
1140 // if this constants fits in 14 bits, we use a mov the assembler will
1141 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1142 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1143 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001144 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001145
1146 if (immediate <= 2097151 && immediate >= -2097152) {
1147 // if this constants fits in 22 bits, we use a mov the assembler will
1148 // turn into: "addl rDest=imm,r0"
1149 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1150 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001151 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001152
1153 /* otherwise, our immediate is big, so we use movl */
1154 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001155 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001156 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001157 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001158
1159 case ISD::UNDEF: {
1160 BuildMI(BB, IA64::IDEF, 0, Result);
1161 return Result;
1162 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001163
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001164 case ISD::GlobalAddress: {
1165 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1166 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001167
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001168 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001169 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001170
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001171 return Result;
1172 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001173
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001174 case ISD::ExternalSymbol: {
1175 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001176// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1177 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001178 return Result;
1179 }
1180
1181 case ISD::FP_EXTEND: {
1182 Tmp1 = SelectExpr(N.getOperand(0));
1183 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1184 return Result;
1185 }
1186
1187 case ISD::ZERO_EXTEND: {
1188 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001189
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001190 switch (N.getOperand(0).getValueType()) {
1191 default: assert(0 && "Cannot zero-extend this type!");
1192 case MVT::i8: Opc = IA64::ZXT1; break;
1193 case MVT::i16: Opc = IA64::ZXT2; break;
1194 case MVT::i32: Opc = IA64::ZXT4; break;
1195
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001197 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001198 unsigned dummy = MakeReg(MVT::i64);
1199 // first load zero:
1200 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1201 // ...then conditionally (PR:Tmp1) add 1:
1202 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1203 .addImm(1).addReg(Tmp1);
1204 return Result; // XXX early exit!
1205 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001206 }
1207
1208 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1209 return Result;
1210 }
1211
1212 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1213
1214assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1215
1216 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001217
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001218 switch (N.getOperand(0).getValueType()) {
1219 default: assert(0 && "Cannot sign-extend this type!");
1220 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001221 Opc = IA64::SXT1; break;
1222 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001223 case MVT::i8: Opc = IA64::SXT1; break;
1224 case MVT::i16: Opc = IA64::SXT2; break;
1225 case MVT::i32: Opc = IA64::SXT4; break;
1226 }
1227
1228 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1229 return Result;
1230 }
1231
1232 case ISD::TRUNCATE: {
1233 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1234 // of R0 appropriately.
1235 switch (N.getOperand(0).getValueType()) {
1236 default: assert(0 && "Unknown truncate!");
1237 case MVT::i64: break;
1238 }
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 unsigned depositPos, depositLen;
1241
1242 switch (N.getValueType()) {
1243 default: assert(0 && "Unknown truncate!");
1244 case MVT::i1: {
1245 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001246 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1247 .addReg(IA64::r0);
1248 return Result; // XXX early exit!
1249 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001250 case MVT::i8: depositPos=0; depositLen=8; break;
1251 case MVT::i16: depositPos=0; depositLen=16; break;
1252 case MVT::i32: depositPos=0; depositLen=32; break;
1253 }
1254 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1255 .addImm(depositPos).addImm(depositLen);
1256 return Result;
1257 }
1258
Misha Brukman7847fca2005-04-22 17:54:37 +00001259/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001260 case ISD::FP_ROUND: {
1261 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001262 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001263 Tmp1 = SelectExpr(N.getOperand(0));
1264 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1265 // we add 0.0 using a single precision add to do rounding
1266 return Result;
1267 }
1268*/
1269
1270// FIXME: the following 4 cases need cleaning
1271 case ISD::SINT_TO_FP: {
1272 Tmp1 = SelectExpr(N.getOperand(0));
1273 Tmp2 = MakeReg(MVT::f64);
1274 unsigned dummy = MakeReg(MVT::f64);
1275 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1276 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1277 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1278 return Result;
1279 }
1280
1281 case ISD::UINT_TO_FP: {
1282 Tmp1 = SelectExpr(N.getOperand(0));
1283 Tmp2 = MakeReg(MVT::f64);
1284 unsigned dummy = MakeReg(MVT::f64);
1285 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1286 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1287 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1288 return Result;
1289 }
1290
1291 case ISD::FP_TO_SINT: {
1292 Tmp1 = SelectExpr(N.getOperand(0));
1293 Tmp2 = MakeReg(MVT::f64);
1294 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1295 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1296 return Result;
1297 }
1298
1299 case ISD::FP_TO_UINT: {
1300 Tmp1 = SelectExpr(N.getOperand(0));
1301 Tmp2 = MakeReg(MVT::f64);
1302 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1303 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1304 return Result;
1305 }
1306
1307 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001308 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1309 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1310 // into an fma, do so:
1311 // ++FusedFP; // Statistic
1312 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1313 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1314 Tmp3 = SelectExpr(N.getOperand(1));
1315 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1316 return Result; // early exit
1317 }
Duraid Madinaed095022005-04-13 06:12:04 +00001318
1319 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001320 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001321 // this add into a shladd, try:
1322 ConstantSDNode *CSD = NULL;
1323 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001324 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001325
Misha Brukman7847fca2005-04-22 17:54:37 +00001326 // ++FusedSHLADD; // Statistic
1327 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1328 int shl_amt = CSD->getValue();
1329 Tmp3 = SelectExpr(N.getOperand(1));
1330
1331 BuildMI(BB, IA64::SHLADD, 3, Result)
1332 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1333 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001334 }
1335 }
1336
1337 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001338 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001339 if(DestType != MVT::f64) { // integer addition:
1340 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001341 case 1: // adding a constant that's 14 bits
1342 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1343 return Result; // early exit
1344 } // fallthrough and emit a reg+reg ADD:
1345 Tmp2 = SelectExpr(N.getOperand(1));
1346 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001347 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001348 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001349 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1350 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001351 return Result;
1352 }
1353
1354 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001355
1356 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001357 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1358 // boring old integer multiply with xma
1359 Tmp1 = SelectExpr(N.getOperand(0));
1360 Tmp2 = SelectExpr(N.getOperand(1));
1361
1362 unsigned TempFR1=MakeReg(MVT::f64);
1363 unsigned TempFR2=MakeReg(MVT::f64);
1364 unsigned TempFR3=MakeReg(MVT::f64);
1365 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1366 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1367 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1368 .addReg(IA64::F0);
1369 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1370 return Result; // early exit
1371 } else { // we are multiplying by an integer constant! yay
1372 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1373 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001374 }
Duraid Madinab2322562005-04-26 07:23:02 +00001375 else { // floating point multiply
1376 Tmp1 = SelectExpr(N.getOperand(0));
1377 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001378 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001379 return Result;
1380 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001381 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001382
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001383 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001384 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1385 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1386 // into an fms, do so:
1387 // ++FusedFP; // Statistic
1388 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1389 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1390 Tmp3 = SelectExpr(N.getOperand(1));
1391 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1392 return Result; // early exit
1393 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001394 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001395 if(DestType != MVT::f64) { // integer subtraction:
1396 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001397 case 1: // subtracting *from* an 8 bit constant:
1398 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1399 return Result; // early exit
1400 } // fallthrough and emit a reg+reg SUB:
1401 Tmp1 = SelectExpr(N.getOperand(0));
1402 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001403 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001404 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001405 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001406 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407 return Result;
1408 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001409
1410 case ISD::FABS: {
1411 Tmp1 = SelectExpr(N.getOperand(0));
1412 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1413 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1414 return Result;
1415 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001416
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001417 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001418 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001419
Misha Brukman4633f1c2005-04-21 23:13:11 +00001420 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001421 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1422 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1423 } else {
1424 Tmp1 = SelectExpr(N.getOperand(0));
1425 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1426 }
1427
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001428 return Result;
1429 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001430
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001431 case ISD::AND: {
1432 switch (N.getValueType()) {
1433 default: assert(0 && "Cannot AND this type!");
1434 case MVT::i1: { // if a bool, we emit a pseudocode AND
1435 unsigned pA = SelectExpr(N.getOperand(0));
1436 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001437
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001438/* our pseudocode for AND is:
1439 *
1440(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1441 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1442 ;;
1443(pB) cmp.ne pTemp,p0 = r0,r0
1444 ;;
1445(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1446
1447*/
1448 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001450 unsigned bogusTemp1 = MakeReg(MVT::i1);
1451 unsigned bogusTemp2 = MakeReg(MVT::i1);
1452 unsigned bogusTemp3 = MakeReg(MVT::i1);
1453 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001454
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001455 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001456 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001457 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001458 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001459 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001460 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001461 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001462 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001463 break;
1464 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001465
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001466 // if not a bool, we just AND away:
1467 case MVT::i8:
1468 case MVT::i16:
1469 case MVT::i32:
1470 case MVT::i64: {
1471 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001472 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1473 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001474 switch (Tmp3) {
1475 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1476 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1477 break;
1478 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1479 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1480 break;
1481 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1482 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1483 break;
1484 default: // otherwise, use dep.z to paste zeros
1485 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1486 .addImm(0).addImm(Tmp3);
1487 break;
1488 }
1489 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001490 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001491 Tmp2 = SelectExpr(N.getOperand(1));
1492 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001493 }
1494 }
1495 return Result;
1496 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001497
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001498 case ISD::OR: {
1499 switch (N.getValueType()) {
1500 default: assert(0 && "Cannot OR this type!");
1501 case MVT::i1: { // if a bool, we emit a pseudocode OR
1502 unsigned pA = SelectExpr(N.getOperand(0));
1503 unsigned pB = SelectExpr(N.getOperand(1));
1504
1505 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001506
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001507/* our pseudocode for OR is:
1508 *
1509
1510pC = pA OR pB
1511-------------
1512
Misha Brukman7847fca2005-04-22 17:54:37 +00001513(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1514 ;;
1515(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001516
1517*/
1518 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001519 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001520 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001521 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001522 break;
1523 }
1524 // if not a bool, we just OR away:
1525 case MVT::i8:
1526 case MVT::i16:
1527 case MVT::i32:
1528 case MVT::i64: {
1529 Tmp1 = SelectExpr(N.getOperand(0));
1530 Tmp2 = SelectExpr(N.getOperand(1));
1531 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1532 break;
1533 }
1534 }
1535 return Result;
1536 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001537
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001538 case ISD::XOR: {
1539 switch (N.getValueType()) {
1540 default: assert(0 && "Cannot XOR this type!");
1541 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1542 unsigned pY = SelectExpr(N.getOperand(0));
1543 unsigned pZ = SelectExpr(N.getOperand(1));
1544
1545/* one possible routine for XOR is:
1546
1547 // Compute px = py ^ pz
1548 // using sum of products: px = (py & !pz) | (pz & !py)
1549 // Uses 5 instructions in 3 cycles.
1550 // cycle 1
1551(pz) cmp.eq.unc px = r0, r0 // px = pz
1552(py) cmp.eq.unc pt = r0, r0 // pt = py
1553 ;;
1554 // cycle 2
1555(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1556(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1557 ;;
1558 } { .mmi
1559 // cycle 3
1560(pt) cmp.eq.or px = r0, r0 // px = px | pt
1561
1562*** Another, which we use here, requires one scratch GR. it is:
1563
1564 mov rt = 0 // initialize rt off critical path
1565 ;;
1566
1567 // cycle 1
1568(pz) cmp.eq.unc px = r0, r0 // px = pz
1569(pz) mov rt = 1 // rt = pz
1570 ;;
1571 // cycle 2
1572(py) cmp.ne px = 1, rt // if (py) px = !pz
1573
1574.. these routines kindly provided by Jim Hull
1575*/
1576 unsigned rt = MakeReg(MVT::i64);
1577
1578 // these two temporaries will never actually appear,
1579 // due to the two-address form of some of the instructions below
1580 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1581 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1582
1583 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1584 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001585 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001586 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001587 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001588 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001589 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001590 break;
1591 }
1592 // if not a bool, we just XOR away:
1593 case MVT::i8:
1594 case MVT::i16:
1595 case MVT::i32:
1596 case MVT::i64: {
1597 Tmp1 = SelectExpr(N.getOperand(0));
1598 Tmp2 = SelectExpr(N.getOperand(1));
1599 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1600 break;
1601 }
1602 }
1603 return Result;
1604 }
1605
Duraid Madina63bbed52005-05-11 05:16:09 +00001606 case ISD::CTPOP: {
1607 Tmp1 = SelectExpr(N.getOperand(0));
1608 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1609 return Result;
1610 }
1611
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001612 case ISD::SHL: {
1613 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001614 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1615 Tmp2 = CN->getValue();
1616 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1617 } else {
1618 Tmp2 = SelectExpr(N.getOperand(1));
1619 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1620 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001621 return Result;
1622 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001623
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001624 case ISD::SRL: {
1625 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001626 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1627 Tmp2 = CN->getValue();
1628 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1629 } else {
1630 Tmp2 = SelectExpr(N.getOperand(1));
1631 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1632 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001633 return Result;
1634 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001635
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001636 case ISD::SRA: {
1637 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001638 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1639 Tmp2 = CN->getValue();
1640 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1641 } else {
1642 Tmp2 = SelectExpr(N.getOperand(1));
1643 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1644 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001645 return Result;
1646 }
1647
1648 case ISD::SDIV:
1649 case ISD::UDIV:
1650 case ISD::SREM:
1651 case ISD::UREM: {
1652
1653 Tmp1 = SelectExpr(N.getOperand(0));
1654 Tmp2 = SelectExpr(N.getOperand(1));
1655
1656 bool isFP=false;
1657
1658 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1659 isFP=true;
1660
1661 bool isModulus=false; // is it a division or a modulus?
1662 bool isSigned=false;
1663
1664 switch(N.getOpcode()) {
1665 case ISD::SDIV: isModulus=false; isSigned=true; break;
1666 case ISD::UDIV: isModulus=false; isSigned=false; break;
1667 case ISD::SREM: isModulus=true; isSigned=true; break;
1668 case ISD::UREM: isModulus=true; isSigned=false; break;
1669 }
1670
Duraid Madina4826a072005-04-06 09:55:17 +00001671 if(!isModulus && !isFP) { // if this is an integer divide,
1672 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001673 case 1: // division by a constant that's a power of 2
1674 Tmp1 = SelectExpr(N.getOperand(0));
1675 if(isSigned) { // argument could be negative, so emit some code:
1676 unsigned divAmt=Tmp3;
1677 unsigned tempGR1=MakeReg(MVT::i64);
1678 unsigned tempGR2=MakeReg(MVT::i64);
1679 unsigned tempGR3=MakeReg(MVT::i64);
1680 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1681 .addReg(Tmp1).addImm(divAmt-1);
1682 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1683 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1684 BuildMI(BB, IA64::ADD, 2, tempGR3)
1685 .addReg(Tmp1).addReg(tempGR2);
1686 BuildMI(BB, IA64::SHRS, 2, Result)
1687 .addReg(tempGR3).addImm(divAmt);
1688 }
1689 else // unsigned div-by-power-of-2 becomes a simple shift right:
1690 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1691 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001692 }
1693 }
1694
Misha Brukman4633f1c2005-04-21 23:13:11 +00001695 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001696 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001697 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1698 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1699 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1700 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1701 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1702 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1703 unsigned TmpF7=MakeReg(MVT::f64);
1704 unsigned TmpF8=MakeReg(MVT::f64);
1705 unsigned TmpF9=MakeReg(MVT::f64);
1706 unsigned TmpF10=MakeReg(MVT::f64);
1707 unsigned TmpF11=MakeReg(MVT::f64);
1708 unsigned TmpF12=MakeReg(MVT::f64);
1709 unsigned TmpF13=MakeReg(MVT::f64);
1710 unsigned TmpF14=MakeReg(MVT::f64);
1711 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001712
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001713 // OK, emit some code:
1714
1715 if(!isFP) {
1716 // first, load the inputs into FP regs.
1717 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1718 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001719
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001720 // next, convert the inputs to FP
1721 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001722 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1723 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001724 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001725 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1726 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001727 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001728
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001729 } else { // this is an FP divide/remainder, so we 'leak' some temp
1730 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1731 TmpF3=Tmp1;
1732 TmpF4=Tmp2;
1733 }
1734
1735 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001736 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1737 BuildMI(BB, IA64::FRCPAS1, 4)
1738 .addReg(TmpF5, MachineOperand::Def)
1739 .addReg(TmpPR, MachineOperand::Def)
1740 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001741
Duraid Madinabeeaab22005-03-31 12:31:11 +00001742 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1743 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1744 // TPCMPNE below
1745 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1746 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001747 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001748 }
1749
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001750 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1751 // precision, don't need this much for f32/i32)
1752 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1753 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1754 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1755 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1756 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1757 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1758 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1759 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1760 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1761 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1762 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1763 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1764 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1765 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1766 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1767 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001768
1769 // FIXME: this is unfortunate :(
1770 // the story is that the dest reg of the fnma above and the fma below
1771 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1772 // be the same register, or this code breaks if the first argument is
1773 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001774 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1775 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1776
Duraid Madina6e02e682005-04-04 05:05:52 +00001777 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1778 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1779 }
1780
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001781 if(!isFP) {
1782 // round to an integer
1783 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001784 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001785 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001786 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001787 } else {
1788 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1789 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1790 // we really do need the above FMOV? ;)
1791 }
1792
1793 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001794 if(isFP) { // extra worrying about div-by-zero
1795 unsigned bogoResult=MakeReg(MVT::f64);
1796
1797 // we do a 'conditional fmov' (of the correct result, depending
1798 // on how the frcpa predicate turned out)
1799 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001800 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001801 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001802 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001803 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001804 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001805 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001806 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001807 } else { // this is a modulus
1808 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001809 // answer = q * (-b) + a
1810 unsigned ModulusResult = MakeReg(MVT::f64);
1811 unsigned TmpF = MakeReg(MVT::f64);
1812 unsigned TmpI = MakeReg(MVT::i64);
1813
1814 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1815 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1816 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1817 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1818 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001819 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001820 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001821 }
1822 }
1823
1824 return Result;
1825 }
1826
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001827 case ISD::SIGN_EXTEND_INREG: {
1828 Tmp1 = SelectExpr(N.getOperand(0));
1829 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1830 switch(MVN->getExtraValueType())
1831 {
1832 default:
1833 Node->dump();
1834 assert(0 && "don't know how to sign extend this type");
1835 break;
1836 case MVT::i8: Opc = IA64::SXT1; break;
1837 case MVT::i16: Opc = IA64::SXT2; break;
1838 case MVT::i32: Opc = IA64::SXT4; break;
1839 }
1840 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1841 return Result;
1842 }
1843
1844 case ISD::SETCC: {
1845 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001846
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001847 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1848 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001849
Misha Brukman7847fca2005-04-22 17:54:37 +00001850 if(ConstantSDNode *CSDN =
1851 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1852 // if we are comparing against a constant zero
1853 if(CSDN->getValue()==0)
1854 Tmp2 = IA64::r0; // then we can just compare against r0
1855 else
1856 Tmp2 = SelectExpr(N.getOperand(1));
1857 } else // not comparing against a constant
1858 Tmp2 = SelectExpr(N.getOperand(1));
1859
1860 switch (SetCC->getCondition()) {
1861 default: assert(0 && "Unknown integer comparison!");
1862 case ISD::SETEQ:
1863 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1864 break;
1865 case ISD::SETGT:
1866 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1867 break;
1868 case ISD::SETGE:
1869 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 break;
1871 case ISD::SETLT:
1872 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1873 break;
1874 case ISD::SETLE:
1875 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1876 break;
1877 case ISD::SETNE:
1878 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1879 break;
1880 case ISD::SETULT:
1881 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1882 break;
1883 case ISD::SETUGT:
1884 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1885 break;
1886 case ISD::SETULE:
1887 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1888 break;
1889 case ISD::SETUGE:
1890 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1891 break;
1892 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001893 }
1894 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001895 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1896 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001897
Misha Brukman7847fca2005-04-22 17:54:37 +00001898 if(ConstantFPSDNode *CFPSDN =
1899 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001900
Misha Brukman7847fca2005-04-22 17:54:37 +00001901 // if we are comparing against a constant +0.0 or +1.0
1902 if(CFPSDN->isExactlyValue(+0.0))
1903 Tmp2 = IA64::F0; // then we can just compare against f0
1904 else if(CFPSDN->isExactlyValue(+1.0))
1905 Tmp2 = IA64::F1; // or f1
1906 else
1907 Tmp2 = SelectExpr(N.getOperand(1));
1908 } else // not comparing against a constant
1909 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001910
Misha Brukman7847fca2005-04-22 17:54:37 +00001911 switch (SetCC->getCondition()) {
1912 default: assert(0 && "Unknown FP comparison!");
1913 case ISD::SETEQ:
1914 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1915 break;
1916 case ISD::SETGT:
1917 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1918 break;
1919 case ISD::SETGE:
1920 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1921 break;
1922 case ISD::SETLT:
1923 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1924 break;
1925 case ISD::SETLE:
1926 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1927 break;
1928 case ISD::SETNE:
1929 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1930 break;
1931 case ISD::SETULT:
1932 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1933 break;
1934 case ISD::SETUGT:
1935 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1936 break;
1937 case ISD::SETULE:
1938 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1939 break;
1940 case ISD::SETUGE:
1941 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1942 break;
1943 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001944 }
1945 }
1946 else
1947 assert(0 && "this setcc not implemented yet");
1948
1949 return Result;
1950 }
1951
1952 case ISD::EXTLOAD:
1953 case ISD::ZEXTLOAD:
1954 case ISD::LOAD: {
1955 // Make sure we generate both values.
1956 if (Result != 1)
1957 ExprMap[N.getValue(1)] = 1; // Generate the token
1958 else
1959 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1960
1961 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001962
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001963 if(opcode == ISD::LOAD) { // this is a LOAD
1964 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001965 default: assert(0 && "Cannot load this type!");
1966 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1967 // FIXME: for now, we treat bool loads the same as i8 loads */
1968 case MVT::i8: Opc = IA64::LD1; break;
1969 case MVT::i16: Opc = IA64::LD2; break;
1970 case MVT::i32: Opc = IA64::LD4; break;
1971 case MVT::i64: Opc = IA64::LD8; break;
1972
1973 case MVT::f32: Opc = IA64::LDF4; break;
1974 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001975 }
1976 } else { // this is an EXTLOAD or ZEXTLOAD
1977 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1978 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001979 default: assert(0 && "Cannot extload/zextload this type!");
1980 // FIXME: bools?
1981 case MVT::i8: Opc = IA64::LD1; break;
1982 case MVT::i16: Opc = IA64::LD2; break;
1983 case MVT::i32: Opc = IA64::LD4; break;
1984 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001985 }
1986 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001987
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001988 SDOperand Chain = N.getOperand(0);
1989 SDOperand Address = N.getOperand(1);
1990
1991 if(Address.getOpcode() == ISD::GlobalAddress) {
1992 Select(Chain);
1993 unsigned dummy = MakeReg(MVT::i64);
1994 unsigned dummy2 = MakeReg(MVT::i64);
1995 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1997 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001998 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1999 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002000 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002001 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002002 // into a predicate register
2003 assert(Opc==IA64::LD1 && "problem loading a bool");
2004 unsigned dummy3 = MakeReg(MVT::i64);
2005 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
2006 // we compare to 0. true? 0. false? 1.
2007 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002008 }
2009 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
2010 Select(Chain);
2011 IA64Lowering.restoreGP(BB);
2012 unsigned dummy = MakeReg(MVT::i64);
2013 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00002014 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002016 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002017 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002018 // into a predicate register
2019 assert(Opc==IA64::LD1 && "problem loading a bool");
2020 unsigned dummy3 = MakeReg(MVT::i64);
2021 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2022 // we compare to 0. true? 0. false? 1.
2023 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002024 }
2025 } else if(Address.getOpcode() == ISD::FrameIndex) {
2026 Select(Chain); // FIXME ? what about bools?
2027 unsigned dummy = MakeReg(MVT::i64);
2028 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00002029 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002030 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002031 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002032 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002033 // into a predicate register
2034 assert(Opc==IA64::LD1 && "problem loading a bool");
2035 unsigned dummy3 = MakeReg(MVT::i64);
2036 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2037 // we compare to 0. true? 0. false? 1.
2038 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002039 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002040 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002041 Select(Chain);
2042 Tmp2 = SelectExpr(Address);
2043 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002045 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002046 // into a predicate register
2047 assert(Opc==IA64::LD1 && "problem loading a bool");
2048 unsigned dummy = MakeReg(MVT::i64);
2049 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2050 // we compare to 0. true? 0. false? 1.
2051 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
2052 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002053 }
2054
2055 return Result;
2056 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002057
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002058 case ISD::CopyFromReg: {
2059 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002060 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002061 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002062
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002063 SDOperand Chain = N.getOperand(0);
2064
2065 Select(Chain);
2066 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2067
2068 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002069 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2070 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002071 // (r) Result =cmp.eq.unc(r0,r0)
2072 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002073 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002074 return Result;
2075 }
2076
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002077 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002078 case ISD::CALL: {
2079 Select(N.getOperand(0));
2080
2081 // The chain for this call is now lowered.
2082 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002083
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002084 //grab the arguments
2085 std::vector<unsigned> argvregs;
2086
2087 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002088 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002089
2090 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002091 // Runtime Architecture Guide to see some examples of what's going
2092 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2093 // while FP args get mapped to F8->F15 as needed)
2094
2095 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002096
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002097 // in reg args
2098 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2099 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002100 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2101 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2102 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2103 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002104
Misha Brukman7847fca2005-04-22 17:54:37 +00002105 switch(N.getOperand(i+2).getValueType())
2106 {
2107 default: // XXX do we need to support MVT::i1 here?
2108 Node->dump();
2109 N.getOperand(i).Val->dump();
2110 std::cerr << "Type for " << i << " is: " <<
2111 N.getOperand(i+2).getValueType() << std::endl;
2112 assert(0 && "Unknown value type for call");
2113 case MVT::i64:
2114 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2115 break;
2116 case MVT::f64:
2117 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2118 .addReg(argvregs[i]);
2119 // FIXME: we don't need to do this _all_ the time:
2120 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2121 break;
2122 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002123 }
2124
2125 //in mem args
2126 for (int i = 8, e = argvregs.size(); i < e; ++i)
2127 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002128 unsigned tempAddr = MakeReg(MVT::i64);
2129
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002130 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002131 default:
2132 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002133 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002134 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002135 N.getOperand(i+2).getValueType() << "\n";
2136 assert(0 && "Unknown value type for call");
2137 case MVT::i1: // FIXME?
2138 case MVT::i8:
2139 case MVT::i16:
2140 case MVT::i32:
2141 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002142 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2143 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2144 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002145 break;
2146 case MVT::f32:
2147 case MVT::f64:
2148 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002149 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2150 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002151 break;
2152 }
2153 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002154
Duraid Madina04aa46d2005-05-20 11:39:17 +00002155 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002156 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002157 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002158 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002159 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2160 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002161 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002162 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002163 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002164 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002165 // "non-pic code with imm relocation against dynamic symbol" errors
2166 BuildMI(BB, IA64::BRCALL, 1)
2167 .addExternalSymbol(ESSDN->getSymbol(), true);
2168 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002169 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002170 else { // otherwise we need to get the function descriptor
2171 // load the branch target (function)'s entry point and
2172 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002173 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002174
2175 unsigned targetEntryPoint=MakeReg(MVT::i64);
2176 unsigned targetGPAddr=MakeReg(MVT::i64);
2177 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002178
Duraid Madinabeeaab22005-03-31 12:31:11 +00002179 // b6 is a scratch branch register, we load the target entry point
2180 // from the base of the function descriptor
2181 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2182 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2183
2184 // save the current GP:
2185 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002186
Duraid Madinabeeaab22005-03-31 12:31:11 +00002187 /* TODO: we need to make sure doing this never, ever loads a
2188 * bogus value into r1 (GP). */
2189 // load the target GP (which is at mem[functiondescriptor+8])
2190 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002191 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002192 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2193
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002194 // and then jump: (well, call)
2195 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002196 // and finally restore the old GP
2197 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2198 IA64Lowering.restoreSP_RP(BB);
2199 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002200
2201 switch (Node->getValueType(0)) {
2202 default: assert(0 && "Unknown value type for call result!");
2203 case MVT::Other: return 1;
2204 case MVT::i1:
2205 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002206 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002207 break;
2208 case MVT::i8:
2209 case MVT::i16:
2210 case MVT::i32:
2211 case MVT::i64:
2212 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2213 break;
2214 case MVT::f64:
2215 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2216 break;
2217 }
2218 return Result+N.ResNo;
2219 }
2220
Misha Brukman4633f1c2005-04-21 23:13:11 +00002221 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002222 return 0;
2223}
2224
2225void ISel::Select(SDOperand N) {
2226 unsigned Tmp1, Tmp2, Opc;
2227 unsigned opcode = N.getOpcode();
2228
Nate Begeman85fdeb22005-03-24 04:39:54 +00002229 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002230 return; // Already selected.
2231
2232 SDNode *Node = N.Val;
2233
2234 switch (Node->getOpcode()) {
2235 default:
2236 Node->dump(); std::cerr << "\n";
2237 assert(0 && "Node not handled yet!");
2238
2239 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002240
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002241 case ISD::TokenFactor: {
2242 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2243 Select(Node->getOperand(i));
2244 return;
2245 }
2246
2247 case ISD::CopyToReg: {
2248 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002249 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002250 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002251
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002252 if (Tmp1 != Tmp2) {
2253 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002254 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2255 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002256 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2257 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002258 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002259 // XXX is this the right way 'round? ;)
2260 }
2261 return;
2262 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002263
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002264 case ISD::RET: {
2265
2266 /* what the heck is going on here:
2267
2268<_sabre_> ret with two operands is obvious: chain and value
2269<camel_> yep
2270<_sabre_> ret with 3 values happens when 'expansion' occurs
2271<_sabre_> e.g. i64 gets split into 2x i32
2272<camel_> oh right
2273<_sabre_> you don't have this case on ia64
2274<camel_> yep
2275<_sabre_> so the two returned values go into EAX/EDX on ia32
2276<camel_> ahhh *memories*
2277<_sabre_> :)
2278<camel_> ok, thanks :)
2279<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2280<_sabre_> this is the first operand always
2281<_sabre_> these operand often define chains, they are the last operand
2282<_sabre_> they are printed as 'ch' if you do DAG.dump()
2283 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002284
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002285 switch (N.getNumOperands()) {
2286 default:
2287 assert(0 && "Unknown return instruction!");
2288 case 2:
2289 Select(N.getOperand(0));
2290 Tmp1 = SelectExpr(N.getOperand(1));
2291 switch (N.getOperand(1).getValueType()) {
2292 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002293 // FIXME: do I need to add support for bools here?
2294 // (return '0' or '1' r8, basically...)
2295 //
2296 // FIXME: need to round floats - 80 bits is bad, the tester
2297 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002298 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002299 // we mark r8 as live on exit up above in LowerArguments()
2300 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2301 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002302 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002303 // we mark F8 as live on exit up above in LowerArguments()
2304 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002305 }
2306 break;
2307 case 1:
2308 Select(N.getOperand(0));
2309 break;
2310 }
2311 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2312 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2313 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2314 return;
2315 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002316
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002317 case ISD::BR: {
2318 Select(N.getOperand(0));
2319 MachineBasicBlock *Dest =
2320 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2321 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2322 // XXX HACK! we do _not_ need long branches all the time
2323 return;
2324 }
2325
2326 case ISD::ImplicitDef: {
2327 Select(N.getOperand(0));
2328 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2329 return;
2330 }
2331
2332 case ISD::BRCOND: {
2333 MachineBasicBlock *Dest =
2334 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2335
2336 Select(N.getOperand(0));
2337 Tmp1 = SelectExpr(N.getOperand(1));
2338 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2339 // XXX HACK! we do _not_ need long branches all the time
2340 return;
2341 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002342
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002343 case ISD::EXTLOAD:
2344 case ISD::ZEXTLOAD:
2345 case ISD::SEXTLOAD:
2346 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002347 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002348 case ISD::CALL:
2349 case ISD::CopyFromReg:
2350 case ISD::DYNAMIC_STACKALLOC:
2351 SelectExpr(N);
2352 return;
2353
2354 case ISD::TRUNCSTORE:
2355 case ISD::STORE: {
2356 Select(N.getOperand(0));
2357 Tmp1 = SelectExpr(N.getOperand(1)); // value
2358
2359 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002360
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002361 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002362 switch (N.getOperand(1).getValueType()) {
2363 default: assert(0 && "Cannot store this type!");
2364 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2365 // FIXME?: for now, we treat bool loads the same as i8 stores */
2366 case MVT::i8: Opc = IA64::ST1; break;
2367 case MVT::i16: Opc = IA64::ST2; break;
2368 case MVT::i32: Opc = IA64::ST4; break;
2369 case MVT::i64: Opc = IA64::ST8; break;
2370
2371 case MVT::f32: Opc = IA64::STF4; break;
2372 case MVT::f64: Opc = IA64::STF8; break;
2373 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002374 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002375 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2376 default: assert(0 && "unknown type in truncstore");
2377 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2378 //FIXME: DAG does not promote this load?
2379 case MVT::i8: Opc = IA64::ST1; break;
2380 case MVT::i16: Opc = IA64::ST2; break;
2381 case MVT::i32: Opc = IA64::ST4; break;
2382 case MVT::f32: Opc = IA64::STF4; break;
2383 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002384 }
2385
2386 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002387 unsigned dummy = MakeReg(MVT::i64);
2388 unsigned dummy2 = MakeReg(MVT::i64);
2389 BuildMI(BB, IA64::ADD, 2, dummy)
2390 .addGlobalAddress(cast<GlobalAddressSDNode>
2391 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2392 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002393
Misha Brukman7847fca2005-04-22 17:54:37 +00002394 if(!isBool)
2395 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2396 else { // we are storing a bool, so emit a little pseudocode
2397 // to store a predicate register as one byte
2398 assert(Opc==IA64::ST1);
2399 unsigned dummy3 = MakeReg(MVT::i64);
2400 unsigned dummy4 = MakeReg(MVT::i64);
2401 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2402 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2403 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2404 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2405 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002406 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2407
Misha Brukman7847fca2005-04-22 17:54:37 +00002408 // FIXME? (what about bools?)
2409
2410 unsigned dummy = MakeReg(MVT::i64);
2411 BuildMI(BB, IA64::MOV, 1, dummy)
2412 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2413 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002414 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002415 Tmp2 = SelectExpr(N.getOperand(2)); //address
2416 if(!isBool)
2417 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2418 else { // we are storing a bool, so emit a little pseudocode
2419 // to store a predicate register as one byte
2420 assert(Opc==IA64::ST1);
2421 unsigned dummy3 = MakeReg(MVT::i64);
2422 unsigned dummy4 = MakeReg(MVT::i64);
2423 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2424 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2425 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2426 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2427 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002428 }
2429 return;
2430 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002431
Chris Lattner16cd04d2005-05-12 23:24:06 +00002432 case ISD::CALLSEQ_START:
2433 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002434 Select(N.getOperand(0));
2435 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002436
Chris Lattner16cd04d2005-05-12 23:24:06 +00002437 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2438 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002439 BuildMI(BB, Opc, 1).addImm(Tmp1);
2440 return;
2441 }
2442
2443 return;
2444 }
2445 assert(0 && "GAME OVER. INSERT COIN?");
2446}
2447
2448
2449/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2450/// into a machine code representation using pattern matching and a machine
2451/// description file.
2452///
2453FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002454 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002455}
2456
2457