blob: 1ca4176c3a5a218a43e6daa728392b71785a16f3 [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);
Nate Begeman7cbd5252005-08-16 19:49:35 +000060 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000061 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
62
Misha Brukman4633f1c2005-04-21 23:13:11 +000063 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000064 setShiftAmountType(MVT::i64);
65
66 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067
68 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000069
70 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
72 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000073 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000074
75 setOperationAction(ISD::SREM , MVT::f32 , Expand);
76 setOperationAction(ISD::SREM , MVT::f64 , Expand);
77
78 setOperationAction(ISD::UREM , MVT::f32 , Expand);
79 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000080
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
82 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
83 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
84
Chris Lattner17234b72005-04-30 04:26:06 +000085 // We don't support sin/cos/sqrt
86 setOperationAction(ISD::FSIN , MVT::f64, Expand);
87 setOperationAction(ISD::FCOS , MVT::f64, Expand);
88 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
89 setOperationAction(ISD::FSIN , MVT::f32, Expand);
90 setOperationAction(ISD::FCOS , MVT::f32, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
92
Andrew Lenharthb5884d32005-05-04 19:25:37 +000093 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000094 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
95 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000096
Duraid Madina9b9d45f2005-03-17 18:17:03 +000097 computeRegisterProperties();
98
99 addLegalFPImmediate(+0.0);
100 addLegalFPImmediate(+1.0);
101 addLegalFPImmediate(-0.0);
102 addLegalFPImmediate(-1.0);
103 }
104
105 /// LowerArguments - This hook must be implemented to indicate how we should
106 /// lower the arguments for the specified function, into the specified DAG.
107 virtual std::vector<SDOperand>
108 LowerArguments(Function &F, SelectionDAG &DAG);
109
110 /// LowerCallTo - This hook lowers an abstract call to a function into an
111 /// actual call.
112 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000113 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000114 bool isTailCall, SDOperand Callee, ArgListTy &Args,
115 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000116
Chris Lattnere0fe2252005-07-05 19:58:54 +0000117 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
118 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000119 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000120 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
121 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000122
123 void restoreGP_SP_RP(MachineBasicBlock* BB)
124 {
125 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
126 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
127 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
128 }
129
Duraid Madinabeeaab22005-03-31 12:31:11 +0000130 void restoreSP_RP(MachineBasicBlock* BB)
131 {
132 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
133 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
134 }
135
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000136 void restoreRP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
139 }
140
141 void restoreGP(MachineBasicBlock* BB)
142 {
143 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
144 }
145
146 };
147}
148
149
150std::vector<SDOperand>
151IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
152 std::vector<SDOperand> ArgValues;
153
154 //
155 // add beautiful description of IA64 stack frame format
156 // here (from intel 24535803.pdf most likely)
157 //
158 MachineFunction &MF = DAG.getMachineFunction();
159 MachineFrameInfo *MFI = MF.getFrameInfo();
160
161 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
162 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
163 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
164
165 MachineBasicBlock& BB = MF.front();
166
Misha Brukman4633f1c2005-04-21 23:13:11 +0000167 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000168 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000169
170 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000173 unsigned argVreg[8];
174 unsigned argPreg[8];
175 unsigned argOpc[8];
176
Duraid Madinabeeaab22005-03-31 12:31:11 +0000177 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000178
Duraid Madinabeeaab22005-03-31 12:31:11 +0000179 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000180 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000181
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000182 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 {
184 SDOperand newroot, argt;
185 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000186
187 switch (getValueType(I->getType())) {
188 default:
189 std::cerr << "ERROR in LowerArgs: unknown type "
190 << getValueType(I->getType()) << "\n";
191 abort();
192 case MVT::f32:
193 // fixme? (well, will need to for weird FP structy stuff,
194 // see intel ABI docs)
195 case MVT::f64:
196//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
197 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
198 // floating point args go into f8..f15 as-needed, the increment
199 argVreg[count] = // is below..:
200 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
201 // FP args go into f8..f15 as needed: (hence the ++)
202 argPreg[count] = args_FP[used_FPArgs++];
203 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000204 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
205 getValueType(I->getType()));
Misha Brukman7847fca2005-04-22 17:54:37 +0000206 break;
207 case MVT::i1: // NOTE: as far as C abi stuff goes,
208 // bools are just boring old ints
209 case MVT::i8:
210 case MVT::i16:
211 case MVT::i32:
212 case MVT::i64:
213//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
214 MF.addLiveIn(args_int[count]); // mark this register as liveIn
215 argVreg[count] =
216 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
217 argPreg[count] = args_int[count];
218 argOpc[count] = IA64::MOV;
219 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000220 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000221 if ( getValueType(I->getType()) != MVT::i64)
222 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
223 newroot);
224 break;
225 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000226 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000227 // Create the frame index object for this incoming parameter...
228 ArgOffset = 16 + 8 * (count - 8);
229 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000230
Misha Brukman7847fca2005-04-22 17:54:37 +0000231 // Create the SelectionDAG nodes corresponding to a load
232 //from this parameter
233 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
234 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000235 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000236 }
237 ++count;
238 DAG.setRoot(newroot.getValue(1));
239 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000240 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000241
Misha Brukman4633f1c2005-04-21 23:13:11 +0000242
Duraid Madinabeeaab22005-03-31 12:31:11 +0000243 // Create a vreg to hold the output of (what will become)
244 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000245 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
246 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
247 // we create a PSEUDO_ALLOC (pseudo)instruction for now
248
249 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
250
251 // hmm:
252 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
253 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
254 // ..hmm.
255
256 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
257
258 // hmm:
259 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
260 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
261 // ..hmm.
262
Duraid Madinabeeaab22005-03-31 12:31:11 +0000263 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000264
Duraid Madinabeeaab22005-03-31 12:31:11 +0000265 // if this is a varargs function, we simply lower llvm.va_start by
266 // pointing to the first entry
267 if(F.isVarArg()) {
268 tempOffset=0;
269 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000270 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000271
Duraid Madinabeeaab22005-03-31 12:31:11 +0000272 // here we actually do the moving of args, and store them to the stack
273 // too if this is a varargs function:
274 for (int i = 0; i < count && i < 8; ++i) {
275 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
276 if(F.isVarArg()) {
277 // if this is a varargs function, we copy the input registers to the stack
278 int FI = MFI->CreateFixedObject(8, tempOffset);
279 tempOffset+=8; //XXX: is it safe to use r22 like this?
280 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
281 // FIXME: we should use st8.spill here, one day
282 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
283 }
284 }
285
Duraid Madinaca494fd2005-04-12 14:54:44 +0000286 // Finally, inform the code generator which regs we return values in.
287 // (see the ISD::RET: case down below)
288 switch (getValueType(F.getReturnType())) {
289 default: assert(0 && "i have no idea where to return this type!");
290 case MVT::isVoid: break;
291 case MVT::i1:
292 case MVT::i8:
293 case MVT::i16:
294 case MVT::i32:
295 case MVT::i64:
296 MF.addLiveOut(IA64::r8);
297 break;
298 case MVT::f32:
299 case MVT::f64:
300 MF.addLiveOut(IA64::F8);
301 break;
302 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000303
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000304 return ArgValues;
305}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000306
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000307std::pair<SDOperand, SDOperand>
308IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000309 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000310 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000311 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000312 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000313
314 MachineFunction &MF = DAG.getMachineFunction();
315
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000316 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000317 unsigned outRegsUsed = 0;
318
319 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000320 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000321 outRegsUsed = 8;
322 } else {
323 outRegsUsed = Args.size();
324 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000325
Duraid Madinabeeaab22005-03-31 12:31:11 +0000326 // FIXME? this WILL fail if we ever try to pass around an arg that
327 // consumes more than a single output slot (a 'real' double, int128
328 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
329 // registers we use. Hopefully, the assembler will notice.
330 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
331 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000332
Chris Lattner16cd04d2005-05-12 23:24:06 +0000333 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000334 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000335
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000336 std::vector<SDOperand> args_to_use;
337 for (unsigned i = 0, e = Args.size(); i != e; ++i)
338 {
339 switch (getValueType(Args[i].second)) {
340 default: assert(0 && "unexpected argument type!");
341 case MVT::i1:
342 case MVT::i8:
343 case MVT::i16:
344 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000345 //promote to 64-bits, sign/zero extending based on type
346 //of the argument
347 if(Args[i].second->isSigned())
348 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
349 Args[i].first);
350 else
351 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
352 Args[i].first);
353 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000354 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000355 //promote to 64-bits
356 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000357 case MVT::f64:
358 case MVT::i64:
359 break;
360 }
361 args_to_use.push_back(Args[i].first);
362 }
363
364 std::vector<MVT::ValueType> RetVals;
365 MVT::ValueType RetTyVT = getValueType(RetTy);
366 if (RetTyVT != MVT::isVoid)
367 RetVals.push_back(RetTyVT);
368 RetVals.push_back(MVT::Other);
369
370 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000371 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000372 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000373 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000374 DAG.getConstant(NumBytes, getPointerTy()));
375 return std::make_pair(TheCall, Chain);
376}
377
Chris Lattnere0fe2252005-07-05 19:58:54 +0000378SDOperand
379IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
380 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000381 // vastart just stores the address of the VarArgsFrameIndex slot.
382 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000383 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
384 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000385}
386
387std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000388LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
389 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000390
391 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000392 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
393 VAListP, DAG.getSrcValue(VAListV));
394 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
395 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000396 unsigned Amt;
397 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
398 Amt = 8;
399 else {
400 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
401 "Other types should have been promoted for varargs!");
402 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000403 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000404 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000405 DAG.getConstant(Amt, Val.getValueType()));
406 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000407 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000408 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000409}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000410
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000411namespace {
412
413 //===--------------------------------------------------------------------===//
414 /// ISel - IA64 specific code to select IA64 machine instructions for
415 /// SelectionDAG operations.
416 ///
417 class ISel : public SelectionDAGISel {
418 /// IA64Lowering - This object fully describes how to lower LLVM code to an
419 /// IA64-specific SelectionDAG.
420 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000421 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
422 // for sdiv and udiv until it is put into the future
423 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000424
425 /// ExprMap - As shared expressions are codegen'd, we keep track of which
426 /// vreg the value is produced in, so we only emit one copy of each compiled
427 /// tree.
428 std::map<SDOperand, unsigned> ExprMap;
429 std::set<SDOperand> LoweredTokens;
430
431 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000432 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
433 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000434
435 /// InstructionSelectBasicBlock - This callback is invoked by
436 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
437 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
438
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000439 unsigned SelectExpr(SDOperand N);
440 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000441 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
442 SDOperand BuildConstmulSequence(SDOperand N);
443
Chris Lattner47c08892005-08-22 18:28:09 +0000444 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000445 };
446}
447
448/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
449/// when it has created a SelectionDAG for us to codegen.
450void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
451
452 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000453 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000454 Select(DAG.getRoot());
455
456 // Clear state used for selection.
457 ExprMap.clear();
458 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000459 ISelDAG = 0;
460}
461
Duraid Madinab2322562005-04-26 07:23:02 +0000462// strip leading '0' characters from a string
463void munchLeadingZeros(std::string& inString) {
464 while(inString.c_str()[0]=='0') {
465 inString.erase(0, 1);
466 }
467}
468
469// strip trailing '0' characters from a string
470void munchTrailingZeros(std::string& inString) {
471 int curPos=inString.length()-1;
472
473 while(inString.c_str()[curPos]=='0') {
474 inString.erase(curPos, 1);
475 curPos--;
476 }
477}
478
479// return how many consecutive '0' characters are at the end of a string
480unsigned int countTrailingZeros(std::string& inString) {
481 int curPos=inString.length()-1;
482 unsigned int zeroCount=0;
483 // assert goes here
484 while(inString.c_str()[curPos--]=='0') {
485 zeroCount++;
486 }
487 return zeroCount;
488}
489
490// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
491// '0' and 'N' (-1) characters)
492void boothEncode(std::string inString, std::string& boothEncodedString) {
493
494 int curpos=0;
495 int replacements=0;
496 int lim=inString.size();
497
498 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000499 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000500 int runlength=0;
501 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000502
Duraid Madinab2322562005-04-26 07:23:02 +0000503 // find the run length
504 for(;inString[curpos+runlength]=='1';runlength++) ;
505
506 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000507 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000508 replaceString+="1";
509
510 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000511 inString.replace(curpos, runlength+1, replaceString);
512 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000513 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000514 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000515 } else { // a zero, we just keep chugging along
516 curpos++;
517 }
518 }
519
520 // clean up (trim the string, reverse it and turn '1's into 'P's)
521 munchTrailingZeros(inString);
522 boothEncodedString="";
523
524 for(int i=inString.size()-1;i>=0;i--)
525 if(inString[i]=='1')
526 boothEncodedString+="P";
527 else
528 boothEncodedString+=inString[i];
529
530}
531
532struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
533 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000534 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000535 unsigned secondVal; // C
536 unsigned secondShift; // D
537 bool isSub;
538};
539
540/* this implements Lefevre's "pattern-based" constant multiplication,
541 * see "Multiplication by an Integer Constant", INRIA report 1999-06
542 *
543 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
544 * to get better booth encodings - this does help in practice
545 * TODO: weight shifts appropriately (most architectures can't
546 * fuse a shift and an add for arbitrary shift amounts) */
547unsigned lefevre(const std::string inString,
548 std::vector<struct shiftaddblob> &ops) {
549 std::string retstring;
550 std::string s = inString;
551 munchTrailingZeros(s);
552
553 int length=s.length()-1;
554
555 if(length==0) {
556 return(0);
557 }
558
559 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000560
Duraid Madinab2322562005-04-26 07:23:02 +0000561 for(int i=0; i<=length; i++) {
562 if (s.c_str()[length-i]=='P') {
563 p.push_back(i);
564 } else if (s.c_str()[length-i]=='N') {
565 n.push_back(i);
566 }
567 }
568
569 std::string t, u;
Duraid Madina4706c032005-04-26 09:42:50 +0000570 int c;
571 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000572 std::map<const int, int> w;
573
Duraid Madina85d5f602005-04-27 11:57:39 +0000574 for(unsigned i=0; i<p.size(); i++) {
575 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000576 w[p[i]-p[j]]++;
577 }
578 }
579
Duraid Madina85d5f602005-04-27 11:57:39 +0000580 for(unsigned i=1; i<n.size(); i++) {
581 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000582 w[n[i]-n[j]]++;
583 }
584 }
585
Duraid Madina85d5f602005-04-27 11:57:39 +0000586 for(unsigned i=0; i<p.size(); i++) {
587 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000588 w[-abs(p[i]-n[j])]++;
589 }
590 }
591
592 std::map<const int, int>::const_iterator ii;
593 std::vector<int> d;
594 std::multimap<int, int> sorted_by_value;
595
596 for(ii = w.begin(); ii!=w.end(); ii++)
597 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
598
599 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
600 it != sorted_by_value.end(); ++it) {
601 d.push_back((*it).second);
602 }
603
604 int int_W=0;
605 int int_d;
606
607 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
608 d.pop_back();
609 retstring=s; // hmmm
610 int x=0;
611 int z=abs(int_d)-1;
612
613 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000614
Duraid Madina85d5f602005-04-27 11:57:39 +0000615 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000616 if( ((base+z+1) < retstring.size()) &&
617 retstring.c_str()[base]=='P' &&
618 retstring.c_str()[base+z+1]=='P')
619 {
620 // match
621 x++;
622 retstring.replace(base, 1, "0");
623 retstring.replace(base+z+1, 1, "p");
624 }
Duraid Madinab2322562005-04-26 07:23:02 +0000625 }
626
Duraid Madina85d5f602005-04-27 11:57:39 +0000627 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000628 if( ((base+z+1) < retstring.size()) &&
629 retstring.c_str()[base]=='N' &&
630 retstring.c_str()[base+z+1]=='N')
631 {
632 // match
633 x++;
634 retstring.replace(base, 1, "0");
635 retstring.replace(base+z+1, 1, "n");
636 }
Duraid Madinab2322562005-04-26 07:23:02 +0000637 }
638
639 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000640 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000641 if( ((base+z+1) < retstring.size()) &&
642 ((retstring.c_str()[base]=='P' &&
643 retstring.c_str()[base+z+1]=='N') ||
644 (retstring.c_str()[base]=='N' &&
645 retstring.c_str()[base+z+1]=='P')) ) {
646 // match
647 x++;
648
649 if(retstring.c_str()[base]=='P') {
650 retstring.replace(base, 1, "0");
651 retstring.replace(base+z+1, 1, "p");
652 } else { // retstring[base]=='N'
653 retstring.replace(base, 1, "0");
654 retstring.replace(base+z+1, 1, "n");
655 }
656 }
Duraid Madinab2322562005-04-26 07:23:02 +0000657 }
658 }
659
660 if(x>int_W) {
661 int_W = x;
662 t = retstring;
663 c = int_d; // tofix
664 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000665
Duraid Madinab2322562005-04-26 07:23:02 +0000666 } d.pop_back(); // hmm
667
668 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000669
Duraid Madina85d5f602005-04-27 11:57:39 +0000670 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000671 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
672 t.replace(i, 1, "0");
673 }
674
Duraid Madina85d5f602005-04-27 11:57:39 +0000675 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000676 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000677 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000678 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000679 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000680 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000681 u.replace(i, 1, "N");
682 }
683
684 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000685 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000686 c=-c;
687 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000688 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000689
Duraid Madina8a3042c2005-05-09 13:18:34 +0000690 int pos=0;
691 while(u[pos]=='0')
692 pos++;
693
694 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000695
696 int g=0;
697 if(hit) {
698 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000699 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000700 bool isP=(u[p]=='P');
701 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000702
703 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000704 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000705 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000706 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000707 }
708 }
709
710 munchLeadingZeros(u);
711
712 int i = lefevre(u, ops);
713
714 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000715
Duraid Madinab2322562005-04-26 07:23:02 +0000716 blob.firstVal=i; blob.firstShift=c;
717 blob.isSub=f;
718 blob.secondVal=i; blob.secondShift=0;
719
720 ops.push_back(blob);
721
722 i = ops.size();
723
724 munchLeadingZeros(t);
725
726 if(t.length()==0)
727 return i;
728
729 if(t.c_str()[0]!='P') {
730 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000731 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000732 bool isP=(t.c_str()[p]=='P');
733 bool isN=(t.c_str()[p]=='N');
734
735 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000736 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000737 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000738 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000739 }
740 }
741
742 int j = lefevre(t, ops);
743
744 int trail=countTrailingZeros(u);
745 blob.secondVal=i; blob.secondShift=trail;
746
747 trail=countTrailingZeros(t);
748 blob.firstVal=j; blob.firstShift=trail;
749
750 switch(g) {
751 case 0:
752 blob.isSub=false; // first + second
753 break;
754 case 1:
755 blob.isSub=true; // first - second
756 break;
757 case 2:
758 blob.isSub=true; // second - first
759 int tmpval, tmpshift;
760 tmpval=blob.firstVal;
761 tmpshift=blob.firstShift;
762 blob.firstVal=blob.secondVal;
763 blob.firstShift=blob.secondShift;
764 blob.secondVal=tmpval;
765 blob.secondShift=tmpshift;
766 break;
767 //assert
768 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000769
Duraid Madinab2322562005-04-26 07:23:02 +0000770 ops.push_back(blob);
771 return ops.size();
772}
773
774SDOperand ISel::BuildConstmulSequence(SDOperand N) {
775 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
776 // in particular, *3 is nicer as *2+1, not *4-1
777 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
778
779 bool flippedSign;
780 unsigned preliminaryShift=0;
781
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000782 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000783
784 // first, we make the constant to multiply by positive
785 if(constant<0) {
786 constant=-constant;
787 flippedSign=true;
788 } else {
789 flippedSign=false;
790 }
791
792 // next, we make it odd.
793 for(; (constant%2==0); preliminaryShift++)
794 constant>>=1;
795
796 //OK, we have a positive, odd number of 64 bits or less. Convert it
797 //to a binary string, constantString[0] is the LSB
798 char constantString[65];
799 for(int i=0; i<64; i++)
800 constantString[i]='0'+((constant>>i)&0x1);
801 constantString[64]=0;
802
803 // now, Booth encode it
804 std::string boothEncodedString;
805 boothEncode(constantString, boothEncodedString);
806
807 std::vector<struct shiftaddblob> ops;
808 // do the transformation, filling out 'ops'
809 lefevre(boothEncodedString, ops);
810
Duraid Madinae75a24a2005-05-15 14:44:13 +0000811 assert(ops.size() < 80 && "constmul code has gone haywire\n");
812 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000813
Duraid Madinab2322562005-04-26 07:23:02 +0000814 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000815 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000816 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
817 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
818 results[ops[i].firstVal-1];
819 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
820 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
821 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
822 results[ops[i].secondVal-1];
823 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
824 if(ops[i].isSub)
825 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
826 else
827 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
828 }
829
830 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000831 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000832 if(preliminaryShift) {
833 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000834 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000835 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000836 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000837 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000838 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000839
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000840 SDOperand finalresult;
841 if(flippedSign) { // if we were multiplying by a negative constant:
842 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
843 // subtract the result from 0 to flip its sign
844 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
845 } else { // there was no preliminary multiply by -1 required
846 finalresult = shiftedresult;
847 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000848
849 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000850}
851
Duraid Madina4826a072005-04-06 09:55:17 +0000852/// ponderIntegerDivisionBy - When handling integer divides, if the divide
853/// is by a constant such that we can efficiently codegen it, this
854/// function says what to do. Currently, it returns 0 if the division must
855/// become a genuine divide, and 1 if the division can be turned into a
856/// right shift.
857static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
858 unsigned& Imm) {
859 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
860 // a constant, give up.
861
862 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
863
Chris Lattner0561b3f2005-08-02 19:26:06 +0000864 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
865 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000866 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000867 }
868
Duraid Madina4826a072005-04-06 09:55:17 +0000869 return 0; // fallthrough
870}
871
Duraid Madinac02780e2005-04-13 04:50:54 +0000872static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
873 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
874 // a constant, give up.
875
876 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
877
Chris Lattner0561b3f2005-08-02 19:26:06 +0000878 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000879 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000880 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000881 }
882
Duraid Madinac02780e2005-04-13 04:50:54 +0000883 return 0; // fallthrough
884}
885
Duraid Madinaf55e4032005-04-07 12:33:38 +0000886static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
887 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
888 // constant, give up.
889 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
890
891 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
892 Imm = v & 0x3FFF; // 14 bits
893 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000894 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000895 return 0; // fallthrough
896}
897
898static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
899 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
900 // constant, give up.
901 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
902
903 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
904 Imm = v & 0xFF; // 8 bits
905 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000906 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000907 return 0; // fallthrough
908}
909
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000910unsigned ISel::SelectExpr(SDOperand N) {
911 unsigned Result;
912 unsigned Tmp1, Tmp2, Tmp3;
913 unsigned Opc = 0;
914 MVT::ValueType DestType = N.getValueType();
915
916 unsigned opcode = N.getOpcode();
917
918 SDNode *Node = N.Val;
919 SDOperand Op0, Op1;
920
921 if (Node->getOpcode() == ISD::CopyFromReg)
922 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000923 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000924
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000925 unsigned &Reg = ExprMap[N];
926 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000927
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000928 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000929 Reg = Result = (N.getValueType() != MVT::Other) ?
930 MakeReg(N.getValueType()) : 1;
931 else {
932 // If this is a call instruction, make sure to prepare ALL of the result
933 // values as well as the chain.
934 if (Node->getNumValues() == 1)
935 Reg = Result = 1; // Void call, just a chain.
936 else {
937 Result = MakeReg(Node->getValueType(0));
938 ExprMap[N.getValue(0)] = Result;
939 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
940 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
941 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
942 }
943 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000944
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000945 switch (N.getOpcode()) {
946 default:
947 Node->dump();
948 assert(0 && "Node not handled!\n");
949
950 case ISD::FrameIndex: {
951 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
952 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
953 return Result;
954 }
955
956 case ISD::ConstantPool: {
957 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
958 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
959 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
960 .addReg(IA64::r1);
961 return Result;
962 }
963
964 case ISD::ConstantFP: {
965 Tmp1 = Result; // Intermediate Register
966 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
967 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
968 Tmp1 = MakeReg(MVT::f64);
969
970 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
971 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
972 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
973 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
974 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
975 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
976 else
977 assert(0 && "Unexpected FP constant!");
978 if (Tmp1 != Result)
979 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
980 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000981 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000982 return Result;
983 }
984
985 case ISD::DYNAMIC_STACKALLOC: {
986 // Generate both result values.
987 if (Result != 1)
988 ExprMap[N.getValue(1)] = 1; // Generate the token
989 else
990 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
991
992 // FIXME: We are currently ignoring the requested alignment for handling
993 // greater than the stack alignment. This will need to be revisited at some
994 // point. Align = N.getOperand(2);
995
996 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
997 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
998 std::cerr << "Cannot allocate stack object with greater alignment than"
999 << " the stack alignment yet!";
1000 abort();
1001 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001002
1003/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001004 Select(N.getOperand(0));
1005 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1006 {
1007 if (CN->getValue() < 32000)
1008 {
1009 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001010 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001011 } else {
1012 Tmp1 = SelectExpr(N.getOperand(1));
1013 // Subtract size from stack pointer, thereby allocating some space.
1014 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1015 }
1016 } else {
1017 Tmp1 = SelectExpr(N.getOperand(1));
1018 // Subtract size from stack pointer, thereby allocating some space.
1019 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1020 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001021*/
1022 Select(N.getOperand(0));
1023 Tmp1 = SelectExpr(N.getOperand(1));
1024 // Subtract size from stack pointer, thereby allocating some space.
1025 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001026 // Put a pointer to the space into the result register, by copying the
1027 // stack pointer.
1028 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1029 return Result;
1030 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001031
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001032 case ISD::SELECT: {
1033 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1034 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1035 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1036
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001037 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001038
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001039 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001040 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001041 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1042 // for i1, we load the condition into an integer register, then
1043 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1044 // of them will go through, since the integer register will hold
1045 // either 0 or 1)
1046 case MVT::i1: {
1047 bogoResult=MakeReg(MVT::i1);
1048
1049 // load the condition into an integer register
1050 unsigned condReg=MakeReg(MVT::i64);
1051 unsigned dummy=MakeReg(MVT::i64);
1052 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1053 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1054 .addImm(1).addReg(Tmp1);
1055
1056 // initialize Result (bool) to false (hence UNC) and if
1057 // the select condition (condReg) is false (0), copy Tmp3
1058 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1059 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1060
1061 // now, if the selection condition is true, write 1 to the
1062 // result if Tmp2 is 1
1063 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1064 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1065 break;
1066 }
1067 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1068 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001069 case MVT::i64:
1070 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001071 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1072 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1073 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001074 break;
1075 case MVT::f64:
1076 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001077 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1078 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1079 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001080 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001081 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001082
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001083 return Result;
1084 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001085
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001086 case ISD::Constant: {
1087 unsigned depositPos=0;
1088 unsigned depositLen=0;
1089 switch (N.getValueType()) {
1090 default: assert(0 && "Cannot use constants of this type!");
1091 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001092 // the constant:
1093 if(cast<ConstantSDNode>(N)->getValue()) // true:
1094 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1095 else // false:
1096 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1097 return Result; // early exit
1098 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001099 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001100 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001101
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001102 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001103
1104 if(immediate==0) { // if the constant is just zero,
1105 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1106 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001107 }
1108
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001109 if (immediate <= 8191 && immediate >= -8192) {
1110 // if this constants fits in 14 bits, we use a mov the assembler will
1111 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1112 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1113 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001114 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001115
1116 if (immediate <= 2097151 && immediate >= -2097152) {
1117 // if this constants fits in 22 bits, we use a mov the assembler will
1118 // turn into: "addl rDest=imm,r0"
1119 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1120 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001121 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001122
1123 /* otherwise, our immediate is big, so we use movl */
1124 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001125 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001126 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001127 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001128
1129 case ISD::UNDEF: {
1130 BuildMI(BB, IA64::IDEF, 0, Result);
1131 return Result;
1132 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001133
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001134 case ISD::GlobalAddress: {
1135 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1136 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001137
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001138 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001139 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001140
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001141 return Result;
1142 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001143
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001144 case ISD::ExternalSymbol: {
1145 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001146// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1147 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001148 return Result;
1149 }
1150
1151 case ISD::FP_EXTEND: {
1152 Tmp1 = SelectExpr(N.getOperand(0));
1153 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1154 return Result;
1155 }
1156
1157 case ISD::ZERO_EXTEND: {
1158 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001159
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001160 switch (N.getOperand(0).getValueType()) {
1161 default: assert(0 && "Cannot zero-extend this type!");
1162 case MVT::i8: Opc = IA64::ZXT1; break;
1163 case MVT::i16: Opc = IA64::ZXT2; break;
1164 case MVT::i32: Opc = IA64::ZXT4; break;
1165
Misha Brukman4633f1c2005-04-21 23:13:11 +00001166 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001167 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001168 unsigned dummy = MakeReg(MVT::i64);
1169 // first load zero:
1170 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1171 // ...then conditionally (PR:Tmp1) add 1:
1172 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1173 .addImm(1).addReg(Tmp1);
1174 return Result; // XXX early exit!
1175 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001176 }
1177
1178 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1179 return Result;
1180 }
1181
1182 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1183
1184assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1185
1186 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001187
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001188 switch (N.getOperand(0).getValueType()) {
1189 default: assert(0 && "Cannot sign-extend this type!");
1190 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001191 Opc = IA64::SXT1; break;
1192 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001193 case MVT::i8: Opc = IA64::SXT1; break;
1194 case MVT::i16: Opc = IA64::SXT2; break;
1195 case MVT::i32: Opc = IA64::SXT4; break;
1196 }
1197
1198 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1199 return Result;
1200 }
1201
1202 case ISD::TRUNCATE: {
1203 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1204 // of R0 appropriately.
1205 switch (N.getOperand(0).getValueType()) {
1206 default: assert(0 && "Unknown truncate!");
1207 case MVT::i64: break;
1208 }
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 unsigned depositPos, depositLen;
1211
1212 switch (N.getValueType()) {
1213 default: assert(0 && "Unknown truncate!");
1214 case MVT::i1: {
1215 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001216 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1217 .addReg(IA64::r0);
1218 return Result; // XXX early exit!
1219 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001220 case MVT::i8: depositPos=0; depositLen=8; break;
1221 case MVT::i16: depositPos=0; depositLen=16; break;
1222 case MVT::i32: depositPos=0; depositLen=32; break;
1223 }
Duraid Madinaa9110342005-08-19 13:25:50 +00001224 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001225 .addImm(depositPos).addImm(depositLen);
1226 return Result;
1227 }
1228
Misha Brukman7847fca2005-04-22 17:54:37 +00001229/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001230 case ISD::FP_ROUND: {
1231 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001232 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001233 Tmp1 = SelectExpr(N.getOperand(0));
1234 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1235 // we add 0.0 using a single precision add to do rounding
1236 return Result;
1237 }
1238*/
1239
1240// FIXME: the following 4 cases need cleaning
1241 case ISD::SINT_TO_FP: {
1242 Tmp1 = SelectExpr(N.getOperand(0));
1243 Tmp2 = MakeReg(MVT::f64);
1244 unsigned dummy = MakeReg(MVT::f64);
1245 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1246 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1247 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1248 return Result;
1249 }
1250
1251 case ISD::UINT_TO_FP: {
1252 Tmp1 = SelectExpr(N.getOperand(0));
1253 Tmp2 = MakeReg(MVT::f64);
1254 unsigned dummy = MakeReg(MVT::f64);
1255 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1256 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1257 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1258 return Result;
1259 }
1260
1261 case ISD::FP_TO_SINT: {
1262 Tmp1 = SelectExpr(N.getOperand(0));
1263 Tmp2 = MakeReg(MVT::f64);
1264 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1265 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1266 return Result;
1267 }
1268
1269 case ISD::FP_TO_UINT: {
1270 Tmp1 = SelectExpr(N.getOperand(0));
1271 Tmp2 = MakeReg(MVT::f64);
1272 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1273 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1274 return Result;
1275 }
1276
1277 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001278 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1279 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1280 // into an fma, do so:
1281 // ++FusedFP; // Statistic
1282 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1283 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1284 Tmp3 = SelectExpr(N.getOperand(1));
1285 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1286 return Result; // early exit
1287 }
Duraid Madinaed095022005-04-13 06:12:04 +00001288
1289 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001290 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001291 // this add into a shladd, try:
1292 ConstantSDNode *CSD = NULL;
1293 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001294 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001295
Misha Brukman7847fca2005-04-22 17:54:37 +00001296 // ++FusedSHLADD; // Statistic
1297 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1298 int shl_amt = CSD->getValue();
1299 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001300
Misha Brukman7847fca2005-04-22 17:54:37 +00001301 BuildMI(BB, IA64::SHLADD, 3, Result)
1302 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1303 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001304 }
1305 }
1306
1307 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001308 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001309 if(DestType != MVT::f64) { // integer addition:
1310 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001311 case 1: // adding a constant that's 14 bits
1312 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1313 return Result; // early exit
1314 } // fallthrough and emit a reg+reg ADD:
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001317 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001318 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001319 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1320 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001321 return Result;
1322 }
1323
1324 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001325
1326 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001327/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1328 */
Jeff Cohen00b168892005-07-27 06:12:32 +00001329 // boring old integer multiply with xma
1330 Tmp1 = SelectExpr(N.getOperand(0));
1331 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinab2322562005-04-26 07:23:02 +00001332
Jeff Cohen00b168892005-07-27 06:12:32 +00001333 unsigned TempFR1=MakeReg(MVT::f64);
1334 unsigned TempFR2=MakeReg(MVT::f64);
1335 unsigned TempFR3=MakeReg(MVT::f64);
1336 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1337 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1338 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1339 .addReg(IA64::F0);
1340 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1341 return Result; // early exit
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001342 /* FIXME } else { // we are multiplying by an integer constant! yay
Jeff Cohen00b168892005-07-27 06:12:32 +00001343 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001344 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001345 }
Duraid Madinab2322562005-04-26 07:23:02 +00001346 else { // floating point multiply
1347 Tmp1 = SelectExpr(N.getOperand(0));
1348 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001349 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001350 return Result;
1351 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001352 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001354 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001355 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1356 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1357 // into an fms, do so:
1358 // ++FusedFP; // Statistic
1359 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1360 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1361 Tmp3 = SelectExpr(N.getOperand(1));
1362 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1363 return Result; // early exit
1364 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001365 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001366 if(DestType != MVT::f64) { // integer subtraction:
1367 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001368 case 1: // subtracting *from* an 8 bit constant:
1369 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1370 return Result; // early exit
1371 } // fallthrough and emit a reg+reg SUB:
1372 Tmp1 = SelectExpr(N.getOperand(0));
1373 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001374 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001375 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001376 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001377 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001378 return Result;
1379 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001380
1381 case ISD::FABS: {
1382 Tmp1 = SelectExpr(N.getOperand(0));
1383 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1384 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1385 return Result;
1386 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001387
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001388 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001389 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001390
Misha Brukman4633f1c2005-04-21 23:13:11 +00001391 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001392 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1393 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1394 } else {
1395 Tmp1 = SelectExpr(N.getOperand(0));
1396 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1397 }
1398
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001399 return Result;
1400 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001401
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001402 case ISD::AND: {
1403 switch (N.getValueType()) {
1404 default: assert(0 && "Cannot AND this type!");
1405 case MVT::i1: { // if a bool, we emit a pseudocode AND
1406 unsigned pA = SelectExpr(N.getOperand(0));
1407 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001408
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001409/* our pseudocode for AND is:
1410 *
1411(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1412 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1413 ;;
1414(pB) cmp.ne pTemp,p0 = r0,r0
1415 ;;
1416(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1417
1418*/
1419 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001420
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001421 unsigned bogusTemp1 = MakeReg(MVT::i1);
1422 unsigned bogusTemp2 = MakeReg(MVT::i1);
1423 unsigned bogusTemp3 = MakeReg(MVT::i1);
1424 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001425
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001426 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001427 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001428 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001429 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001430 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001431 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001432 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001433 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001434 break;
1435 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001436
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001437 // if not a bool, we just AND away:
1438 case MVT::i8:
1439 case MVT::i16:
1440 case MVT::i32:
1441 case MVT::i64: {
1442 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001443 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001444 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001445 switch (Tmp3) {
1446 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1447 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1448 break;
1449 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1450 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1451 break;
1452 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1453 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1454 break;
1455 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001456 // FIXME: assert the dep.z is in bounds
1457 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001458 .addImm(0).addImm(Tmp3);
1459 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001460 }
1461 return Result; // early exit
1462 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001463 Tmp2 = SelectExpr(N.getOperand(1));
1464 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001465 }
1466 }
1467 return Result;
1468 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001470 case ISD::OR: {
1471 switch (N.getValueType()) {
1472 default: assert(0 && "Cannot OR this type!");
1473 case MVT::i1: { // if a bool, we emit a pseudocode OR
1474 unsigned pA = SelectExpr(N.getOperand(0));
1475 unsigned pB = SelectExpr(N.getOperand(1));
1476
1477 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001479/* our pseudocode for OR is:
1480 *
1481
1482pC = pA OR pB
1483-------------
1484
Misha Brukman7847fca2005-04-22 17:54:37 +00001485(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1486 ;;
1487(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001488
1489*/
1490 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001491 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001492 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001493 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001494 break;
1495 }
1496 // if not a bool, we just OR away:
1497 case MVT::i8:
1498 case MVT::i16:
1499 case MVT::i32:
1500 case MVT::i64: {
1501 Tmp1 = SelectExpr(N.getOperand(0));
1502 Tmp2 = SelectExpr(N.getOperand(1));
1503 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1504 break;
1505 }
1506 }
1507 return Result;
1508 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001509
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001510 case ISD::XOR: {
1511 switch (N.getValueType()) {
1512 default: assert(0 && "Cannot XOR this type!");
1513 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1514 unsigned pY = SelectExpr(N.getOperand(0));
1515 unsigned pZ = SelectExpr(N.getOperand(1));
1516
1517/* one possible routine for XOR is:
1518
1519 // Compute px = py ^ pz
1520 // using sum of products: px = (py & !pz) | (pz & !py)
1521 // Uses 5 instructions in 3 cycles.
1522 // cycle 1
1523(pz) cmp.eq.unc px = r0, r0 // px = pz
1524(py) cmp.eq.unc pt = r0, r0 // pt = py
1525 ;;
1526 // cycle 2
1527(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1528(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1529 ;;
1530 } { .mmi
1531 // cycle 3
1532(pt) cmp.eq.or px = r0, r0 // px = px | pt
1533
1534*** Another, which we use here, requires one scratch GR. it is:
1535
1536 mov rt = 0 // initialize rt off critical path
1537 ;;
1538
1539 // cycle 1
1540(pz) cmp.eq.unc px = r0, r0 // px = pz
1541(pz) mov rt = 1 // rt = pz
1542 ;;
1543 // cycle 2
1544(py) cmp.ne px = 1, rt // if (py) px = !pz
1545
1546.. these routines kindly provided by Jim Hull
1547*/
1548 unsigned rt = MakeReg(MVT::i64);
1549
1550 // these two temporaries will never actually appear,
1551 // due to the two-address form of some of the instructions below
1552 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1553 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1554
1555 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1556 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001557 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001558 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001559 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001560 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001561 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001562 break;
1563 }
1564 // if not a bool, we just XOR away:
1565 case MVT::i8:
1566 case MVT::i16:
1567 case MVT::i32:
1568 case MVT::i64: {
1569 Tmp1 = SelectExpr(N.getOperand(0));
1570 Tmp2 = SelectExpr(N.getOperand(1));
1571 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1572 break;
1573 }
1574 }
1575 return Result;
1576 }
1577
Duraid Madina63bbed52005-05-11 05:16:09 +00001578 case ISD::CTPOP: {
1579 Tmp1 = SelectExpr(N.getOperand(0));
1580 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1581 return Result;
1582 }
1583
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001584 case ISD::SHL: {
1585 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001586 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1587 Tmp2 = CN->getValue();
1588 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1589 } else {
1590 Tmp2 = SelectExpr(N.getOperand(1));
1591 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1592 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001593 return Result;
1594 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001595
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001596 case ISD::SRL: {
1597 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001598 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1599 Tmp2 = CN->getValue();
1600 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1601 } else {
1602 Tmp2 = SelectExpr(N.getOperand(1));
1603 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1604 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001605 return Result;
1606 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001607
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001608 case ISD::SRA: {
1609 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1611 Tmp2 = CN->getValue();
1612 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1613 } else {
1614 Tmp2 = SelectExpr(N.getOperand(1));
1615 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1616 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001617 return Result;
1618 }
1619
1620 case ISD::SDIV:
1621 case ISD::UDIV:
1622 case ISD::SREM:
1623 case ISD::UREM: {
1624
1625 Tmp1 = SelectExpr(N.getOperand(0));
1626 Tmp2 = SelectExpr(N.getOperand(1));
1627
1628 bool isFP=false;
1629
1630 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1631 isFP=true;
1632
1633 bool isModulus=false; // is it a division or a modulus?
1634 bool isSigned=false;
1635
1636 switch(N.getOpcode()) {
1637 case ISD::SDIV: isModulus=false; isSigned=true; break;
1638 case ISD::UDIV: isModulus=false; isSigned=false; break;
1639 case ISD::SREM: isModulus=true; isSigned=true; break;
1640 case ISD::UREM: isModulus=true; isSigned=false; break;
1641 }
1642
Duraid Madina4826a072005-04-06 09:55:17 +00001643 if(!isModulus && !isFP) { // if this is an integer divide,
1644 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001645 case 1: // division by a constant that's a power of 2
1646 Tmp1 = SelectExpr(N.getOperand(0));
1647 if(isSigned) { // argument could be negative, so emit some code:
1648 unsigned divAmt=Tmp3;
1649 unsigned tempGR1=MakeReg(MVT::i64);
1650 unsigned tempGR2=MakeReg(MVT::i64);
1651 unsigned tempGR3=MakeReg(MVT::i64);
1652 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1653 .addReg(Tmp1).addImm(divAmt-1);
1654 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1655 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1656 BuildMI(BB, IA64::ADD, 2, tempGR3)
1657 .addReg(Tmp1).addReg(tempGR2);
1658 BuildMI(BB, IA64::SHRS, 2, Result)
1659 .addReg(tempGR3).addImm(divAmt);
1660 }
1661 else // unsigned div-by-power-of-2 becomes a simple shift right:
1662 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1663 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001664 }
1665 }
1666
Misha Brukman4633f1c2005-04-21 23:13:11 +00001667 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001668 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001669 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1670 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1671 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1672 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1673 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1674 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1675 unsigned TmpF7=MakeReg(MVT::f64);
1676 unsigned TmpF8=MakeReg(MVT::f64);
1677 unsigned TmpF9=MakeReg(MVT::f64);
1678 unsigned TmpF10=MakeReg(MVT::f64);
1679 unsigned TmpF11=MakeReg(MVT::f64);
1680 unsigned TmpF12=MakeReg(MVT::f64);
1681 unsigned TmpF13=MakeReg(MVT::f64);
1682 unsigned TmpF14=MakeReg(MVT::f64);
1683 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001684
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001685 // OK, emit some code:
1686
1687 if(!isFP) {
1688 // first, load the inputs into FP regs.
1689 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1690 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001691
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001692 // next, convert the inputs to FP
1693 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001694 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1695 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001696 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001697 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1698 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001699 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001700
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001701 } else { // this is an FP divide/remainder, so we 'leak' some temp
1702 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1703 TmpF3=Tmp1;
1704 TmpF4=Tmp2;
1705 }
1706
1707 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001708 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1709 BuildMI(BB, IA64::FRCPAS1, 4)
1710 .addReg(TmpF5, MachineOperand::Def)
1711 .addReg(TmpPR, MachineOperand::Def)
1712 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001713
Duraid Madinabeeaab22005-03-31 12:31:11 +00001714 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1715 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1716 // TPCMPNE below
1717 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1718 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001719 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001720 }
1721
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001722 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1723 // precision, don't need this much for f32/i32)
1724 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1725 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1726 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1727 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1728 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1729 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1730 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1731 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1732 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1733 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1734 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1735 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1736 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1737 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1738 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1739 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001740
1741 // FIXME: this is unfortunate :(
1742 // the story is that the dest reg of the fnma above and the fma below
1743 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1744 // be the same register, or this code breaks if the first argument is
1745 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001746 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1747 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1748
Duraid Madina6e02e682005-04-04 05:05:52 +00001749 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1750 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1751 }
1752
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001753 if(!isFP) {
1754 // round to an integer
1755 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001756 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001757 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001758 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001759 } else {
1760 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1761 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1762 // we really do need the above FMOV? ;)
1763 }
1764
1765 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001766 if(isFP) { // extra worrying about div-by-zero
1767 unsigned bogoResult=MakeReg(MVT::f64);
1768
1769 // we do a 'conditional fmov' (of the correct result, depending
1770 // on how the frcpa predicate turned out)
1771 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001772 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001773 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001774 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001775 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001776 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001777 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001778 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001779 } else { // this is a modulus
1780 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001781 // answer = q * (-b) + a
1782 unsigned ModulusResult = MakeReg(MVT::f64);
1783 unsigned TmpF = MakeReg(MVT::f64);
1784 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001785
Misha Brukman7847fca2005-04-22 17:54:37 +00001786 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1787 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1788 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1789 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1790 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001791 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001792 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001793 }
1794 }
1795
1796 return Result;
1797 }
1798
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001799 case ISD::SIGN_EXTEND_INREG: {
1800 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001801 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001802 default:
1803 Node->dump();
1804 assert(0 && "don't know how to sign extend this type");
1805 break;
1806 case MVT::i8: Opc = IA64::SXT1; break;
1807 case MVT::i16: Opc = IA64::SXT2; break;
1808 case MVT::i32: Opc = IA64::SXT4; break;
1809 }
1810 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1811 return Result;
1812 }
1813
1814 case ISD::SETCC: {
1815 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001816 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1817 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001818
Chris Lattner88ac32c2005-08-09 20:21:10 +00001819 if(ConstantSDNode *CSDN =
1820 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1821 // if we are comparing against a constant zero
1822 if(CSDN->getValue()==0)
1823 Tmp2 = IA64::r0; // then we can just compare against r0
1824 else
1825 Tmp2 = SelectExpr(N.getOperand(1));
1826 } else // not comparing against a constant
1827 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001828
Chris Lattner88ac32c2005-08-09 20:21:10 +00001829 switch (CC) {
1830 default: assert(0 && "Unknown integer comparison!");
1831 case ISD::SETEQ:
1832 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 break;
1834 case ISD::SETGT:
1835 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 break;
1837 case ISD::SETGE:
1838 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 break;
1840 case ISD::SETLT:
1841 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 break;
1843 case ISD::SETLE:
1844 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1845 break;
1846 case ISD::SETNE:
1847 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1848 break;
1849 case ISD::SETULT:
1850 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1851 break;
1852 case ISD::SETUGT:
1853 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1854 break;
1855 case ISD::SETULE:
1856 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 break;
1858 case ISD::SETUGE:
1859 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1860 break;
1861 }
1862 } else { // if not integer, should be FP.
1863 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1864 "error: SETCC should have had incoming f32 promoted to f64!\n");
1865
1866 if(ConstantFPSDNode *CFPSDN =
1867 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1868
1869 // if we are comparing against a constant +0.0 or +1.0
1870 if(CFPSDN->isExactlyValue(+0.0))
1871 Tmp2 = IA64::F0; // then we can just compare against f0
1872 else if(CFPSDN->isExactlyValue(+1.0))
1873 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001874 else
1875 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001876 } else // not comparing against a constant
1877 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001878
Chris Lattner88ac32c2005-08-09 20:21:10 +00001879 switch (CC) {
1880 default: assert(0 && "Unknown FP comparison!");
1881 case ISD::SETEQ:
1882 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1883 break;
1884 case ISD::SETGT:
1885 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1886 break;
1887 case ISD::SETGE:
1888 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 break;
1890 case ISD::SETLT:
1891 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1892 break;
1893 case ISD::SETLE:
1894 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1895 break;
1896 case ISD::SETNE:
1897 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1898 break;
1899 case ISD::SETULT:
1900 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1901 break;
1902 case ISD::SETUGT:
1903 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1904 break;
1905 case ISD::SETULE:
1906 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 break;
1908 case ISD::SETUGE:
1909 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1910 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001911 }
1912 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001913 return Result;
1914 }
1915
1916 case ISD::EXTLOAD:
1917 case ISD::ZEXTLOAD:
1918 case ISD::LOAD: {
1919 // Make sure we generate both values.
1920 if (Result != 1)
1921 ExprMap[N.getValue(1)] = 1; // Generate the token
1922 else
1923 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1924
1925 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001926
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001927 if(opcode == ISD::LOAD) { // this is a LOAD
1928 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001929 default: assert(0 && "Cannot load this type!");
1930 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1931 // FIXME: for now, we treat bool loads the same as i8 loads */
1932 case MVT::i8: Opc = IA64::LD1; break;
1933 case MVT::i16: Opc = IA64::LD2; break;
1934 case MVT::i32: Opc = IA64::LD4; break;
1935 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001936
Misha Brukman7847fca2005-04-22 17:54:37 +00001937 case MVT::f32: Opc = IA64::LDF4; break;
1938 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001939 }
1940 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001941 MVT::ValueType TypeBeingLoaded =
1942 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001943 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001944 default: assert(0 && "Cannot extload/zextload this type!");
1945 // FIXME: bools?
1946 case MVT::i8: Opc = IA64::LD1; break;
1947 case MVT::i16: Opc = IA64::LD2; break;
1948 case MVT::i32: Opc = IA64::LD4; break;
1949 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001950 }
1951 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001952
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001953 SDOperand Chain = N.getOperand(0);
1954 SDOperand Address = N.getOperand(1);
1955
1956 if(Address.getOpcode() == ISD::GlobalAddress) {
1957 Select(Chain);
1958 unsigned dummy = MakeReg(MVT::i64);
1959 unsigned dummy2 = MakeReg(MVT::i64);
1960 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001961 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1962 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001963 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1964 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001965 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001966 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001967 // into a predicate register
1968 assert(Opc==IA64::LD1 && "problem loading a bool");
1969 unsigned dummy3 = MakeReg(MVT::i64);
1970 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1971 // we compare to 0. true? 0. false? 1.
1972 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001973 }
1974 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1975 Select(Chain);
1976 IA64Lowering.restoreGP(BB);
1977 unsigned dummy = MakeReg(MVT::i64);
1978 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001979 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001980 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001981 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001982 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001983 // into a predicate register
1984 assert(Opc==IA64::LD1 && "problem loading a bool");
1985 unsigned dummy3 = MakeReg(MVT::i64);
1986 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1987 // we compare to 0. true? 0. false? 1.
1988 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001989 }
1990 } else if(Address.getOpcode() == ISD::FrameIndex) {
1991 Select(Chain); // FIXME ? what about bools?
1992 unsigned dummy = MakeReg(MVT::i64);
1993 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001994 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001995 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 // into a predicate register
1999 assert(Opc==IA64::LD1 && "problem loading a bool");
2000 unsigned dummy3 = MakeReg(MVT::i64);
2001 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2002 // we compare to 0. true? 0. false? 1.
2003 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002004 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002005 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002006 Select(Chain);
2007 Tmp2 = SelectExpr(Address);
2008 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002009 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002010 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002011 // into a predicate register
2012 assert(Opc==IA64::LD1 && "problem loading a bool");
2013 unsigned dummy = MakeReg(MVT::i64);
2014 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2015 // we compare to 0. true? 0. false? 1.
2016 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00002017 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002018 }
2019
2020 return Result;
2021 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002022
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002023 case ISD::CopyFromReg: {
2024 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002025 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002026 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002027
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002028 SDOperand Chain = N.getOperand(0);
2029
2030 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002031 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002032
2033 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002034 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2035 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002036 // (r) Result =cmp.eq.unc(r0,r0)
2037 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002038 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002039 return Result;
2040 }
2041
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002042 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002043 case ISD::CALL: {
2044 Select(N.getOperand(0));
2045
2046 // The chain for this call is now lowered.
2047 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002048
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002049 //grab the arguments
2050 std::vector<unsigned> argvregs;
2051
2052 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002053 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002054
2055 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002056 // Runtime Architecture Guide to see some examples of what's going
2057 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2058 // while FP args get mapped to F8->F15 as needed)
2059
2060 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002061
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002062 // in reg args
2063 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2064 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002065 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2066 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2067 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2068 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002069
Misha Brukman7847fca2005-04-22 17:54:37 +00002070 switch(N.getOperand(i+2).getValueType())
2071 {
2072 default: // XXX do we need to support MVT::i1 here?
2073 Node->dump();
2074 N.getOperand(i).Val->dump();
2075 std::cerr << "Type for " << i << " is: " <<
2076 N.getOperand(i+2).getValueType() << std::endl;
2077 assert(0 && "Unknown value type for call");
2078 case MVT::i64:
2079 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2080 break;
2081 case MVT::f64:
2082 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2083 .addReg(argvregs[i]);
2084 // FIXME: we don't need to do this _all_ the time:
2085 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2086 break;
2087 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002088 }
2089
2090 //in mem args
2091 for (int i = 8, e = argvregs.size(); i < e; ++i)
2092 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002093 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002094
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002095 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002096 default:
2097 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002098 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002099 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002100 N.getOperand(i+2).getValueType() << "\n";
2101 assert(0 && "Unknown value type for call");
2102 case MVT::i1: // FIXME?
2103 case MVT::i8:
2104 case MVT::i16:
2105 case MVT::i32:
2106 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002107 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2108 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2109 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002110 break;
2111 case MVT::f32:
2112 case MVT::f64:
2113 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002114 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2115 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002116 break;
2117 }
2118 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002119
Duraid Madina04aa46d2005-05-20 11:39:17 +00002120 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002121 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002122 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002123 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002124 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2125 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002126 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002127 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002128 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002129 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002130 // "non-pic code with imm relocation against dynamic symbol" errors
2131 BuildMI(BB, IA64::BRCALL, 1)
2132 .addExternalSymbol(ESSDN->getSymbol(), true);
2133 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002134 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002135 else { // otherwise we need to get the function descriptor
2136 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002137 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002138 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002139
2140 unsigned targetEntryPoint=MakeReg(MVT::i64);
2141 unsigned targetGPAddr=MakeReg(MVT::i64);
2142 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002143
Duraid Madinabeeaab22005-03-31 12:31:11 +00002144 // b6 is a scratch branch register, we load the target entry point
2145 // from the base of the function descriptor
2146 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2147 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2148
2149 // save the current GP:
2150 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002151
Duraid Madinabeeaab22005-03-31 12:31:11 +00002152 /* TODO: we need to make sure doing this never, ever loads a
2153 * bogus value into r1 (GP). */
2154 // load the target GP (which is at mem[functiondescriptor+8])
2155 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002156 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002157 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2158
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002159 // and then jump: (well, call)
2160 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002161 // and finally restore the old GP
2162 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2163 IA64Lowering.restoreSP_RP(BB);
2164 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002165
2166 switch (Node->getValueType(0)) {
2167 default: assert(0 && "Unknown value type for call result!");
2168 case MVT::Other: return 1;
2169 case MVT::i1:
2170 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002171 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002172 break;
2173 case MVT::i8:
2174 case MVT::i16:
2175 case MVT::i32:
2176 case MVT::i64:
2177 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2178 break;
2179 case MVT::f64:
2180 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2181 break;
2182 }
2183 return Result+N.ResNo;
2184 }
2185
Misha Brukman4633f1c2005-04-21 23:13:11 +00002186 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002187 return 0;
2188}
2189
2190void ISel::Select(SDOperand N) {
2191 unsigned Tmp1, Tmp2, Opc;
2192 unsigned opcode = N.getOpcode();
2193
Nate Begeman85fdeb22005-03-24 04:39:54 +00002194 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002195 return; // Already selected.
2196
2197 SDNode *Node = N.Val;
2198
2199 switch (Node->getOpcode()) {
2200 default:
2201 Node->dump(); std::cerr << "\n";
2202 assert(0 && "Node not handled yet!");
2203
2204 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002205
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002206 case ISD::TokenFactor: {
2207 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2208 Select(Node->getOperand(i));
2209 return;
2210 }
2211
2212 case ISD::CopyToReg: {
2213 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002214 Tmp1 = SelectExpr(N.getOperand(2));
2215 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002216
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002217 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002218 // if a bool, we use pseudocode
2219 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002220 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2221 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002222 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2223 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002224 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002225 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002226 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002227 }
2228 return;
2229 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002230
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002231 case ISD::RET: {
2232
2233 /* what the heck is going on here:
2234
2235<_sabre_> ret with two operands is obvious: chain and value
2236<camel_> yep
2237<_sabre_> ret with 3 values happens when 'expansion' occurs
2238<_sabre_> e.g. i64 gets split into 2x i32
2239<camel_> oh right
2240<_sabre_> you don't have this case on ia64
2241<camel_> yep
2242<_sabre_> so the two returned values go into EAX/EDX on ia32
2243<camel_> ahhh *memories*
2244<_sabre_> :)
2245<camel_> ok, thanks :)
2246<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2247<_sabre_> this is the first operand always
2248<_sabre_> these operand often define chains, they are the last operand
2249<_sabre_> they are printed as 'ch' if you do DAG.dump()
2250 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002251
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002252 switch (N.getNumOperands()) {
2253 default:
2254 assert(0 && "Unknown return instruction!");
2255 case 2:
2256 Select(N.getOperand(0));
2257 Tmp1 = SelectExpr(N.getOperand(1));
2258 switch (N.getOperand(1).getValueType()) {
2259 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002260 // FIXME: do I need to add support for bools here?
2261 // (return '0' or '1' r8, basically...)
2262 //
2263 // FIXME: need to round floats - 80 bits is bad, the tester
2264 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002265 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002266 // we mark r8 as live on exit up above in LowerArguments()
2267 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2268 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002269 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002270 // we mark F8 as live on exit up above in LowerArguments()
2271 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002272 }
2273 break;
2274 case 1:
2275 Select(N.getOperand(0));
2276 break;
2277 }
2278 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2279 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2280 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2281 return;
2282 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002283
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002284 case ISD::BR: {
2285 Select(N.getOperand(0));
2286 MachineBasicBlock *Dest =
2287 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2288 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2289 // XXX HACK! we do _not_ need long branches all the time
2290 return;
2291 }
2292
2293 case ISD::ImplicitDef: {
2294 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002295 BuildMI(BB, IA64::IDEF, 0,
2296 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002297 return;
2298 }
2299
2300 case ISD::BRCOND: {
2301 MachineBasicBlock *Dest =
2302 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2303
2304 Select(N.getOperand(0));
2305 Tmp1 = SelectExpr(N.getOperand(1));
2306 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2307 // XXX HACK! we do _not_ need long branches all the time
2308 return;
2309 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002310
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002311 case ISD::EXTLOAD:
2312 case ISD::ZEXTLOAD:
2313 case ISD::SEXTLOAD:
2314 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002315 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002316 case ISD::CALL:
2317 case ISD::CopyFromReg:
2318 case ISD::DYNAMIC_STACKALLOC:
2319 SelectExpr(N);
2320 return;
2321
2322 case ISD::TRUNCSTORE:
2323 case ISD::STORE: {
2324 Select(N.getOperand(0));
2325 Tmp1 = SelectExpr(N.getOperand(1)); // value
2326
2327 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002328
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002329 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002330 switch (N.getOperand(1).getValueType()) {
2331 default: assert(0 && "Cannot store this type!");
2332 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2333 // FIXME?: for now, we treat bool loads the same as i8 stores */
2334 case MVT::i8: Opc = IA64::ST1; break;
2335 case MVT::i16: Opc = IA64::ST2; break;
2336 case MVT::i32: Opc = IA64::ST4; break;
2337 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002338
Misha Brukman7847fca2005-04-22 17:54:37 +00002339 case MVT::f32: Opc = IA64::STF4; break;
2340 case MVT::f64: Opc = IA64::STF8; break;
2341 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002342 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002343 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002344 default: assert(0 && "unknown type in truncstore");
2345 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2346 //FIXME: DAG does not promote this load?
2347 case MVT::i8: Opc = IA64::ST1; break;
2348 case MVT::i16: Opc = IA64::ST2; break;
2349 case MVT::i32: Opc = IA64::ST4; break;
2350 case MVT::f32: Opc = IA64::STF4; break;
2351 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002352 }
2353
2354 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002355 unsigned dummy = MakeReg(MVT::i64);
2356 unsigned dummy2 = MakeReg(MVT::i64);
2357 BuildMI(BB, IA64::ADD, 2, dummy)
2358 .addGlobalAddress(cast<GlobalAddressSDNode>
2359 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2360 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002361
Misha Brukman7847fca2005-04-22 17:54:37 +00002362 if(!isBool)
2363 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2364 else { // we are storing a bool, so emit a little pseudocode
2365 // to store a predicate register as one byte
2366 assert(Opc==IA64::ST1);
2367 unsigned dummy3 = MakeReg(MVT::i64);
2368 unsigned dummy4 = MakeReg(MVT::i64);
2369 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2370 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2371 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2372 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2373 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002374 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2375
Misha Brukman7847fca2005-04-22 17:54:37 +00002376 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002377
Misha Brukman7847fca2005-04-22 17:54:37 +00002378 unsigned dummy = MakeReg(MVT::i64);
2379 BuildMI(BB, IA64::MOV, 1, dummy)
2380 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2381 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002382 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002383 Tmp2 = SelectExpr(N.getOperand(2)); //address
2384 if(!isBool)
2385 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2386 else { // we are storing a bool, so emit a little pseudocode
2387 // to store a predicate register as one byte
2388 assert(Opc==IA64::ST1);
2389 unsigned dummy3 = MakeReg(MVT::i64);
2390 unsigned dummy4 = MakeReg(MVT::i64);
2391 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2392 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2393 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2394 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2395 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002396 }
2397 return;
2398 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002399
Chris Lattner16cd04d2005-05-12 23:24:06 +00002400 case ISD::CALLSEQ_START:
2401 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002402 Select(N.getOperand(0));
2403 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002404
Chris Lattner16cd04d2005-05-12 23:24:06 +00002405 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2406 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002407 BuildMI(BB, Opc, 1).addImm(Tmp1);
2408 return;
2409 }
2410
2411 return;
2412 }
2413 assert(0 && "GAME OVER. INSERT COIN?");
2414}
2415
2416
2417/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2418/// into a machine code representation using pattern matching and a machine
2419/// description file.
2420///
2421FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002422 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002423}
2424
2425