blob: 2d00c3be4724deaf2c48b111aba61b31ae3520a7 [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>
Nate Begeman8e21e712005-03-26 01:29:23 +0000112 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
113 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000114
115 virtual std::pair<SDOperand, SDOperand>
116 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
117
118 virtual std::pair<SDOperand,SDOperand>
119 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
120 const Type *ArgTy, SelectionDAG &DAG);
121
122 virtual std::pair<SDOperand, SDOperand>
123 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
124 SelectionDAG &DAG);
125
126 void restoreGP_SP_RP(MachineBasicBlock* BB)
127 {
128 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
129 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
130 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
131 }
132
Duraid Madinabeeaab22005-03-31 12:31:11 +0000133 void restoreSP_RP(MachineBasicBlock* BB)
134 {
135 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
136 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
137 }
138
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000139 void restoreRP(MachineBasicBlock* BB)
140 {
141 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
142 }
143
144 void restoreGP(MachineBasicBlock* BB)
145 {
146 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
147 }
148
149 };
150}
151
152
153std::vector<SDOperand>
154IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
155 std::vector<SDOperand> ArgValues;
156
157 //
158 // add beautiful description of IA64 stack frame format
159 // here (from intel 24535803.pdf most likely)
160 //
161 MachineFunction &MF = DAG.getMachineFunction();
162 MachineFrameInfo *MFI = MF.getFrameInfo();
163
164 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
165 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
166 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167
168 MachineBasicBlock& BB = MF.front();
169
Misha Brukman4633f1c2005-04-21 23:13:11 +0000170 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
173 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000175
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000176 unsigned argVreg[8];
177 unsigned argPreg[8];
178 unsigned argOpc[8];
179
Duraid Madinabeeaab22005-03-31 12:31:11 +0000180 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000181
Duraid Madinabeeaab22005-03-31 12:31:11 +0000182 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000184
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000185 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000186 {
187 SDOperand newroot, argt;
188 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000189
190 switch (getValueType(I->getType())) {
191 default:
192 std::cerr << "ERROR in LowerArgs: unknown type "
193 << getValueType(I->getType()) << "\n";
194 abort();
195 case MVT::f32:
196 // fixme? (well, will need to for weird FP structy stuff,
197 // see intel ABI docs)
198 case MVT::f64:
199//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
200 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
201 // floating point args go into f8..f15 as-needed, the increment
202 argVreg[count] = // is below..:
203 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
204 // FP args go into f8..f15 as needed: (hence the ++)
205 argPreg[count] = args_FP[used_FPArgs++];
206 argOpc[count] = IA64::FMOV;
207 argt = newroot = DAG.getCopyFromReg(argVreg[count],
208 getValueType(I->getType()), DAG.getRoot());
209 break;
210 case MVT::i1: // NOTE: as far as C abi stuff goes,
211 // bools are just boring old ints
212 case MVT::i8:
213 case MVT::i16:
214 case MVT::i32:
215 case MVT::i64:
216//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
217 MF.addLiveIn(args_int[count]); // mark this register as liveIn
218 argVreg[count] =
219 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
220 argPreg[count] = args_int[count];
221 argOpc[count] = IA64::MOV;
222 argt = newroot =
223 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
224 if ( getValueType(I->getType()) != MVT::i64)
225 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
226 newroot);
227 break;
228 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000229 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000230 // Create the frame index object for this incoming parameter...
231 ArgOffset = 16 + 8 * (count - 8);
232 int FI = MFI->CreateFixedObject(8, ArgOffset);
233
234 // Create the SelectionDAG nodes corresponding to a load
235 //from this parameter
236 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
237 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000238 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000239 }
240 ++count;
241 DAG.setRoot(newroot.getValue(1));
242 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000243 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000244
Misha Brukman4633f1c2005-04-21 23:13:11 +0000245
Duraid Madinabeeaab22005-03-31 12:31:11 +0000246 // Create a vreg to hold the output of (what will become)
247 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000248 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
249 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
250 // we create a PSEUDO_ALLOC (pseudo)instruction for now
251
252 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
253
254 // hmm:
255 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
256 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
257 // ..hmm.
258
259 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
260
261 // hmm:
262 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
263 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
264 // ..hmm.
265
Duraid Madinabeeaab22005-03-31 12:31:11 +0000266 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000267
Duraid Madinabeeaab22005-03-31 12:31:11 +0000268 // if this is a varargs function, we simply lower llvm.va_start by
269 // pointing to the first entry
270 if(F.isVarArg()) {
271 tempOffset=0;
272 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000273 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000274
Duraid Madinabeeaab22005-03-31 12:31:11 +0000275 // here we actually do the moving of args, and store them to the stack
276 // too if this is a varargs function:
277 for (int i = 0; i < count && i < 8; ++i) {
278 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
279 if(F.isVarArg()) {
280 // if this is a varargs function, we copy the input registers to the stack
281 int FI = MFI->CreateFixedObject(8, tempOffset);
282 tempOffset+=8; //XXX: is it safe to use r22 like this?
283 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
284 // FIXME: we should use st8.spill here, one day
285 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
286 }
287 }
288
Duraid Madinaca494fd2005-04-12 14:54:44 +0000289 // Finally, inform the code generator which regs we return values in.
290 // (see the ISD::RET: case down below)
291 switch (getValueType(F.getReturnType())) {
292 default: assert(0 && "i have no idea where to return this type!");
293 case MVT::isVoid: break;
294 case MVT::i1:
295 case MVT::i8:
296 case MVT::i16:
297 case MVT::i32:
298 case MVT::i64:
299 MF.addLiveOut(IA64::r8);
300 break;
301 case MVT::f32:
302 case MVT::f64:
303 MF.addLiveOut(IA64::F8);
304 break;
305 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000306
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000307 return ArgValues;
308}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000309
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000310std::pair<SDOperand, SDOperand>
311IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000312 const Type *RetTy, bool isVarArg,
313 SDOperand Callee, ArgListTy &Args,
314 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000315
316 MachineFunction &MF = DAG.getMachineFunction();
317
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000318 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000319 unsigned outRegsUsed = 0;
320
321 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000322 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000323 outRegsUsed = 8;
324 } else {
325 outRegsUsed = Args.size();
326 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000327
Duraid Madinabeeaab22005-03-31 12:31:11 +0000328 // FIXME? this WILL fail if we ever try to pass around an arg that
329 // consumes more than a single output slot (a 'real' double, int128
330 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
331 // registers we use. Hopefully, the assembler will notice.
332 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
333 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000334
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000335 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
336 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000337
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000338 std::vector<SDOperand> args_to_use;
339 for (unsigned i = 0, e = Args.size(); i != e; ++i)
340 {
341 switch (getValueType(Args[i].second)) {
342 default: assert(0 && "unexpected argument type!");
343 case MVT::i1:
344 case MVT::i8:
345 case MVT::i16:
346 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000347 //promote to 64-bits, sign/zero extending based on type
348 //of the argument
349 if(Args[i].second->isSigned())
350 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
351 Args[i].first);
352 else
353 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
354 Args[i].first);
355 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000357 //promote to 64-bits
358 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000359 case MVT::f64:
360 case MVT::i64:
361 break;
362 }
363 args_to_use.push_back(Args[i].first);
364 }
365
366 std::vector<MVT::ValueType> RetVals;
367 MVT::ValueType RetTyVT = getValueType(RetTy);
368 if (RetTyVT != MVT::isVoid)
369 RetVals.push_back(RetTyVT);
370 RetVals.push_back(MVT::Other);
371
372 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000373 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000374 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
375 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
376 DAG.getConstant(NumBytes, getPointerTy()));
377 return std::make_pair(TheCall, Chain);
378}
379
380std::pair<SDOperand, SDOperand>
381IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
382 // vastart just returns the address of the VarArgsFrameIndex slot.
383 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
384}
385
386std::pair<SDOperand,SDOperand> IA64TargetLowering::
387LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
388 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000389
390 MVT::ValueType ArgVT = getValueType(ArgTy);
391 SDOperand Result;
392 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000393 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000394 } else {
395 unsigned Amt;
396 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
397 Amt = 8;
398 else {
399 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
400 "Other types should have been promoted for varargs!");
401 Amt = 8;
402 }
403 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
404 DAG.getConstant(Amt, VAList.getValueType()));
405 }
406 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000407}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000408
409std::pair<SDOperand, SDOperand> IA64TargetLowering::
410LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
411 SelectionDAG &DAG) {
412
413 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000414 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000415}
416
417
418namespace {
419
420 //===--------------------------------------------------------------------===//
421 /// ISel - IA64 specific code to select IA64 machine instructions for
422 /// SelectionDAG operations.
423 ///
424 class ISel : public SelectionDAGISel {
425 /// IA64Lowering - This object fully describes how to lower LLVM code to an
426 /// IA64-specific SelectionDAG.
427 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000428 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
429 // for sdiv and udiv until it is put into the future
430 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000431
432 /// ExprMap - As shared expressions are codegen'd, we keep track of which
433 /// vreg the value is produced in, so we only emit one copy of each compiled
434 /// tree.
435 std::map<SDOperand, unsigned> ExprMap;
436 std::set<SDOperand> LoweredTokens;
437
438 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000439 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
440 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000441
442 /// InstructionSelectBasicBlock - This callback is invoked by
443 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
444 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
445
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000446 unsigned SelectExpr(SDOperand N);
447 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000448 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
449 SDOperand BuildConstmulSequence(SDOperand N);
450
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000451 };
452}
453
454/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
455/// when it has created a SelectionDAG for us to codegen.
456void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
457
458 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000459 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000460 Select(DAG.getRoot());
461
462 // Clear state used for selection.
463 ExprMap.clear();
464 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000465 ISelDAG = 0;
466}
467
Duraid Madinab2322562005-04-26 07:23:02 +0000468// strip leading '0' characters from a string
469void munchLeadingZeros(std::string& inString) {
470 while(inString.c_str()[0]=='0') {
471 inString.erase(0, 1);
472 }
473}
474
475// strip trailing '0' characters from a string
476void munchTrailingZeros(std::string& inString) {
477 int curPos=inString.length()-1;
478
479 while(inString.c_str()[curPos]=='0') {
480 inString.erase(curPos, 1);
481 curPos--;
482 }
483}
484
485// return how many consecutive '0' characters are at the end of a string
486unsigned int countTrailingZeros(std::string& inString) {
487 int curPos=inString.length()-1;
488 unsigned int zeroCount=0;
489 // assert goes here
490 while(inString.c_str()[curPos--]=='0') {
491 zeroCount++;
492 }
493 return zeroCount;
494}
495
496// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
497// '0' and 'N' (-1) characters)
498void boothEncode(std::string inString, std::string& boothEncodedString) {
499
500 int curpos=0;
501 int replacements=0;
502 int lim=inString.size();
503
504 while(curpos<lim) {
505 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
506 int runlength=0;
507 std::string replaceString="N";
508
509 // find the run length
510 for(;inString[curpos+runlength]=='1';runlength++) ;
511
512 for(int i=0; i<runlength-1; i++)
513 replaceString+="0";
514 replaceString+="1";
515
516 if(runlength>1) {
517 inString.replace(curpos, runlength+1, replaceString);
518 curpos+=runlength-1;
519 } else
520 curpos++;
521 } else { // a zero, we just keep chugging along
522 curpos++;
523 }
524 }
525
526 // clean up (trim the string, reverse it and turn '1's into 'P's)
527 munchTrailingZeros(inString);
528 boothEncodedString="";
529
530 for(int i=inString.size()-1;i>=0;i--)
531 if(inString[i]=='1')
532 boothEncodedString+="P";
533 else
534 boothEncodedString+=inString[i];
535
536}
537
538struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
539 unsigned firstVal; // A
540 unsigned firstShift; // B
541 unsigned secondVal; // C
542 unsigned secondShift; // D
543 bool isSub;
544};
545
546/* this implements Lefevre's "pattern-based" constant multiplication,
547 * see "Multiplication by an Integer Constant", INRIA report 1999-06
548 *
549 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
550 * to get better booth encodings - this does help in practice
551 * TODO: weight shifts appropriately (most architectures can't
552 * fuse a shift and an add for arbitrary shift amounts) */
553unsigned lefevre(const std::string inString,
554 std::vector<struct shiftaddblob> &ops) {
555 std::string retstring;
556 std::string s = inString;
557 munchTrailingZeros(s);
558
559 int length=s.length()-1;
560
561 if(length==0) {
562 return(0);
563 }
564
565 std::vector<int> p,n;
566
567 for(int i=0; i<=length; i++) {
568 if (s.c_str()[length-i]=='P') {
569 p.push_back(i);
570 } else if (s.c_str()[length-i]=='N') {
571 n.push_back(i);
572 }
573 }
574
575 std::string t, u;
Duraid Madina4706c032005-04-26 09:42:50 +0000576 int c;
577 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000578 std::map<const int, int> w;
579
Duraid Madina85d5f602005-04-27 11:57:39 +0000580 for(unsigned i=0; i<p.size(); i++) {
581 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000582 w[p[i]-p[j]]++;
583 }
584 }
585
Duraid Madina85d5f602005-04-27 11:57:39 +0000586 for(unsigned i=1; i<n.size(); i++) {
587 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000588 w[n[i]-n[j]]++;
589 }
590 }
591
Duraid Madina85d5f602005-04-27 11:57:39 +0000592 for(unsigned i=0; i<p.size(); i++) {
593 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000594 w[-abs(p[i]-n[j])]++;
595 }
596 }
597
598 std::map<const int, int>::const_iterator ii;
599 std::vector<int> d;
600 std::multimap<int, int> sorted_by_value;
601
602 for(ii = w.begin(); ii!=w.end(); ii++)
603 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
604
605 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
606 it != sorted_by_value.end(); ++it) {
607 d.push_back((*it).second);
608 }
609
610 int int_W=0;
611 int int_d;
612
613 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
614 d.pop_back();
615 retstring=s; // hmmm
616 int x=0;
617 int z=abs(int_d)-1;
618
619 if(int_d>0) {
620
Duraid Madina85d5f602005-04-27 11:57:39 +0000621 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000622 if( ((base+z+1) < retstring.size()) &&
623 retstring.c_str()[base]=='P' &&
624 retstring.c_str()[base+z+1]=='P')
625 {
626 // match
627 x++;
628 retstring.replace(base, 1, "0");
629 retstring.replace(base+z+1, 1, "p");
630 }
631 }
632
Duraid Madina85d5f602005-04-27 11:57:39 +0000633 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000634 if( ((base+z+1) < retstring.size()) &&
635 retstring.c_str()[base]=='N' &&
636 retstring.c_str()[base+z+1]=='N')
637 {
638 // match
639 x++;
640 retstring.replace(base, 1, "0");
641 retstring.replace(base+z+1, 1, "n");
642 }
643 }
644
645 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000646 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000647 if( ((base+z+1) < retstring.size()) &&
648 ((retstring.c_str()[base]=='P' &&
649 retstring.c_str()[base+z+1]=='N') ||
650 (retstring.c_str()[base]=='N' &&
651 retstring.c_str()[base+z+1]=='P')) ) {
652 // match
653 x++;
654
655 if(retstring.c_str()[base]=='P') {
656 retstring.replace(base, 1, "0");
657 retstring.replace(base+z+1, 1, "p");
658 } else { // retstring[base]=='N'
659 retstring.replace(base, 1, "0");
660 retstring.replace(base+z+1, 1, "n");
661 }
662 }
663 }
664 }
665
666 if(x>int_W) {
667 int_W = x;
668 t = retstring;
669 c = int_d; // tofix
670 }
671
672 } d.pop_back(); // hmm
673
674 u = t;
675
Duraid Madina85d5f602005-04-27 11:57:39 +0000676 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000677 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
678 t.replace(i, 1, "0");
679 }
680
Duraid Madina85d5f602005-04-27 11:57:39 +0000681 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000682 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000683 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000684 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000685 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000686 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000687 u.replace(i, 1, "N");
688 }
689
690 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000691 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000692 c=-c;
693 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000694 f=false;
Duraid Madinab2322562005-04-26 07:23:02 +0000695
Duraid Madina8a3042c2005-05-09 13:18:34 +0000696 int pos=0;
697 while(u[pos]=='0')
698 pos++;
699
700 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000701
702 int g=0;
703 if(hit) {
704 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000705 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000706 bool isP=(u[p]=='P');
707 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000708
709 if(isP)
710 u.replace(p, 1, "N");
711 if(isN)
712 u.replace(p, 1, "P");
713 }
714 }
715
716 munchLeadingZeros(u);
717
718 int i = lefevre(u, ops);
719
720 shiftaddblob blob;
721
722 blob.firstVal=i; blob.firstShift=c;
723 blob.isSub=f;
724 blob.secondVal=i; blob.secondShift=0;
725
726 ops.push_back(blob);
727
728 i = ops.size();
729
730 munchLeadingZeros(t);
731
732 if(t.length()==0)
733 return i;
734
735 if(t.c_str()[0]!='P') {
736 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000737 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000738 bool isP=(t.c_str()[p]=='P');
739 bool isN=(t.c_str()[p]=='N');
740
741 if(isP)
742 t.replace(p, 1, "N");
743 if(isN)
744 t.replace(p, 1, "P");
745 }
746 }
747
748 int j = lefevre(t, ops);
749
750 int trail=countTrailingZeros(u);
751 blob.secondVal=i; blob.secondShift=trail;
752
753 trail=countTrailingZeros(t);
754 blob.firstVal=j; blob.firstShift=trail;
755
756 switch(g) {
757 case 0:
758 blob.isSub=false; // first + second
759 break;
760 case 1:
761 blob.isSub=true; // first - second
762 break;
763 case 2:
764 blob.isSub=true; // second - first
765 int tmpval, tmpshift;
766 tmpval=blob.firstVal;
767 tmpshift=blob.firstShift;
768 blob.firstVal=blob.secondVal;
769 blob.firstShift=blob.secondShift;
770 blob.secondVal=tmpval;
771 blob.secondShift=tmpshift;
772 break;
773 //assert
774 }
775
776 ops.push_back(blob);
777 return ops.size();
778}
779
780SDOperand ISel::BuildConstmulSequence(SDOperand N) {
781 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
782 // in particular, *3 is nicer as *2+1, not *4-1
783 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
784
785 bool flippedSign;
786 unsigned preliminaryShift=0;
787
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000788 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000789
790 // first, we make the constant to multiply by positive
791 if(constant<0) {
792 constant=-constant;
793 flippedSign=true;
794 } else {
795 flippedSign=false;
796 }
797
798 // next, we make it odd.
799 for(; (constant%2==0); preliminaryShift++)
800 constant>>=1;
801
802 //OK, we have a positive, odd number of 64 bits or less. Convert it
803 //to a binary string, constantString[0] is the LSB
804 char constantString[65];
805 for(int i=0; i<64; i++)
806 constantString[i]='0'+((constant>>i)&0x1);
807 constantString[64]=0;
808
809 // now, Booth encode it
810 std::string boothEncodedString;
811 boothEncode(constantString, boothEncodedString);
812
813 std::vector<struct shiftaddblob> ops;
814 // do the transformation, filling out 'ops'
815 lefevre(boothEncodedString, ops);
816
817 SDOperand results[ops.size()]; // temporary results (of adds/subs of shifts)
818
819 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000820 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000821 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
822 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
823 results[ops[i].firstVal-1];
824 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
825 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
826 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
827 results[ops[i].secondVal-1];
828 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
829 if(ops[i].isSub)
830 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
831 else
832 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
833 }
834
835 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000836 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000837 if(preliminaryShift) {
838 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000839 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Duraid Madinab2322562005-04-26 07:23:02 +0000840 results[ops.size()-1], finalshift);
841 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000842 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000843 }
844
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000845 SDOperand finalresult;
846 if(flippedSign) { // if we were multiplying by a negative constant:
847 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
848 // subtract the result from 0 to flip its sign
849 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
850 } else { // there was no preliminary multiply by -1 required
851 finalresult = shiftedresult;
852 }
853
Duraid Madinab2322562005-04-26 07:23:02 +0000854 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000855}
856
Duraid Madina4826a072005-04-06 09:55:17 +0000857/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
858/// returns zero when the input is not exactly a power of two.
Duraid Madinac02780e2005-04-13 04:50:54 +0000859static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000860 if (Val == 0 || (Val & (Val-1))) return 0;
861 unsigned Count = 0;
862 while (Val != 1) {
863 Val >>= 1;
864 ++Count;
865 }
866 return Count;
867}
868
Duraid Madinac02780e2005-04-13 04:50:54 +0000869/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
870/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
871static unsigned ExactLog2sub1(uint64_t Val) {
872 unsigned int n;
873 for(n=0; n<64; n++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000874 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000875 return n;
876 }
877 return 666;
878}
879
Duraid Madina4826a072005-04-06 09:55:17 +0000880/// ponderIntegerDivisionBy - When handling integer divides, if the divide
881/// is by a constant such that we can efficiently codegen it, this
882/// function says what to do. Currently, it returns 0 if the division must
883/// become a genuine divide, and 1 if the division can be turned into a
884/// right shift.
885static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
886 unsigned& Imm) {
887 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
888 // a constant, give up.
889
890 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
891
Misha Brukman4633f1c2005-04-21 23:13:11 +0000892 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
Duraid Madina4826a072005-04-06 09:55:17 +0000893 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000894 }
895
Duraid Madina4826a072005-04-06 09:55:17 +0000896 return 0; // fallthrough
897}
898
Duraid Madinac02780e2005-04-13 04:50:54 +0000899static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
900 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
901 // a constant, give up.
902
903 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
904
905 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
906 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000907 }
908
Duraid Madinac02780e2005-04-13 04:50:54 +0000909 return 0; // fallthrough
910}
911
Duraid Madinaf55e4032005-04-07 12:33:38 +0000912static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
913 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
914 // constant, give up.
915 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
916
917 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
918 Imm = v & 0x3FFF; // 14 bits
919 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000920 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000921 return 0; // fallthrough
922}
923
924static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
925 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
926 // constant, give up.
927 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
928
929 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
930 Imm = v & 0xFF; // 8 bits
931 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000932 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000933 return 0; // fallthrough
934}
935
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000936unsigned ISel::SelectExpr(SDOperand N) {
937 unsigned Result;
938 unsigned Tmp1, Tmp2, Tmp3;
939 unsigned Opc = 0;
940 MVT::ValueType DestType = N.getValueType();
941
942 unsigned opcode = N.getOpcode();
943
944 SDNode *Node = N.Val;
945 SDOperand Op0, Op1;
946
947 if (Node->getOpcode() == ISD::CopyFromReg)
948 // Just use the specified register as our input.
949 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000950
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000951 unsigned &Reg = ExprMap[N];
952 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000953
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000954 if (N.getOpcode() != ISD::CALL)
955 Reg = Result = (N.getValueType() != MVT::Other) ?
956 MakeReg(N.getValueType()) : 1;
957 else {
958 // If this is a call instruction, make sure to prepare ALL of the result
959 // values as well as the chain.
960 if (Node->getNumValues() == 1)
961 Reg = Result = 1; // Void call, just a chain.
962 else {
963 Result = MakeReg(Node->getValueType(0));
964 ExprMap[N.getValue(0)] = Result;
965 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
966 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
967 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
968 }
969 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000970
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000971 switch (N.getOpcode()) {
972 default:
973 Node->dump();
974 assert(0 && "Node not handled!\n");
975
976 case ISD::FrameIndex: {
977 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
978 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
979 return Result;
980 }
981
982 case ISD::ConstantPool: {
983 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
984 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
985 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
986 .addReg(IA64::r1);
987 return Result;
988 }
989
990 case ISD::ConstantFP: {
991 Tmp1 = Result; // Intermediate Register
992 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
993 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
994 Tmp1 = MakeReg(MVT::f64);
995
996 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
997 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
998 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
999 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1000 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1001 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
1002 else
1003 assert(0 && "Unexpected FP constant!");
1004 if (Tmp1 != Result)
1005 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
1006 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001007 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001008 return Result;
1009 }
1010
1011 case ISD::DYNAMIC_STACKALLOC: {
1012 // Generate both result values.
1013 if (Result != 1)
1014 ExprMap[N.getValue(1)] = 1; // Generate the token
1015 else
1016 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1017
1018 // FIXME: We are currently ignoring the requested alignment for handling
1019 // greater than the stack alignment. This will need to be revisited at some
1020 // point. Align = N.getOperand(2);
1021
1022 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1023 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1024 std::cerr << "Cannot allocate stack object with greater alignment than"
1025 << " the stack alignment yet!";
1026 abort();
1027 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001028
1029/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001030 Select(N.getOperand(0));
1031 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1032 {
1033 if (CN->getValue() < 32000)
1034 {
1035 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001036 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001037 } else {
1038 Tmp1 = SelectExpr(N.getOperand(1));
1039 // Subtract size from stack pointer, thereby allocating some space.
1040 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1041 }
1042 } else {
1043 Tmp1 = SelectExpr(N.getOperand(1));
1044 // Subtract size from stack pointer, thereby allocating some space.
1045 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1046 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001047*/
1048 Select(N.getOperand(0));
1049 Tmp1 = SelectExpr(N.getOperand(1));
1050 // Subtract size from stack pointer, thereby allocating some space.
1051 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001052 // Put a pointer to the space into the result register, by copying the
1053 // stack pointer.
1054 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1055 return Result;
1056 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001057
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001058 case ISD::SELECT: {
1059 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1060 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1061 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1062
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001063 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001064
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001065 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001066 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001067 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1068 // for i1, we load the condition into an integer register, then
1069 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1070 // of them will go through, since the integer register will hold
1071 // either 0 or 1)
1072 case MVT::i1: {
1073 bogoResult=MakeReg(MVT::i1);
1074
1075 // load the condition into an integer register
1076 unsigned condReg=MakeReg(MVT::i64);
1077 unsigned dummy=MakeReg(MVT::i64);
1078 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1079 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1080 .addImm(1).addReg(Tmp1);
1081
1082 // initialize Result (bool) to false (hence UNC) and if
1083 // the select condition (condReg) is false (0), copy Tmp3
1084 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1085 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1086
1087 // now, if the selection condition is true, write 1 to the
1088 // result if Tmp2 is 1
1089 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1090 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1091 break;
1092 }
1093 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1094 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001095 case MVT::i64:
1096 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001097 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1098 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1099 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001100 break;
1101 case MVT::f64:
1102 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001103 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1104 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1105 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001106 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001107 }
Duraid Madina4bd708d2005-05-02 06:41:13 +00001108
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001109 return Result;
1110 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001111
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001112 case ISD::Constant: {
1113 unsigned depositPos=0;
1114 unsigned depositLen=0;
1115 switch (N.getValueType()) {
1116 default: assert(0 && "Cannot use constants of this type!");
1117 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001118 // the constant:
1119 if(cast<ConstantSDNode>(N)->getValue()) // true:
1120 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1121 else // false:
1122 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1123 return Result; // early exit
1124 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001125 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001126 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001128 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001129
1130 if(immediate==0) { // if the constant is just zero,
1131 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1132 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001133 }
1134
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001135 if (immediate <= 8191 && immediate >= -8192) {
1136 // if this constants fits in 14 bits, we use a mov the assembler will
1137 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1138 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1139 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001140 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001141
1142 if (immediate <= 2097151 && immediate >= -2097152) {
1143 // if this constants fits in 22 bits, we use a mov the assembler will
1144 // turn into: "addl rDest=imm,r0"
1145 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1146 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001147 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001148
1149 /* otherwise, our immediate is big, so we use movl */
1150 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001151 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001152 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001153 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001154
1155 case ISD::UNDEF: {
1156 BuildMI(BB, IA64::IDEF, 0, Result);
1157 return Result;
1158 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001159
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001160 case ISD::GlobalAddress: {
1161 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1162 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001163
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001164 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001165 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001166
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001167 return Result;
1168 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001169
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001170 case ISD::ExternalSymbol: {
1171 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001172// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1173 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001174 return Result;
1175 }
1176
1177 case ISD::FP_EXTEND: {
1178 Tmp1 = SelectExpr(N.getOperand(0));
1179 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1180 return Result;
1181 }
1182
1183 case ISD::ZERO_EXTEND: {
1184 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001185
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001186 switch (N.getOperand(0).getValueType()) {
1187 default: assert(0 && "Cannot zero-extend this type!");
1188 case MVT::i8: Opc = IA64::ZXT1; break;
1189 case MVT::i16: Opc = IA64::ZXT2; break;
1190 case MVT::i32: Opc = IA64::ZXT4; break;
1191
Misha Brukman4633f1c2005-04-21 23:13:11 +00001192 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001193 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001194 unsigned dummy = MakeReg(MVT::i64);
1195 // first load zero:
1196 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1197 // ...then conditionally (PR:Tmp1) add 1:
1198 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1199 .addImm(1).addReg(Tmp1);
1200 return Result; // XXX early exit!
1201 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001202 }
1203
1204 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1205 return Result;
1206 }
1207
1208 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1209
1210assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1211
1212 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001213
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001214 switch (N.getOperand(0).getValueType()) {
1215 default: assert(0 && "Cannot sign-extend this type!");
1216 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001217 Opc = IA64::SXT1; break;
1218 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001219 case MVT::i8: Opc = IA64::SXT1; break;
1220 case MVT::i16: Opc = IA64::SXT2; break;
1221 case MVT::i32: Opc = IA64::SXT4; break;
1222 }
1223
1224 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1225 return Result;
1226 }
1227
1228 case ISD::TRUNCATE: {
1229 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1230 // of R0 appropriately.
1231 switch (N.getOperand(0).getValueType()) {
1232 default: assert(0 && "Unknown truncate!");
1233 case MVT::i64: break;
1234 }
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 unsigned depositPos, depositLen;
1237
1238 switch (N.getValueType()) {
1239 default: assert(0 && "Unknown truncate!");
1240 case MVT::i1: {
1241 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001242 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1243 .addReg(IA64::r0);
1244 return Result; // XXX early exit!
1245 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001246 case MVT::i8: depositPos=0; depositLen=8; break;
1247 case MVT::i16: depositPos=0; depositLen=16; break;
1248 case MVT::i32: depositPos=0; depositLen=32; break;
1249 }
1250 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1251 .addImm(depositPos).addImm(depositLen);
1252 return Result;
1253 }
1254
Misha Brukman7847fca2005-04-22 17:54:37 +00001255/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001256 case ISD::FP_ROUND: {
1257 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001258 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001259 Tmp1 = SelectExpr(N.getOperand(0));
1260 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1261 // we add 0.0 using a single precision add to do rounding
1262 return Result;
1263 }
1264*/
1265
1266// FIXME: the following 4 cases need cleaning
1267 case ISD::SINT_TO_FP: {
1268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = MakeReg(MVT::f64);
1270 unsigned dummy = MakeReg(MVT::f64);
1271 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1272 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1273 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1274 return Result;
1275 }
1276
1277 case ISD::UINT_TO_FP: {
1278 Tmp1 = SelectExpr(N.getOperand(0));
1279 Tmp2 = MakeReg(MVT::f64);
1280 unsigned dummy = MakeReg(MVT::f64);
1281 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1282 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1283 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1284 return Result;
1285 }
1286
1287 case ISD::FP_TO_SINT: {
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 Tmp2 = MakeReg(MVT::f64);
1290 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1291 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1292 return Result;
1293 }
1294
1295 case ISD::FP_TO_UINT: {
1296 Tmp1 = SelectExpr(N.getOperand(0));
1297 Tmp2 = MakeReg(MVT::f64);
1298 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1299 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1300 return Result;
1301 }
1302
1303 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001304 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1305 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1306 // into an fma, do so:
1307 // ++FusedFP; // Statistic
1308 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1309 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1310 Tmp3 = SelectExpr(N.getOperand(1));
1311 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1312 return Result; // early exit
1313 }
Duraid Madinaed095022005-04-13 06:12:04 +00001314
1315 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001316 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001317 // this add into a shladd, try:
1318 ConstantSDNode *CSD = NULL;
1319 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001320 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001321
Misha Brukman7847fca2005-04-22 17:54:37 +00001322 // ++FusedSHLADD; // Statistic
1323 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1324 int shl_amt = CSD->getValue();
1325 Tmp3 = SelectExpr(N.getOperand(1));
1326
1327 BuildMI(BB, IA64::SHLADD, 3, Result)
1328 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1329 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001330 }
1331 }
1332
1333 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001334 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001335 if(DestType != MVT::f64) { // integer addition:
1336 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001337 case 1: // adding a constant that's 14 bits
1338 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1339 return Result; // early exit
1340 } // fallthrough and emit a reg+reg ADD:
1341 Tmp2 = SelectExpr(N.getOperand(1));
1342 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001343 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001344 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001345 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1346 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001347 return Result;
1348 }
1349
1350 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001351
1352 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001353 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1354 // boring old integer multiply with xma
1355 Tmp1 = SelectExpr(N.getOperand(0));
1356 Tmp2 = SelectExpr(N.getOperand(1));
1357
1358 unsigned TempFR1=MakeReg(MVT::f64);
1359 unsigned TempFR2=MakeReg(MVT::f64);
1360 unsigned TempFR3=MakeReg(MVT::f64);
1361 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1362 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1363 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1364 .addReg(IA64::F0);
1365 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1366 return Result; // early exit
1367 } else { // we are multiplying by an integer constant! yay
1368 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1369 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001370 }
Duraid Madinab2322562005-04-26 07:23:02 +00001371 else { // floating point multiply
1372 Tmp1 = SelectExpr(N.getOperand(0));
1373 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001374 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001375 return Result;
1376 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001377 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001378
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001379 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001380 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1381 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1382 // into an fms, do so:
1383 // ++FusedFP; // Statistic
1384 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1385 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1386 Tmp3 = SelectExpr(N.getOperand(1));
1387 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1388 return Result; // early exit
1389 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001390 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001391 if(DestType != MVT::f64) { // integer subtraction:
1392 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001393 case 1: // subtracting *from* an 8 bit constant:
1394 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1395 return Result; // early exit
1396 } // fallthrough and emit a reg+reg SUB:
1397 Tmp1 = SelectExpr(N.getOperand(0));
1398 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001399 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001400 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001401 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001402 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001403 return Result;
1404 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001405
1406 case ISD::FABS: {
1407 Tmp1 = SelectExpr(N.getOperand(0));
1408 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1409 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1410 return Result;
1411 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001412
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001413 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001414 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001415
Misha Brukman4633f1c2005-04-21 23:13:11 +00001416 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001417 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1418 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1419 } else {
1420 Tmp1 = SelectExpr(N.getOperand(0));
1421 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1422 }
1423
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001424 return Result;
1425 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001426
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001427 case ISD::AND: {
1428 switch (N.getValueType()) {
1429 default: assert(0 && "Cannot AND this type!");
1430 case MVT::i1: { // if a bool, we emit a pseudocode AND
1431 unsigned pA = SelectExpr(N.getOperand(0));
1432 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001433
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001434/* our pseudocode for AND is:
1435 *
1436(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1437 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1438 ;;
1439(pB) cmp.ne pTemp,p0 = r0,r0
1440 ;;
1441(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1442
1443*/
1444 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001445
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001446 unsigned bogusTemp1 = MakeReg(MVT::i1);
1447 unsigned bogusTemp2 = MakeReg(MVT::i1);
1448 unsigned bogusTemp3 = MakeReg(MVT::i1);
1449 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001450
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001451 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001452 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001453 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001454 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001455 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001456 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001457 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001458 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001459 break;
1460 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001461
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001462 // if not a bool, we just AND away:
1463 case MVT::i8:
1464 case MVT::i16:
1465 case MVT::i32:
1466 case MVT::i64: {
1467 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001468 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1469 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001470 switch (Tmp3) {
1471 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1472 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1473 break;
1474 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1475 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1476 break;
1477 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1478 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1479 break;
1480 default: // otherwise, use dep.z to paste zeros
1481 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1482 .addImm(0).addImm(Tmp3);
1483 break;
1484 }
1485 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001486 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001487 Tmp2 = SelectExpr(N.getOperand(1));
1488 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001489 }
1490 }
1491 return Result;
1492 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001493
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001494 case ISD::OR: {
1495 switch (N.getValueType()) {
1496 default: assert(0 && "Cannot OR this type!");
1497 case MVT::i1: { // if a bool, we emit a pseudocode OR
1498 unsigned pA = SelectExpr(N.getOperand(0));
1499 unsigned pB = SelectExpr(N.getOperand(1));
1500
1501 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001502
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001503/* our pseudocode for OR is:
1504 *
1505
1506pC = pA OR pB
1507-------------
1508
Misha Brukman7847fca2005-04-22 17:54:37 +00001509(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1510 ;;
1511(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001512
1513*/
1514 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001515 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001516 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001517 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001518 break;
1519 }
1520 // if not a bool, we just OR away:
1521 case MVT::i8:
1522 case MVT::i16:
1523 case MVT::i32:
1524 case MVT::i64: {
1525 Tmp1 = SelectExpr(N.getOperand(0));
1526 Tmp2 = SelectExpr(N.getOperand(1));
1527 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1528 break;
1529 }
1530 }
1531 return Result;
1532 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001533
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001534 case ISD::XOR: {
1535 switch (N.getValueType()) {
1536 default: assert(0 && "Cannot XOR this type!");
1537 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1538 unsigned pY = SelectExpr(N.getOperand(0));
1539 unsigned pZ = SelectExpr(N.getOperand(1));
1540
1541/* one possible routine for XOR is:
1542
1543 // Compute px = py ^ pz
1544 // using sum of products: px = (py & !pz) | (pz & !py)
1545 // Uses 5 instructions in 3 cycles.
1546 // cycle 1
1547(pz) cmp.eq.unc px = r0, r0 // px = pz
1548(py) cmp.eq.unc pt = r0, r0 // pt = py
1549 ;;
1550 // cycle 2
1551(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1552(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1553 ;;
1554 } { .mmi
1555 // cycle 3
1556(pt) cmp.eq.or px = r0, r0 // px = px | pt
1557
1558*** Another, which we use here, requires one scratch GR. it is:
1559
1560 mov rt = 0 // initialize rt off critical path
1561 ;;
1562
1563 // cycle 1
1564(pz) cmp.eq.unc px = r0, r0 // px = pz
1565(pz) mov rt = 1 // rt = pz
1566 ;;
1567 // cycle 2
1568(py) cmp.ne px = 1, rt // if (py) px = !pz
1569
1570.. these routines kindly provided by Jim Hull
1571*/
1572 unsigned rt = MakeReg(MVT::i64);
1573
1574 // these two temporaries will never actually appear,
1575 // due to the two-address form of some of the instructions below
1576 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1577 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1578
1579 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1580 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001581 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001582 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001583 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001584 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001585 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001586 break;
1587 }
1588 // if not a bool, we just XOR away:
1589 case MVT::i8:
1590 case MVT::i16:
1591 case MVT::i32:
1592 case MVT::i64: {
1593 Tmp1 = SelectExpr(N.getOperand(0));
1594 Tmp2 = SelectExpr(N.getOperand(1));
1595 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1596 break;
1597 }
1598 }
1599 return Result;
1600 }
1601
Duraid Madina63bbed52005-05-11 05:16:09 +00001602 case ISD::CTPOP: {
1603 Tmp1 = SelectExpr(N.getOperand(0));
1604 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1605 return Result;
1606 }
1607
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001608 case ISD::SHL: {
1609 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1611 Tmp2 = CN->getValue();
1612 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1613 } else {
1614 Tmp2 = SelectExpr(N.getOperand(1));
1615 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1616 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001617 return Result;
1618 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001619
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001620 case ISD::SRL: {
1621 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001622 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1623 Tmp2 = CN->getValue();
1624 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1625 } else {
1626 Tmp2 = SelectExpr(N.getOperand(1));
1627 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1628 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001629 return Result;
1630 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001631
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001632 case ISD::SRA: {
1633 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001634 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1635 Tmp2 = CN->getValue();
1636 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1637 } else {
1638 Tmp2 = SelectExpr(N.getOperand(1));
1639 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1640 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001641 return Result;
1642 }
1643
1644 case ISD::SDIV:
1645 case ISD::UDIV:
1646 case ISD::SREM:
1647 case ISD::UREM: {
1648
1649 Tmp1 = SelectExpr(N.getOperand(0));
1650 Tmp2 = SelectExpr(N.getOperand(1));
1651
1652 bool isFP=false;
1653
1654 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1655 isFP=true;
1656
1657 bool isModulus=false; // is it a division or a modulus?
1658 bool isSigned=false;
1659
1660 switch(N.getOpcode()) {
1661 case ISD::SDIV: isModulus=false; isSigned=true; break;
1662 case ISD::UDIV: isModulus=false; isSigned=false; break;
1663 case ISD::SREM: isModulus=true; isSigned=true; break;
1664 case ISD::UREM: isModulus=true; isSigned=false; break;
1665 }
1666
Duraid Madina4826a072005-04-06 09:55:17 +00001667 if(!isModulus && !isFP) { // if this is an integer divide,
1668 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001669 case 1: // division by a constant that's a power of 2
1670 Tmp1 = SelectExpr(N.getOperand(0));
1671 if(isSigned) { // argument could be negative, so emit some code:
1672 unsigned divAmt=Tmp3;
1673 unsigned tempGR1=MakeReg(MVT::i64);
1674 unsigned tempGR2=MakeReg(MVT::i64);
1675 unsigned tempGR3=MakeReg(MVT::i64);
1676 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1677 .addReg(Tmp1).addImm(divAmt-1);
1678 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1679 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1680 BuildMI(BB, IA64::ADD, 2, tempGR3)
1681 .addReg(Tmp1).addReg(tempGR2);
1682 BuildMI(BB, IA64::SHRS, 2, Result)
1683 .addReg(tempGR3).addImm(divAmt);
1684 }
1685 else // unsigned div-by-power-of-2 becomes a simple shift right:
1686 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1687 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001688 }
1689 }
1690
Misha Brukman4633f1c2005-04-21 23:13:11 +00001691 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001692 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001693 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1694 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1695 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1696 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1697 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1698 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1699 unsigned TmpF7=MakeReg(MVT::f64);
1700 unsigned TmpF8=MakeReg(MVT::f64);
1701 unsigned TmpF9=MakeReg(MVT::f64);
1702 unsigned TmpF10=MakeReg(MVT::f64);
1703 unsigned TmpF11=MakeReg(MVT::f64);
1704 unsigned TmpF12=MakeReg(MVT::f64);
1705 unsigned TmpF13=MakeReg(MVT::f64);
1706 unsigned TmpF14=MakeReg(MVT::f64);
1707 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001708
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001709 // OK, emit some code:
1710
1711 if(!isFP) {
1712 // first, load the inputs into FP regs.
1713 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1714 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001715
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001716 // next, convert the inputs to FP
1717 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001718 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1719 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001720 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001721 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1722 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001723 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001724
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001725 } else { // this is an FP divide/remainder, so we 'leak' some temp
1726 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1727 TmpF3=Tmp1;
1728 TmpF4=Tmp2;
1729 }
1730
1731 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001732 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1733 BuildMI(BB, IA64::FRCPAS1, 4)
1734 .addReg(TmpF5, MachineOperand::Def)
1735 .addReg(TmpPR, MachineOperand::Def)
1736 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001737
Duraid Madinabeeaab22005-03-31 12:31:11 +00001738 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1739 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1740 // TPCMPNE below
1741 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1742 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001743 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001744 }
1745
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001746 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1747 // precision, don't need this much for f32/i32)
1748 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1749 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1750 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1751 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1752 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1753 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1754 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1755 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1756 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1757 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1758 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1759 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1760 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1761 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1762 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1763 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001764
1765 // FIXME: this is unfortunate :(
1766 // the story is that the dest reg of the fnma above and the fma below
1767 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1768 // be the same register, or this code breaks if the first argument is
1769 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001770 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1771 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1772
Duraid Madina6e02e682005-04-04 05:05:52 +00001773 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1774 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1775 }
1776
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001777 if(!isFP) {
1778 // round to an integer
1779 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001780 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001781 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001782 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001783 } else {
1784 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1785 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1786 // we really do need the above FMOV? ;)
1787 }
1788
1789 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001790 if(isFP) { // extra worrying about div-by-zero
1791 unsigned bogoResult=MakeReg(MVT::f64);
1792
1793 // we do a 'conditional fmov' (of the correct result, depending
1794 // on how the frcpa predicate turned out)
1795 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001796 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001797 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001798 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001799 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001800 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001801 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001802 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001803 } else { // this is a modulus
1804 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001805 // answer = q * (-b) + a
1806 unsigned ModulusResult = MakeReg(MVT::f64);
1807 unsigned TmpF = MakeReg(MVT::f64);
1808 unsigned TmpI = MakeReg(MVT::i64);
1809
1810 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1811 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1812 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1813 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1814 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001815 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001816 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001817 }
1818 }
1819
1820 return Result;
1821 }
1822
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001823 case ISD::SIGN_EXTEND_INREG: {
1824 Tmp1 = SelectExpr(N.getOperand(0));
1825 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1826 switch(MVN->getExtraValueType())
1827 {
1828 default:
1829 Node->dump();
1830 assert(0 && "don't know how to sign extend this type");
1831 break;
1832 case MVT::i8: Opc = IA64::SXT1; break;
1833 case MVT::i16: Opc = IA64::SXT2; break;
1834 case MVT::i32: Opc = IA64::SXT4; break;
1835 }
1836 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1837 return Result;
1838 }
1839
1840 case ISD::SETCC: {
1841 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001842
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001843 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1844 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001845
Misha Brukman7847fca2005-04-22 17:54:37 +00001846 if(ConstantSDNode *CSDN =
1847 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1848 // if we are comparing against a constant zero
1849 if(CSDN->getValue()==0)
1850 Tmp2 = IA64::r0; // then we can just compare against r0
1851 else
1852 Tmp2 = SelectExpr(N.getOperand(1));
1853 } else // not comparing against a constant
1854 Tmp2 = SelectExpr(N.getOperand(1));
1855
1856 switch (SetCC->getCondition()) {
1857 default: assert(0 && "Unknown integer comparison!");
1858 case ISD::SETEQ:
1859 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1860 break;
1861 case ISD::SETGT:
1862 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 break;
1864 case ISD::SETGE:
1865 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETLT:
1868 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETLE:
1871 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETNE:
1874 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETULT:
1877 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETUGT:
1880 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETULE:
1883 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETUGE:
1886 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
1888 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001889 }
1890 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001891 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1892 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001893
Misha Brukman7847fca2005-04-22 17:54:37 +00001894 if(ConstantFPSDNode *CFPSDN =
1895 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001896
Misha Brukman7847fca2005-04-22 17:54:37 +00001897 // if we are comparing against a constant +0.0 or +1.0
1898 if(CFPSDN->isExactlyValue(+0.0))
1899 Tmp2 = IA64::F0; // then we can just compare against f0
1900 else if(CFPSDN->isExactlyValue(+1.0))
1901 Tmp2 = IA64::F1; // or f1
1902 else
1903 Tmp2 = SelectExpr(N.getOperand(1));
1904 } else // not comparing against a constant
1905 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001906
Misha Brukman7847fca2005-04-22 17:54:37 +00001907 switch (SetCC->getCondition()) {
1908 default: assert(0 && "Unknown FP comparison!");
1909 case ISD::SETEQ:
1910 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1911 break;
1912 case ISD::SETGT:
1913 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1914 break;
1915 case ISD::SETGE:
1916 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1917 break;
1918 case ISD::SETLT:
1919 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1920 break;
1921 case ISD::SETLE:
1922 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1923 break;
1924 case ISD::SETNE:
1925 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1926 break;
1927 case ISD::SETULT:
1928 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1929 break;
1930 case ISD::SETUGT:
1931 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1932 break;
1933 case ISD::SETULE:
1934 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1935 break;
1936 case ISD::SETUGE:
1937 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1938 break;
1939 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001940 }
1941 }
1942 else
1943 assert(0 && "this setcc not implemented yet");
1944
1945 return Result;
1946 }
1947
1948 case ISD::EXTLOAD:
1949 case ISD::ZEXTLOAD:
1950 case ISD::LOAD: {
1951 // Make sure we generate both values.
1952 if (Result != 1)
1953 ExprMap[N.getValue(1)] = 1; // Generate the token
1954 else
1955 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1956
1957 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001958
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001959 if(opcode == ISD::LOAD) { // this is a LOAD
1960 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001961 default: assert(0 && "Cannot load this type!");
1962 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1963 // FIXME: for now, we treat bool loads the same as i8 loads */
1964 case MVT::i8: Opc = IA64::LD1; break;
1965 case MVT::i16: Opc = IA64::LD2; break;
1966 case MVT::i32: Opc = IA64::LD4; break;
1967 case MVT::i64: Opc = IA64::LD8; break;
1968
1969 case MVT::f32: Opc = IA64::LDF4; break;
1970 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001971 }
1972 } else { // this is an EXTLOAD or ZEXTLOAD
1973 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1974 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001975 default: assert(0 && "Cannot extload/zextload this type!");
1976 // FIXME: bools?
1977 case MVT::i8: Opc = IA64::LD1; break;
1978 case MVT::i16: Opc = IA64::LD2; break;
1979 case MVT::i32: Opc = IA64::LD4; break;
1980 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001981 }
1982 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001983
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001984 SDOperand Chain = N.getOperand(0);
1985 SDOperand Address = N.getOperand(1);
1986
1987 if(Address.getOpcode() == ISD::GlobalAddress) {
1988 Select(Chain);
1989 unsigned dummy = MakeReg(MVT::i64);
1990 unsigned dummy2 = MakeReg(MVT::i64);
1991 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001992 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1993 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001994 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1995 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 // into a predicate register
1999 assert(Opc==IA64::LD1 && "problem loading a bool");
2000 unsigned dummy3 = MakeReg(MVT::i64);
2001 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
2002 // we compare to 0. true? 0. false? 1.
2003 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002004 }
2005 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
2006 Select(Chain);
2007 IA64Lowering.restoreGP(BB);
2008 unsigned dummy = MakeReg(MVT::i64);
2009 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00002010 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002011 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002012 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002013 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002014 // into a predicate register
2015 assert(Opc==IA64::LD1 && "problem loading a bool");
2016 unsigned dummy3 = MakeReg(MVT::i64);
2017 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2018 // we compare to 0. true? 0. false? 1.
2019 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002020 }
2021 } else if(Address.getOpcode() == ISD::FrameIndex) {
2022 Select(Chain); // FIXME ? what about bools?
2023 unsigned dummy = MakeReg(MVT::i64);
2024 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00002025 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002026 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002027 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002028 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002029 // into a predicate register
2030 assert(Opc==IA64::LD1 && "problem loading a bool");
2031 unsigned dummy3 = MakeReg(MVT::i64);
2032 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2033 // we compare to 0. true? 0. false? 1.
2034 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002035 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002036 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002037 Select(Chain);
2038 Tmp2 = SelectExpr(Address);
2039 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002040 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002041 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002042 // into a predicate register
2043 assert(Opc==IA64::LD1 && "problem loading a bool");
2044 unsigned dummy = MakeReg(MVT::i64);
2045 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2046 // we compare to 0. true? 0. false? 1.
2047 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
2048 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002049 }
2050
2051 return Result;
2052 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002053
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002054 case ISD::CopyFromReg: {
2055 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002056 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002057 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002058
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002059 SDOperand Chain = N.getOperand(0);
2060
2061 Select(Chain);
2062 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2063
2064 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002065 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2066 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002067 // (r) Result =cmp.eq.unc(r0,r0)
2068 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002069 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002070 return Result;
2071 }
2072
2073 case ISD::CALL: {
2074 Select(N.getOperand(0));
2075
2076 // The chain for this call is now lowered.
2077 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002078
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002079 //grab the arguments
2080 std::vector<unsigned> argvregs;
2081
2082 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002083 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002084
2085 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002086 // Runtime Architecture Guide to see some examples of what's going
2087 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2088 // while FP args get mapped to F8->F15 as needed)
2089
2090 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002091
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002092 // in reg args
2093 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2094 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002095 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2096 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2097 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2098 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002099
Misha Brukman7847fca2005-04-22 17:54:37 +00002100 switch(N.getOperand(i+2).getValueType())
2101 {
2102 default: // XXX do we need to support MVT::i1 here?
2103 Node->dump();
2104 N.getOperand(i).Val->dump();
2105 std::cerr << "Type for " << i << " is: " <<
2106 N.getOperand(i+2).getValueType() << std::endl;
2107 assert(0 && "Unknown value type for call");
2108 case MVT::i64:
2109 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2110 break;
2111 case MVT::f64:
2112 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2113 .addReg(argvregs[i]);
2114 // FIXME: we don't need to do this _all_ the time:
2115 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2116 break;
2117 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002118 }
2119
2120 //in mem args
2121 for (int i = 8, e = argvregs.size(); i < e; ++i)
2122 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002123 unsigned tempAddr = MakeReg(MVT::i64);
2124
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002125 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002126 default:
2127 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002128 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002130 N.getOperand(i+2).getValueType() << "\n";
2131 assert(0 && "Unknown value type for call");
2132 case MVT::i1: // FIXME?
2133 case MVT::i8:
2134 case MVT::i16:
2135 case MVT::i32:
2136 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002137 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2138 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2139 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002140 break;
2141 case MVT::f32:
2142 case MVT::f64:
2143 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002144 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2145 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002146 break;
2147 }
2148 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002149
2150 /* XXX we want to re-enable direct branches! crippling them now
Misha Brukman4633f1c2005-04-21 23:13:11 +00002151 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002152 //build the right kind of call
2153 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002154 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002155 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002156 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2157 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002158 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002159 ^^^^^^^^^^^^^ we want this code one day XXX */
Duraid Madinabeeaab22005-03-31 12:31:11 +00002160 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002161 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002162 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002163 // "non-pic code with imm relocation against dynamic symbol" errors
2164 BuildMI(BB, IA64::BRCALL, 1)
2165 .addExternalSymbol(ESSDN->getSymbol(), true);
2166 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002167 }
2168 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002169 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002170
2171 unsigned targetEntryPoint=MakeReg(MVT::i64);
2172 unsigned targetGPAddr=MakeReg(MVT::i64);
2173 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002174
Duraid Madinabeeaab22005-03-31 12:31:11 +00002175 // b6 is a scratch branch register, we load the target entry point
2176 // from the base of the function descriptor
2177 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2178 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2179
2180 // save the current GP:
2181 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002182
Duraid Madinabeeaab22005-03-31 12:31:11 +00002183 /* TODO: we need to make sure doing this never, ever loads a
2184 * bogus value into r1 (GP). */
2185 // load the target GP (which is at mem[functiondescriptor+8])
2186 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002187 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002188 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2189
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002190 // and then jump: (well, call)
2191 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002192 // and finally restore the old GP
2193 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2194 IA64Lowering.restoreSP_RP(BB);
2195 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002196
2197 switch (Node->getValueType(0)) {
2198 default: assert(0 && "Unknown value type for call result!");
2199 case MVT::Other: return 1;
2200 case MVT::i1:
2201 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002202 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002203 break;
2204 case MVT::i8:
2205 case MVT::i16:
2206 case MVT::i32:
2207 case MVT::i64:
2208 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2209 break;
2210 case MVT::f64:
2211 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2212 break;
2213 }
2214 return Result+N.ResNo;
2215 }
2216
Misha Brukman4633f1c2005-04-21 23:13:11 +00002217 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002218 return 0;
2219}
2220
2221void ISel::Select(SDOperand N) {
2222 unsigned Tmp1, Tmp2, Opc;
2223 unsigned opcode = N.getOpcode();
2224
Nate Begeman85fdeb22005-03-24 04:39:54 +00002225 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002226 return; // Already selected.
2227
2228 SDNode *Node = N.Val;
2229
2230 switch (Node->getOpcode()) {
2231 default:
2232 Node->dump(); std::cerr << "\n";
2233 assert(0 && "Node not handled yet!");
2234
2235 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002236
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002237 case ISD::TokenFactor: {
2238 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2239 Select(Node->getOperand(i));
2240 return;
2241 }
2242
2243 case ISD::CopyToReg: {
2244 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002245 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002246 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002247
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002248 if (Tmp1 != Tmp2) {
2249 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002250 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2251 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002252 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2253 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002254 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002255 // XXX is this the right way 'round? ;)
2256 }
2257 return;
2258 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002259
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002260 case ISD::RET: {
2261
2262 /* what the heck is going on here:
2263
2264<_sabre_> ret with two operands is obvious: chain and value
2265<camel_> yep
2266<_sabre_> ret with 3 values happens when 'expansion' occurs
2267<_sabre_> e.g. i64 gets split into 2x i32
2268<camel_> oh right
2269<_sabre_> you don't have this case on ia64
2270<camel_> yep
2271<_sabre_> so the two returned values go into EAX/EDX on ia32
2272<camel_> ahhh *memories*
2273<_sabre_> :)
2274<camel_> ok, thanks :)
2275<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2276<_sabre_> this is the first operand always
2277<_sabre_> these operand often define chains, they are the last operand
2278<_sabre_> they are printed as 'ch' if you do DAG.dump()
2279 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002280
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002281 switch (N.getNumOperands()) {
2282 default:
2283 assert(0 && "Unknown return instruction!");
2284 case 2:
2285 Select(N.getOperand(0));
2286 Tmp1 = SelectExpr(N.getOperand(1));
2287 switch (N.getOperand(1).getValueType()) {
2288 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002289 // FIXME: do I need to add support for bools here?
2290 // (return '0' or '1' r8, basically...)
2291 //
2292 // FIXME: need to round floats - 80 bits is bad, the tester
2293 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002294 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002295 // we mark r8 as live on exit up above in LowerArguments()
2296 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2297 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002298 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002299 // we mark F8 as live on exit up above in LowerArguments()
2300 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002301 }
2302 break;
2303 case 1:
2304 Select(N.getOperand(0));
2305 break;
2306 }
2307 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2308 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2309 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2310 return;
2311 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002312
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002313 case ISD::BR: {
2314 Select(N.getOperand(0));
2315 MachineBasicBlock *Dest =
2316 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2317 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2318 // XXX HACK! we do _not_ need long branches all the time
2319 return;
2320 }
2321
2322 case ISD::ImplicitDef: {
2323 Select(N.getOperand(0));
2324 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2325 return;
2326 }
2327
2328 case ISD::BRCOND: {
2329 MachineBasicBlock *Dest =
2330 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2331
2332 Select(N.getOperand(0));
2333 Tmp1 = SelectExpr(N.getOperand(1));
2334 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2335 // XXX HACK! we do _not_ need long branches all the time
2336 return;
2337 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002338
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002339 case ISD::EXTLOAD:
2340 case ISD::ZEXTLOAD:
2341 case ISD::SEXTLOAD:
2342 case ISD::LOAD:
2343 case ISD::CALL:
2344 case ISD::CopyFromReg:
2345 case ISD::DYNAMIC_STACKALLOC:
2346 SelectExpr(N);
2347 return;
2348
2349 case ISD::TRUNCSTORE:
2350 case ISD::STORE: {
2351 Select(N.getOperand(0));
2352 Tmp1 = SelectExpr(N.getOperand(1)); // value
2353
2354 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002355
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002356 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002357 switch (N.getOperand(1).getValueType()) {
2358 default: assert(0 && "Cannot store this type!");
2359 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2360 // FIXME?: for now, we treat bool loads the same as i8 stores */
2361 case MVT::i8: Opc = IA64::ST1; break;
2362 case MVT::i16: Opc = IA64::ST2; break;
2363 case MVT::i32: Opc = IA64::ST4; break;
2364 case MVT::i64: Opc = IA64::ST8; break;
2365
2366 case MVT::f32: Opc = IA64::STF4; break;
2367 case MVT::f64: Opc = IA64::STF8; break;
2368 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002369 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002370 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2371 default: assert(0 && "unknown type in truncstore");
2372 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2373 //FIXME: DAG does not promote this load?
2374 case MVT::i8: Opc = IA64::ST1; break;
2375 case MVT::i16: Opc = IA64::ST2; break;
2376 case MVT::i32: Opc = IA64::ST4; break;
2377 case MVT::f32: Opc = IA64::STF4; break;
2378 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002379 }
2380
2381 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002382 unsigned dummy = MakeReg(MVT::i64);
2383 unsigned dummy2 = MakeReg(MVT::i64);
2384 BuildMI(BB, IA64::ADD, 2, dummy)
2385 .addGlobalAddress(cast<GlobalAddressSDNode>
2386 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2387 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002388
Misha Brukman7847fca2005-04-22 17:54:37 +00002389 if(!isBool)
2390 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2391 else { // we are storing a bool, so emit a little pseudocode
2392 // to store a predicate register as one byte
2393 assert(Opc==IA64::ST1);
2394 unsigned dummy3 = MakeReg(MVT::i64);
2395 unsigned dummy4 = MakeReg(MVT::i64);
2396 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2397 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2398 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2399 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2400 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002401 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2402
Misha Brukman7847fca2005-04-22 17:54:37 +00002403 // FIXME? (what about bools?)
2404
2405 unsigned dummy = MakeReg(MVT::i64);
2406 BuildMI(BB, IA64::MOV, 1, dummy)
2407 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2408 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002409 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002410 Tmp2 = SelectExpr(N.getOperand(2)); //address
2411 if(!isBool)
2412 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2413 else { // we are storing a bool, so emit a little pseudocode
2414 // to store a predicate register as one byte
2415 assert(Opc==IA64::ST1);
2416 unsigned dummy3 = MakeReg(MVT::i64);
2417 unsigned dummy4 = MakeReg(MVT::i64);
2418 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2419 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2420 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2421 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2422 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002423 }
2424 return;
2425 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002426
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002427 case ISD::ADJCALLSTACKDOWN:
2428 case ISD::ADJCALLSTACKUP: {
2429 Select(N.getOperand(0));
2430 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002431
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002432 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
2433 IA64::ADJUSTCALLSTACKUP;
2434 BuildMI(BB, Opc, 1).addImm(Tmp1);
2435 return;
2436 }
2437
2438 return;
2439 }
2440 assert(0 && "GAME OVER. INSERT COIN?");
2441}
2442
2443
2444/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2445/// into a machine code representation using pattern matching and a machine
2446/// description file.
2447///
2448FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002449 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002450}
2451
2452