blob: 5428a8b9ed991e524e00a390df9f38ceabfd61a9 [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000060 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
61
Misha Brukman4633f1c2005-04-21 23:13:11 +000062 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000063 setShiftAmountType(MVT::i64);
64
65 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000066
67 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000068
69 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000073
74 setOperationAction(ISD::SREM , MVT::f32 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
76
77 setOperationAction(ISD::UREM , MVT::f32 , Expand);
78 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000079
Duraid Madina9b9d45f2005-03-17 18:17:03 +000080 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
81 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
82 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
83
Chris Lattner17234b72005-04-30 04:26:06 +000084 // We don't support sin/cos/sqrt
85 setOperationAction(ISD::FSIN , MVT::f64, Expand);
86 setOperationAction(ISD::FCOS , MVT::f64, Expand);
87 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
88 setOperationAction(ISD::FSIN , MVT::f32, Expand);
89 setOperationAction(ISD::FCOS , MVT::f32, Expand);
90 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
91
Andrew Lenharthb5884d32005-05-04 19:25:37 +000092 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000093 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
94 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000095
Duraid Madina9b9d45f2005-03-17 18:17:03 +000096 computeRegisterProperties();
97
98 addLegalFPImmediate(+0.0);
99 addLegalFPImmediate(+1.0);
100 addLegalFPImmediate(-0.0);
101 addLegalFPImmediate(-1.0);
102 }
103
104 /// LowerArguments - This hook must be implemented to indicate how we should
105 /// lower the arguments for the specified function, into the specified DAG.
106 virtual std::vector<SDOperand>
107 LowerArguments(Function &F, SelectionDAG &DAG);
108
109 /// LowerCallTo - This hook lowers an abstract call to a function into an
110 /// actual call.
111 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000112 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000113 bool isTailCall, SDOperand Callee, ArgListTy &Args,
114 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000115
Chris Lattnere0fe2252005-07-05 19:58:54 +0000116 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
117 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000118 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000119 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
120 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000121
122 void restoreGP_SP_RP(MachineBasicBlock* BB)
123 {
124 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
125 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
126 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
127 }
128
Duraid Madinabeeaab22005-03-31 12:31:11 +0000129 void restoreSP_RP(MachineBasicBlock* BB)
130 {
131 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
132 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
133 }
134
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000135 void restoreRP(MachineBasicBlock* BB)
136 {
137 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
138 }
139
140 void restoreGP(MachineBasicBlock* BB)
141 {
142 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
143 }
144
145 };
146}
147
148
149std::vector<SDOperand>
150IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
151 std::vector<SDOperand> ArgValues;
152
153 //
154 // add beautiful description of IA64 stack frame format
155 // here (from intel 24535803.pdf most likely)
156 //
157 MachineFunction &MF = DAG.getMachineFunction();
158 MachineFrameInfo *MFI = MF.getFrameInfo();
159
160 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
161 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
162 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
163
164 MachineBasicBlock& BB = MF.front();
165
Misha Brukman4633f1c2005-04-21 23:13:11 +0000166 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000167 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000168
169 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000170 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000171
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000172 unsigned argVreg[8];
173 unsigned argPreg[8];
174 unsigned argOpc[8];
175
Duraid Madinabeeaab22005-03-31 12:31:11 +0000176 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000177
Duraid Madinabeeaab22005-03-31 12:31:11 +0000178 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000179 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000180
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000181 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000182 {
183 SDOperand newroot, argt;
184 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000185
186 switch (getValueType(I->getType())) {
187 default:
188 std::cerr << "ERROR in LowerArgs: unknown type "
189 << getValueType(I->getType()) << "\n";
190 abort();
191 case MVT::f32:
192 // fixme? (well, will need to for weird FP structy stuff,
193 // see intel ABI docs)
194 case MVT::f64:
195//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
196 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
197 // floating point args go into f8..f15 as-needed, the increment
198 argVreg[count] = // is below..:
199 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
200 // FP args go into f8..f15 as needed: (hence the ++)
201 argPreg[count] = args_FP[used_FPArgs++];
202 argOpc[count] = IA64::FMOV;
203 argt = newroot = DAG.getCopyFromReg(argVreg[count],
204 getValueType(I->getType()), DAG.getRoot());
205 break;
206 case MVT::i1: // NOTE: as far as C abi stuff goes,
207 // bools are just boring old ints
208 case MVT::i8:
209 case MVT::i16:
210 case MVT::i32:
211 case MVT::i64:
212//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
213 MF.addLiveIn(args_int[count]); // mark this register as liveIn
214 argVreg[count] =
215 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
216 argPreg[count] = args_int[count];
217 argOpc[count] = IA64::MOV;
218 argt = newroot =
219 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
220 if ( getValueType(I->getType()) != MVT::i64)
221 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
222 newroot);
223 break;
224 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000225 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000226 // Create the frame index object for this incoming parameter...
227 ArgOffset = 16 + 8 * (count - 8);
228 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000229
Misha Brukman7847fca2005-04-22 17:54:37 +0000230 // Create the SelectionDAG nodes corresponding to a load
231 //from this parameter
232 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
233 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000234 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000235 }
236 ++count;
237 DAG.setRoot(newroot.getValue(1));
238 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000240
Misha Brukman4633f1c2005-04-21 23:13:11 +0000241
Duraid Madinabeeaab22005-03-31 12:31:11 +0000242 // Create a vreg to hold the output of (what will become)
243 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000244 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
245 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
246 // we create a PSEUDO_ALLOC (pseudo)instruction for now
247
248 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
249
250 // hmm:
251 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
252 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
253 // ..hmm.
254
255 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
256
257 // hmm:
258 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
259 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
260 // ..hmm.
261
Duraid Madinabeeaab22005-03-31 12:31:11 +0000262 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000263
Duraid Madinabeeaab22005-03-31 12:31:11 +0000264 // if this is a varargs function, we simply lower llvm.va_start by
265 // pointing to the first entry
266 if(F.isVarArg()) {
267 tempOffset=0;
268 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000269 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000270
Duraid Madinabeeaab22005-03-31 12:31:11 +0000271 // here we actually do the moving of args, and store them to the stack
272 // too if this is a varargs function:
273 for (int i = 0; i < count && i < 8; ++i) {
274 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
275 if(F.isVarArg()) {
276 // if this is a varargs function, we copy the input registers to the stack
277 int FI = MFI->CreateFixedObject(8, tempOffset);
278 tempOffset+=8; //XXX: is it safe to use r22 like this?
279 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
280 // FIXME: we should use st8.spill here, one day
281 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
282 }
283 }
284
Duraid Madinaca494fd2005-04-12 14:54:44 +0000285 // Finally, inform the code generator which regs we return values in.
286 // (see the ISD::RET: case down below)
287 switch (getValueType(F.getReturnType())) {
288 default: assert(0 && "i have no idea where to return this type!");
289 case MVT::isVoid: break;
290 case MVT::i1:
291 case MVT::i8:
292 case MVT::i16:
293 case MVT::i32:
294 case MVT::i64:
295 MF.addLiveOut(IA64::r8);
296 break;
297 case MVT::f32:
298 case MVT::f64:
299 MF.addLiveOut(IA64::F8);
300 break;
301 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000302
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000303 return ArgValues;
304}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000305
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000306std::pair<SDOperand, SDOperand>
307IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000308 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000309 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000310 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000311 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000312
313 MachineFunction &MF = DAG.getMachineFunction();
314
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000315 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000316 unsigned outRegsUsed = 0;
317
318 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000319 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000320 outRegsUsed = 8;
321 } else {
322 outRegsUsed = Args.size();
323 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000324
Duraid Madinabeeaab22005-03-31 12:31:11 +0000325 // FIXME? this WILL fail if we ever try to pass around an arg that
326 // consumes more than a single output slot (a 'real' double, int128
327 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
328 // registers we use. Hopefully, the assembler will notice.
329 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
330 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000331
Chris Lattner16cd04d2005-05-12 23:24:06 +0000332 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000333 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000334
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000335 std::vector<SDOperand> args_to_use;
336 for (unsigned i = 0, e = Args.size(); i != e; ++i)
337 {
338 switch (getValueType(Args[i].second)) {
339 default: assert(0 && "unexpected argument type!");
340 case MVT::i1:
341 case MVT::i8:
342 case MVT::i16:
343 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000344 //promote to 64-bits, sign/zero extending based on type
345 //of the argument
346 if(Args[i].second->isSigned())
347 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
348 Args[i].first);
349 else
350 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
351 Args[i].first);
352 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000353 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000354 //promote to 64-bits
355 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 case MVT::f64:
357 case MVT::i64:
358 break;
359 }
360 args_to_use.push_back(Args[i].first);
361 }
362
363 std::vector<MVT::ValueType> RetVals;
364 MVT::ValueType RetTyVT = getValueType(RetTy);
365 if (RetTyVT != MVT::isVoid)
366 RetVals.push_back(RetTyVT);
367 RetVals.push_back(MVT::Other);
368
369 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000370 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000371 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000372 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000373 DAG.getConstant(NumBytes, getPointerTy()));
374 return std::make_pair(TheCall, Chain);
375}
376
Chris Lattnere0fe2252005-07-05 19:58:54 +0000377SDOperand
378IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
379 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000380 // vastart just stores the address of the VarArgsFrameIndex slot.
381 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000382 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
383 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000384}
385
386std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000387LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
388 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000389
390 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000391 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
392 VAListP, DAG.getSrcValue(VAListV));
393 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
394 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000395 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;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000402 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000403 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000404 DAG.getConstant(Amt, Val.getValueType()));
405 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000406 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000407 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000408}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000409
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000410namespace {
411
412 //===--------------------------------------------------------------------===//
413 /// ISel - IA64 specific code to select IA64 machine instructions for
414 /// SelectionDAG operations.
415 ///
416 class ISel : public SelectionDAGISel {
417 /// IA64Lowering - This object fully describes how to lower LLVM code to an
418 /// IA64-specific SelectionDAG.
419 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000420 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
421 // for sdiv and udiv until it is put into the future
422 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000423
424 /// ExprMap - As shared expressions are codegen'd, we keep track of which
425 /// vreg the value is produced in, so we only emit one copy of each compiled
426 /// tree.
427 std::map<SDOperand, unsigned> ExprMap;
428 std::set<SDOperand> LoweredTokens;
429
430 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000431 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
432 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000433
434 /// InstructionSelectBasicBlock - This callback is invoked by
435 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
436 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
437
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000438 unsigned SelectExpr(SDOperand N);
439 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000440 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
441 SDOperand BuildConstmulSequence(SDOperand N);
442
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000443 };
444}
445
446/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
447/// when it has created a SelectionDAG for us to codegen.
448void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
449
450 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000451 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000452 Select(DAG.getRoot());
453
454 // Clear state used for selection.
455 ExprMap.clear();
456 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000457 ISelDAG = 0;
458}
459
Duraid Madinab2322562005-04-26 07:23:02 +0000460// strip leading '0' characters from a string
461void munchLeadingZeros(std::string& inString) {
462 while(inString.c_str()[0]=='0') {
463 inString.erase(0, 1);
464 }
465}
466
467// strip trailing '0' characters from a string
468void munchTrailingZeros(std::string& inString) {
469 int curPos=inString.length()-1;
470
471 while(inString.c_str()[curPos]=='0') {
472 inString.erase(curPos, 1);
473 curPos--;
474 }
475}
476
477// return how many consecutive '0' characters are at the end of a string
478unsigned int countTrailingZeros(std::string& inString) {
479 int curPos=inString.length()-1;
480 unsigned int zeroCount=0;
481 // assert goes here
482 while(inString.c_str()[curPos--]=='0') {
483 zeroCount++;
484 }
485 return zeroCount;
486}
487
488// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
489// '0' and 'N' (-1) characters)
490void boothEncode(std::string inString, std::string& boothEncodedString) {
491
492 int curpos=0;
493 int replacements=0;
494 int lim=inString.size();
495
496 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000497 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000498 int runlength=0;
499 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000500
Duraid Madinab2322562005-04-26 07:23:02 +0000501 // find the run length
502 for(;inString[curpos+runlength]=='1';runlength++) ;
503
504 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000505 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000506 replaceString+="1";
507
508 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000509 inString.replace(curpos, runlength+1, replaceString);
510 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000511 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000512 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000513 } else { // a zero, we just keep chugging along
514 curpos++;
515 }
516 }
517
518 // clean up (trim the string, reverse it and turn '1's into 'P's)
519 munchTrailingZeros(inString);
520 boothEncodedString="";
521
522 for(int i=inString.size()-1;i>=0;i--)
523 if(inString[i]=='1')
524 boothEncodedString+="P";
525 else
526 boothEncodedString+=inString[i];
527
528}
529
530struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
531 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000532 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000533 unsigned secondVal; // C
534 unsigned secondShift; // D
535 bool isSub;
536};
537
538/* this implements Lefevre's "pattern-based" constant multiplication,
539 * see "Multiplication by an Integer Constant", INRIA report 1999-06
540 *
541 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
542 * to get better booth encodings - this does help in practice
543 * TODO: weight shifts appropriately (most architectures can't
544 * fuse a shift and an add for arbitrary shift amounts) */
545unsigned lefevre(const std::string inString,
546 std::vector<struct shiftaddblob> &ops) {
547 std::string retstring;
548 std::string s = inString;
549 munchTrailingZeros(s);
550
551 int length=s.length()-1;
552
553 if(length==0) {
554 return(0);
555 }
556
557 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000558
Duraid Madinab2322562005-04-26 07:23:02 +0000559 for(int i=0; i<=length; i++) {
560 if (s.c_str()[length-i]=='P') {
561 p.push_back(i);
562 } else if (s.c_str()[length-i]=='N') {
563 n.push_back(i);
564 }
565 }
566
567 std::string t, u;
Duraid Madina4706c032005-04-26 09:42:50 +0000568 int c;
569 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000570 std::map<const int, int> w;
571
Duraid Madina85d5f602005-04-27 11:57:39 +0000572 for(unsigned i=0; i<p.size(); i++) {
573 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000574 w[p[i]-p[j]]++;
575 }
576 }
577
Duraid Madina85d5f602005-04-27 11:57:39 +0000578 for(unsigned i=1; i<n.size(); i++) {
579 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000580 w[n[i]-n[j]]++;
581 }
582 }
583
Duraid Madina85d5f602005-04-27 11:57:39 +0000584 for(unsigned i=0; i<p.size(); i++) {
585 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000586 w[-abs(p[i]-n[j])]++;
587 }
588 }
589
590 std::map<const int, int>::const_iterator ii;
591 std::vector<int> d;
592 std::multimap<int, int> sorted_by_value;
593
594 for(ii = w.begin(); ii!=w.end(); ii++)
595 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
596
597 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
598 it != sorted_by_value.end(); ++it) {
599 d.push_back((*it).second);
600 }
601
602 int int_W=0;
603 int int_d;
604
605 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
606 d.pop_back();
607 retstring=s; // hmmm
608 int x=0;
609 int z=abs(int_d)-1;
610
611 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000612
Duraid Madina85d5f602005-04-27 11:57:39 +0000613 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000614 if( ((base+z+1) < retstring.size()) &&
615 retstring.c_str()[base]=='P' &&
616 retstring.c_str()[base+z+1]=='P')
617 {
618 // match
619 x++;
620 retstring.replace(base, 1, "0");
621 retstring.replace(base+z+1, 1, "p");
622 }
Duraid Madinab2322562005-04-26 07:23:02 +0000623 }
624
Duraid Madina85d5f602005-04-27 11:57:39 +0000625 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000626 if( ((base+z+1) < retstring.size()) &&
627 retstring.c_str()[base]=='N' &&
628 retstring.c_str()[base+z+1]=='N')
629 {
630 // match
631 x++;
632 retstring.replace(base, 1, "0");
633 retstring.replace(base+z+1, 1, "n");
634 }
Duraid Madinab2322562005-04-26 07:23:02 +0000635 }
636
637 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000638 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000639 if( ((base+z+1) < retstring.size()) &&
640 ((retstring.c_str()[base]=='P' &&
641 retstring.c_str()[base+z+1]=='N') ||
642 (retstring.c_str()[base]=='N' &&
643 retstring.c_str()[base+z+1]=='P')) ) {
644 // match
645 x++;
646
647 if(retstring.c_str()[base]=='P') {
648 retstring.replace(base, 1, "0");
649 retstring.replace(base+z+1, 1, "p");
650 } else { // retstring[base]=='N'
651 retstring.replace(base, 1, "0");
652 retstring.replace(base+z+1, 1, "n");
653 }
654 }
Duraid Madinab2322562005-04-26 07:23:02 +0000655 }
656 }
657
658 if(x>int_W) {
659 int_W = x;
660 t = retstring;
661 c = int_d; // tofix
662 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000663
Duraid Madinab2322562005-04-26 07:23:02 +0000664 } d.pop_back(); // hmm
665
666 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000667
Duraid Madina85d5f602005-04-27 11:57:39 +0000668 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000669 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
670 t.replace(i, 1, "0");
671 }
672
Duraid Madina85d5f602005-04-27 11:57:39 +0000673 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000674 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000675 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000676 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000677 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000678 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000679 u.replace(i, 1, "N");
680 }
681
682 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000683 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000684 c=-c;
685 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000686 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000687
Duraid Madina8a3042c2005-05-09 13:18:34 +0000688 int pos=0;
689 while(u[pos]=='0')
690 pos++;
691
692 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000693
694 int g=0;
695 if(hit) {
696 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000697 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000698 bool isP=(u[p]=='P');
699 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000700
701 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000702 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000703 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000704 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000705 }
706 }
707
708 munchLeadingZeros(u);
709
710 int i = lefevre(u, ops);
711
712 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000713
Duraid Madinab2322562005-04-26 07:23:02 +0000714 blob.firstVal=i; blob.firstShift=c;
715 blob.isSub=f;
716 blob.secondVal=i; blob.secondShift=0;
717
718 ops.push_back(blob);
719
720 i = ops.size();
721
722 munchLeadingZeros(t);
723
724 if(t.length()==0)
725 return i;
726
727 if(t.c_str()[0]!='P') {
728 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000729 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000730 bool isP=(t.c_str()[p]=='P');
731 bool isN=(t.c_str()[p]=='N');
732
733 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000734 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000735 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000736 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000737 }
738 }
739
740 int j = lefevre(t, ops);
741
742 int trail=countTrailingZeros(u);
743 blob.secondVal=i; blob.secondShift=trail;
744
745 trail=countTrailingZeros(t);
746 blob.firstVal=j; blob.firstShift=trail;
747
748 switch(g) {
749 case 0:
750 blob.isSub=false; // first + second
751 break;
752 case 1:
753 blob.isSub=true; // first - second
754 break;
755 case 2:
756 blob.isSub=true; // second - first
757 int tmpval, tmpshift;
758 tmpval=blob.firstVal;
759 tmpshift=blob.firstShift;
760 blob.firstVal=blob.secondVal;
761 blob.firstShift=blob.secondShift;
762 blob.secondVal=tmpval;
763 blob.secondShift=tmpshift;
764 break;
765 //assert
766 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000767
Duraid Madinab2322562005-04-26 07:23:02 +0000768 ops.push_back(blob);
769 return ops.size();
770}
771
772SDOperand ISel::BuildConstmulSequence(SDOperand N) {
773 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
774 // in particular, *3 is nicer as *2+1, not *4-1
775 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
776
777 bool flippedSign;
778 unsigned preliminaryShift=0;
779
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000780 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000781
782 // first, we make the constant to multiply by positive
783 if(constant<0) {
784 constant=-constant;
785 flippedSign=true;
786 } else {
787 flippedSign=false;
788 }
789
790 // next, we make it odd.
791 for(; (constant%2==0); preliminaryShift++)
792 constant>>=1;
793
794 //OK, we have a positive, odd number of 64 bits or less. Convert it
795 //to a binary string, constantString[0] is the LSB
796 char constantString[65];
797 for(int i=0; i<64; i++)
798 constantString[i]='0'+((constant>>i)&0x1);
799 constantString[64]=0;
800
801 // now, Booth encode it
802 std::string boothEncodedString;
803 boothEncode(constantString, boothEncodedString);
804
805 std::vector<struct shiftaddblob> ops;
806 // do the transformation, filling out 'ops'
807 lefevre(boothEncodedString, ops);
808
Duraid Madinae75a24a2005-05-15 14:44:13 +0000809 assert(ops.size() < 80 && "constmul code has gone haywire\n");
810 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000811
Duraid Madinab2322562005-04-26 07:23:02 +0000812 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000813 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000814 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
815 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
816 results[ops[i].firstVal-1];
817 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
818 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
819 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
820 results[ops[i].secondVal-1];
821 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
822 if(ops[i].isSub)
823 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
824 else
825 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
826 }
827
828 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000829 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000830 if(preliminaryShift) {
831 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000832 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000833 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000834 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000835 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000836 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000837
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000838 SDOperand finalresult;
839 if(flippedSign) { // if we were multiplying by a negative constant:
840 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
841 // subtract the result from 0 to flip its sign
842 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
843 } else { // there was no preliminary multiply by -1 required
844 finalresult = shiftedresult;
845 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000846
847 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000848}
849
Duraid Madina4826a072005-04-06 09:55:17 +0000850/// ponderIntegerDivisionBy - When handling integer divides, if the divide
851/// is by a constant such that we can efficiently codegen it, this
852/// function says what to do. Currently, it returns 0 if the division must
853/// become a genuine divide, and 1 if the division can be turned into a
854/// right shift.
855static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
856 unsigned& Imm) {
857 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
858 // a constant, give up.
859
860 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
861
Chris Lattner0561b3f2005-08-02 19:26:06 +0000862 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
863 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000864 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000865 }
866
Duraid Madina4826a072005-04-06 09:55:17 +0000867 return 0; // fallthrough
868}
869
Duraid Madinac02780e2005-04-13 04:50:54 +0000870static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
871 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
872 // a constant, give up.
873
874 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
875
Chris Lattner0561b3f2005-08-02 19:26:06 +0000876 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
877 Imm = Log2_64(v);
Duraid Madinac02780e2005-04-13 04:50:54 +0000878 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000879 }
880
Duraid Madinac02780e2005-04-13 04:50:54 +0000881 return 0; // fallthrough
882}
883
Duraid Madinaf55e4032005-04-07 12:33:38 +0000884static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
885 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
886 // constant, give up.
887 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
888
889 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
890 Imm = v & 0x3FFF; // 14 bits
891 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000892 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000893 return 0; // fallthrough
894}
895
896static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
897 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
898 // constant, give up.
899 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
900
901 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
902 Imm = v & 0xFF; // 8 bits
903 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000904 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000905 return 0; // fallthrough
906}
907
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000908unsigned ISel::SelectExpr(SDOperand N) {
909 unsigned Result;
910 unsigned Tmp1, Tmp2, Tmp3;
911 unsigned Opc = 0;
912 MVT::ValueType DestType = N.getValueType();
913
914 unsigned opcode = N.getOpcode();
915
916 SDNode *Node = N.Val;
917 SDOperand Op0, Op1;
918
919 if (Node->getOpcode() == ISD::CopyFromReg)
920 // Just use the specified register as our input.
921 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000922
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000923 unsigned &Reg = ExprMap[N];
924 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000925
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000926 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000927 Reg = Result = (N.getValueType() != MVT::Other) ?
928 MakeReg(N.getValueType()) : 1;
929 else {
930 // If this is a call instruction, make sure to prepare ALL of the result
931 // values as well as the chain.
932 if (Node->getNumValues() == 1)
933 Reg = Result = 1; // Void call, just a chain.
934 else {
935 Result = MakeReg(Node->getValueType(0));
936 ExprMap[N.getValue(0)] = Result;
937 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
938 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
939 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
940 }
941 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000942
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000943 switch (N.getOpcode()) {
944 default:
945 Node->dump();
946 assert(0 && "Node not handled!\n");
947
948 case ISD::FrameIndex: {
949 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
950 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
951 return Result;
952 }
953
954 case ISD::ConstantPool: {
955 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
956 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
957 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
958 .addReg(IA64::r1);
959 return Result;
960 }
961
962 case ISD::ConstantFP: {
963 Tmp1 = Result; // Intermediate Register
964 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
965 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
966 Tmp1 = MakeReg(MVT::f64);
967
968 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
969 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
970 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
971 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
972 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
973 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
974 else
975 assert(0 && "Unexpected FP constant!");
976 if (Tmp1 != Result)
977 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
978 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000979 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000980 return Result;
981 }
982
983 case ISD::DYNAMIC_STACKALLOC: {
984 // Generate both result values.
985 if (Result != 1)
986 ExprMap[N.getValue(1)] = 1; // Generate the token
987 else
988 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
989
990 // FIXME: We are currently ignoring the requested alignment for handling
991 // greater than the stack alignment. This will need to be revisited at some
992 // point. Align = N.getOperand(2);
993
994 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
995 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
996 std::cerr << "Cannot allocate stack object with greater alignment than"
997 << " the stack alignment yet!";
998 abort();
999 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001000
1001/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001002 Select(N.getOperand(0));
1003 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1004 {
1005 if (CN->getValue() < 32000)
1006 {
1007 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001008 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001009 } else {
1010 Tmp1 = SelectExpr(N.getOperand(1));
1011 // Subtract size from stack pointer, thereby allocating some space.
1012 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1013 }
1014 } else {
1015 Tmp1 = SelectExpr(N.getOperand(1));
1016 // Subtract size from stack pointer, thereby allocating some space.
1017 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1018 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001019*/
1020 Select(N.getOperand(0));
1021 Tmp1 = SelectExpr(N.getOperand(1));
1022 // Subtract size from stack pointer, thereby allocating some space.
1023 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001024 // Put a pointer to the space into the result register, by copying the
1025 // stack pointer.
1026 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1027 return Result;
1028 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001029
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001030 case ISD::SELECT: {
1031 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1032 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1033 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1034
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001035 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001036
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001037 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001038 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001039 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1040 // for i1, we load the condition into an integer register, then
1041 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1042 // of them will go through, since the integer register will hold
1043 // either 0 or 1)
1044 case MVT::i1: {
1045 bogoResult=MakeReg(MVT::i1);
1046
1047 // load the condition into an integer register
1048 unsigned condReg=MakeReg(MVT::i64);
1049 unsigned dummy=MakeReg(MVT::i64);
1050 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1051 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1052 .addImm(1).addReg(Tmp1);
1053
1054 // initialize Result (bool) to false (hence UNC) and if
1055 // the select condition (condReg) is false (0), copy Tmp3
1056 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1057 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1058
1059 // now, if the selection condition is true, write 1 to the
1060 // result if Tmp2 is 1
1061 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1062 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1063 break;
1064 }
1065 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1066 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001067 case MVT::i64:
1068 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001069 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1070 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1071 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001072 break;
1073 case MVT::f64:
1074 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001075 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1076 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1077 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001078 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001079 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001080
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001081 return Result;
1082 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001083
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001084 case ISD::Constant: {
1085 unsigned depositPos=0;
1086 unsigned depositLen=0;
1087 switch (N.getValueType()) {
1088 default: assert(0 && "Cannot use constants of this type!");
1089 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001090 // the constant:
1091 if(cast<ConstantSDNode>(N)->getValue()) // true:
1092 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1093 else // false:
1094 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1095 return Result; // early exit
1096 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001097 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001098 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001099
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001100 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001101
1102 if(immediate==0) { // if the constant is just zero,
1103 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1104 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001105 }
1106
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001107 if (immediate <= 8191 && immediate >= -8192) {
1108 // if this constants fits in 14 bits, we use a mov the assembler will
1109 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1110 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1111 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001112 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001113
1114 if (immediate <= 2097151 && immediate >= -2097152) {
1115 // if this constants fits in 22 bits, we use a mov the assembler will
1116 // turn into: "addl rDest=imm,r0"
1117 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1118 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001119 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001120
1121 /* otherwise, our immediate is big, so we use movl */
1122 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001123 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001124 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001125 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001126
1127 case ISD::UNDEF: {
1128 BuildMI(BB, IA64::IDEF, 0, Result);
1129 return Result;
1130 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001131
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001132 case ISD::GlobalAddress: {
1133 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1134 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001135
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001136 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001137 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001138
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001139 return Result;
1140 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001142 case ISD::ExternalSymbol: {
1143 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001144// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1145 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001146 return Result;
1147 }
1148
1149 case ISD::FP_EXTEND: {
1150 Tmp1 = SelectExpr(N.getOperand(0));
1151 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1152 return Result;
1153 }
1154
1155 case ISD::ZERO_EXTEND: {
1156 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001157
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001158 switch (N.getOperand(0).getValueType()) {
1159 default: assert(0 && "Cannot zero-extend this type!");
1160 case MVT::i8: Opc = IA64::ZXT1; break;
1161 case MVT::i16: Opc = IA64::ZXT2; break;
1162 case MVT::i32: Opc = IA64::ZXT4; break;
1163
Misha Brukman4633f1c2005-04-21 23:13:11 +00001164 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001165 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001166 unsigned dummy = MakeReg(MVT::i64);
1167 // first load zero:
1168 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1169 // ...then conditionally (PR:Tmp1) add 1:
1170 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1171 .addImm(1).addReg(Tmp1);
1172 return Result; // XXX early exit!
1173 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001174 }
1175
1176 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1177 return Result;
1178 }
1179
1180 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1181
1182assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1183
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 sign-extend this type!");
1188 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001189 Opc = IA64::SXT1; break;
1190 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001191 case MVT::i8: Opc = IA64::SXT1; break;
1192 case MVT::i16: Opc = IA64::SXT2; break;
1193 case MVT::i32: Opc = IA64::SXT4; break;
1194 }
1195
1196 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1197 return Result;
1198 }
1199
1200 case ISD::TRUNCATE: {
1201 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1202 // of R0 appropriately.
1203 switch (N.getOperand(0).getValueType()) {
1204 default: assert(0 && "Unknown truncate!");
1205 case MVT::i64: break;
1206 }
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 unsigned depositPos, depositLen;
1209
1210 switch (N.getValueType()) {
1211 default: assert(0 && "Unknown truncate!");
1212 case MVT::i1: {
1213 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001214 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1215 .addReg(IA64::r0);
1216 return Result; // XXX early exit!
1217 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001218 case MVT::i8: depositPos=0; depositLen=8; break;
1219 case MVT::i16: depositPos=0; depositLen=16; break;
1220 case MVT::i32: depositPos=0; depositLen=32; break;
1221 }
1222 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1223 .addImm(depositPos).addImm(depositLen);
1224 return Result;
1225 }
1226
Misha Brukman7847fca2005-04-22 17:54:37 +00001227/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001228 case ISD::FP_ROUND: {
1229 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001230 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001231 Tmp1 = SelectExpr(N.getOperand(0));
1232 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1233 // we add 0.0 using a single precision add to do rounding
1234 return Result;
1235 }
1236*/
1237
1238// FIXME: the following 4 cases need cleaning
1239 case ISD::SINT_TO_FP: {
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = MakeReg(MVT::f64);
1242 unsigned dummy = MakeReg(MVT::f64);
1243 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1244 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1245 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1246 return Result;
1247 }
1248
1249 case ISD::UINT_TO_FP: {
1250 Tmp1 = SelectExpr(N.getOperand(0));
1251 Tmp2 = MakeReg(MVT::f64);
1252 unsigned dummy = MakeReg(MVT::f64);
1253 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1254 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1255 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1256 return Result;
1257 }
1258
1259 case ISD::FP_TO_SINT: {
1260 Tmp1 = SelectExpr(N.getOperand(0));
1261 Tmp2 = MakeReg(MVT::f64);
1262 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1263 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1264 return Result;
1265 }
1266
1267 case ISD::FP_TO_UINT: {
1268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = MakeReg(MVT::f64);
1270 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1271 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1272 return Result;
1273 }
1274
1275 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001276 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1277 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1278 // into an fma, do so:
1279 // ++FusedFP; // Statistic
1280 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1281 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1282 Tmp3 = SelectExpr(N.getOperand(1));
1283 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1284 return Result; // early exit
1285 }
Duraid Madinaed095022005-04-13 06:12:04 +00001286
1287 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001288 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001289 // this add into a shladd, try:
1290 ConstantSDNode *CSD = NULL;
1291 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001292 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001293
Misha Brukman7847fca2005-04-22 17:54:37 +00001294 // ++FusedSHLADD; // Statistic
1295 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1296 int shl_amt = CSD->getValue();
1297 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001298
Misha Brukman7847fca2005-04-22 17:54:37 +00001299 BuildMI(BB, IA64::SHLADD, 3, Result)
1300 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1301 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001302 }
1303 }
1304
1305 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001306 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001307 if(DestType != MVT::f64) { // integer addition:
1308 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001309 case 1: // adding a constant that's 14 bits
1310 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1311 return Result; // early exit
1312 } // fallthrough and emit a reg+reg ADD:
1313 Tmp2 = SelectExpr(N.getOperand(1));
1314 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001315 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001316 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001317 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1318 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001319 return Result;
1320 }
1321
1322 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001323
1324 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001325/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1326 */
Jeff Cohen00b168892005-07-27 06:12:32 +00001327 // boring old integer multiply with xma
1328 Tmp1 = SelectExpr(N.getOperand(0));
1329 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinab2322562005-04-26 07:23:02 +00001330
Jeff Cohen00b168892005-07-27 06:12:32 +00001331 unsigned TempFR1=MakeReg(MVT::f64);
1332 unsigned TempFR2=MakeReg(MVT::f64);
1333 unsigned TempFR3=MakeReg(MVT::f64);
1334 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1335 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1336 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1337 .addReg(IA64::F0);
1338 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1339 return Result; // early exit
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001340 /* FIXME } else { // we are multiplying by an integer constant! yay
Jeff Cohen00b168892005-07-27 06:12:32 +00001341 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001342 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001343 }
Duraid Madinab2322562005-04-26 07:23:02 +00001344 else { // floating point multiply
1345 Tmp1 = SelectExpr(N.getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001347 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001348 return Result;
1349 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001350 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001351
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001352 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001353 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1354 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1355 // into an fms, do so:
1356 // ++FusedFP; // Statistic
1357 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1358 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1359 Tmp3 = SelectExpr(N.getOperand(1));
1360 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1361 return Result; // early exit
1362 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001363 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001364 if(DestType != MVT::f64) { // integer subtraction:
1365 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001366 case 1: // subtracting *from* an 8 bit constant:
1367 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1368 return Result; // early exit
1369 } // fallthrough and emit a reg+reg SUB:
1370 Tmp1 = SelectExpr(N.getOperand(0));
1371 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001372 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001373 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001374 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001375 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001376 return Result;
1377 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001378
1379 case ISD::FABS: {
1380 Tmp1 = SelectExpr(N.getOperand(0));
1381 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1382 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1383 return Result;
1384 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001385
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001386 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001387 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001388
Misha Brukman4633f1c2005-04-21 23:13:11 +00001389 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001390 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1391 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1392 } else {
1393 Tmp1 = SelectExpr(N.getOperand(0));
1394 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1395 }
1396
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001397 return Result;
1398 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001399
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001400 case ISD::AND: {
1401 switch (N.getValueType()) {
1402 default: assert(0 && "Cannot AND this type!");
1403 case MVT::i1: { // if a bool, we emit a pseudocode AND
1404 unsigned pA = SelectExpr(N.getOperand(0));
1405 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001406
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407/* our pseudocode for AND is:
1408 *
1409(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1410 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1411 ;;
1412(pB) cmp.ne pTemp,p0 = r0,r0
1413 ;;
1414(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1415
1416*/
1417 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001418
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001419 unsigned bogusTemp1 = MakeReg(MVT::i1);
1420 unsigned bogusTemp2 = MakeReg(MVT::i1);
1421 unsigned bogusTemp3 = MakeReg(MVT::i1);
1422 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001423
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001424 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001425 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001426 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001427 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001428 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001429 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001430 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001431 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001432 break;
1433 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001434
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001435 // if not a bool, we just AND away:
1436 case MVT::i8:
1437 case MVT::i16:
1438 case MVT::i32:
1439 case MVT::i64: {
1440 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001441 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1442 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001443 switch (Tmp3) {
1444 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1445 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1446 break;
1447 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1448 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1449 break;
1450 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1451 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1452 break;
1453 default: // otherwise, use dep.z to paste zeros
1454 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1455 .addImm(0).addImm(Tmp3);
1456 break;
1457 }
1458 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001459 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001460 Tmp2 = SelectExpr(N.getOperand(1));
1461 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001462 }
1463 }
1464 return Result;
1465 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001466
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001467 case ISD::OR: {
1468 switch (N.getValueType()) {
1469 default: assert(0 && "Cannot OR this type!");
1470 case MVT::i1: { // if a bool, we emit a pseudocode OR
1471 unsigned pA = SelectExpr(N.getOperand(0));
1472 unsigned pB = SelectExpr(N.getOperand(1));
1473
1474 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001475
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001476/* our pseudocode for OR is:
1477 *
1478
1479pC = pA OR pB
1480-------------
1481
Misha Brukman7847fca2005-04-22 17:54:37 +00001482(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1483 ;;
1484(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001485
1486*/
1487 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001488 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001489 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001490 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001491 break;
1492 }
1493 // if not a bool, we just OR away:
1494 case MVT::i8:
1495 case MVT::i16:
1496 case MVT::i32:
1497 case MVT::i64: {
1498 Tmp1 = SelectExpr(N.getOperand(0));
1499 Tmp2 = SelectExpr(N.getOperand(1));
1500 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1501 break;
1502 }
1503 }
1504 return Result;
1505 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001506
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001507 case ISD::XOR: {
1508 switch (N.getValueType()) {
1509 default: assert(0 && "Cannot XOR this type!");
1510 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1511 unsigned pY = SelectExpr(N.getOperand(0));
1512 unsigned pZ = SelectExpr(N.getOperand(1));
1513
1514/* one possible routine for XOR is:
1515
1516 // Compute px = py ^ pz
1517 // using sum of products: px = (py & !pz) | (pz & !py)
1518 // Uses 5 instructions in 3 cycles.
1519 // cycle 1
1520(pz) cmp.eq.unc px = r0, r0 // px = pz
1521(py) cmp.eq.unc pt = r0, r0 // pt = py
1522 ;;
1523 // cycle 2
1524(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1525(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1526 ;;
1527 } { .mmi
1528 // cycle 3
1529(pt) cmp.eq.or px = r0, r0 // px = px | pt
1530
1531*** Another, which we use here, requires one scratch GR. it is:
1532
1533 mov rt = 0 // initialize rt off critical path
1534 ;;
1535
1536 // cycle 1
1537(pz) cmp.eq.unc px = r0, r0 // px = pz
1538(pz) mov rt = 1 // rt = pz
1539 ;;
1540 // cycle 2
1541(py) cmp.ne px = 1, rt // if (py) px = !pz
1542
1543.. these routines kindly provided by Jim Hull
1544*/
1545 unsigned rt = MakeReg(MVT::i64);
1546
1547 // these two temporaries will never actually appear,
1548 // due to the two-address form of some of the instructions below
1549 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1550 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1551
1552 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1553 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001554 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001555 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001556 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001557 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001558 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001559 break;
1560 }
1561 // if not a bool, we just XOR away:
1562 case MVT::i8:
1563 case MVT::i16:
1564 case MVT::i32:
1565 case MVT::i64: {
1566 Tmp1 = SelectExpr(N.getOperand(0));
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1569 break;
1570 }
1571 }
1572 return Result;
1573 }
1574
Duraid Madina63bbed52005-05-11 05:16:09 +00001575 case ISD::CTPOP: {
1576 Tmp1 = SelectExpr(N.getOperand(0));
1577 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1578 return Result;
1579 }
1580
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001581 case ISD::SHL: {
1582 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001583 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1584 Tmp2 = CN->getValue();
1585 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1586 } else {
1587 Tmp2 = SelectExpr(N.getOperand(1));
1588 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1589 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001590 return Result;
1591 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001592
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001593 case ISD::SRL: {
1594 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001595 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1596 Tmp2 = CN->getValue();
1597 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1598 } else {
1599 Tmp2 = SelectExpr(N.getOperand(1));
1600 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1601 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001602 return Result;
1603 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001604
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001605 case ISD::SRA: {
1606 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001607 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1608 Tmp2 = CN->getValue();
1609 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1610 } else {
1611 Tmp2 = SelectExpr(N.getOperand(1));
1612 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1613 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001614 return Result;
1615 }
1616
1617 case ISD::SDIV:
1618 case ISD::UDIV:
1619 case ISD::SREM:
1620 case ISD::UREM: {
1621
1622 Tmp1 = SelectExpr(N.getOperand(0));
1623 Tmp2 = SelectExpr(N.getOperand(1));
1624
1625 bool isFP=false;
1626
1627 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1628 isFP=true;
1629
1630 bool isModulus=false; // is it a division or a modulus?
1631 bool isSigned=false;
1632
1633 switch(N.getOpcode()) {
1634 case ISD::SDIV: isModulus=false; isSigned=true; break;
1635 case ISD::UDIV: isModulus=false; isSigned=false; break;
1636 case ISD::SREM: isModulus=true; isSigned=true; break;
1637 case ISD::UREM: isModulus=true; isSigned=false; break;
1638 }
1639
Duraid Madina4826a072005-04-06 09:55:17 +00001640 if(!isModulus && !isFP) { // if this is an integer divide,
1641 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001642 case 1: // division by a constant that's a power of 2
1643 Tmp1 = SelectExpr(N.getOperand(0));
1644 if(isSigned) { // argument could be negative, so emit some code:
1645 unsigned divAmt=Tmp3;
1646 unsigned tempGR1=MakeReg(MVT::i64);
1647 unsigned tempGR2=MakeReg(MVT::i64);
1648 unsigned tempGR3=MakeReg(MVT::i64);
1649 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1650 .addReg(Tmp1).addImm(divAmt-1);
1651 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1652 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1653 BuildMI(BB, IA64::ADD, 2, tempGR3)
1654 .addReg(Tmp1).addReg(tempGR2);
1655 BuildMI(BB, IA64::SHRS, 2, Result)
1656 .addReg(tempGR3).addImm(divAmt);
1657 }
1658 else // unsigned div-by-power-of-2 becomes a simple shift right:
1659 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1660 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001661 }
1662 }
1663
Misha Brukman4633f1c2005-04-21 23:13:11 +00001664 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001665 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001666 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1667 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1668 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1669 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1670 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1671 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1672 unsigned TmpF7=MakeReg(MVT::f64);
1673 unsigned TmpF8=MakeReg(MVT::f64);
1674 unsigned TmpF9=MakeReg(MVT::f64);
1675 unsigned TmpF10=MakeReg(MVT::f64);
1676 unsigned TmpF11=MakeReg(MVT::f64);
1677 unsigned TmpF12=MakeReg(MVT::f64);
1678 unsigned TmpF13=MakeReg(MVT::f64);
1679 unsigned TmpF14=MakeReg(MVT::f64);
1680 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001681
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001682 // OK, emit some code:
1683
1684 if(!isFP) {
1685 // first, load the inputs into FP regs.
1686 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1687 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001688
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001689 // next, convert the inputs to FP
1690 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001691 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1692 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001693 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001694 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1695 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001696 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001697
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001698 } else { // this is an FP divide/remainder, so we 'leak' some temp
1699 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1700 TmpF3=Tmp1;
1701 TmpF4=Tmp2;
1702 }
1703
1704 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001705 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1706 BuildMI(BB, IA64::FRCPAS1, 4)
1707 .addReg(TmpF5, MachineOperand::Def)
1708 .addReg(TmpPR, MachineOperand::Def)
1709 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001710
Duraid Madinabeeaab22005-03-31 12:31:11 +00001711 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1712 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1713 // TPCMPNE below
1714 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1715 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001716 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001717 }
1718
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001719 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1720 // precision, don't need this much for f32/i32)
1721 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1722 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1723 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1724 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1725 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1726 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1727 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1728 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1729 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1730 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1731 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1732 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1733 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1734 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1735 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1736 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001737
1738 // FIXME: this is unfortunate :(
1739 // the story is that the dest reg of the fnma above and the fma below
1740 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1741 // be the same register, or this code breaks if the first argument is
1742 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001743 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1744 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1745
Duraid Madina6e02e682005-04-04 05:05:52 +00001746 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1747 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1748 }
1749
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001750 if(!isFP) {
1751 // round to an integer
1752 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001753 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001754 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001755 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001756 } else {
1757 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1758 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1759 // we really do need the above FMOV? ;)
1760 }
1761
1762 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001763 if(isFP) { // extra worrying about div-by-zero
1764 unsigned bogoResult=MakeReg(MVT::f64);
1765
1766 // we do a 'conditional fmov' (of the correct result, depending
1767 // on how the frcpa predicate turned out)
1768 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001769 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001770 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001771 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001772 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001773 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001774 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001775 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001776 } else { // this is a modulus
1777 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001778 // answer = q * (-b) + a
1779 unsigned ModulusResult = MakeReg(MVT::f64);
1780 unsigned TmpF = MakeReg(MVT::f64);
1781 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001782
Misha Brukman7847fca2005-04-22 17:54:37 +00001783 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1784 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1785 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1786 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1787 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001788 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001789 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001790 }
1791 }
1792
1793 return Result;
1794 }
1795
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001796 case ISD::SIGN_EXTEND_INREG: {
1797 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001798 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001799 default:
1800 Node->dump();
1801 assert(0 && "don't know how to sign extend this type");
1802 break;
1803 case MVT::i8: Opc = IA64::SXT1; break;
1804 case MVT::i16: Opc = IA64::SXT2; break;
1805 case MVT::i32: Opc = IA64::SXT4; break;
1806 }
1807 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1808 return Result;
1809 }
1810
1811 case ISD::SETCC: {
1812 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001813 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1814 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001815
Chris Lattner88ac32c2005-08-09 20:21:10 +00001816 if(ConstantSDNode *CSDN =
1817 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1818 // if we are comparing against a constant zero
1819 if(CSDN->getValue()==0)
1820 Tmp2 = IA64::r0; // then we can just compare against r0
1821 else
1822 Tmp2 = SelectExpr(N.getOperand(1));
1823 } else // not comparing against a constant
1824 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001825
Chris Lattner88ac32c2005-08-09 20:21:10 +00001826 switch (CC) {
1827 default: assert(0 && "Unknown integer comparison!");
1828 case ISD::SETEQ:
1829 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1830 break;
1831 case ISD::SETGT:
1832 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 break;
1834 case ISD::SETGE:
1835 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 break;
1837 case ISD::SETLT:
1838 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 break;
1840 case ISD::SETLE:
1841 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 break;
1843 case ISD::SETNE:
1844 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1845 break;
1846 case ISD::SETULT:
1847 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1848 break;
1849 case ISD::SETUGT:
1850 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1851 break;
1852 case ISD::SETULE:
1853 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1854 break;
1855 case ISD::SETUGE:
1856 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 break;
1858 }
1859 } else { // if not integer, should be FP.
1860 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1861 "error: SETCC should have had incoming f32 promoted to f64!\n");
1862
1863 if(ConstantFPSDNode *CFPSDN =
1864 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1865
1866 // if we are comparing against a constant +0.0 or +1.0
1867 if(CFPSDN->isExactlyValue(+0.0))
1868 Tmp2 = IA64::F0; // then we can just compare against f0
1869 else if(CFPSDN->isExactlyValue(+1.0))
1870 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001871 else
1872 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001873 } else // not comparing against a constant
1874 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001875
Chris Lattner88ac32c2005-08-09 20:21:10 +00001876 switch (CC) {
1877 default: assert(0 && "Unknown FP comparison!");
1878 case ISD::SETEQ:
1879 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1880 break;
1881 case ISD::SETGT:
1882 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1883 break;
1884 case ISD::SETGE:
1885 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1886 break;
1887 case ISD::SETLT:
1888 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 break;
1890 case ISD::SETLE:
1891 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1892 break;
1893 case ISD::SETNE:
1894 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1895 break;
1896 case ISD::SETULT:
1897 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1898 break;
1899 case ISD::SETUGT:
1900 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1901 break;
1902 case ISD::SETULE:
1903 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1904 break;
1905 case ISD::SETUGE:
1906 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001908 }
1909 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001910 return Result;
1911 }
1912
1913 case ISD::EXTLOAD:
1914 case ISD::ZEXTLOAD:
1915 case ISD::LOAD: {
1916 // Make sure we generate both values.
1917 if (Result != 1)
1918 ExprMap[N.getValue(1)] = 1; // Generate the token
1919 else
1920 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1921
1922 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001923
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001924 if(opcode == ISD::LOAD) { // this is a LOAD
1925 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001926 default: assert(0 && "Cannot load this type!");
1927 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1928 // FIXME: for now, we treat bool loads the same as i8 loads */
1929 case MVT::i8: Opc = IA64::LD1; break;
1930 case MVT::i16: Opc = IA64::LD2; break;
1931 case MVT::i32: Opc = IA64::LD4; break;
1932 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001933
Misha Brukman7847fca2005-04-22 17:54:37 +00001934 case MVT::f32: Opc = IA64::LDF4; break;
1935 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001936 }
1937 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001938 MVT::ValueType TypeBeingLoaded =
1939 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001940 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001941 default: assert(0 && "Cannot extload/zextload this type!");
1942 // FIXME: bools?
1943 case MVT::i8: Opc = IA64::LD1; break;
1944 case MVT::i16: Opc = IA64::LD2; break;
1945 case MVT::i32: Opc = IA64::LD4; break;
1946 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001947 }
1948 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001949
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001950 SDOperand Chain = N.getOperand(0);
1951 SDOperand Address = N.getOperand(1);
1952
1953 if(Address.getOpcode() == ISD::GlobalAddress) {
1954 Select(Chain);
1955 unsigned dummy = MakeReg(MVT::i64);
1956 unsigned dummy2 = MakeReg(MVT::i64);
1957 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001958 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1959 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001960 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1961 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001962 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001963 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001964 // into a predicate register
1965 assert(Opc==IA64::LD1 && "problem loading a bool");
1966 unsigned dummy3 = MakeReg(MVT::i64);
1967 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1968 // we compare to 0. true? 0. false? 1.
1969 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001970 }
1971 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1972 Select(Chain);
1973 IA64Lowering.restoreGP(BB);
1974 unsigned dummy = MakeReg(MVT::i64);
1975 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001976 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001977 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001978 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001979 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001980 // into a predicate register
1981 assert(Opc==IA64::LD1 && "problem loading a bool");
1982 unsigned dummy3 = MakeReg(MVT::i64);
1983 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1984 // we compare to 0. true? 0. false? 1.
1985 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001986 }
1987 } else if(Address.getOpcode() == ISD::FrameIndex) {
1988 Select(Chain); // FIXME ? what about bools?
1989 unsigned dummy = MakeReg(MVT::i64);
1990 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001991 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001992 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001993 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001994 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001995 // into a predicate register
1996 assert(Opc==IA64::LD1 && "problem loading a bool");
1997 unsigned dummy3 = MakeReg(MVT::i64);
1998 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1999 // we compare to 0. true? 0. false? 1.
2000 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002001 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002002 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002003 Select(Chain);
2004 Tmp2 = SelectExpr(Address);
2005 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002006 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002007 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002008 // into a predicate register
2009 assert(Opc==IA64::LD1 && "problem loading a bool");
2010 unsigned dummy = MakeReg(MVT::i64);
2011 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2012 // we compare to 0. true? 0. false? 1.
2013 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00002014 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 }
2016
2017 return Result;
2018 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002019
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002020 case ISD::CopyFromReg: {
2021 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002022 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002023 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002024
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002025 SDOperand Chain = N.getOperand(0);
2026
2027 Select(Chain);
2028 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2029
2030 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002031 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2032 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002033 // (r) Result =cmp.eq.unc(r0,r0)
2034 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002035 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002036 return Result;
2037 }
2038
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002039 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002040 case ISD::CALL: {
2041 Select(N.getOperand(0));
2042
2043 // The chain for this call is now lowered.
2044 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002045
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002046 //grab the arguments
2047 std::vector<unsigned> argvregs;
2048
2049 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002050 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002051
2052 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002053 // Runtime Architecture Guide to see some examples of what's going
2054 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2055 // while FP args get mapped to F8->F15 as needed)
2056
2057 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002058
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002059 // in reg args
2060 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2061 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002062 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2063 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2064 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2065 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002066
Misha Brukman7847fca2005-04-22 17:54:37 +00002067 switch(N.getOperand(i+2).getValueType())
2068 {
2069 default: // XXX do we need to support MVT::i1 here?
2070 Node->dump();
2071 N.getOperand(i).Val->dump();
2072 std::cerr << "Type for " << i << " is: " <<
2073 N.getOperand(i+2).getValueType() << std::endl;
2074 assert(0 && "Unknown value type for call");
2075 case MVT::i64:
2076 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2077 break;
2078 case MVT::f64:
2079 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2080 .addReg(argvregs[i]);
2081 // FIXME: we don't need to do this _all_ the time:
2082 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2083 break;
2084 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002085 }
2086
2087 //in mem args
2088 for (int i = 8, e = argvregs.size(); i < e; ++i)
2089 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002090 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002091
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002092 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002093 default:
2094 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002095 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002096 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002097 N.getOperand(i+2).getValueType() << "\n";
2098 assert(0 && "Unknown value type for call");
2099 case MVT::i1: // FIXME?
2100 case MVT::i8:
2101 case MVT::i16:
2102 case MVT::i32:
2103 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002104 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2105 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2106 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002107 break;
2108 case MVT::f32:
2109 case MVT::f64:
2110 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002111 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2112 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002113 break;
2114 }
2115 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002116
Duraid Madina04aa46d2005-05-20 11:39:17 +00002117 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002118 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002119 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002120 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002121 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2122 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002123 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002124 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002125 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002126 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002127 // "non-pic code with imm relocation against dynamic symbol" errors
2128 BuildMI(BB, IA64::BRCALL, 1)
2129 .addExternalSymbol(ESSDN->getSymbol(), true);
2130 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002131 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002132 else { // otherwise we need to get the function descriptor
2133 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002134 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002135 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002136
2137 unsigned targetEntryPoint=MakeReg(MVT::i64);
2138 unsigned targetGPAddr=MakeReg(MVT::i64);
2139 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002140
Duraid Madinabeeaab22005-03-31 12:31:11 +00002141 // b6 is a scratch branch register, we load the target entry point
2142 // from the base of the function descriptor
2143 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2144 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2145
2146 // save the current GP:
2147 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002148
Duraid Madinabeeaab22005-03-31 12:31:11 +00002149 /* TODO: we need to make sure doing this never, ever loads a
2150 * bogus value into r1 (GP). */
2151 // load the target GP (which is at mem[functiondescriptor+8])
2152 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002153 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002154 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2155
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002156 // and then jump: (well, call)
2157 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002158 // and finally restore the old GP
2159 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2160 IA64Lowering.restoreSP_RP(BB);
2161 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002162
2163 switch (Node->getValueType(0)) {
2164 default: assert(0 && "Unknown value type for call result!");
2165 case MVT::Other: return 1;
2166 case MVT::i1:
2167 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002168 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002169 break;
2170 case MVT::i8:
2171 case MVT::i16:
2172 case MVT::i32:
2173 case MVT::i64:
2174 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2175 break;
2176 case MVT::f64:
2177 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2178 break;
2179 }
2180 return Result+N.ResNo;
2181 }
2182
Misha Brukman4633f1c2005-04-21 23:13:11 +00002183 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002184 return 0;
2185}
2186
2187void ISel::Select(SDOperand N) {
2188 unsigned Tmp1, Tmp2, Opc;
2189 unsigned opcode = N.getOpcode();
2190
Nate Begeman85fdeb22005-03-24 04:39:54 +00002191 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002192 return; // Already selected.
2193
2194 SDNode *Node = N.Val;
2195
2196 switch (Node->getOpcode()) {
2197 default:
2198 Node->dump(); std::cerr << "\n";
2199 assert(0 && "Node not handled yet!");
2200
2201 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002202
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002203 case ISD::TokenFactor: {
2204 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2205 Select(Node->getOperand(i));
2206 return;
2207 }
2208
2209 case ISD::CopyToReg: {
2210 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002211 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002212 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002214 if (Tmp1 != Tmp2) {
2215 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002216 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2217 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002218 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2219 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002220 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002221 // XXX is this the right way 'round? ;)
2222 }
2223 return;
2224 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002225
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002226 case ISD::RET: {
2227
2228 /* what the heck is going on here:
2229
2230<_sabre_> ret with two operands is obvious: chain and value
2231<camel_> yep
2232<_sabre_> ret with 3 values happens when 'expansion' occurs
2233<_sabre_> e.g. i64 gets split into 2x i32
2234<camel_> oh right
2235<_sabre_> you don't have this case on ia64
2236<camel_> yep
2237<_sabre_> so the two returned values go into EAX/EDX on ia32
2238<camel_> ahhh *memories*
2239<_sabre_> :)
2240<camel_> ok, thanks :)
2241<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2242<_sabre_> this is the first operand always
2243<_sabre_> these operand often define chains, they are the last operand
2244<_sabre_> they are printed as 'ch' if you do DAG.dump()
2245 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002246
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002247 switch (N.getNumOperands()) {
2248 default:
2249 assert(0 && "Unknown return instruction!");
2250 case 2:
2251 Select(N.getOperand(0));
2252 Tmp1 = SelectExpr(N.getOperand(1));
2253 switch (N.getOperand(1).getValueType()) {
2254 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002255 // FIXME: do I need to add support for bools here?
2256 // (return '0' or '1' r8, basically...)
2257 //
2258 // FIXME: need to round floats - 80 bits is bad, the tester
2259 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002260 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002261 // we mark r8 as live on exit up above in LowerArguments()
2262 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2263 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002264 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002265 // we mark F8 as live on exit up above in LowerArguments()
2266 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002267 }
2268 break;
2269 case 1:
2270 Select(N.getOperand(0));
2271 break;
2272 }
2273 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2274 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2275 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2276 return;
2277 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002278
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002279 case ISD::BR: {
2280 Select(N.getOperand(0));
2281 MachineBasicBlock *Dest =
2282 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2283 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2284 // XXX HACK! we do _not_ need long branches all the time
2285 return;
2286 }
2287
2288 case ISD::ImplicitDef: {
2289 Select(N.getOperand(0));
2290 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2291 return;
2292 }
2293
2294 case ISD::BRCOND: {
2295 MachineBasicBlock *Dest =
2296 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2297
2298 Select(N.getOperand(0));
2299 Tmp1 = SelectExpr(N.getOperand(1));
2300 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2301 // XXX HACK! we do _not_ need long branches all the time
2302 return;
2303 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002304
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002305 case ISD::EXTLOAD:
2306 case ISD::ZEXTLOAD:
2307 case ISD::SEXTLOAD:
2308 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002309 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002310 case ISD::CALL:
2311 case ISD::CopyFromReg:
2312 case ISD::DYNAMIC_STACKALLOC:
2313 SelectExpr(N);
2314 return;
2315
2316 case ISD::TRUNCSTORE:
2317 case ISD::STORE: {
2318 Select(N.getOperand(0));
2319 Tmp1 = SelectExpr(N.getOperand(1)); // value
2320
2321 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002322
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002323 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002324 switch (N.getOperand(1).getValueType()) {
2325 default: assert(0 && "Cannot store this type!");
2326 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2327 // FIXME?: for now, we treat bool loads the same as i8 stores */
2328 case MVT::i8: Opc = IA64::ST1; break;
2329 case MVT::i16: Opc = IA64::ST2; break;
2330 case MVT::i32: Opc = IA64::ST4; break;
2331 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002332
Misha Brukman7847fca2005-04-22 17:54:37 +00002333 case MVT::f32: Opc = IA64::STF4; break;
2334 case MVT::f64: Opc = IA64::STF8; break;
2335 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002336 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002337 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002338 default: assert(0 && "unknown type in truncstore");
2339 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2340 //FIXME: DAG does not promote this load?
2341 case MVT::i8: Opc = IA64::ST1; break;
2342 case MVT::i16: Opc = IA64::ST2; break;
2343 case MVT::i32: Opc = IA64::ST4; break;
2344 case MVT::f32: Opc = IA64::STF4; break;
2345 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002346 }
2347
2348 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002349 unsigned dummy = MakeReg(MVT::i64);
2350 unsigned dummy2 = MakeReg(MVT::i64);
2351 BuildMI(BB, IA64::ADD, 2, dummy)
2352 .addGlobalAddress(cast<GlobalAddressSDNode>
2353 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2354 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002355
Misha Brukman7847fca2005-04-22 17:54:37 +00002356 if(!isBool)
2357 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2358 else { // we are storing a bool, so emit a little pseudocode
2359 // to store a predicate register as one byte
2360 assert(Opc==IA64::ST1);
2361 unsigned dummy3 = MakeReg(MVT::i64);
2362 unsigned dummy4 = MakeReg(MVT::i64);
2363 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2364 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2365 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2366 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2367 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002368 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2369
Misha Brukman7847fca2005-04-22 17:54:37 +00002370 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002371
Misha Brukman7847fca2005-04-22 17:54:37 +00002372 unsigned dummy = MakeReg(MVT::i64);
2373 BuildMI(BB, IA64::MOV, 1, dummy)
2374 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2375 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002376 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002377 Tmp2 = SelectExpr(N.getOperand(2)); //address
2378 if(!isBool)
2379 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2380 else { // we are storing a bool, so emit a little pseudocode
2381 // to store a predicate register as one byte
2382 assert(Opc==IA64::ST1);
2383 unsigned dummy3 = MakeReg(MVT::i64);
2384 unsigned dummy4 = MakeReg(MVT::i64);
2385 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2386 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2387 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2388 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2389 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002390 }
2391 return;
2392 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002393
Chris Lattner16cd04d2005-05-12 23:24:06 +00002394 case ISD::CALLSEQ_START:
2395 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002396 Select(N.getOperand(0));
2397 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002398
Chris Lattner16cd04d2005-05-12 23:24:06 +00002399 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2400 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002401 BuildMI(BB, Opc, 1).addImm(Tmp1);
2402 return;
2403 }
2404
2405 return;
2406 }
2407 assert(0 && "GAME OVER. INSERT COIN?");
2408}
2409
2410
2411/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2412/// into a machine code representation using pattern matching and a machine
2413/// description file.
2414///
2415FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002416 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002417}
2418
2419