blob: f1de338b2c0947e9079ff95b9e725e2ba993211f [file] [log] [blame]
Duraid Madina91ed0a12005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002//
Duraid Madina91ed0a12005-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 Brukman89b8c8d2005-04-21 23:13:11 +00007//
Duraid Madina91ed0a12005-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 Madina81ebb572005-04-26 07:23:02 +000031#include <map>
Duraid Madina91ed0a12005-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 Brukman89b8c8d2005-04-21 23:13:11 +000040
Duraid Madina91ed0a12005-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 Brukman89b8c8d2005-04-21 23:13:11 +000049
Duraid Madina91ed0a12005-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 Brukman89b8c8d2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina91ed0a12005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman89b8c8d2005-04-21 23:13:11 +000058
Chris Lattnera3a135a2005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman371e4952005-08-16 19:49:35 +000060 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Duraid Madina91ed0a12005-03-17 18:17:03 +000061 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
62
Misha Brukman89b8c8d2005-04-21 23:13:11 +000063 setSetCCResultType(MVT::i1);
Duraid Madina91ed0a12005-03-17 18:17:03 +000064 setShiftAmountType(MVT::i64);
65
66 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina91ed0a12005-03-17 18:17:03 +000067
68 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina91ed0a12005-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 Madinaf6b666f2005-04-14 08:37:32 +000073 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina91ed0a12005-03-17 18:17:03 +000074
Chris Lattner0815dcae2005-09-28 22:29:17 +000075 setOperationAction(ISD::FREM , MVT::f32 , Expand);
76 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Duraid Madina91ed0a12005-03-17 18:17:03 +000077
78 setOperationAction(ISD::UREM , MVT::f32 , Expand);
79 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman89b8c8d2005-04-21 23:13:11 +000080
Duraid Madina91ed0a12005-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);
Chris Lattnercd8b4212005-10-07 04:50:48 +000084
85 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
Nate Begemanae5d9bd2005-10-21 01:52:45 +000087
Chris Lattner9c6bbaf2005-04-30 04:26:06 +000088 // We don't support sin/cos/sqrt
89 setOperationAction(ISD::FSIN , MVT::f64, Expand);
90 setOperationAction(ISD::FCOS , MVT::f64, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
92 setOperationAction(ISD::FSIN , MVT::f32, Expand);
93 setOperationAction(ISD::FCOS , MVT::f32, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
95
Andrew Lenharthb8e94c32005-05-04 19:25:37 +000096 //IA64 has these, but they are not implemented
Chris Lattner669c3b12005-05-11 05:03:56 +000097 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
98 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Nate Begemanae5d9bd2005-10-21 01:52:45 +000099 // FIXME: implement mulhs (xma.h) and mulhu (xma.hu)
100 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
101 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +0000102
Chris Lattner93feffb2005-12-01 18:19:53 +0000103 // We don't have line number support yet.
104 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000105 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
106 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner93feffb2005-12-01 18:19:53 +0000107
Duraid Madina91ed0a12005-03-17 18:17:03 +0000108 computeRegisterProperties();
109
110 addLegalFPImmediate(+0.0);
111 addLegalFPImmediate(+1.0);
112 addLegalFPImmediate(-0.0);
113 addLegalFPImmediate(-1.0);
114 }
115
116 /// LowerArguments - This hook must be implemented to indicate how we should
117 /// lower the arguments for the specified function, into the specified DAG.
118 virtual std::vector<SDOperand>
119 LowerArguments(Function &F, SelectionDAG &DAG);
120
121 /// LowerCallTo - This hook lowers an abstract call to a function into an
122 /// actual call.
123 virtual std::pair<SDOperand, SDOperand>
Chris Lattner36674a12005-05-12 19:56:45 +0000124 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000125 bool isTailCall, SDOperand Callee, ArgListTy &Args,
126 SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000127
Chris Lattnera7220852005-07-05 19:58:54 +0000128 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
129 Value *VAListV, SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000130 virtual std::pair<SDOperand,SDOperand>
Chris Lattnera7220852005-07-05 19:58:54 +0000131 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
132 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000133
134 void restoreGP_SP_RP(MachineBasicBlock* BB)
135 {
136 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
137 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
138 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
139 }
140
Duraid Madina21ce5f72005-03-31 12:31:11 +0000141 void restoreSP_RP(MachineBasicBlock* BB)
142 {
143 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
144 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
145 }
146
Duraid Madina91ed0a12005-03-17 18:17:03 +0000147 void restoreRP(MachineBasicBlock* BB)
148 {
149 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
150 }
151
152 void restoreGP(MachineBasicBlock* BB)
153 {
154 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
155 }
156
157 };
158}
159
160
161std::vector<SDOperand>
162IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
163 std::vector<SDOperand> ArgValues;
164
165 //
166 // add beautiful description of IA64 stack frame format
167 // here (from intel 24535803.pdf most likely)
168 //
169 MachineFunction &MF = DAG.getMachineFunction();
170 MachineFrameInfo *MFI = MF.getFrameInfo();
171
172 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
173 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
174 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
175
176 MachineBasicBlock& BB = MF.front();
177
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000178 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000179 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000180
181 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000182 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000183
Duraid Madina91ed0a12005-03-17 18:17:03 +0000184 unsigned argVreg[8];
185 unsigned argPreg[8];
186 unsigned argOpc[8];
187
Duraid Madina21ce5f72005-03-31 12:31:11 +0000188 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000189
Duraid Madina21ce5f72005-03-31 12:31:11 +0000190 unsigned ArgOffset = 0;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000191 int count = 0;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000192
Alkis Evlogimenos1a485612005-03-19 09:22:17 +0000193 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina91ed0a12005-03-17 18:17:03 +0000194 {
195 SDOperand newroot, argt;
196 if(count < 8) { // need to fix this logic? maybe.
Misha Brukmane73e76d2005-04-22 17:54:37 +0000197
198 switch (getValueType(I->getType())) {
199 default:
200 std::cerr << "ERROR in LowerArgs: unknown type "
201 << getValueType(I->getType()) << "\n";
202 abort();
203 case MVT::f32:
204 // fixme? (well, will need to for weird FP structy stuff,
205 // see intel ABI docs)
206 case MVT::f64:
207//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
208 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
209 // floating point args go into f8..f15 as-needed, the increment
210 argVreg[count] = // is below..:
211 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
212 // FP args go into f8..f15 as needed: (hence the ++)
213 argPreg[count] = args_FP[used_FPArgs++];
214 argOpc[count] = IA64::FMOV;
Chris Lattner7c762782005-08-16 21:56:37 +0000215 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattnerd2f2aff2005-08-22 21:33:11 +0000216 MVT::f64);
217 if (I->getType() == Type::FloatTy)
218 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukmane73e76d2005-04-22 17:54:37 +0000219 break;
220 case MVT::i1: // NOTE: as far as C abi stuff goes,
221 // bools are just boring old ints
222 case MVT::i8:
223 case MVT::i16:
224 case MVT::i32:
225 case MVT::i64:
226//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
227 MF.addLiveIn(args_int[count]); // mark this register as liveIn
228 argVreg[count] =
229 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
230 argPreg[count] = args_int[count];
231 argOpc[count] = IA64::MOV;
232 argt = newroot =
Chris Lattner7c762782005-08-16 21:56:37 +0000233 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukmane73e76d2005-04-22 17:54:37 +0000234 if ( getValueType(I->getType()) != MVT::i64)
235 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
236 newroot);
237 break;
238 }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000239 } else { // more than 8 args go into the frame
Misha Brukmane73e76d2005-04-22 17:54:37 +0000240 // Create the frame index object for this incoming parameter...
241 ArgOffset = 16 + 8 * (count - 8);
242 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Misha Brukmane73e76d2005-04-22 17:54:37 +0000244 // Create the SelectionDAG nodes corresponding to a load
245 //from this parameter
246 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
247 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000248 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina91ed0a12005-03-17 18:17:03 +0000249 }
250 ++count;
251 DAG.setRoot(newroot.getValue(1));
252 ArgValues.push_back(argt);
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000253 }
Duraid Madina21ce5f72005-03-31 12:31:11 +0000254
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000255
Duraid Madina21ce5f72005-03-31 12:31:11 +0000256 // Create a vreg to hold the output of (what will become)
257 // the "alloc" instruction
Duraid Madina91ed0a12005-03-17 18:17:03 +0000258 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
259 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
260 // we create a PSEUDO_ALLOC (pseudo)instruction for now
261
262 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
263
264 // hmm:
265 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
266 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
267 // ..hmm.
268
269 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
270
271 // hmm:
272 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
273 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
274 // ..hmm.
275
Duraid Madina21ce5f72005-03-31 12:31:11 +0000276 unsigned tempOffset=0;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000277
Duraid Madina21ce5f72005-03-31 12:31:11 +0000278 // if this is a varargs function, we simply lower llvm.va_start by
279 // pointing to the first entry
280 if(F.isVarArg()) {
281 tempOffset=0;
282 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000283 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000284
Duraid Madina21ce5f72005-03-31 12:31:11 +0000285 // here we actually do the moving of args, and store them to the stack
286 // too if this is a varargs function:
287 for (int i = 0; i < count && i < 8; ++i) {
288 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
289 if(F.isVarArg()) {
290 // if this is a varargs function, we copy the input registers to the stack
291 int FI = MFI->CreateFixedObject(8, tempOffset);
292 tempOffset+=8; //XXX: is it safe to use r22 like this?
293 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
294 // FIXME: we should use st8.spill here, one day
295 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
296 }
297 }
298
Duraid Madinab6dfb222005-04-12 14:54:44 +0000299 // Finally, inform the code generator which regs we return values in.
300 // (see the ISD::RET: case down below)
301 switch (getValueType(F.getReturnType())) {
302 default: assert(0 && "i have no idea where to return this type!");
303 case MVT::isVoid: break;
304 case MVT::i1:
305 case MVT::i8:
306 case MVT::i16:
307 case MVT::i32:
308 case MVT::i64:
309 MF.addLiveOut(IA64::r8);
310 break;
311 case MVT::f32:
312 case MVT::f64:
313 MF.addLiveOut(IA64::F8);
314 break;
315 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000316
Duraid Madina91ed0a12005-03-17 18:17:03 +0000317 return ArgValues;
318}
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000319
Duraid Madina91ed0a12005-03-17 18:17:03 +0000320std::pair<SDOperand, SDOperand>
321IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000322 const Type *RetTy, bool isVarArg,
Chris Lattner2e77db62005-05-13 18:50:42 +0000323 unsigned CallingConv, bool isTailCall,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000324 SDOperand Callee, ArgListTy &Args,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000325 SelectionDAG &DAG) {
Duraid Madina91ed0a12005-03-17 18:17:03 +0000326
327 MachineFunction &MF = DAG.getMachineFunction();
328
Duraid Madina91ed0a12005-03-17 18:17:03 +0000329 unsigned NumBytes = 16;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000330 unsigned outRegsUsed = 0;
331
332 if (Args.size() > 8) {
Duraid Madina91ed0a12005-03-17 18:17:03 +0000333 NumBytes += (Args.size() - 8) * 8;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000334 outRegsUsed = 8;
335 } else {
336 outRegsUsed = Args.size();
337 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000338
Duraid Madina21ce5f72005-03-31 12:31:11 +0000339 // FIXME? this WILL fail if we ever try to pass around an arg that
340 // consumes more than a single output slot (a 'real' double, int128
341 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
342 // registers we use. Hopefully, the assembler will notice.
343 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
344 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000345
Chris Lattner2dce7032005-05-12 23:24:06 +0000346 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000347 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000348
Duraid Madina91ed0a12005-03-17 18:17:03 +0000349 std::vector<SDOperand> args_to_use;
350 for (unsigned i = 0, e = Args.size(); i != e; ++i)
351 {
352 switch (getValueType(Args[i].second)) {
353 default: assert(0 && "unexpected argument type!");
354 case MVT::i1:
355 case MVT::i8:
356 case MVT::i16:
357 case MVT::i32:
Misha Brukmane73e76d2005-04-22 17:54:37 +0000358 //promote to 64-bits, sign/zero extending based on type
359 //of the argument
360 if(Args[i].second->isSigned())
361 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
362 Args[i].first);
363 else
364 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
365 Args[i].first);
366 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000367 case MVT::f32:
Misha Brukmane73e76d2005-04-22 17:54:37 +0000368 //promote to 64-bits
369 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000370 case MVT::f64:
371 case MVT::i64:
372 break;
373 }
374 args_to_use.push_back(Args[i].first);
375 }
376
377 std::vector<MVT::ValueType> RetVals;
378 MVT::ValueType RetTyVT = getValueType(RetTy);
379 if (RetTyVT != MVT::isVoid)
380 RetVals.push_back(RetTyVT);
381 RetVals.push_back(MVT::Other);
382
383 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000384 Callee, args_to_use), 0);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000385 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000386 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000387 DAG.getConstant(NumBytes, getPointerTy()));
388 return std::make_pair(TheCall, Chain);
389}
390
Chris Lattnera7220852005-07-05 19:58:54 +0000391SDOperand
392IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
393 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000394 // vastart just stores the address of the VarArgsFrameIndex slot.
395 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnera7220852005-07-05 19:58:54 +0000396 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
397 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina91ed0a12005-03-17 18:17:03 +0000398}
399
400std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnera7220852005-07-05 19:58:54 +0000401LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
402 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madina21ce5f72005-03-31 12:31:11 +0000403
404 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnera7220852005-07-05 19:58:54 +0000405 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
406 VAListP, DAG.getSrcValue(VAListV));
407 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
408 DAG.getSrcValue(NULL));
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000409 unsigned Amt;
410 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
411 Amt = 8;
412 else {
413 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
414 "Other types should have been promoted for varargs!");
415 Amt = 8;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000416 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000417 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000418 DAG.getConstant(Amt, Val.getValueType()));
419 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera7220852005-07-05 19:58:54 +0000420 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madina21ce5f72005-03-31 12:31:11 +0000421 return std::make_pair(Result, Chain);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000422}
Duraid Madina91ed0a12005-03-17 18:17:03 +0000423
Duraid Madina91ed0a12005-03-17 18:17:03 +0000424namespace {
425
426 //===--------------------------------------------------------------------===//
427 /// ISel - IA64 specific code to select IA64 machine instructions for
428 /// SelectionDAG operations.
429 ///
430 class ISel : public SelectionDAGISel {
431 /// IA64Lowering - This object fully describes how to lower LLVM code to an
432 /// IA64-specific SelectionDAG.
433 IA64TargetLowering IA64Lowering;
Duraid Madina81ebb572005-04-26 07:23:02 +0000434 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
435 // for sdiv and udiv until it is put into the future
436 // dag combiner
Duraid Madina91ed0a12005-03-17 18:17:03 +0000437
438 /// ExprMap - As shared expressions are codegen'd, we keep track of which
439 /// vreg the value is produced in, so we only emit one copy of each compiled
440 /// tree.
441 std::map<SDOperand, unsigned> ExprMap;
442 std::set<SDOperand> LoweredTokens;
443
444 public:
Duraid Madina81ebb572005-04-26 07:23:02 +0000445 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
446 ISelDAG(0) { }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000447
448 /// InstructionSelectBasicBlock - This callback is invoked by
449 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
450 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
451
Duraid Madina91ed0a12005-03-17 18:17:03 +0000452 unsigned SelectExpr(SDOperand N);
453 void Select(SDOperand N);
Duraid Madina81ebb572005-04-26 07:23:02 +0000454 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
455 SDOperand BuildConstmulSequence(SDOperand N);
456
Chris Lattner9d465182005-08-22 18:28:09 +0000457 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000458 };
459}
460
461/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
462/// when it has created a SelectionDAG for us to codegen.
463void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
464
465 // Codegen the basic block.
Duraid Madina81ebb572005-04-26 07:23:02 +0000466 ISelDAG = &DAG;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000467 Select(DAG.getRoot());
468
469 // Clear state used for selection.
470 ExprMap.clear();
471 LoweredTokens.clear();
Duraid Madina81ebb572005-04-26 07:23:02 +0000472 ISelDAG = 0;
473}
474
Duraid Madina81ebb572005-04-26 07:23:02 +0000475// strip leading '0' characters from a string
476void munchLeadingZeros(std::string& inString) {
477 while(inString.c_str()[0]=='0') {
478 inString.erase(0, 1);
479 }
480}
481
482// strip trailing '0' characters from a string
483void munchTrailingZeros(std::string& inString) {
484 int curPos=inString.length()-1;
485
486 while(inString.c_str()[curPos]=='0') {
487 inString.erase(curPos, 1);
488 curPos--;
489 }
490}
491
492// return how many consecutive '0' characters are at the end of a string
493unsigned int countTrailingZeros(std::string& inString) {
494 int curPos=inString.length()-1;
495 unsigned int zeroCount=0;
496 // assert goes here
497 while(inString.c_str()[curPos--]=='0') {
498 zeroCount++;
499 }
500 return zeroCount;
501}
502
503// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
504// '0' and 'N' (-1) characters)
505void boothEncode(std::string inString, std::string& boothEncodedString) {
506
507 int curpos=0;
508 int replacements=0;
509 int lim=inString.size();
510
511 while(curpos<lim) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000512 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madina81ebb572005-04-26 07:23:02 +0000513 int runlength=0;
514 std::string replaceString="N";
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000515
Duraid Madina81ebb572005-04-26 07:23:02 +0000516 // find the run length
517 for(;inString[curpos+runlength]=='1';runlength++) ;
518
519 for(int i=0; i<runlength-1; i++)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000520 replaceString+="0";
Duraid Madina81ebb572005-04-26 07:23:02 +0000521 replaceString+="1";
522
523 if(runlength>1) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000524 inString.replace(curpos, runlength+1, replaceString);
525 curpos+=runlength-1;
Duraid Madina81ebb572005-04-26 07:23:02 +0000526 } else
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000527 curpos++;
Duraid Madina81ebb572005-04-26 07:23:02 +0000528 } else { // a zero, we just keep chugging along
529 curpos++;
530 }
531 }
532
533 // clean up (trim the string, reverse it and turn '1's into 'P's)
534 munchTrailingZeros(inString);
535 boothEncodedString="";
536
537 for(int i=inString.size()-1;i>=0;i--)
538 if(inString[i]=='1')
539 boothEncodedString+="P";
540 else
541 boothEncodedString+=inString[i];
542
543}
544
545struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
546 unsigned firstVal; // A
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000547 unsigned firstShift; // B
Duraid Madina81ebb572005-04-26 07:23:02 +0000548 unsigned secondVal; // C
549 unsigned secondShift; // D
550 bool isSub;
551};
552
553/* this implements Lefevre's "pattern-based" constant multiplication,
554 * see "Multiplication by an Integer Constant", INRIA report 1999-06
555 *
556 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
557 * to get better booth encodings - this does help in practice
558 * TODO: weight shifts appropriately (most architectures can't
559 * fuse a shift and an add for arbitrary shift amounts) */
560unsigned lefevre(const std::string inString,
561 std::vector<struct shiftaddblob> &ops) {
562 std::string retstring;
563 std::string s = inString;
564 munchTrailingZeros(s);
565
566 int length=s.length()-1;
567
568 if(length==0) {
569 return(0);
570 }
571
572 std::vector<int> p,n;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000573
Duraid Madina81ebb572005-04-26 07:23:02 +0000574 for(int i=0; i<=length; i++) {
575 if (s.c_str()[length-i]=='P') {
576 p.push_back(i);
577 } else if (s.c_str()[length-i]=='N') {
578 n.push_back(i);
579 }
580 }
581
582 std::string t, u;
Chris Lattnerdaae1e12005-08-25 00:03:21 +0000583 int c = 0;
Duraid Madinae06ef802005-04-26 09:42:50 +0000584 bool f;
Duraid Madina81ebb572005-04-26 07:23:02 +0000585 std::map<const int, int> w;
586
Duraid Madinab071bef2005-04-27 11:57:39 +0000587 for(unsigned i=0; i<p.size(); i++) {
588 for(unsigned j=0; j<i; j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000589 w[p[i]-p[j]]++;
590 }
591 }
592
Duraid Madinab071bef2005-04-27 11:57:39 +0000593 for(unsigned i=1; i<n.size(); i++) {
594 for(unsigned j=0; j<i; j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000595 w[n[i]-n[j]]++;
596 }
597 }
598
Duraid Madinab071bef2005-04-27 11:57:39 +0000599 for(unsigned i=0; i<p.size(); i++) {
600 for(unsigned j=0; j<n.size(); j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000601 w[-abs(p[i]-n[j])]++;
602 }
603 }
604
605 std::map<const int, int>::const_iterator ii;
606 std::vector<int> d;
607 std::multimap<int, int> sorted_by_value;
608
609 for(ii = w.begin(); ii!=w.end(); ii++)
610 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
611
612 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
613 it != sorted_by_value.end(); ++it) {
614 d.push_back((*it).second);
615 }
616
617 int int_W=0;
618 int int_d;
619
620 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
621 d.pop_back();
622 retstring=s; // hmmm
623 int x=0;
624 int z=abs(int_d)-1;
625
626 if(int_d>0) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000627
Duraid Madinab071bef2005-04-27 11:57:39 +0000628 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000629 if( ((base+z+1) < retstring.size()) &&
630 retstring.c_str()[base]=='P' &&
631 retstring.c_str()[base+z+1]=='P')
632 {
633 // match
634 x++;
635 retstring.replace(base, 1, "0");
636 retstring.replace(base+z+1, 1, "p");
637 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000638 }
639
Duraid Madinab071bef2005-04-27 11:57:39 +0000640 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000641 if( ((base+z+1) < retstring.size()) &&
642 retstring.c_str()[base]=='N' &&
643 retstring.c_str()[base+z+1]=='N')
644 {
645 // match
646 x++;
647 retstring.replace(base, 1, "0");
648 retstring.replace(base+z+1, 1, "n");
649 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000650 }
651
652 } else {
Duraid Madinab071bef2005-04-27 11:57:39 +0000653 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000654 if( ((base+z+1) < retstring.size()) &&
655 ((retstring.c_str()[base]=='P' &&
656 retstring.c_str()[base+z+1]=='N') ||
657 (retstring.c_str()[base]=='N' &&
658 retstring.c_str()[base+z+1]=='P')) ) {
659 // match
660 x++;
661
662 if(retstring.c_str()[base]=='P') {
663 retstring.replace(base, 1, "0");
664 retstring.replace(base+z+1, 1, "p");
665 } else { // retstring[base]=='N'
666 retstring.replace(base, 1, "0");
667 retstring.replace(base+z+1, 1, "n");
668 }
669 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000670 }
671 }
672
673 if(x>int_W) {
674 int_W = x;
675 t = retstring;
676 c = int_d; // tofix
677 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000678
Duraid Madina81ebb572005-04-26 07:23:02 +0000679 } d.pop_back(); // hmm
680
681 u = t;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000682
Duraid Madinab071bef2005-04-27 11:57:39 +0000683 for(unsigned i=0; i<t.length(); i++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000684 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
685 t.replace(i, 1, "0");
686 }
687
Duraid Madinab071bef2005-04-27 11:57:39 +0000688 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina74a01fa2005-05-09 13:18:34 +0000689 if(u[i]=='P' || u[i]=='N')
Duraid Madina81ebb572005-04-26 07:23:02 +0000690 u.replace(i, 1, "0");
Duraid Madina74a01fa2005-05-09 13:18:34 +0000691 if(u[i]=='p')
Duraid Madina81ebb572005-04-26 07:23:02 +0000692 u.replace(i, 1, "P");
Duraid Madina74a01fa2005-05-09 13:18:34 +0000693 if(u[i]=='n')
Duraid Madina81ebb572005-04-26 07:23:02 +0000694 u.replace(i, 1, "N");
695 }
696
697 if( c<0 ) {
Duraid Madinae06ef802005-04-26 09:42:50 +0000698 f=true;
Duraid Madina81ebb572005-04-26 07:23:02 +0000699 c=-c;
700 } else
Duraid Madinae06ef802005-04-26 09:42:50 +0000701 f=false;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000702
Duraid Madina74a01fa2005-05-09 13:18:34 +0000703 int pos=0;
704 while(u[pos]=='0')
705 pos++;
706
707 bool hit=(u[pos]=='N');
Duraid Madina81ebb572005-04-26 07:23:02 +0000708
709 int g=0;
710 if(hit) {
711 g=1;
Duraid Madinab071bef2005-04-27 11:57:39 +0000712 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina74a01fa2005-05-09 13:18:34 +0000713 bool isP=(u[p]=='P');
714 bool isN=(u[p]=='N');
Duraid Madina81ebb572005-04-26 07:23:02 +0000715
716 if(isP)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000717 u.replace(p, 1, "N");
Duraid Madina81ebb572005-04-26 07:23:02 +0000718 if(isN)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000719 u.replace(p, 1, "P");
Duraid Madina81ebb572005-04-26 07:23:02 +0000720 }
721 }
722
723 munchLeadingZeros(u);
724
725 int i = lefevre(u, ops);
726
727 shiftaddblob blob;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000728
Duraid Madina81ebb572005-04-26 07:23:02 +0000729 blob.firstVal=i; blob.firstShift=c;
730 blob.isSub=f;
731 blob.secondVal=i; blob.secondShift=0;
732
733 ops.push_back(blob);
734
735 i = ops.size();
736
737 munchLeadingZeros(t);
738
739 if(t.length()==0)
740 return i;
741
742 if(t.c_str()[0]!='P') {
743 g=2;
Duraid Madinab071bef2005-04-27 11:57:39 +0000744 for(unsigned p=0; p<t.length(); p++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000745 bool isP=(t.c_str()[p]=='P');
746 bool isN=(t.c_str()[p]=='N');
747
748 if(isP)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000749 t.replace(p, 1, "N");
Duraid Madina81ebb572005-04-26 07:23:02 +0000750 if(isN)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000751 t.replace(p, 1, "P");
Duraid Madina81ebb572005-04-26 07:23:02 +0000752 }
753 }
754
755 int j = lefevre(t, ops);
756
757 int trail=countTrailingZeros(u);
758 blob.secondVal=i; blob.secondShift=trail;
759
760 trail=countTrailingZeros(t);
761 blob.firstVal=j; blob.firstShift=trail;
762
763 switch(g) {
764 case 0:
765 blob.isSub=false; // first + second
766 break;
767 case 1:
768 blob.isSub=true; // first - second
769 break;
770 case 2:
771 blob.isSub=true; // second - first
772 int tmpval, tmpshift;
773 tmpval=blob.firstVal;
774 tmpshift=blob.firstShift;
775 blob.firstVal=blob.secondVal;
776 blob.firstShift=blob.secondShift;
777 blob.secondVal=tmpval;
778 blob.secondShift=tmpshift;
779 break;
780 //assert
781 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000782
Duraid Madina81ebb572005-04-26 07:23:02 +0000783 ops.push_back(blob);
784 return ops.size();
785}
786
787SDOperand ISel::BuildConstmulSequence(SDOperand N) {
788 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
789 // in particular, *3 is nicer as *2+1, not *4-1
790 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
791
792 bool flippedSign;
793 unsigned preliminaryShift=0;
794
Duraid Madina7acd5d52005-05-02 07:27:14 +0000795 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madina81ebb572005-04-26 07:23:02 +0000796
797 // first, we make the constant to multiply by positive
798 if(constant<0) {
799 constant=-constant;
800 flippedSign=true;
801 } else {
802 flippedSign=false;
803 }
804
805 // next, we make it odd.
806 for(; (constant%2==0); preliminaryShift++)
807 constant>>=1;
808
809 //OK, we have a positive, odd number of 64 bits or less. Convert it
810 //to a binary string, constantString[0] is the LSB
811 char constantString[65];
812 for(int i=0; i<64; i++)
813 constantString[i]='0'+((constant>>i)&0x1);
814 constantString[64]=0;
815
816 // now, Booth encode it
817 std::string boothEncodedString;
818 boothEncode(constantString, boothEncodedString);
819
820 std::vector<struct shiftaddblob> ops;
821 // do the transformation, filling out 'ops'
822 lefevre(boothEncodedString, ops);
823
Duraid Madina9f406cb2005-05-15 14:44:13 +0000824 assert(ops.size() < 80 && "constmul code has gone haywire\n");
825 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000826
Duraid Madina81ebb572005-04-26 07:23:02 +0000827 // now turn 'ops' into DAG bits
Duraid Madinab071bef2005-04-27 11:57:39 +0000828 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000829 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
830 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
831 results[ops[i].firstVal-1];
832 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
833 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
834 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
835 results[ops[i].secondVal-1];
836 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
837 if(ops[i].isSub)
838 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
839 else
840 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
841 }
842
843 // don't forget flippedSign and preliminaryShift!
Duraid Madina7acd5d52005-05-02 07:27:14 +0000844 SDOperand shiftedresult;
Duraid Madina81ebb572005-04-26 07:23:02 +0000845 if(preliminaryShift) {
846 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina7acd5d52005-05-02 07:27:14 +0000847 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000848 results[ops.size()-1], finalshift);
Duraid Madina81ebb572005-04-26 07:23:02 +0000849 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina7acd5d52005-05-02 07:27:14 +0000850 shiftedresult = results[ops.size()-1];
Duraid Madina81ebb572005-04-26 07:23:02 +0000851 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000852
Duraid Madina7acd5d52005-05-02 07:27:14 +0000853 SDOperand finalresult;
854 if(flippedSign) { // if we were multiplying by a negative constant:
855 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
856 // subtract the result from 0 to flip its sign
857 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
858 } else { // there was no preliminary multiply by -1 required
859 finalresult = shiftedresult;
860 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000861
862 return finalresult;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000863}
864
Duraid Madinac36b6c32005-04-06 09:55:17 +0000865/// ponderIntegerDivisionBy - When handling integer divides, if the divide
866/// is by a constant such that we can efficiently codegen it, this
867/// function says what to do. Currently, it returns 0 if the division must
868/// become a genuine divide, and 1 if the division can be turned into a
869/// right shift.
870static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
871 unsigned& Imm) {
872 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
873 // a constant, give up.
874
875 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
876
Chris Lattner6667bdb2005-08-02 19:26:06 +0000877 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
878 Imm = Log2_64(v);
Duraid Madinac36b6c32005-04-06 09:55:17 +0000879 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000880 }
881
Duraid Madinac36b6c32005-04-06 09:55:17 +0000882 return 0; // fallthrough
883}
884
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000885static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
886 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
887 // a constant, give up.
888
889 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
890
Chris Lattner6667bdb2005-08-02 19:26:06 +0000891 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskey9b0a2752005-08-20 11:05:23 +0000892 Imm = Log2_64(v) + 1;
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000893 return 1; // say so
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000894 }
895
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000896 return 0; // fallthrough
897}
898
Duraid Madina8419da82005-04-07 12:33:38 +0000899static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
900 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
901 // constant, give up.
902 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
903
904 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
905 Imm = v & 0x3FFF; // 14 bits
906 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000907 }
Duraid Madina8419da82005-04-07 12:33:38 +0000908 return 0; // fallthrough
909}
910
911static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
912 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
913 // constant, give up.
914 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
915
916 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
917 Imm = v & 0xFF; // 8 bits
918 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000919 }
Duraid Madina8419da82005-04-07 12:33:38 +0000920 return 0; // fallthrough
921}
922
Duraid Madina91ed0a12005-03-17 18:17:03 +0000923unsigned ISel::SelectExpr(SDOperand N) {
924 unsigned Result;
925 unsigned Tmp1, Tmp2, Tmp3;
926 unsigned Opc = 0;
927 MVT::ValueType DestType = N.getValueType();
928
929 unsigned opcode = N.getOpcode();
930
931 SDNode *Node = N.Val;
932 SDOperand Op0, Op1;
933
934 if (Node->getOpcode() == ISD::CopyFromReg)
935 // Just use the specified register as our input.
Chris Lattner7c762782005-08-16 21:56:37 +0000936 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000937
Duraid Madina91ed0a12005-03-17 18:17:03 +0000938 unsigned &Reg = ExprMap[N];
939 if (Reg) return Reg;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000940
Chris Lattner8abab9b2005-05-13 20:29:26 +0000941 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina91ed0a12005-03-17 18:17:03 +0000942 Reg = Result = (N.getValueType() != MVT::Other) ?
943 MakeReg(N.getValueType()) : 1;
944 else {
945 // If this is a call instruction, make sure to prepare ALL of the result
946 // values as well as the chain.
947 if (Node->getNumValues() == 1)
948 Reg = Result = 1; // Void call, just a chain.
949 else {
950 Result = MakeReg(Node->getValueType(0));
951 ExprMap[N.getValue(0)] = Result;
952 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
953 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
954 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
955 }
956 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000957
Duraid Madina91ed0a12005-03-17 18:17:03 +0000958 switch (N.getOpcode()) {
959 default:
960 Node->dump();
961 assert(0 && "Node not handled!\n");
962
963 case ISD::FrameIndex: {
964 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
965 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
966 return Result;
967 }
968
969 case ISD::ConstantPool: {
Chris Lattnerc30405e2005-08-26 17:15:30 +0000970 Tmp1 = BB->getParent()->getConstantPool()->
971 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina91ed0a12005-03-17 18:17:03 +0000972 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
973 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
974 .addReg(IA64::r1);
975 return Result;
976 }
977
978 case ISD::ConstantFP: {
979 Tmp1 = Result; // Intermediate Register
980 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
981 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
982 Tmp1 = MakeReg(MVT::f64);
983
984 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
985 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
986 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
987 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
988 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
989 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
990 else
991 assert(0 && "Unexpected FP constant!");
992 if (Tmp1 != Result)
993 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
994 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukmane73e76d2005-04-22 17:54:37 +0000995 .addReg(IA64::F0);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000996 return Result;
997 }
998
999 case ISD::DYNAMIC_STACKALLOC: {
1000 // Generate both result values.
1001 if (Result != 1)
1002 ExprMap[N.getValue(1)] = 1; // Generate the token
1003 else
1004 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1005
1006 // FIXME: We are currently ignoring the requested alignment for handling
1007 // greater than the stack alignment. This will need to be revisited at some
1008 // point. Align = N.getOperand(2);
1009
1010 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1011 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1012 std::cerr << "Cannot allocate stack object with greater alignment than"
1013 << " the stack alignment yet!";
1014 abort();
1015 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001016
1017/*
Duraid Madina91ed0a12005-03-17 18:17:03 +00001018 Select(N.getOperand(0));
1019 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1020 {
1021 if (CN->getValue() < 32000)
1022 {
1023 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001024 .addImm(-CN->getValue());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001025 } else {
1026 Tmp1 = SelectExpr(N.getOperand(1));
1027 // Subtract size from stack pointer, thereby allocating some space.
1028 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1029 }
1030 } else {
1031 Tmp1 = SelectExpr(N.getOperand(1));
1032 // Subtract size from stack pointer, thereby allocating some space.
1033 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1034 }
Duraid Madina21ce5f72005-03-31 12:31:11 +00001035*/
1036 Select(N.getOperand(0));
1037 Tmp1 = SelectExpr(N.getOperand(1));
1038 // Subtract size from stack pointer, thereby allocating some space.
1039 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001040 // Put a pointer to the space into the result register, by copying the
1041 // stack pointer.
1042 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1043 return Result;
1044 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001045
Duraid Madina91ed0a12005-03-17 18:17:03 +00001046 case ISD::SELECT: {
1047 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1048 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1049 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1050
Duraid Madina91ed0a12005-03-17 18:17:03 +00001051 unsigned bogoResult;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001052
Duraid Madina91ed0a12005-03-17 18:17:03 +00001053 switch (N.getOperand(1).getValueType()) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001054 default: assert(0 &&
Duraid Madina0e731882005-05-02 06:41:13 +00001055 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1056 // for i1, we load the condition into an integer register, then
1057 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1058 // of them will go through, since the integer register will hold
1059 // either 0 or 1)
1060 case MVT::i1: {
1061 bogoResult=MakeReg(MVT::i1);
1062
1063 // load the condition into an integer register
1064 unsigned condReg=MakeReg(MVT::i64);
1065 unsigned dummy=MakeReg(MVT::i64);
1066 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1067 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1068 .addImm(1).addReg(Tmp1);
1069
1070 // initialize Result (bool) to false (hence UNC) and if
1071 // the select condition (condReg) is false (0), copy Tmp3
1072 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1073 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1074
1075 // now, if the selection condition is true, write 1 to the
1076 // result if Tmp2 is 1
1077 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1078 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1079 break;
1080 }
1081 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1082 // with Tmp2 if Tmp1 is true
Misha Brukmane73e76d2005-04-22 17:54:37 +00001083 case MVT::i64:
1084 bogoResult=MakeReg(MVT::i64);
Duraid Madina0e731882005-05-02 06:41:13 +00001085 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1086 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1087 .addReg(Tmp1);
Misha Brukmane73e76d2005-04-22 17:54:37 +00001088 break;
1089 case MVT::f64:
1090 bogoResult=MakeReg(MVT::f64);
Duraid Madina0e731882005-05-02 06:41:13 +00001091 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1092 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1093 .addReg(Tmp1);
Misha Brukmane73e76d2005-04-22 17:54:37 +00001094 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001095 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001096
Duraid Madina91ed0a12005-03-17 18:17:03 +00001097 return Result;
1098 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001099
Duraid Madina91ed0a12005-03-17 18:17:03 +00001100 case ISD::Constant: {
1101 unsigned depositPos=0;
1102 unsigned depositLen=0;
1103 switch (N.getValueType()) {
1104 default: assert(0 && "Cannot use constants of this type!");
1105 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukmane73e76d2005-04-22 17:54:37 +00001106 // the constant:
1107 if(cast<ConstantSDNode>(N)->getValue()) // true:
1108 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1109 else // false:
1110 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1111 return Result; // early exit
1112 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001113 case MVT::i64: break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001114 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001115
Duraid Madina91ed0a12005-03-17 18:17:03 +00001116 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madinafb43ef72005-04-11 05:55:56 +00001117
1118 if(immediate==0) { // if the constant is just zero,
1119 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1120 return Result; // early exit
Duraid Madina91ed0a12005-03-17 18:17:03 +00001121 }
1122
Duraid Madinafb43ef72005-04-11 05:55:56 +00001123 if (immediate <= 8191 && immediate >= -8192) {
1124 // if this constants fits in 14 bits, we use a mov the assembler will
1125 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1126 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1127 return Result; // early exit
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001128 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001129
1130 if (immediate <= 2097151 && immediate >= -2097152) {
1131 // if this constants fits in 22 bits, we use a mov the assembler will
1132 // turn into: "addl rDest=imm,r0"
1133 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1134 return Result; // early exit
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001135 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001136
1137 /* otherwise, our immediate is big, so we use movl */
1138 uint64_t Imm = immediate;
Duraid Madina8de7ac02005-04-11 07:16:39 +00001139 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madinafb43ef72005-04-11 05:55:56 +00001140 return Result;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001141 }
Duraid Madinac935c832005-04-02 10:33:53 +00001142
1143 case ISD::UNDEF: {
1144 BuildMI(BB, IA64::IDEF, 0, Result);
1145 return Result;
1146 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001147
Duraid Madina91ed0a12005-03-17 18:17:03 +00001148 case ISD::GlobalAddress: {
1149 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1150 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001151
Duraid Madina91ed0a12005-03-17 18:17:03 +00001152 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001153 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001154
Duraid Madina91ed0a12005-03-17 18:17:03 +00001155 return Result;
1156 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001157
Duraid Madina91ed0a12005-03-17 18:17:03 +00001158 case ISD::ExternalSymbol: {
1159 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madina21ce5f72005-03-31 12:31:11 +00001160// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1161 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina91ed0a12005-03-17 18:17:03 +00001162 return Result;
1163 }
1164
1165 case ISD::FP_EXTEND: {
1166 Tmp1 = SelectExpr(N.getOperand(0));
1167 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1168 return Result;
1169 }
1170
Chris Lattner2493f0e2005-09-02 00:15:30 +00001171 case ISD::ANY_EXTEND:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001172 case ISD::ZERO_EXTEND: {
1173 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001174
Chris Lattner2493f0e2005-09-02 00:15:30 +00001175 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1176 "Cannot zero-extend this type!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001177
Chris Lattner2493f0e2005-09-02 00:15:30 +00001178 // if the predicate reg has 1, we want a '1' in our GR.
1179 unsigned dummy = MakeReg(MVT::i64);
1180 // first load zero:
1181 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1182 // ...then conditionally (PR:Tmp1) add 1:
1183 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1184 .addImm(1).addReg(Tmp1);
1185 return Result; // XXX early exit!
1186 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001187
Chris Lattner2493f0e2005-09-02 00:15:30 +00001188 case ISD::SIGN_EXTEND:
1189 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1190 "Cannot zero-extend this type!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001191
1192 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner2493f0e2005-09-02 00:15:30 +00001193 assert(0 && "don't know how to sign_extend from bool yet!");
1194 abort();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001195
Chris Lattner2493f0e2005-09-02 00:15:30 +00001196 case ISD::TRUNCATE:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001197 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1198 // of R0 appropriately.
Chris Lattner2493f0e2005-09-02 00:15:30 +00001199 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1200 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001201 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina91ed0a12005-03-17 18:17:03 +00001202
Chris Lattner2493f0e2005-09-02 00:15:30 +00001203 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1204 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1205 return Result; // XXX early exit!
Duraid Madina91ed0a12005-03-17 18:17:03 +00001206
Misha Brukmane73e76d2005-04-22 17:54:37 +00001207/*
Duraid Madina91ed0a12005-03-17 18:17:03 +00001208 case ISD::FP_ROUND: {
1209 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001210 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001211 Tmp1 = SelectExpr(N.getOperand(0));
1212 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1213 // we add 0.0 using a single precision add to do rounding
1214 return Result;
1215 }
1216*/
1217
1218// FIXME: the following 4 cases need cleaning
1219 case ISD::SINT_TO_FP: {
1220 Tmp1 = SelectExpr(N.getOperand(0));
1221 Tmp2 = MakeReg(MVT::f64);
1222 unsigned dummy = MakeReg(MVT::f64);
1223 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1224 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1225 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1226 return Result;
1227 }
1228
1229 case ISD::UINT_TO_FP: {
1230 Tmp1 = SelectExpr(N.getOperand(0));
1231 Tmp2 = MakeReg(MVT::f64);
1232 unsigned dummy = MakeReg(MVT::f64);
1233 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1234 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1235 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1236 return Result;
1237 }
1238
1239 case ISD::FP_TO_SINT: {
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = MakeReg(MVT::f64);
1242 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1243 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1244 return Result;
1245 }
1246
1247 case ISD::FP_TO_UINT: {
1248 Tmp1 = SelectExpr(N.getOperand(0));
1249 Tmp2 = MakeReg(MVT::f64);
1250 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1251 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1252 return Result;
1253 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001254
1255 case ISD::FADD: {
1256 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1257 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1258 // into an fma, do so:
1259 // ++FusedFP; // Statistic
Duraid Madinac36b6c32005-04-06 09:55:17 +00001260 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1261 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1262 Tmp3 = SelectExpr(N.getOperand(1));
1263 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1264 return Result; // early exit
1265 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001266
1267 //else, fallthrough:
1268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = SelectExpr(N.getOperand(1));
1270 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1271 return Result;
1272 }
Duraid Madina2f231252005-04-13 06:12:04 +00001273
Chris Lattner0815dcae2005-09-28 22:29:17 +00001274 case ISD::ADD: {
1275 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001276 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madina2f231252005-04-13 06:12:04 +00001277 // this add into a shladd, try:
1278 ConstantSDNode *CSD = NULL;
1279 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001280 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madina2f231252005-04-13 06:12:04 +00001281
Misha Brukmane73e76d2005-04-22 17:54:37 +00001282 // ++FusedSHLADD; // Statistic
1283 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1284 int shl_amt = CSD->getValue();
1285 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001286
Misha Brukmane73e76d2005-04-22 17:54:37 +00001287 BuildMI(BB, IA64::SHLADD, 3, Result)
1288 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1289 return Result; // early exit
Duraid Madina2f231252005-04-13 06:12:04 +00001290 }
1291 }
1292
1293 //else, fallthrough:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001294 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001295 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1296 case 1: // adding a constant that's 14 bits
1297 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1298 return Result; // early exit
1299 } // fallthrough and emit a reg+reg ADD:
1300 Tmp2 = SelectExpr(N.getOperand(1));
1301 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001302 return Result;
1303 }
1304
Chris Lattner0815dcae2005-09-28 22:29:17 +00001305 case ISD::FMUL:
1306 Tmp1 = SelectExpr(N.getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(1));
1308 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 return Result;
1310
Duraid Madina91ed0a12005-03-17 18:17:03 +00001311 case ISD::MUL: {
Duraid Madinac36b6c32005-04-06 09:55:17 +00001312
Chris Lattner0815dcae2005-09-28 22:29:17 +00001313 // TODO: speed!
Duraid Madina1c2f9fd2005-08-10 12:38:57 +00001314/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1315 */
Chris Lattner0815dcae2005-09-28 22:29:17 +00001316 // boring old integer multiply with xma
Duraid Madina81ebb572005-04-26 07:23:02 +00001317 Tmp1 = SelectExpr(N.getOperand(0));
1318 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001319
1320 unsigned TempFR1=MakeReg(MVT::f64);
1321 unsigned TempFR2=MakeReg(MVT::f64);
1322 unsigned TempFR3=MakeReg(MVT::f64);
1323 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1324 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1325 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1326 .addReg(IA64::F0);
1327 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1328 return Result; // early exit
1329 /* FIXME } else { // we are multiplying by an integer constant! yay
1330 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1331 } */
Duraid Madina91ed0a12005-03-17 18:17:03 +00001332 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001333
Chris Lattner0815dcae2005-09-28 22:29:17 +00001334 case ISD::FSUB:
1335 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madinac36b6c32005-04-06 09:55:17 +00001336 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1337 // into an fms, do so:
Chris Lattner0815dcae2005-09-28 22:29:17 +00001338 // ++FusedFP; // Statistic
Duraid Madinac36b6c32005-04-06 09:55:17 +00001339 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1340 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1341 Tmp3 = SelectExpr(N.getOperand(1));
1342 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1343 return Result; // early exit
1344 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001345
Duraid Madina91ed0a12005-03-17 18:17:03 +00001346 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001347 Tmp1 = SelectExpr(N.getOperand(0));
1348 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1349 return Result;
1350
1351 case ISD::SUB: {
1352 Tmp2 = SelectExpr(N.getOperand(1));
1353 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1354 case 1: // subtracting *from* an 8 bit constant:
1355 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1356 return Result; // early exit
1357 } // fallthrough and emit a reg+reg SUB:
1358 Tmp1 = SelectExpr(N.getOperand(0));
1359 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001360 return Result;
1361 }
Duraid Madina0720dc12005-04-02 05:18:38 +00001362
1363 case ISD::FABS: {
1364 Tmp1 = SelectExpr(N.getOperand(0));
1365 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1366 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1367 return Result;
1368 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001369
Duraid Madina0720dc12005-04-02 05:18:38 +00001370 case ISD::FNEG: {
Duraid Madina0720dc12005-04-02 05:18:38 +00001371 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madinac935c832005-04-02 10:33:53 +00001372
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001373 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madinac935c832005-04-02 10:33:53 +00001374 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1375 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1376 } else {
1377 Tmp1 = SelectExpr(N.getOperand(0));
1378 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1379 }
1380
Duraid Madina0720dc12005-04-02 05:18:38 +00001381 return Result;
1382 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001383
Duraid Madina91ed0a12005-03-17 18:17:03 +00001384 case ISD::AND: {
1385 switch (N.getValueType()) {
1386 default: assert(0 && "Cannot AND this type!");
1387 case MVT::i1: { // if a bool, we emit a pseudocode AND
1388 unsigned pA = SelectExpr(N.getOperand(0));
1389 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001390
Duraid Madina91ed0a12005-03-17 18:17:03 +00001391/* our pseudocode for AND is:
1392 *
1393(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1394 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1395 ;;
1396(pB) cmp.ne pTemp,p0 = r0,r0
1397 ;;
1398(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1399
1400*/
1401 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001402
Duraid Madina91ed0a12005-03-17 18:17:03 +00001403 unsigned bogusTemp1 = MakeReg(MVT::i1);
1404 unsigned bogusTemp2 = MakeReg(MVT::i1);
1405 unsigned bogusTemp3 = MakeReg(MVT::i1);
1406 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001407
Duraid Madina91ed0a12005-03-17 18:17:03 +00001408 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001409 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001410 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001411 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001412 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001413 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001414 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001415 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001416 break;
1417 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001418
Duraid Madina91ed0a12005-03-17 18:17:03 +00001419 // if not a bool, we just AND away:
1420 case MVT::i8:
1421 case MVT::i16:
1422 case MVT::i32:
1423 case MVT::i64: {
1424 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina3588ea92005-08-21 15:43:53 +00001425 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinae7ef27b2005-04-13 04:50:54 +00001426 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukmane73e76d2005-04-22 17:54:37 +00001427 switch (Tmp3) {
1428 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1429 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1430 break;
1431 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1432 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1433 break;
1434 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1435 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1436 break;
1437 default: // otherwise, use dep.z to paste zeros
Duraid Madina3588ea92005-08-21 15:43:53 +00001438 // FIXME: assert the dep.z is in bounds
1439 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001440 .addImm(0).addImm(Tmp3);
1441 break;
Duraid Madina3588ea92005-08-21 15:43:53 +00001442 }
1443 return Result; // early exit
1444 } // fallthrough and emit a simple AND:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001445 Tmp2 = SelectExpr(N.getOperand(1));
1446 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001447 }
1448 }
1449 return Result;
1450 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001451
Duraid Madina91ed0a12005-03-17 18:17:03 +00001452 case ISD::OR: {
1453 switch (N.getValueType()) {
1454 default: assert(0 && "Cannot OR this type!");
1455 case MVT::i1: { // if a bool, we emit a pseudocode OR
1456 unsigned pA = SelectExpr(N.getOperand(0));
1457 unsigned pB = SelectExpr(N.getOperand(1));
1458
1459 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001460
Duraid Madina91ed0a12005-03-17 18:17:03 +00001461/* our pseudocode for OR is:
1462 *
1463
1464pC = pA OR pB
1465-------------
1466
Misha Brukmane73e76d2005-04-22 17:54:37 +00001467(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1468 ;;
1469(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina91ed0a12005-03-17 18:17:03 +00001470
1471*/
1472 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001473 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madinaf221c262005-10-28 17:46:35 +00001474 BuildMI(BB, IA64::TPCMPEQ, 4, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001475 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001476 break;
1477 }
1478 // if not a bool, we just OR away:
1479 case MVT::i8:
1480 case MVT::i16:
1481 case MVT::i32:
1482 case MVT::i64: {
1483 Tmp1 = SelectExpr(N.getOperand(0));
1484 Tmp2 = SelectExpr(N.getOperand(1));
1485 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1486 break;
1487 }
1488 }
1489 return Result;
1490 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001491
Duraid Madina91ed0a12005-03-17 18:17:03 +00001492 case ISD::XOR: {
1493 switch (N.getValueType()) {
1494 default: assert(0 && "Cannot XOR this type!");
1495 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1496 unsigned pY = SelectExpr(N.getOperand(0));
1497 unsigned pZ = SelectExpr(N.getOperand(1));
1498
1499/* one possible routine for XOR is:
1500
1501 // Compute px = py ^ pz
1502 // using sum of products: px = (py & !pz) | (pz & !py)
1503 // Uses 5 instructions in 3 cycles.
1504 // cycle 1
1505(pz) cmp.eq.unc px = r0, r0 // px = pz
1506(py) cmp.eq.unc pt = r0, r0 // pt = py
1507 ;;
1508 // cycle 2
1509(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1510(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1511 ;;
1512 } { .mmi
1513 // cycle 3
1514(pt) cmp.eq.or px = r0, r0 // px = px | pt
1515
1516*** Another, which we use here, requires one scratch GR. it is:
1517
1518 mov rt = 0 // initialize rt off critical path
1519 ;;
1520
1521 // cycle 1
1522(pz) cmp.eq.unc px = r0, r0 // px = pz
1523(pz) mov rt = 1 // rt = pz
1524 ;;
1525 // cycle 2
1526(py) cmp.ne px = 1, rt // if (py) px = !pz
1527
1528.. these routines kindly provided by Jim Hull
1529*/
1530 unsigned rt = MakeReg(MVT::i64);
1531
1532 // these two temporaries will never actually appear,
1533 // due to the two-address form of some of the instructions below
1534 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1535 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1536
1537 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1538 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001539 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001540 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001541 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001542 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001543 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001544 break;
1545 }
1546 // if not a bool, we just XOR away:
1547 case MVT::i8:
1548 case MVT::i16:
1549 case MVT::i32:
1550 case MVT::i64: {
1551 Tmp1 = SelectExpr(N.getOperand(0));
1552 Tmp2 = SelectExpr(N.getOperand(1));
1553 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1554 break;
1555 }
1556 }
1557 return Result;
1558 }
1559
Duraid Madina25163d82005-05-11 05:16:09 +00001560 case ISD::CTPOP: {
1561 Tmp1 = SelectExpr(N.getOperand(0));
1562 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1563 return Result;
1564 }
1565
Duraid Madina91ed0a12005-03-17 18:17:03 +00001566 case ISD::SHL: {
1567 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001568 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1569 Tmp2 = CN->getValue();
1570 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1571 } else {
1572 Tmp2 = SelectExpr(N.getOperand(1));
1573 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1574 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001575 return Result;
1576 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001577
Duraid Madina91ed0a12005-03-17 18:17:03 +00001578 case ISD::SRL: {
1579 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001580 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1581 Tmp2 = CN->getValue();
1582 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1583 } else {
1584 Tmp2 = SelectExpr(N.getOperand(1));
1585 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1586 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001587 return Result;
1588 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001589
Duraid Madina91ed0a12005-03-17 18:17:03 +00001590 case ISD::SRA: {
1591 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001592 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1593 Tmp2 = CN->getValue();
1594 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1595 } else {
1596 Tmp2 = SelectExpr(N.getOperand(1));
1597 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1598 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001599 return Result;
1600 }
1601
Chris Lattner0815dcae2005-09-28 22:29:17 +00001602 case ISD::FDIV:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001603 case ISD::SDIV:
1604 case ISD::UDIV:
1605 case ISD::SREM:
1606 case ISD::UREM: {
1607
1608 Tmp1 = SelectExpr(N.getOperand(0));
1609 Tmp2 = SelectExpr(N.getOperand(1));
1610
1611 bool isFP=false;
1612
1613 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1614 isFP=true;
1615
1616 bool isModulus=false; // is it a division or a modulus?
1617 bool isSigned=false;
1618
1619 switch(N.getOpcode()) {
Chris Lattner0815dcae2005-09-28 22:29:17 +00001620 case ISD::FDIV:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001621 case ISD::SDIV: isModulus=false; isSigned=true; break;
1622 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001623 case ISD::FREM:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001624 case ISD::SREM: isModulus=true; isSigned=true; break;
1625 case ISD::UREM: isModulus=true; isSigned=false; break;
1626 }
1627
Duraid Madinac36b6c32005-04-06 09:55:17 +00001628 if(!isModulus && !isFP) { // if this is an integer divide,
1629 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001630 case 1: // division by a constant that's a power of 2
1631 Tmp1 = SelectExpr(N.getOperand(0));
1632 if(isSigned) { // argument could be negative, so emit some code:
1633 unsigned divAmt=Tmp3;
1634 unsigned tempGR1=MakeReg(MVT::i64);
1635 unsigned tempGR2=MakeReg(MVT::i64);
1636 unsigned tempGR3=MakeReg(MVT::i64);
1637 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1638 .addReg(Tmp1).addImm(divAmt-1);
1639 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1640 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1641 BuildMI(BB, IA64::ADD, 2, tempGR3)
1642 .addReg(Tmp1).addReg(tempGR2);
1643 BuildMI(BB, IA64::SHRS, 2, Result)
1644 .addReg(tempGR3).addImm(divAmt);
1645 }
1646 else // unsigned div-by-power-of-2 becomes a simple shift right:
1647 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1648 return Result; // early exit
Duraid Madinac36b6c32005-04-06 09:55:17 +00001649 }
1650 }
1651
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001652 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madina21ce5f72005-03-31 12:31:11 +00001653 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina91ed0a12005-03-17 18:17:03 +00001654 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1655 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1656 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1657 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1658 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1659 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1660 unsigned TmpF7=MakeReg(MVT::f64);
1661 unsigned TmpF8=MakeReg(MVT::f64);
1662 unsigned TmpF9=MakeReg(MVT::f64);
1663 unsigned TmpF10=MakeReg(MVT::f64);
1664 unsigned TmpF11=MakeReg(MVT::f64);
1665 unsigned TmpF12=MakeReg(MVT::f64);
1666 unsigned TmpF13=MakeReg(MVT::f64);
1667 unsigned TmpF14=MakeReg(MVT::f64);
1668 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001669
Duraid Madina91ed0a12005-03-17 18:17:03 +00001670 // OK, emit some code:
1671
1672 if(!isFP) {
1673 // first, load the inputs into FP regs.
1674 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1675 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001676
Duraid Madina91ed0a12005-03-17 18:17:03 +00001677 // next, convert the inputs to FP
1678 if(isSigned) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001679 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1680 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001681 } else {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001682 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1683 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001684 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001685
Duraid Madina91ed0a12005-03-17 18:17:03 +00001686 } else { // this is an FP divide/remainder, so we 'leak' some temp
1687 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1688 TmpF3=Tmp1;
1689 TmpF4=Tmp2;
1690 }
1691
1692 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina41ff5022005-04-08 10:01:48 +00001693 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1694 BuildMI(BB, IA64::FRCPAS1, 4)
1695 .addReg(TmpF5, MachineOperand::Def)
1696 .addReg(TmpPR, MachineOperand::Def)
1697 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001698
Duraid Madina21ce5f72005-03-31 12:31:11 +00001699 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1700 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1701 // TPCMPNE below
1702 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1703 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001704 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001705 }
1706
Duraid Madina91ed0a12005-03-17 18:17:03 +00001707 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1708 // precision, don't need this much for f32/i32)
1709 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1710 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1711 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1712 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1713 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1714 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1715 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1716 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1717 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1718 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1719 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1720 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1721 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1722 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1723 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1724 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina9935f442005-04-04 05:05:52 +00001725
1726 // FIXME: this is unfortunate :(
1727 // the story is that the dest reg of the fnma above and the fma below
1728 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1729 // be the same register, or this code breaks if the first argument is
1730 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina91ed0a12005-03-17 18:17:03 +00001731 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1732 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1733
Duraid Madina9935f442005-04-04 05:05:52 +00001734 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1735 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1736 }
1737
Duraid Madina91ed0a12005-03-17 18:17:03 +00001738 if(!isFP) {
1739 // round to an integer
1740 if(isSigned)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001741 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001742 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00001743 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001744 } else {
1745 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1746 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1747 // we really do need the above FMOV? ;)
1748 }
1749
1750 if(!isModulus) {
Duraid Madina21ce5f72005-03-31 12:31:11 +00001751 if(isFP) { // extra worrying about div-by-zero
1752 unsigned bogoResult=MakeReg(MVT::f64);
1753
1754 // we do a 'conditional fmov' (of the correct result, depending
1755 // on how the frcpa predicate turned out)
1756 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001757 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001758 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001759 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001760 }
Duraid Madina9935f442005-04-04 05:05:52 +00001761 else {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001762 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina9935f442005-04-04 05:05:52 +00001763 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001764 } else { // this is a modulus
1765 if(!isFP) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001766 // answer = q * (-b) + a
1767 unsigned ModulusResult = MakeReg(MVT::f64);
1768 unsigned TmpF = MakeReg(MVT::f64);
1769 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001770
Misha Brukmane73e76d2005-04-22 17:54:37 +00001771 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1772 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1773 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1774 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1775 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001776 } else { // FP modulus! The horror... the horror....
Misha Brukmane73e76d2005-04-22 17:54:37 +00001777 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001778 }
1779 }
1780
1781 return Result;
1782 }
1783
Duraid Madina91ed0a12005-03-17 18:17:03 +00001784 case ISD::SIGN_EXTEND_INREG: {
1785 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner53676df2005-07-10 01:56:13 +00001786 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina91ed0a12005-03-17 18:17:03 +00001787 default:
1788 Node->dump();
1789 assert(0 && "don't know how to sign extend this type");
1790 break;
1791 case MVT::i8: Opc = IA64::SXT1; break;
1792 case MVT::i16: Opc = IA64::SXT2; break;
1793 case MVT::i32: Opc = IA64::SXT4; break;
1794 }
1795 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1796 return Result;
1797 }
1798
1799 case ISD::SETCC: {
1800 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner6ec77452005-08-09 20:21:10 +00001801 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1802 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madinafb43ef72005-04-11 05:55:56 +00001803
Chris Lattner6ec77452005-08-09 20:21:10 +00001804 if(ConstantSDNode *CSDN =
1805 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1806 // if we are comparing against a constant zero
1807 if(CSDN->getValue()==0)
1808 Tmp2 = IA64::r0; // then we can just compare against r0
1809 else
1810 Tmp2 = SelectExpr(N.getOperand(1));
1811 } else // not comparing against a constant
1812 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinafb43ef72005-04-11 05:55:56 +00001813
Chris Lattner6ec77452005-08-09 20:21:10 +00001814 switch (CC) {
1815 default: assert(0 && "Unknown integer comparison!");
1816 case ISD::SETEQ:
1817 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1818 break;
1819 case ISD::SETGT:
1820 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1821 break;
1822 case ISD::SETGE:
1823 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1824 break;
1825 case ISD::SETLT:
1826 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1827 break;
1828 case ISD::SETLE:
1829 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1830 break;
1831 case ISD::SETNE:
1832 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 break;
1834 case ISD::SETULT:
1835 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 break;
1837 case ISD::SETUGT:
1838 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 break;
1840 case ISD::SETULE:
1841 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 break;
1843 case ISD::SETUGE:
1844 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1845 break;
1846 }
1847 } else { // if not integer, should be FP.
1848 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1849 "error: SETCC should have had incoming f32 promoted to f64!\n");
1850
1851 if(ConstantFPSDNode *CFPSDN =
1852 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1853
1854 // if we are comparing against a constant +0.0 or +1.0
1855 if(CFPSDN->isExactlyValue(+0.0))
1856 Tmp2 = IA64::F0; // then we can just compare against f0
1857 else if(CFPSDN->isExactlyValue(+1.0))
1858 Tmp2 = IA64::F1; // or f1
Misha Brukmane73e76d2005-04-22 17:54:37 +00001859 else
1860 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner6ec77452005-08-09 20:21:10 +00001861 } else // not comparing against a constant
1862 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001863
Chris Lattner6ec77452005-08-09 20:21:10 +00001864 switch (CC) {
1865 default: assert(0 && "Unknown FP comparison!");
1866 case ISD::SETEQ:
1867 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1868 break;
1869 case ISD::SETGT:
1870 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 break;
1872 case ISD::SETGE:
1873 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1874 break;
1875 case ISD::SETLT:
1876 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1877 break;
1878 case ISD::SETLE:
1879 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1880 break;
1881 case ISD::SETNE:
1882 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1883 break;
1884 case ISD::SETULT:
1885 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1886 break;
1887 case ISD::SETUGT:
1888 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1889 break;
1890 case ISD::SETULE:
1891 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1892 break;
1893 case ISD::SETUGE:
1894 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1895 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001896 }
1897 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001898 return Result;
1899 }
1900
1901 case ISD::EXTLOAD:
1902 case ISD::ZEXTLOAD:
1903 case ISD::LOAD: {
1904 // Make sure we generate both values.
1905 if (Result != 1)
1906 ExprMap[N.getValue(1)] = 1; // Generate the token
1907 else
1908 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1909
1910 bool isBool=false;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001911
Duraid Madina91ed0a12005-03-17 18:17:03 +00001912 if(opcode == ISD::LOAD) { // this is a LOAD
1913 switch (Node->getValueType(0)) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001914 default: assert(0 && "Cannot load this type!");
1915 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1916 // FIXME: for now, we treat bool loads the same as i8 loads */
1917 case MVT::i8: Opc = IA64::LD1; break;
1918 case MVT::i16: Opc = IA64::LD2; break;
1919 case MVT::i32: Opc = IA64::LD4; break;
1920 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001921
Misha Brukmane73e76d2005-04-22 17:54:37 +00001922 case MVT::f32: Opc = IA64::LDF4; break;
1923 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001924 }
1925 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattner53676df2005-07-10 01:56:13 +00001926 MVT::ValueType TypeBeingLoaded =
1927 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina91ed0a12005-03-17 18:17:03 +00001928 switch (TypeBeingLoaded) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001929 default: assert(0 && "Cannot extload/zextload this type!");
1930 // FIXME: bools?
1931 case MVT::i8: Opc = IA64::LD1; break;
1932 case MVT::i16: Opc = IA64::LD2; break;
1933 case MVT::i32: Opc = IA64::LD4; break;
1934 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001935 }
1936 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001937
Duraid Madina91ed0a12005-03-17 18:17:03 +00001938 SDOperand Chain = N.getOperand(0);
1939 SDOperand Address = N.getOperand(1);
1940
1941 if(Address.getOpcode() == ISD::GlobalAddress) {
1942 Select(Chain);
1943 unsigned dummy = MakeReg(MVT::i64);
1944 unsigned dummy2 = MakeReg(MVT::i64);
1945 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001946 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1947 .addReg(IA64::r1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001948 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1949 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001950 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001951 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001952 // into a predicate register
1953 assert(Opc==IA64::LD1 && "problem loading a bool");
1954 unsigned dummy3 = MakeReg(MVT::i64);
1955 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1956 // we compare to 0. true? 0. false? 1.
1957 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001958 }
1959 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattnerc30405e2005-08-26 17:15:30 +00001960 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00001961 getConstantPoolIndex(CP->get());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001962 Select(Chain);
1963 IA64Lowering.restoreGP(BB);
1964 unsigned dummy = MakeReg(MVT::i64);
Duraid Madinaf221c262005-10-28 17:46:35 +00001965 unsigned dummy2 = MakeReg(MVT::i64);
1966 BuildMI(BB, IA64::MOVLIMM64, 1, dummy2).addConstantPoolIndex(CPIdx);
1967 BuildMI(BB, IA64::ADD, 2, dummy).addReg(dummy2).addReg(IA64::r1); //CPI+GP
1968
1969
1970 // OLD BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
1971 // (FIXME!) .addReg(IA64::r1); // CPI+GP
Duraid Madina91ed0a12005-03-17 18:17:03 +00001972 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001973 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001974 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001975 // into a predicate register
1976 assert(Opc==IA64::LD1 && "problem loading a bool");
1977 unsigned dummy3 = MakeReg(MVT::i64);
1978 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1979 // we compare to 0. true? 0. false? 1.
1980 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001981 }
1982 } else if(Address.getOpcode() == ISD::FrameIndex) {
1983 Select(Chain); // FIXME ? what about bools?
1984 unsigned dummy = MakeReg(MVT::i64);
1985 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001986 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001987 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001988 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001989 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001990 // into a predicate register
1991 assert(Opc==IA64::LD1 && "problem loading a bool");
1992 unsigned dummy3 = MakeReg(MVT::i64);
1993 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1994 // we compare to 0. true? 0. false? 1.
1995 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001996 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001997 } else { // none of the above...
Duraid Madina91ed0a12005-03-17 18:17:03 +00001998 Select(Chain);
1999 Tmp2 = SelectExpr(Address);
2000 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002001 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002002 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002003 // into a predicate register
2004 assert(Opc==IA64::LD1 && "problem loading a bool");
2005 unsigned dummy = MakeReg(MVT::i64);
2006 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2007 // we compare to 0. true? 0. false? 1.
2008 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002009 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002010 }
2011
2012 return Result;
2013 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002014
Duraid Madina91ed0a12005-03-17 18:17:03 +00002015 case ISD::CopyFromReg: {
2016 if (Result == 1)
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002017 Result = ExprMap[N.getValue(0)] =
Misha Brukmane73e76d2005-04-22 17:54:37 +00002018 MakeReg(N.getValue(0).getValueType());
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002019
Duraid Madina91ed0a12005-03-17 18:17:03 +00002020 SDOperand Chain = N.getOperand(0);
2021
2022 Select(Chain);
Chris Lattner7c762782005-08-16 21:56:37 +00002023 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina91ed0a12005-03-17 18:17:03 +00002024
2025 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukmane73e76d2005-04-22 17:54:37 +00002026 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2027 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002028 // (r) Result =cmp.eq.unc(r0,r0)
2029 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00002030 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina91ed0a12005-03-17 18:17:03 +00002031 return Result;
2032 }
2033
Chris Lattner8abab9b2005-05-13 20:29:26 +00002034 case ISD::TAILCALL:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002035 case ISD::CALL: {
2036 Select(N.getOperand(0));
2037
2038 // The chain for this call is now lowered.
2039 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002040
Duraid Madina91ed0a12005-03-17 18:17:03 +00002041 //grab the arguments
2042 std::vector<unsigned> argvregs;
2043
2044 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002045 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002046
2047 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina91ed0a12005-03-17 18:17:03 +00002048 // Runtime Architecture Guide to see some examples of what's going
2049 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2050 // while FP args get mapped to F8->F15 as needed)
2051
2052 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002053
Duraid Madina91ed0a12005-03-17 18:17:03 +00002054 // in reg args
2055 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2056 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002057 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2058 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2059 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2060 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina91ed0a12005-03-17 18:17:03 +00002061
Misha Brukmane73e76d2005-04-22 17:54:37 +00002062 switch(N.getOperand(i+2).getValueType())
2063 {
2064 default: // XXX do we need to support MVT::i1 here?
2065 Node->dump();
2066 N.getOperand(i).Val->dump();
2067 std::cerr << "Type for " << i << " is: " <<
2068 N.getOperand(i+2).getValueType() << std::endl;
2069 assert(0 && "Unknown value type for call");
2070 case MVT::i64:
2071 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2072 break;
2073 case MVT::f64:
2074 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2075 .addReg(argvregs[i]);
2076 // FIXME: we don't need to do this _all_ the time:
2077 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2078 break;
2079 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002080 }
2081
2082 //in mem args
2083 for (int i = 8, e = argvregs.size(); i < e; ++i)
2084 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002085 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002086
Duraid Madina91ed0a12005-03-17 18:17:03 +00002087 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002088 default:
2089 Node->dump();
Duraid Madina91ed0a12005-03-17 18:17:03 +00002090 N.getOperand(i).Val->dump();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002091 std::cerr << "Type for " << i << " is: " <<
Duraid Madina91ed0a12005-03-17 18:17:03 +00002092 N.getOperand(i+2).getValueType() << "\n";
2093 assert(0 && "Unknown value type for call");
2094 case MVT::i1: // FIXME?
2095 case MVT::i8:
2096 case MVT::i16:
2097 case MVT::i32:
2098 case MVT::i64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002099 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2100 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2101 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002102 break;
2103 case MVT::f32:
2104 case MVT::f64:
2105 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002106 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2107 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002108 break;
2109 }
2110 }
Duraid Madina21ce5f72005-03-31 12:31:11 +00002111
Duraid Madina3a5eb612005-05-20 11:39:17 +00002112 // build the right kind of call. if we can branch directly, do so:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002113 if (GlobalAddressSDNode *GASD =
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002114 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina91ed0a12005-03-17 18:17:03 +00002115 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002116 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2117 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina3a5eb612005-05-20 11:39:17 +00002118 } else
Duraid Madina21ce5f72005-03-31 12:31:11 +00002119 if (ExternalSymbolSDNode *ESSDN =
Misha Brukmane73e76d2005-04-22 17:54:37 +00002120 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madina21ce5f72005-03-31 12:31:11 +00002121 { // FIXME : currently need this case for correctness, to avoid
Misha Brukmane73e76d2005-04-22 17:54:37 +00002122 // "non-pic code with imm relocation against dynamic symbol" errors
2123 BuildMI(BB, IA64::BRCALL, 1)
2124 .addExternalSymbol(ESSDN->getSymbol(), true);
2125 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002126 }
Duraid Madina3a5eb612005-05-20 11:39:17 +00002127 else { // otherwise we need to get the function descriptor
2128 // load the branch target (function)'s entry point and
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002129 // GP, then branch
Duraid Madina91ed0a12005-03-17 18:17:03 +00002130 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina21ce5f72005-03-31 12:31:11 +00002131
2132 unsigned targetEntryPoint=MakeReg(MVT::i64);
2133 unsigned targetGPAddr=MakeReg(MVT::i64);
2134 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002135
Duraid Madina21ce5f72005-03-31 12:31:11 +00002136 // b6 is a scratch branch register, we load the target entry point
2137 // from the base of the function descriptor
2138 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2139 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2140
2141 // save the current GP:
2142 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002143
Duraid Madina21ce5f72005-03-31 12:31:11 +00002144 /* TODO: we need to make sure doing this never, ever loads a
2145 * bogus value into r1 (GP). */
2146 // load the target GP (which is at mem[functiondescriptor+8])
2147 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002148 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madina21ce5f72005-03-31 12:31:11 +00002149 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2150
Duraid Madina91ed0a12005-03-17 18:17:03 +00002151 // and then jump: (well, call)
2152 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madina21ce5f72005-03-31 12:31:11 +00002153 // and finally restore the old GP
2154 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2155 IA64Lowering.restoreSP_RP(BB);
2156 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002157
2158 switch (Node->getValueType(0)) {
2159 default: assert(0 && "Unknown value type for call result!");
2160 case MVT::Other: return 1;
2161 case MVT::i1:
2162 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002163 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002164 break;
2165 case MVT::i8:
2166 case MVT::i16:
2167 case MVT::i32:
2168 case MVT::i64:
2169 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2170 break;
2171 case MVT::f64:
2172 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2173 break;
2174 }
2175 return Result+N.ResNo;
2176 }
2177
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002178 } // <- uhhh XXX
Duraid Madina91ed0a12005-03-17 18:17:03 +00002179 return 0;
2180}
2181
2182void ISel::Select(SDOperand N) {
2183 unsigned Tmp1, Tmp2, Opc;
2184 unsigned opcode = N.getOpcode();
2185
Nate Begeman95210522005-03-24 04:39:54 +00002186 if (!LoweredTokens.insert(N).second)
Duraid Madina91ed0a12005-03-17 18:17:03 +00002187 return; // Already selected.
2188
2189 SDNode *Node = N.Val;
2190
2191 switch (Node->getOpcode()) {
2192 default:
2193 Node->dump(); std::cerr << "\n";
2194 assert(0 && "Node not handled yet!");
2195
2196 case ISD::EntryToken: return; // Noop
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002197
Duraid Madina91ed0a12005-03-17 18:17:03 +00002198 case ISD::TokenFactor: {
2199 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2200 Select(Node->getOperand(i));
2201 return;
2202 }
2203
2204 case ISD::CopyToReg: {
2205 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00002206 Tmp1 = SelectExpr(N.getOperand(2));
2207 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002208
Duraid Madina91ed0a12005-03-17 18:17:03 +00002209 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00002210 // if a bool, we use pseudocode
2211 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002212 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2213 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002214 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2215 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00002216 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002217 // XXX is this the right way 'round? ;)
Chris Lattner7c762782005-08-16 21:56:37 +00002218 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina91ed0a12005-03-17 18:17:03 +00002219 }
2220 return;
2221 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002222
Duraid Madina91ed0a12005-03-17 18:17:03 +00002223 case ISD::RET: {
2224
2225 /* what the heck is going on here:
2226
2227<_sabre_> ret with two operands is obvious: chain and value
2228<camel_> yep
2229<_sabre_> ret with 3 values happens when 'expansion' occurs
2230<_sabre_> e.g. i64 gets split into 2x i32
2231<camel_> oh right
2232<_sabre_> you don't have this case on ia64
2233<camel_> yep
2234<_sabre_> so the two returned values go into EAX/EDX on ia32
2235<camel_> ahhh *memories*
2236<_sabre_> :)
2237<camel_> ok, thanks :)
2238<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2239<_sabre_> this is the first operand always
2240<_sabre_> these operand often define chains, they are the last operand
2241<_sabre_> they are printed as 'ch' if you do DAG.dump()
2242 */
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002243
Duraid Madina91ed0a12005-03-17 18:17:03 +00002244 switch (N.getNumOperands()) {
2245 default:
2246 assert(0 && "Unknown return instruction!");
2247 case 2:
2248 Select(N.getOperand(0));
2249 Tmp1 = SelectExpr(N.getOperand(1));
2250 switch (N.getOperand(1).getValueType()) {
2251 default: assert(0 && "All other types should have been promoted!!");
Misha Brukmane73e76d2005-04-22 17:54:37 +00002252 // FIXME: do I need to add support for bools here?
2253 // (return '0' or '1' r8, basically...)
2254 //
2255 // FIXME: need to round floats - 80 bits is bad, the tester
2256 // told me so
Duraid Madina91ed0a12005-03-17 18:17:03 +00002257 case MVT::i64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002258 // we mark r8 as live on exit up above in LowerArguments()
2259 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2260 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00002261 case MVT::f64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002262 // we mark F8 as live on exit up above in LowerArguments()
2263 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002264 }
2265 break;
2266 case 1:
2267 Select(N.getOperand(0));
2268 break;
2269 }
2270 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2271 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2272 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2273 return;
2274 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002275
Duraid Madina91ed0a12005-03-17 18:17:03 +00002276 case ISD::BR: {
2277 Select(N.getOperand(0));
2278 MachineBasicBlock *Dest =
2279 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2280 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2281 // XXX HACK! we do _not_ need long branches all the time
2282 return;
2283 }
2284
Duraid Madina91ed0a12005-03-17 18:17:03 +00002285 case ISD::BRCOND: {
2286 MachineBasicBlock *Dest =
2287 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2288
2289 Select(N.getOperand(0));
2290 Tmp1 = SelectExpr(N.getOperand(1));
2291 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2292 // XXX HACK! we do _not_ need long branches all the time
2293 return;
2294 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002295
Duraid Madina91ed0a12005-03-17 18:17:03 +00002296 case ISD::EXTLOAD:
2297 case ISD::ZEXTLOAD:
2298 case ISD::SEXTLOAD:
2299 case ISD::LOAD:
Chris Lattner8abab9b2005-05-13 20:29:26 +00002300 case ISD::TAILCALL:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002301 case ISD::CALL:
2302 case ISD::CopyFromReg:
2303 case ISD::DYNAMIC_STACKALLOC:
2304 SelectExpr(N);
2305 return;
2306
2307 case ISD::TRUNCSTORE:
2308 case ISD::STORE: {
2309 Select(N.getOperand(0));
2310 Tmp1 = SelectExpr(N.getOperand(1)); // value
2311
2312 bool isBool=false;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002313
Duraid Madina91ed0a12005-03-17 18:17:03 +00002314 if(opcode == ISD::STORE) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002315 switch (N.getOperand(1).getValueType()) {
2316 default: assert(0 && "Cannot store this type!");
2317 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2318 // FIXME?: for now, we treat bool loads the same as i8 stores */
2319 case MVT::i8: Opc = IA64::ST1; break;
2320 case MVT::i16: Opc = IA64::ST2; break;
2321 case MVT::i32: Opc = IA64::ST4; break;
2322 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002323
Misha Brukmane73e76d2005-04-22 17:54:37 +00002324 case MVT::f32: Opc = IA64::STF4; break;
2325 case MVT::f64: Opc = IA64::STF8; break;
2326 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002327 } else { // truncstore
Chris Lattner36db1ed2005-07-10 00:29:18 +00002328 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002329 default: assert(0 && "unknown type in truncstore");
2330 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2331 //FIXME: DAG does not promote this load?
2332 case MVT::i8: Opc = IA64::ST1; break;
2333 case MVT::i16: Opc = IA64::ST2; break;
2334 case MVT::i32: Opc = IA64::ST4; break;
2335 case MVT::f32: Opc = IA64::STF4; break;
2336 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002337 }
2338
2339 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002340 unsigned dummy = MakeReg(MVT::i64);
2341 unsigned dummy2 = MakeReg(MVT::i64);
2342 BuildMI(BB, IA64::ADD, 2, dummy)
2343 .addGlobalAddress(cast<GlobalAddressSDNode>
2344 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2345 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002346
Misha Brukmane73e76d2005-04-22 17:54:37 +00002347 if(!isBool)
2348 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2349 else { // we are storing a bool, so emit a little pseudocode
2350 // to store a predicate register as one byte
2351 assert(Opc==IA64::ST1);
2352 unsigned dummy3 = MakeReg(MVT::i64);
2353 unsigned dummy4 = MakeReg(MVT::i64);
2354 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2355 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2356 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2357 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2358 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002359 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2360
Misha Brukmane73e76d2005-04-22 17:54:37 +00002361 // FIXME? (what about bools?)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002362
Misha Brukmane73e76d2005-04-22 17:54:37 +00002363 unsigned dummy = MakeReg(MVT::i64);
2364 BuildMI(BB, IA64::MOV, 1, dummy)
2365 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2366 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002367 } else { // otherwise
Misha Brukmane73e76d2005-04-22 17:54:37 +00002368 Tmp2 = SelectExpr(N.getOperand(2)); //address
2369 if(!isBool)
2370 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2371 else { // we are storing a bool, so emit a little pseudocode
2372 // to store a predicate register as one byte
2373 assert(Opc==IA64::ST1);
2374 unsigned dummy3 = MakeReg(MVT::i64);
2375 unsigned dummy4 = MakeReg(MVT::i64);
2376 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2377 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2378 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2379 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2380 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002381 }
2382 return;
2383 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002384
Chris Lattner2dce7032005-05-12 23:24:06 +00002385 case ISD::CALLSEQ_START:
2386 case ISD::CALLSEQ_END: {
Duraid Madina91ed0a12005-03-17 18:17:03 +00002387 Select(N.getOperand(0));
2388 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002389
Chris Lattner2dce7032005-05-12 23:24:06 +00002390 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2391 IA64::ADJUSTCALLSTACKUP;
Duraid Madina91ed0a12005-03-17 18:17:03 +00002392 BuildMI(BB, Opc, 1).addImm(Tmp1);
2393 return;
2394 }
2395
2396 return;
2397 }
2398 assert(0 && "GAME OVER. INSERT COIN?");
2399}
2400
2401
2402/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2403/// into a machine code representation using pattern matching and a machine
2404/// description file.
2405///
2406FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002407 return new ISel(TM);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002408}
2409
2410