blob: 3f8942c5cf64c7b646d80e346df64d76e3b52e8a [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 Begeman6b9e00d2006-01-11 23:20:28 +000099 setOperationAction(ISD::ROTL , MVT::i64 , Expand);
100 setOperationAction(ISD::ROTR , MVT::i64 , Expand);
Nate Begemanae5d9bd2005-10-21 01:52:45 +0000101 // FIXME: implement mulhs (xma.h) and mulhu (xma.hu)
102 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
103 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +0000104
Chris Lattner93feffb2005-12-01 18:19:53 +0000105 // We don't have line number support yet.
106 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000107 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
108 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner93feffb2005-12-01 18:19:53 +0000109
Duraid Madina91ed0a12005-03-17 18:17:03 +0000110 computeRegisterProperties();
111
112 addLegalFPImmediate(+0.0);
113 addLegalFPImmediate(+1.0);
114 addLegalFPImmediate(-0.0);
115 addLegalFPImmediate(-1.0);
116 }
117
118 /// LowerArguments - This hook must be implemented to indicate how we should
119 /// lower the arguments for the specified function, into the specified DAG.
120 virtual std::vector<SDOperand>
121 LowerArguments(Function &F, SelectionDAG &DAG);
122
123 /// LowerCallTo - This hook lowers an abstract call to a function into an
124 /// actual call.
125 virtual std::pair<SDOperand, SDOperand>
Chris Lattner36674a12005-05-12 19:56:45 +0000126 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000127 bool isTailCall, SDOperand Callee, ArgListTy &Args,
128 SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000129
Chris Lattnera7220852005-07-05 19:58:54 +0000130 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
131 Value *VAListV, SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000132 virtual std::pair<SDOperand,SDOperand>
Chris Lattnera7220852005-07-05 19:58:54 +0000133 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
134 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000135
136 void restoreGP_SP_RP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
139 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
140 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
141 }
142
Duraid Madina21ce5f72005-03-31 12:31:11 +0000143 void restoreSP_RP(MachineBasicBlock* BB)
144 {
145 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
146 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
147 }
148
Duraid Madina91ed0a12005-03-17 18:17:03 +0000149 void restoreRP(MachineBasicBlock* BB)
150 {
151 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
152 }
153
154 void restoreGP(MachineBasicBlock* BB)
155 {
156 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
157 }
158
159 };
160}
161
162
163std::vector<SDOperand>
164IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
165 std::vector<SDOperand> ArgValues;
166
167 //
168 // add beautiful description of IA64 stack frame format
169 // here (from intel 24535803.pdf most likely)
170 //
171 MachineFunction &MF = DAG.getMachineFunction();
172 MachineFrameInfo *MFI = MF.getFrameInfo();
173
174 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
175 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
176 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
177
178 MachineBasicBlock& BB = MF.front();
179
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000180 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000181 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000182
183 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000184 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000185
Duraid Madina91ed0a12005-03-17 18:17:03 +0000186 unsigned argVreg[8];
187 unsigned argPreg[8];
188 unsigned argOpc[8];
189
Duraid Madina21ce5f72005-03-31 12:31:11 +0000190 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000191
Duraid Madina21ce5f72005-03-31 12:31:11 +0000192 unsigned ArgOffset = 0;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000193 int count = 0;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000194
Alkis Evlogimenos1a485612005-03-19 09:22:17 +0000195 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina91ed0a12005-03-17 18:17:03 +0000196 {
197 SDOperand newroot, argt;
198 if(count < 8) { // need to fix this logic? maybe.
Misha Brukmane73e76d2005-04-22 17:54:37 +0000199
200 switch (getValueType(I->getType())) {
201 default:
202 std::cerr << "ERROR in LowerArgs: unknown type "
203 << getValueType(I->getType()) << "\n";
204 abort();
205 case MVT::f32:
206 // fixme? (well, will need to for weird FP structy stuff,
207 // see intel ABI docs)
208 case MVT::f64:
209//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
210 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
211 // floating point args go into f8..f15 as-needed, the increment
212 argVreg[count] = // is below..:
213 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
214 // FP args go into f8..f15 as needed: (hence the ++)
215 argPreg[count] = args_FP[used_FPArgs++];
216 argOpc[count] = IA64::FMOV;
Chris Lattner7c762782005-08-16 21:56:37 +0000217 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattnerd2f2aff2005-08-22 21:33:11 +0000218 MVT::f64);
219 if (I->getType() == Type::FloatTy)
220 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukmane73e76d2005-04-22 17:54:37 +0000221 break;
222 case MVT::i1: // NOTE: as far as C abi stuff goes,
223 // bools are just boring old ints
224 case MVT::i8:
225 case MVT::i16:
226 case MVT::i32:
227 case MVT::i64:
228//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
229 MF.addLiveIn(args_int[count]); // mark this register as liveIn
230 argVreg[count] =
231 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
232 argPreg[count] = args_int[count];
233 argOpc[count] = IA64::MOV;
234 argt = newroot =
Chris Lattner7c762782005-08-16 21:56:37 +0000235 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukmane73e76d2005-04-22 17:54:37 +0000236 if ( getValueType(I->getType()) != MVT::i64)
237 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
238 newroot);
239 break;
240 }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000241 } else { // more than 8 args go into the frame
Misha Brukmane73e76d2005-04-22 17:54:37 +0000242 // Create the frame index object for this incoming parameter...
243 ArgOffset = 16 + 8 * (count - 8);
244 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000245
Misha Brukmane73e76d2005-04-22 17:54:37 +0000246 // Create the SelectionDAG nodes corresponding to a load
247 //from this parameter
248 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
249 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000250 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina91ed0a12005-03-17 18:17:03 +0000251 }
252 ++count;
253 DAG.setRoot(newroot.getValue(1));
254 ArgValues.push_back(argt);
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000255 }
Duraid Madina21ce5f72005-03-31 12:31:11 +0000256
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000257
Duraid Madina21ce5f72005-03-31 12:31:11 +0000258 // Create a vreg to hold the output of (what will become)
259 // the "alloc" instruction
Duraid Madina91ed0a12005-03-17 18:17:03 +0000260 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
261 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
262 // we create a PSEUDO_ALLOC (pseudo)instruction for now
263
264 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
265
266 // hmm:
267 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
268 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
269 // ..hmm.
270
271 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
272
273 // hmm:
274 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
275 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
276 // ..hmm.
277
Duraid Madina21ce5f72005-03-31 12:31:11 +0000278 unsigned tempOffset=0;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000279
Duraid Madina21ce5f72005-03-31 12:31:11 +0000280 // if this is a varargs function, we simply lower llvm.va_start by
281 // pointing to the first entry
282 if(F.isVarArg()) {
283 tempOffset=0;
284 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000285 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000286
Duraid Madina21ce5f72005-03-31 12:31:11 +0000287 // here we actually do the moving of args, and store them to the stack
288 // too if this is a varargs function:
289 for (int i = 0; i < count && i < 8; ++i) {
290 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
291 if(F.isVarArg()) {
292 // if this is a varargs function, we copy the input registers to the stack
293 int FI = MFI->CreateFixedObject(8, tempOffset);
294 tempOffset+=8; //XXX: is it safe to use r22 like this?
295 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
296 // FIXME: we should use st8.spill here, one day
297 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
298 }
299 }
300
Duraid Madinab6dfb222005-04-12 14:54:44 +0000301 // Finally, inform the code generator which regs we return values in.
302 // (see the ISD::RET: case down below)
303 switch (getValueType(F.getReturnType())) {
304 default: assert(0 && "i have no idea where to return this type!");
305 case MVT::isVoid: break;
306 case MVT::i1:
307 case MVT::i8:
308 case MVT::i16:
309 case MVT::i32:
310 case MVT::i64:
311 MF.addLiveOut(IA64::r8);
312 break;
313 case MVT::f32:
314 case MVT::f64:
315 MF.addLiveOut(IA64::F8);
316 break;
317 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000318
Duraid Madina91ed0a12005-03-17 18:17:03 +0000319 return ArgValues;
320}
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000321
Duraid Madina91ed0a12005-03-17 18:17:03 +0000322std::pair<SDOperand, SDOperand>
323IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000324 const Type *RetTy, bool isVarArg,
Chris Lattner2e77db62005-05-13 18:50:42 +0000325 unsigned CallingConv, bool isTailCall,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000326 SDOperand Callee, ArgListTy &Args,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000327 SelectionDAG &DAG) {
Duraid Madina91ed0a12005-03-17 18:17:03 +0000328
329 MachineFunction &MF = DAG.getMachineFunction();
330
Duraid Madina91ed0a12005-03-17 18:17:03 +0000331 unsigned NumBytes = 16;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000332 unsigned outRegsUsed = 0;
333
334 if (Args.size() > 8) {
Duraid Madina91ed0a12005-03-17 18:17:03 +0000335 NumBytes += (Args.size() - 8) * 8;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000336 outRegsUsed = 8;
337 } else {
338 outRegsUsed = Args.size();
339 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000340
Duraid Madina21ce5f72005-03-31 12:31:11 +0000341 // FIXME? this WILL fail if we ever try to pass around an arg that
342 // consumes more than a single output slot (a 'real' double, int128
343 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
344 // registers we use. Hopefully, the assembler will notice.
345 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
346 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000347
Chris Lattner2dce7032005-05-12 23:24:06 +0000348 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000349 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000350
Duraid Madina91ed0a12005-03-17 18:17:03 +0000351 std::vector<SDOperand> args_to_use;
352 for (unsigned i = 0, e = Args.size(); i != e; ++i)
353 {
354 switch (getValueType(Args[i].second)) {
355 default: assert(0 && "unexpected argument type!");
356 case MVT::i1:
357 case MVT::i8:
358 case MVT::i16:
359 case MVT::i32:
Misha Brukmane73e76d2005-04-22 17:54:37 +0000360 //promote to 64-bits, sign/zero extending based on type
361 //of the argument
362 if(Args[i].second->isSigned())
363 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
364 Args[i].first);
365 else
366 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
367 Args[i].first);
368 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000369 case MVT::f32:
Misha Brukmane73e76d2005-04-22 17:54:37 +0000370 //promote to 64-bits
371 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000372 case MVT::f64:
373 case MVT::i64:
374 break;
375 }
376 args_to_use.push_back(Args[i].first);
377 }
378
379 std::vector<MVT::ValueType> RetVals;
380 MVT::ValueType RetTyVT = getValueType(RetTy);
381 if (RetTyVT != MVT::isVoid)
382 RetVals.push_back(RetTyVT);
383 RetVals.push_back(MVT::Other);
384
385 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukmane73e76d2005-04-22 17:54:37 +0000386 Callee, args_to_use), 0);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000387 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000388 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina91ed0a12005-03-17 18:17:03 +0000389 DAG.getConstant(NumBytes, getPointerTy()));
390 return std::make_pair(TheCall, Chain);
391}
392
Chris Lattnera7220852005-07-05 19:58:54 +0000393SDOperand
394IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
395 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000396 // vastart just stores the address of the VarArgsFrameIndex slot.
397 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnera7220852005-07-05 19:58:54 +0000398 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
399 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina91ed0a12005-03-17 18:17:03 +0000400}
401
402std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnera7220852005-07-05 19:58:54 +0000403LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
404 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madina21ce5f72005-03-31 12:31:11 +0000405
406 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnera7220852005-07-05 19:58:54 +0000407 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
408 VAListP, DAG.getSrcValue(VAListV));
409 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
410 DAG.getSrcValue(NULL));
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000411 unsigned Amt;
412 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
413 Amt = 8;
414 else {
415 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
416 "Other types should have been promoted for varargs!");
417 Amt = 8;
Duraid Madina21ce5f72005-03-31 12:31:11 +0000418 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000419 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000420 DAG.getConstant(Amt, Val.getValueType()));
421 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera7220852005-07-05 19:58:54 +0000422 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madina21ce5f72005-03-31 12:31:11 +0000423 return std::make_pair(Result, Chain);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000424}
Duraid Madina91ed0a12005-03-17 18:17:03 +0000425
Duraid Madina91ed0a12005-03-17 18:17:03 +0000426namespace {
427
428 //===--------------------------------------------------------------------===//
429 /// ISel - IA64 specific code to select IA64 machine instructions for
430 /// SelectionDAG operations.
431 ///
432 class ISel : public SelectionDAGISel {
433 /// IA64Lowering - This object fully describes how to lower LLVM code to an
434 /// IA64-specific SelectionDAG.
435 IA64TargetLowering IA64Lowering;
Duraid Madina81ebb572005-04-26 07:23:02 +0000436 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
437 // for sdiv and udiv until it is put into the future
438 // dag combiner
Duraid Madina91ed0a12005-03-17 18:17:03 +0000439
440 /// ExprMap - As shared expressions are codegen'd, we keep track of which
441 /// vreg the value is produced in, so we only emit one copy of each compiled
442 /// tree.
443 std::map<SDOperand, unsigned> ExprMap;
444 std::set<SDOperand> LoweredTokens;
445
446 public:
Duraid Madina81ebb572005-04-26 07:23:02 +0000447 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
448 ISelDAG(0) { }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000449
450 /// InstructionSelectBasicBlock - This callback is invoked by
451 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
452 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
453
Duraid Madina91ed0a12005-03-17 18:17:03 +0000454 unsigned SelectExpr(SDOperand N);
455 void Select(SDOperand N);
Duraid Madina81ebb572005-04-26 07:23:02 +0000456 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
457 SDOperand BuildConstmulSequence(SDOperand N);
458
Chris Lattner9d465182005-08-22 18:28:09 +0000459 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina91ed0a12005-03-17 18:17:03 +0000460 };
461}
462
463/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
464/// when it has created a SelectionDAG for us to codegen.
465void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
466
467 // Codegen the basic block.
Duraid Madina81ebb572005-04-26 07:23:02 +0000468 ISelDAG = &DAG;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000469 Select(DAG.getRoot());
470
471 // Clear state used for selection.
472 ExprMap.clear();
473 LoweredTokens.clear();
Duraid Madina81ebb572005-04-26 07:23:02 +0000474 ISelDAG = 0;
475}
476
Duraid Madina81ebb572005-04-26 07:23:02 +0000477// strip leading '0' characters from a string
478void munchLeadingZeros(std::string& inString) {
479 while(inString.c_str()[0]=='0') {
480 inString.erase(0, 1);
481 }
482}
483
484// strip trailing '0' characters from a string
485void munchTrailingZeros(std::string& inString) {
486 int curPos=inString.length()-1;
487
488 while(inString.c_str()[curPos]=='0') {
489 inString.erase(curPos, 1);
490 curPos--;
491 }
492}
493
494// return how many consecutive '0' characters are at the end of a string
495unsigned int countTrailingZeros(std::string& inString) {
496 int curPos=inString.length()-1;
497 unsigned int zeroCount=0;
498 // assert goes here
499 while(inString.c_str()[curPos--]=='0') {
500 zeroCount++;
501 }
502 return zeroCount;
503}
504
505// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
506// '0' and 'N' (-1) characters)
507void boothEncode(std::string inString, std::string& boothEncodedString) {
508
509 int curpos=0;
510 int replacements=0;
511 int lim=inString.size();
512
513 while(curpos<lim) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000514 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madina81ebb572005-04-26 07:23:02 +0000515 int runlength=0;
516 std::string replaceString="N";
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000517
Duraid Madina81ebb572005-04-26 07:23:02 +0000518 // find the run length
519 for(;inString[curpos+runlength]=='1';runlength++) ;
520
521 for(int i=0; i<runlength-1; i++)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000522 replaceString+="0";
Duraid Madina81ebb572005-04-26 07:23:02 +0000523 replaceString+="1";
524
525 if(runlength>1) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000526 inString.replace(curpos, runlength+1, replaceString);
527 curpos+=runlength-1;
Duraid Madina81ebb572005-04-26 07:23:02 +0000528 } else
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000529 curpos++;
Duraid Madina81ebb572005-04-26 07:23:02 +0000530 } else { // a zero, we just keep chugging along
531 curpos++;
532 }
533 }
534
535 // clean up (trim the string, reverse it and turn '1's into 'P's)
536 munchTrailingZeros(inString);
537 boothEncodedString="";
538
539 for(int i=inString.size()-1;i>=0;i--)
540 if(inString[i]=='1')
541 boothEncodedString+="P";
542 else
543 boothEncodedString+=inString[i];
544
545}
546
547struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
548 unsigned firstVal; // A
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000549 unsigned firstShift; // B
Duraid Madina81ebb572005-04-26 07:23:02 +0000550 unsigned secondVal; // C
551 unsigned secondShift; // D
552 bool isSub;
553};
554
555/* this implements Lefevre's "pattern-based" constant multiplication,
556 * see "Multiplication by an Integer Constant", INRIA report 1999-06
557 *
558 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
559 * to get better booth encodings - this does help in practice
560 * TODO: weight shifts appropriately (most architectures can't
561 * fuse a shift and an add for arbitrary shift amounts) */
562unsigned lefevre(const std::string inString,
563 std::vector<struct shiftaddblob> &ops) {
564 std::string retstring;
565 std::string s = inString;
566 munchTrailingZeros(s);
567
568 int length=s.length()-1;
569
570 if(length==0) {
571 return(0);
572 }
573
574 std::vector<int> p,n;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000575
Duraid Madina81ebb572005-04-26 07:23:02 +0000576 for(int i=0; i<=length; i++) {
577 if (s.c_str()[length-i]=='P') {
578 p.push_back(i);
579 } else if (s.c_str()[length-i]=='N') {
580 n.push_back(i);
581 }
582 }
583
584 std::string t, u;
Chris Lattnerdaae1e12005-08-25 00:03:21 +0000585 int c = 0;
Duraid Madinae06ef802005-04-26 09:42:50 +0000586 bool f;
Duraid Madina81ebb572005-04-26 07:23:02 +0000587 std::map<const int, int> w;
588
Duraid Madinab071bef2005-04-27 11:57:39 +0000589 for(unsigned i=0; i<p.size(); i++) {
590 for(unsigned j=0; j<i; j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000591 w[p[i]-p[j]]++;
592 }
593 }
594
Duraid Madinab071bef2005-04-27 11:57:39 +0000595 for(unsigned i=1; i<n.size(); i++) {
596 for(unsigned j=0; j<i; j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000597 w[n[i]-n[j]]++;
598 }
599 }
600
Duraid Madinab071bef2005-04-27 11:57:39 +0000601 for(unsigned i=0; i<p.size(); i++) {
602 for(unsigned j=0; j<n.size(); j++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000603 w[-abs(p[i]-n[j])]++;
604 }
605 }
606
607 std::map<const int, int>::const_iterator ii;
608 std::vector<int> d;
609 std::multimap<int, int> sorted_by_value;
610
611 for(ii = w.begin(); ii!=w.end(); ii++)
612 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
613
614 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
615 it != sorted_by_value.end(); ++it) {
616 d.push_back((*it).second);
617 }
618
619 int int_W=0;
620 int int_d;
621
622 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
623 d.pop_back();
624 retstring=s; // hmmm
625 int x=0;
626 int z=abs(int_d)-1;
627
628 if(int_d>0) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000629
Duraid Madinab071bef2005-04-27 11:57:39 +0000630 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000631 if( ((base+z+1) < retstring.size()) &&
632 retstring.c_str()[base]=='P' &&
633 retstring.c_str()[base+z+1]=='P')
634 {
635 // match
636 x++;
637 retstring.replace(base, 1, "0");
638 retstring.replace(base+z+1, 1, "p");
639 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000640 }
641
Duraid Madinab071bef2005-04-27 11:57:39 +0000642 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000643 if( ((base+z+1) < retstring.size()) &&
644 retstring.c_str()[base]=='N' &&
645 retstring.c_str()[base+z+1]=='N')
646 {
647 // match
648 x++;
649 retstring.replace(base, 1, "0");
650 retstring.replace(base+z+1, 1, "n");
651 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000652 }
653
654 } else {
Duraid Madinab071bef2005-04-27 11:57:39 +0000655 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000656 if( ((base+z+1) < retstring.size()) &&
657 ((retstring.c_str()[base]=='P' &&
658 retstring.c_str()[base+z+1]=='N') ||
659 (retstring.c_str()[base]=='N' &&
660 retstring.c_str()[base+z+1]=='P')) ) {
661 // match
662 x++;
663
664 if(retstring.c_str()[base]=='P') {
665 retstring.replace(base, 1, "0");
666 retstring.replace(base+z+1, 1, "p");
667 } else { // retstring[base]=='N'
668 retstring.replace(base, 1, "0");
669 retstring.replace(base+z+1, 1, "n");
670 }
671 }
Duraid Madina81ebb572005-04-26 07:23:02 +0000672 }
673 }
674
675 if(x>int_W) {
676 int_W = x;
677 t = retstring;
678 c = int_d; // tofix
679 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000680
Duraid Madina81ebb572005-04-26 07:23:02 +0000681 } d.pop_back(); // hmm
682
683 u = t;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000684
Duraid Madinab071bef2005-04-27 11:57:39 +0000685 for(unsigned i=0; i<t.length(); i++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000686 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
687 t.replace(i, 1, "0");
688 }
689
Duraid Madinab071bef2005-04-27 11:57:39 +0000690 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina74a01fa2005-05-09 13:18:34 +0000691 if(u[i]=='P' || u[i]=='N')
Duraid Madina81ebb572005-04-26 07:23:02 +0000692 u.replace(i, 1, "0");
Duraid Madina74a01fa2005-05-09 13:18:34 +0000693 if(u[i]=='p')
Duraid Madina81ebb572005-04-26 07:23:02 +0000694 u.replace(i, 1, "P");
Duraid Madina74a01fa2005-05-09 13:18:34 +0000695 if(u[i]=='n')
Duraid Madina81ebb572005-04-26 07:23:02 +0000696 u.replace(i, 1, "N");
697 }
698
699 if( c<0 ) {
Duraid Madinae06ef802005-04-26 09:42:50 +0000700 f=true;
Duraid Madina81ebb572005-04-26 07:23:02 +0000701 c=-c;
702 } else
Duraid Madinae06ef802005-04-26 09:42:50 +0000703 f=false;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000704
Duraid Madina74a01fa2005-05-09 13:18:34 +0000705 int pos=0;
706 while(u[pos]=='0')
707 pos++;
708
709 bool hit=(u[pos]=='N');
Duraid Madina81ebb572005-04-26 07:23:02 +0000710
711 int g=0;
712 if(hit) {
713 g=1;
Duraid Madinab071bef2005-04-27 11:57:39 +0000714 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina74a01fa2005-05-09 13:18:34 +0000715 bool isP=(u[p]=='P');
716 bool isN=(u[p]=='N');
Duraid Madina81ebb572005-04-26 07:23:02 +0000717
718 if(isP)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000719 u.replace(p, 1, "N");
Duraid Madina81ebb572005-04-26 07:23:02 +0000720 if(isN)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000721 u.replace(p, 1, "P");
Duraid Madina81ebb572005-04-26 07:23:02 +0000722 }
723 }
724
725 munchLeadingZeros(u);
726
727 int i = lefevre(u, ops);
728
729 shiftaddblob blob;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000730
Duraid Madina81ebb572005-04-26 07:23:02 +0000731 blob.firstVal=i; blob.firstShift=c;
732 blob.isSub=f;
733 blob.secondVal=i; blob.secondShift=0;
734
735 ops.push_back(blob);
736
737 i = ops.size();
738
739 munchLeadingZeros(t);
740
741 if(t.length()==0)
742 return i;
743
744 if(t.c_str()[0]!='P') {
745 g=2;
Duraid Madinab071bef2005-04-27 11:57:39 +0000746 for(unsigned p=0; p<t.length(); p++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000747 bool isP=(t.c_str()[p]=='P');
748 bool isN=(t.c_str()[p]=='N');
749
750 if(isP)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000751 t.replace(p, 1, "N");
Duraid Madina81ebb572005-04-26 07:23:02 +0000752 if(isN)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000753 t.replace(p, 1, "P");
Duraid Madina81ebb572005-04-26 07:23:02 +0000754 }
755 }
756
757 int j = lefevre(t, ops);
758
759 int trail=countTrailingZeros(u);
760 blob.secondVal=i; blob.secondShift=trail;
761
762 trail=countTrailingZeros(t);
763 blob.firstVal=j; blob.firstShift=trail;
764
765 switch(g) {
766 case 0:
767 blob.isSub=false; // first + second
768 break;
769 case 1:
770 blob.isSub=true; // first - second
771 break;
772 case 2:
773 blob.isSub=true; // second - first
774 int tmpval, tmpshift;
775 tmpval=blob.firstVal;
776 tmpshift=blob.firstShift;
777 blob.firstVal=blob.secondVal;
778 blob.firstShift=blob.secondShift;
779 blob.secondVal=tmpval;
780 blob.secondShift=tmpshift;
781 break;
782 //assert
783 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000784
Duraid Madina81ebb572005-04-26 07:23:02 +0000785 ops.push_back(blob);
786 return ops.size();
787}
788
789SDOperand ISel::BuildConstmulSequence(SDOperand N) {
790 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
791 // in particular, *3 is nicer as *2+1, not *4-1
792 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
793
794 bool flippedSign;
795 unsigned preliminaryShift=0;
796
Duraid Madina7acd5d52005-05-02 07:27:14 +0000797 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madina81ebb572005-04-26 07:23:02 +0000798
799 // first, we make the constant to multiply by positive
800 if(constant<0) {
801 constant=-constant;
802 flippedSign=true;
803 } else {
804 flippedSign=false;
805 }
806
807 // next, we make it odd.
808 for(; (constant%2==0); preliminaryShift++)
809 constant>>=1;
810
811 //OK, we have a positive, odd number of 64 bits or less. Convert it
812 //to a binary string, constantString[0] is the LSB
813 char constantString[65];
814 for(int i=0; i<64; i++)
815 constantString[i]='0'+((constant>>i)&0x1);
816 constantString[64]=0;
817
818 // now, Booth encode it
819 std::string boothEncodedString;
820 boothEncode(constantString, boothEncodedString);
821
822 std::vector<struct shiftaddblob> ops;
823 // do the transformation, filling out 'ops'
824 lefevre(boothEncodedString, ops);
825
Duraid Madina9f406cb2005-05-15 14:44:13 +0000826 assert(ops.size() < 80 && "constmul code has gone haywire\n");
827 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000828
Duraid Madina81ebb572005-04-26 07:23:02 +0000829 // now turn 'ops' into DAG bits
Duraid Madinab071bef2005-04-27 11:57:39 +0000830 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madina81ebb572005-04-26 07:23:02 +0000831 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
832 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
833 results[ops[i].firstVal-1];
834 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
835 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
836 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
837 results[ops[i].secondVal-1];
838 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
839 if(ops[i].isSub)
840 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
841 else
842 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
843 }
844
845 // don't forget flippedSign and preliminaryShift!
Duraid Madina7acd5d52005-05-02 07:27:14 +0000846 SDOperand shiftedresult;
Duraid Madina81ebb572005-04-26 07:23:02 +0000847 if(preliminaryShift) {
848 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina7acd5d52005-05-02 07:27:14 +0000849 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000850 results[ops.size()-1], finalshift);
Duraid Madina81ebb572005-04-26 07:23:02 +0000851 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina7acd5d52005-05-02 07:27:14 +0000852 shiftedresult = results[ops.size()-1];
Duraid Madina81ebb572005-04-26 07:23:02 +0000853 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000854
Duraid Madina7acd5d52005-05-02 07:27:14 +0000855 SDOperand finalresult;
856 if(flippedSign) { // if we were multiplying by a negative constant:
857 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
858 // subtract the result from 0 to flip its sign
859 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
860 } else { // there was no preliminary multiply by -1 required
861 finalresult = shiftedresult;
862 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000863
864 return finalresult;
Duraid Madina91ed0a12005-03-17 18:17:03 +0000865}
866
Duraid Madinac36b6c32005-04-06 09:55:17 +0000867/// ponderIntegerDivisionBy - When handling integer divides, if the divide
868/// is by a constant such that we can efficiently codegen it, this
869/// function says what to do. Currently, it returns 0 if the division must
870/// become a genuine divide, and 1 if the division can be turned into a
871/// right shift.
872static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
873 unsigned& Imm) {
874 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
875 // a constant, give up.
876
877 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
878
Chris Lattner6667bdb2005-08-02 19:26:06 +0000879 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
880 Imm = Log2_64(v);
Duraid Madinac36b6c32005-04-06 09:55:17 +0000881 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000882 }
883
Duraid Madinac36b6c32005-04-06 09:55:17 +0000884 return 0; // fallthrough
885}
886
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000887static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
888 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
889 // a constant, give up.
890
891 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
892
Chris Lattner6667bdb2005-08-02 19:26:06 +0000893 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskey9b0a2752005-08-20 11:05:23 +0000894 Imm = Log2_64(v) + 1;
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000895 return 1; // say so
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000896 }
897
Duraid Madinae7ef27b2005-04-13 04:50:54 +0000898 return 0; // fallthrough
899}
900
Duraid Madina8419da82005-04-07 12:33:38 +0000901static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
902 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
903 // constant, give up.
904 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
905
906 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
907 Imm = v & 0x3FFF; // 14 bits
908 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000909 }
Duraid Madina8419da82005-04-07 12:33:38 +0000910 return 0; // fallthrough
911}
912
913static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
914 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
915 // constant, give up.
916 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
917
918 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
919 Imm = v & 0xFF; // 8 bits
920 return 1;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000921 }
Duraid Madina8419da82005-04-07 12:33:38 +0000922 return 0; // fallthrough
923}
924
Duraid Madina91ed0a12005-03-17 18:17:03 +0000925unsigned ISel::SelectExpr(SDOperand N) {
926 unsigned Result;
927 unsigned Tmp1, Tmp2, Tmp3;
928 unsigned Opc = 0;
929 MVT::ValueType DestType = N.getValueType();
930
931 unsigned opcode = N.getOpcode();
932
933 SDNode *Node = N.Val;
934 SDOperand Op0, Op1;
935
936 if (Node->getOpcode() == ISD::CopyFromReg)
937 // Just use the specified register as our input.
Chris Lattner7c762782005-08-16 21:56:37 +0000938 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000939
Duraid Madina91ed0a12005-03-17 18:17:03 +0000940 unsigned &Reg = ExprMap[N];
941 if (Reg) return Reg;
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000942
Chris Lattner8abab9b2005-05-13 20:29:26 +0000943 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina91ed0a12005-03-17 18:17:03 +0000944 Reg = Result = (N.getValueType() != MVT::Other) ?
945 MakeReg(N.getValueType()) : 1;
946 else {
947 // If this is a call instruction, make sure to prepare ALL of the result
948 // values as well as the chain.
949 if (Node->getNumValues() == 1)
950 Reg = Result = 1; // Void call, just a chain.
951 else {
952 Result = MakeReg(Node->getValueType(0));
953 ExprMap[N.getValue(0)] = Result;
954 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
955 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
956 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
957 }
958 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +0000959
Duraid Madina91ed0a12005-03-17 18:17:03 +0000960 switch (N.getOpcode()) {
961 default:
962 Node->dump();
963 assert(0 && "Node not handled!\n");
964
965 case ISD::FrameIndex: {
966 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
967 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
968 return Result;
969 }
970
971 case ISD::ConstantPool: {
Chris Lattnerc30405e2005-08-26 17:15:30 +0000972 Tmp1 = BB->getParent()->getConstantPool()->
973 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina91ed0a12005-03-17 18:17:03 +0000974 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
975 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
976 .addReg(IA64::r1);
977 return Result;
978 }
979
980 case ISD::ConstantFP: {
981 Tmp1 = Result; // Intermediate Register
982 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
983 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
984 Tmp1 = MakeReg(MVT::f64);
985
986 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
987 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
988 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
989 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
990 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
991 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
992 else
993 assert(0 && "Unexpected FP constant!");
994 if (Tmp1 != Result)
995 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
996 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukmane73e76d2005-04-22 17:54:37 +0000997 .addReg(IA64::F0);
Duraid Madina91ed0a12005-03-17 18:17:03 +0000998 return Result;
999 }
1000
1001 case ISD::DYNAMIC_STACKALLOC: {
1002 // Generate both result values.
1003 if (Result != 1)
1004 ExprMap[N.getValue(1)] = 1; // Generate the token
1005 else
1006 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1007
1008 // FIXME: We are currently ignoring the requested alignment for handling
1009 // greater than the stack alignment. This will need to be revisited at some
1010 // point. Align = N.getOperand(2);
1011
1012 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1013 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1014 std::cerr << "Cannot allocate stack object with greater alignment than"
1015 << " the stack alignment yet!";
1016 abort();
1017 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001018
1019/*
Duraid Madina91ed0a12005-03-17 18:17:03 +00001020 Select(N.getOperand(0));
1021 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1022 {
1023 if (CN->getValue() < 32000)
1024 {
1025 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001026 .addImm(-CN->getValue());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001027 } else {
1028 Tmp1 = SelectExpr(N.getOperand(1));
1029 // Subtract size from stack pointer, thereby allocating some space.
1030 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1031 }
1032 } else {
1033 Tmp1 = SelectExpr(N.getOperand(1));
1034 // Subtract size from stack pointer, thereby allocating some space.
1035 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1036 }
Duraid Madina21ce5f72005-03-31 12:31:11 +00001037*/
1038 Select(N.getOperand(0));
1039 Tmp1 = SelectExpr(N.getOperand(1));
1040 // Subtract size from stack pointer, thereby allocating some space.
1041 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001042 // Put a pointer to the space into the result register, by copying the
1043 // stack pointer.
1044 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1045 return Result;
1046 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001047
Duraid Madina91ed0a12005-03-17 18:17:03 +00001048 case ISD::SELECT: {
1049 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1050 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1051 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1052
Duraid Madina91ed0a12005-03-17 18:17:03 +00001053 unsigned bogoResult;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001054
Duraid Madina91ed0a12005-03-17 18:17:03 +00001055 switch (N.getOperand(1).getValueType()) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001056 default: assert(0 &&
Duraid Madina0e731882005-05-02 06:41:13 +00001057 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1058 // for i1, we load the condition into an integer register, then
1059 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1060 // of them will go through, since the integer register will hold
1061 // either 0 or 1)
1062 case MVT::i1: {
1063 bogoResult=MakeReg(MVT::i1);
1064
1065 // load the condition into an integer register
1066 unsigned condReg=MakeReg(MVT::i64);
1067 unsigned dummy=MakeReg(MVT::i64);
1068 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1069 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1070 .addImm(1).addReg(Tmp1);
1071
1072 // initialize Result (bool) to false (hence UNC) and if
1073 // the select condition (condReg) is false (0), copy Tmp3
1074 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1075 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1076
1077 // now, if the selection condition is true, write 1 to the
1078 // result if Tmp2 is 1
1079 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1080 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1081 break;
1082 }
1083 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1084 // with Tmp2 if Tmp1 is true
Misha Brukmane73e76d2005-04-22 17:54:37 +00001085 case MVT::i64:
1086 bogoResult=MakeReg(MVT::i64);
Duraid Madina0e731882005-05-02 06:41:13 +00001087 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1088 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1089 .addReg(Tmp1);
Misha Brukmane73e76d2005-04-22 17:54:37 +00001090 break;
1091 case MVT::f64:
1092 bogoResult=MakeReg(MVT::f64);
Duraid Madina0e731882005-05-02 06:41:13 +00001093 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1094 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1095 .addReg(Tmp1);
Misha Brukmane73e76d2005-04-22 17:54:37 +00001096 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001097 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001098
Duraid Madina91ed0a12005-03-17 18:17:03 +00001099 return Result;
1100 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001101
Duraid Madina91ed0a12005-03-17 18:17:03 +00001102 case ISD::Constant: {
1103 unsigned depositPos=0;
1104 unsigned depositLen=0;
1105 switch (N.getValueType()) {
1106 default: assert(0 && "Cannot use constants of this type!");
1107 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukmane73e76d2005-04-22 17:54:37 +00001108 // the constant:
1109 if(cast<ConstantSDNode>(N)->getValue()) // true:
1110 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1111 else // false:
1112 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1113 return Result; // early exit
1114 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001115 case MVT::i64: break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001116 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001117
Duraid Madina91ed0a12005-03-17 18:17:03 +00001118 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madinafb43ef72005-04-11 05:55:56 +00001119
1120 if(immediate==0) { // if the constant is just zero,
1121 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1122 return Result; // early exit
Duraid Madina91ed0a12005-03-17 18:17:03 +00001123 }
1124
Duraid Madinafb43ef72005-04-11 05:55:56 +00001125 if (immediate <= 8191 && immediate >= -8192) {
1126 // if this constants fits in 14 bits, we use a mov the assembler will
1127 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1128 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1129 return Result; // early exit
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001130 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001131
1132 if (immediate <= 2097151 && immediate >= -2097152) {
1133 // if this constants fits in 22 bits, we use a mov the assembler will
1134 // turn into: "addl rDest=imm,r0"
1135 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1136 return Result; // early exit
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001137 }
Duraid Madinafb43ef72005-04-11 05:55:56 +00001138
1139 /* otherwise, our immediate is big, so we use movl */
1140 uint64_t Imm = immediate;
Duraid Madina8de7ac02005-04-11 07:16:39 +00001141 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madinafb43ef72005-04-11 05:55:56 +00001142 return Result;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001143 }
Duraid Madinac935c832005-04-02 10:33:53 +00001144
1145 case ISD::UNDEF: {
1146 BuildMI(BB, IA64::IDEF, 0, Result);
1147 return Result;
1148 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001149
Duraid Madina91ed0a12005-03-17 18:17:03 +00001150 case ISD::GlobalAddress: {
1151 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1152 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001153
Duraid Madina91ed0a12005-03-17 18:17:03 +00001154 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001155 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001156
Duraid Madina91ed0a12005-03-17 18:17:03 +00001157 return Result;
1158 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001159
Duraid Madina91ed0a12005-03-17 18:17:03 +00001160 case ISD::ExternalSymbol: {
1161 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madina21ce5f72005-03-31 12:31:11 +00001162// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1163 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina91ed0a12005-03-17 18:17:03 +00001164 return Result;
1165 }
1166
1167 case ISD::FP_EXTEND: {
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1170 return Result;
1171 }
1172
Chris Lattner2493f0e2005-09-02 00:15:30 +00001173 case ISD::ANY_EXTEND:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001174 case ISD::ZERO_EXTEND: {
1175 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001176
Chris Lattner2493f0e2005-09-02 00:15:30 +00001177 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1178 "Cannot zero-extend this type!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001179
Chris Lattner2493f0e2005-09-02 00:15:30 +00001180 // if the predicate reg has 1, we want a '1' in our GR.
1181 unsigned dummy = MakeReg(MVT::i64);
1182 // first load zero:
1183 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1184 // ...then conditionally (PR:Tmp1) add 1:
1185 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1186 .addImm(1).addReg(Tmp1);
1187 return Result; // XXX early exit!
1188 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001189
Chris Lattner2493f0e2005-09-02 00:15:30 +00001190 case ISD::SIGN_EXTEND:
1191 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1192 "Cannot zero-extend this type!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001193
1194 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner2493f0e2005-09-02 00:15:30 +00001195 assert(0 && "don't know how to sign_extend from bool yet!");
1196 abort();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001197
Chris Lattner2493f0e2005-09-02 00:15:30 +00001198 case ISD::TRUNCATE:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001199 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1200 // of R0 appropriately.
Chris Lattner2493f0e2005-09-02 00:15:30 +00001201 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1202 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina91ed0a12005-03-17 18:17:03 +00001204
Chris Lattner2493f0e2005-09-02 00:15:30 +00001205 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1206 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1207 return Result; // XXX early exit!
Duraid Madina91ed0a12005-03-17 18:17:03 +00001208
Misha Brukmane73e76d2005-04-22 17:54:37 +00001209/*
Duraid Madina91ed0a12005-03-17 18:17:03 +00001210 case ISD::FP_ROUND: {
1211 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001212 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001213 Tmp1 = SelectExpr(N.getOperand(0));
1214 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1215 // we add 0.0 using a single precision add to do rounding
1216 return Result;
1217 }
1218*/
1219
1220// FIXME: the following 4 cases need cleaning
1221 case ISD::SINT_TO_FP: {
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = MakeReg(MVT::f64);
1224 unsigned dummy = MakeReg(MVT::f64);
1225 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1226 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1227 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1228 return Result;
1229 }
1230
1231 case ISD::UINT_TO_FP: {
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = MakeReg(MVT::f64);
1234 unsigned dummy = MakeReg(MVT::f64);
1235 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1236 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1237 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1238 return Result;
1239 }
1240
1241 case ISD::FP_TO_SINT: {
1242 Tmp1 = SelectExpr(N.getOperand(0));
1243 Tmp2 = MakeReg(MVT::f64);
1244 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1245 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1246 return Result;
1247 }
1248
1249 case ISD::FP_TO_UINT: {
1250 Tmp1 = SelectExpr(N.getOperand(0));
1251 Tmp2 = MakeReg(MVT::f64);
1252 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1253 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1254 return Result;
1255 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001256
1257 case ISD::FADD: {
1258 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1259 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1260 // into an fma, do so:
1261 // ++FusedFP; // Statistic
Duraid Madinac36b6c32005-04-06 09:55:17 +00001262 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1263 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1264 Tmp3 = SelectExpr(N.getOperand(1));
1265 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1266 return Result; // early exit
1267 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001268
1269 //else, fallthrough:
1270 Tmp1 = SelectExpr(N.getOperand(0));
1271 Tmp2 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1273 return Result;
1274 }
Duraid Madina2f231252005-04-13 06:12:04 +00001275
Chris Lattner0815dcae2005-09-28 22:29:17 +00001276 case ISD::ADD: {
1277 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001278 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madina2f231252005-04-13 06:12:04 +00001279 // this add into a shladd, try:
1280 ConstantSDNode *CSD = NULL;
1281 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukmane73e76d2005-04-22 17:54:37 +00001282 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madina2f231252005-04-13 06:12:04 +00001283
Misha Brukmane73e76d2005-04-22 17:54:37 +00001284 // ++FusedSHLADD; // Statistic
1285 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1286 int shl_amt = CSD->getValue();
1287 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001288
Misha Brukmane73e76d2005-04-22 17:54:37 +00001289 BuildMI(BB, IA64::SHLADD, 3, Result)
1290 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1291 return Result; // early exit
Duraid Madina2f231252005-04-13 06:12:04 +00001292 }
1293 }
1294
1295 //else, fallthrough:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001296 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001297 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1298 case 1: // adding a constant that's 14 bits
1299 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1300 return Result; // early exit
1301 } // fallthrough and emit a reg+reg ADD:
1302 Tmp2 = SelectExpr(N.getOperand(1));
1303 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001304 return Result;
1305 }
1306
Chris Lattner0815dcae2005-09-28 22:29:17 +00001307 case ISD::FMUL:
1308 Tmp1 = SelectExpr(N.getOperand(0));
1309 Tmp2 = SelectExpr(N.getOperand(1));
1310 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1311 return Result;
1312
Duraid Madina91ed0a12005-03-17 18:17:03 +00001313 case ISD::MUL: {
Duraid Madinac36b6c32005-04-06 09:55:17 +00001314
Chris Lattner0815dcae2005-09-28 22:29:17 +00001315 // TODO: speed!
Duraid Madina1c2f9fd2005-08-10 12:38:57 +00001316/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1317 */
Chris Lattner0815dcae2005-09-28 22:29:17 +00001318 // boring old integer multiply with xma
Duraid Madina81ebb572005-04-26 07:23:02 +00001319 Tmp1 = SelectExpr(N.getOperand(0));
1320 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001321
1322 unsigned TempFR1=MakeReg(MVT::f64);
1323 unsigned TempFR2=MakeReg(MVT::f64);
1324 unsigned TempFR3=MakeReg(MVT::f64);
1325 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1326 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1327 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1328 .addReg(IA64::F0);
1329 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1330 return Result; // early exit
1331 /* FIXME } else { // we are multiplying by an integer constant! yay
1332 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1333 } */
Duraid Madina91ed0a12005-03-17 18:17:03 +00001334 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001335
Chris Lattner0815dcae2005-09-28 22:29:17 +00001336 case ISD::FSUB:
1337 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madinac36b6c32005-04-06 09:55:17 +00001338 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1339 // into an fms, do so:
Chris Lattner0815dcae2005-09-28 22:29:17 +00001340 // ++FusedFP; // Statistic
Duraid Madinac36b6c32005-04-06 09:55:17 +00001341 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1342 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1343 Tmp3 = SelectExpr(N.getOperand(1));
1344 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1345 return Result; // early exit
1346 }
Chris Lattner0815dcae2005-09-28 22:29:17 +00001347
Duraid Madina91ed0a12005-03-17 18:17:03 +00001348 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner0815dcae2005-09-28 22:29:17 +00001349 Tmp1 = SelectExpr(N.getOperand(0));
1350 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1351 return Result;
1352
1353 case ISD::SUB: {
1354 Tmp2 = SelectExpr(N.getOperand(1));
1355 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1356 case 1: // subtracting *from* an 8 bit constant:
1357 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1358 return Result; // early exit
1359 } // fallthrough and emit a reg+reg SUB:
1360 Tmp1 = SelectExpr(N.getOperand(0));
1361 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001362 return Result;
1363 }
Duraid Madina0720dc12005-04-02 05:18:38 +00001364
1365 case ISD::FABS: {
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1368 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1369 return Result;
1370 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001371
Duraid Madina0720dc12005-04-02 05:18:38 +00001372 case ISD::FNEG: {
Duraid Madina0720dc12005-04-02 05:18:38 +00001373 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madinac935c832005-04-02 10:33:53 +00001374
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001375 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madinac935c832005-04-02 10:33:53 +00001376 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1377 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1378 } else {
1379 Tmp1 = SelectExpr(N.getOperand(0));
1380 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1381 }
1382
Duraid Madina0720dc12005-04-02 05:18:38 +00001383 return Result;
1384 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001385
Duraid Madina91ed0a12005-03-17 18:17:03 +00001386 case ISD::AND: {
1387 switch (N.getValueType()) {
1388 default: assert(0 && "Cannot AND this type!");
1389 case MVT::i1: { // if a bool, we emit a pseudocode AND
1390 unsigned pA = SelectExpr(N.getOperand(0));
1391 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001392
Duraid Madina91ed0a12005-03-17 18:17:03 +00001393/* our pseudocode for AND is:
1394 *
1395(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1396 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1397 ;;
1398(pB) cmp.ne pTemp,p0 = r0,r0
1399 ;;
1400(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1401
1402*/
1403 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001404
Duraid Madina91ed0a12005-03-17 18:17:03 +00001405 unsigned bogusTemp1 = MakeReg(MVT::i1);
1406 unsigned bogusTemp2 = MakeReg(MVT::i1);
1407 unsigned bogusTemp3 = MakeReg(MVT::i1);
1408 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001409
Duraid Madina91ed0a12005-03-17 18:17:03 +00001410 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001411 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001412 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001413 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001414 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001415 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001416 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001417 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001418 break;
1419 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001420
Duraid Madina91ed0a12005-03-17 18:17:03 +00001421 // if not a bool, we just AND away:
1422 case MVT::i8:
1423 case MVT::i16:
1424 case MVT::i32:
1425 case MVT::i64: {
1426 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina3588ea92005-08-21 15:43:53 +00001427 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinae7ef27b2005-04-13 04:50:54 +00001428 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukmane73e76d2005-04-22 17:54:37 +00001429 switch (Tmp3) {
1430 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1431 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1432 break;
1433 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1434 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1435 break;
1436 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1437 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1438 break;
1439 default: // otherwise, use dep.z to paste zeros
Duraid Madina3588ea92005-08-21 15:43:53 +00001440 // FIXME: assert the dep.z is in bounds
1441 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001442 .addImm(0).addImm(Tmp3);
1443 break;
Duraid Madina3588ea92005-08-21 15:43:53 +00001444 }
1445 return Result; // early exit
1446 } // fallthrough and emit a simple AND:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001447 Tmp2 = SelectExpr(N.getOperand(1));
1448 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001449 }
1450 }
1451 return Result;
1452 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001453
Duraid Madina91ed0a12005-03-17 18:17:03 +00001454 case ISD::OR: {
1455 switch (N.getValueType()) {
1456 default: assert(0 && "Cannot OR this type!");
1457 case MVT::i1: { // if a bool, we emit a pseudocode OR
1458 unsigned pA = SelectExpr(N.getOperand(0));
1459 unsigned pB = SelectExpr(N.getOperand(1));
1460
1461 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001462
Duraid Madina91ed0a12005-03-17 18:17:03 +00001463/* our pseudocode for OR is:
1464 *
1465
1466pC = pA OR pB
1467-------------
1468
Misha Brukmane73e76d2005-04-22 17:54:37 +00001469(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1470 ;;
1471(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina91ed0a12005-03-17 18:17:03 +00001472
1473*/
1474 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001475 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madinaf221c262005-10-28 17:46:35 +00001476 BuildMI(BB, IA64::TPCMPEQ, 4, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001477 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001478 break;
1479 }
1480 // if not a bool, we just OR away:
1481 case MVT::i8:
1482 case MVT::i16:
1483 case MVT::i32:
1484 case MVT::i64: {
1485 Tmp1 = SelectExpr(N.getOperand(0));
1486 Tmp2 = SelectExpr(N.getOperand(1));
1487 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1488 break;
1489 }
1490 }
1491 return Result;
1492 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001493
Duraid Madina91ed0a12005-03-17 18:17:03 +00001494 case ISD::XOR: {
1495 switch (N.getValueType()) {
1496 default: assert(0 && "Cannot XOR this type!");
1497 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1498 unsigned pY = SelectExpr(N.getOperand(0));
1499 unsigned pZ = SelectExpr(N.getOperand(1));
1500
1501/* one possible routine for XOR is:
1502
1503 // Compute px = py ^ pz
1504 // using sum of products: px = (py & !pz) | (pz & !py)
1505 // Uses 5 instructions in 3 cycles.
1506 // cycle 1
1507(pz) cmp.eq.unc px = r0, r0 // px = pz
1508(py) cmp.eq.unc pt = r0, r0 // pt = py
1509 ;;
1510 // cycle 2
1511(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1512(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1513 ;;
1514 } { .mmi
1515 // cycle 3
1516(pt) cmp.eq.or px = r0, r0 // px = px | pt
1517
1518*** Another, which we use here, requires one scratch GR. it is:
1519
1520 mov rt = 0 // initialize rt off critical path
1521 ;;
1522
1523 // cycle 1
1524(pz) cmp.eq.unc px = r0, r0 // px = pz
1525(pz) mov rt = 1 // rt = pz
1526 ;;
1527 // cycle 2
1528(py) cmp.ne px = 1, rt // if (py) px = !pz
1529
1530.. these routines kindly provided by Jim Hull
1531*/
1532 unsigned rt = MakeReg(MVT::i64);
1533
1534 // these two temporaries will never actually appear,
1535 // due to the two-address form of some of the instructions below
1536 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1537 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1538
1539 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1540 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001541 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001542 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001543 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001544 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001545 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001546 break;
1547 }
1548 // if not a bool, we just XOR away:
1549 case MVT::i8:
1550 case MVT::i16:
1551 case MVT::i32:
1552 case MVT::i64: {
1553 Tmp1 = SelectExpr(N.getOperand(0));
1554 Tmp2 = SelectExpr(N.getOperand(1));
1555 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1556 break;
1557 }
1558 }
1559 return Result;
1560 }
1561
Duraid Madina25163d82005-05-11 05:16:09 +00001562 case ISD::CTPOP: {
1563 Tmp1 = SelectExpr(N.getOperand(0));
1564 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1565 return Result;
1566 }
1567
Duraid Madina91ed0a12005-03-17 18:17:03 +00001568 case ISD::SHL: {
1569 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001570 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1571 Tmp2 = CN->getValue();
1572 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1573 } else {
1574 Tmp2 = SelectExpr(N.getOperand(1));
1575 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1576 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001577 return Result;
1578 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001579
Duraid Madina91ed0a12005-03-17 18:17:03 +00001580 case ISD::SRL: {
1581 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001582 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1583 Tmp2 = CN->getValue();
1584 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1585 } else {
1586 Tmp2 = SelectExpr(N.getOperand(1));
1587 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1588 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001589 return Result;
1590 }
Misha Brukmane73e76d2005-04-22 17:54:37 +00001591
Duraid Madina91ed0a12005-03-17 18:17:03 +00001592 case ISD::SRA: {
1593 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina8419da82005-04-07 12:33:38 +00001594 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1595 Tmp2 = CN->getValue();
1596 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1597 } else {
1598 Tmp2 = SelectExpr(N.getOperand(1));
1599 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1600 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001601 return Result;
1602 }
1603
Chris Lattner0815dcae2005-09-28 22:29:17 +00001604 case ISD::FDIV:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001605 case ISD::SDIV:
1606 case ISD::UDIV:
1607 case ISD::SREM:
1608 case ISD::UREM: {
1609
1610 Tmp1 = SelectExpr(N.getOperand(0));
1611 Tmp2 = SelectExpr(N.getOperand(1));
1612
1613 bool isFP=false;
1614
1615 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1616 isFP=true;
1617
1618 bool isModulus=false; // is it a division or a modulus?
1619 bool isSigned=false;
1620
1621 switch(N.getOpcode()) {
Chris Lattner0815dcae2005-09-28 22:29:17 +00001622 case ISD::FDIV:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001623 case ISD::SDIV: isModulus=false; isSigned=true; break;
1624 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001625 case ISD::FREM:
Duraid Madina91ed0a12005-03-17 18:17:03 +00001626 case ISD::SREM: isModulus=true; isSigned=true; break;
1627 case ISD::UREM: isModulus=true; isSigned=false; break;
1628 }
1629
Duraid Madinac36b6c32005-04-06 09:55:17 +00001630 if(!isModulus && !isFP) { // if this is an integer divide,
1631 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001632 case 1: // division by a constant that's a power of 2
1633 Tmp1 = SelectExpr(N.getOperand(0));
1634 if(isSigned) { // argument could be negative, so emit some code:
1635 unsigned divAmt=Tmp3;
1636 unsigned tempGR1=MakeReg(MVT::i64);
1637 unsigned tempGR2=MakeReg(MVT::i64);
1638 unsigned tempGR3=MakeReg(MVT::i64);
1639 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1640 .addReg(Tmp1).addImm(divAmt-1);
1641 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1642 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1643 BuildMI(BB, IA64::ADD, 2, tempGR3)
1644 .addReg(Tmp1).addReg(tempGR2);
1645 BuildMI(BB, IA64::SHRS, 2, Result)
1646 .addReg(tempGR3).addImm(divAmt);
1647 }
1648 else // unsigned div-by-power-of-2 becomes a simple shift right:
1649 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1650 return Result; // early exit
Duraid Madinac36b6c32005-04-06 09:55:17 +00001651 }
1652 }
1653
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001654 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madina21ce5f72005-03-31 12:31:11 +00001655 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina91ed0a12005-03-17 18:17:03 +00001656 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1657 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1658 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1659 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1660 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1661 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1662 unsigned TmpF7=MakeReg(MVT::f64);
1663 unsigned TmpF8=MakeReg(MVT::f64);
1664 unsigned TmpF9=MakeReg(MVT::f64);
1665 unsigned TmpF10=MakeReg(MVT::f64);
1666 unsigned TmpF11=MakeReg(MVT::f64);
1667 unsigned TmpF12=MakeReg(MVT::f64);
1668 unsigned TmpF13=MakeReg(MVT::f64);
1669 unsigned TmpF14=MakeReg(MVT::f64);
1670 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001671
Duraid Madina91ed0a12005-03-17 18:17:03 +00001672 // OK, emit some code:
1673
1674 if(!isFP) {
1675 // first, load the inputs into FP regs.
1676 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1677 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001678
Duraid Madina91ed0a12005-03-17 18:17:03 +00001679 // next, convert the inputs to FP
1680 if(isSigned) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001681 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1682 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001683 } else {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001684 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1685 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001686 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001687
Duraid Madina91ed0a12005-03-17 18:17:03 +00001688 } else { // this is an FP divide/remainder, so we 'leak' some temp
1689 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1690 TmpF3=Tmp1;
1691 TmpF4=Tmp2;
1692 }
1693
1694 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina41ff5022005-04-08 10:01:48 +00001695 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1696 BuildMI(BB, IA64::FRCPAS1, 4)
1697 .addReg(TmpF5, MachineOperand::Def)
1698 .addReg(TmpPR, MachineOperand::Def)
1699 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001700
Duraid Madina21ce5f72005-03-31 12:31:11 +00001701 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1702 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1703 // TPCMPNE below
1704 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1705 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001706 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001707 }
1708
Duraid Madina91ed0a12005-03-17 18:17:03 +00001709 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1710 // precision, don't need this much for f32/i32)
1711 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1712 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1713 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1714 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1715 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1716 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1717 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1718 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1719 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1720 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1721 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1722 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1723 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1724 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1725 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1726 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina9935f442005-04-04 05:05:52 +00001727
1728 // FIXME: this is unfortunate :(
1729 // the story is that the dest reg of the fnma above and the fma below
1730 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1731 // be the same register, or this code breaks if the first argument is
1732 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina91ed0a12005-03-17 18:17:03 +00001733 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1734 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1735
Duraid Madina9935f442005-04-04 05:05:52 +00001736 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1737 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1738 }
1739
Duraid Madina91ed0a12005-03-17 18:17:03 +00001740 if(!isFP) {
1741 // round to an integer
1742 if(isSigned)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001743 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001744 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00001745 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001746 } else {
1747 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1748 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1749 // we really do need the above FMOV? ;)
1750 }
1751
1752 if(!isModulus) {
Duraid Madina21ce5f72005-03-31 12:31:11 +00001753 if(isFP) { // extra worrying about div-by-zero
1754 unsigned bogoResult=MakeReg(MVT::f64);
1755
1756 // we do a 'conditional fmov' (of the correct result, depending
1757 // on how the frcpa predicate turned out)
1758 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001759 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001760 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001761 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madina21ce5f72005-03-31 12:31:11 +00001762 }
Duraid Madina9935f442005-04-04 05:05:52 +00001763 else {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001764 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina9935f442005-04-04 05:05:52 +00001765 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001766 } else { // this is a modulus
1767 if(!isFP) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001768 // answer = q * (-b) + a
1769 unsigned ModulusResult = MakeReg(MVT::f64);
1770 unsigned TmpF = MakeReg(MVT::f64);
1771 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001772
Misha Brukmane73e76d2005-04-22 17:54:37 +00001773 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1774 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1775 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1776 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1777 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001778 } else { // FP modulus! The horror... the horror....
Misha Brukmane73e76d2005-04-22 17:54:37 +00001779 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina91ed0a12005-03-17 18:17:03 +00001780 }
1781 }
1782
1783 return Result;
1784 }
1785
Duraid Madina91ed0a12005-03-17 18:17:03 +00001786 case ISD::SIGN_EXTEND_INREG: {
1787 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner53676df2005-07-10 01:56:13 +00001788 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina91ed0a12005-03-17 18:17:03 +00001789 default:
1790 Node->dump();
1791 assert(0 && "don't know how to sign extend this type");
1792 break;
1793 case MVT::i8: Opc = IA64::SXT1; break;
1794 case MVT::i16: Opc = IA64::SXT2; break;
1795 case MVT::i32: Opc = IA64::SXT4; break;
1796 }
1797 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1798 return Result;
1799 }
1800
1801 case ISD::SETCC: {
1802 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner6ec77452005-08-09 20:21:10 +00001803 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1804 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madinafb43ef72005-04-11 05:55:56 +00001805
Chris Lattner6ec77452005-08-09 20:21:10 +00001806 if(ConstantSDNode *CSDN =
1807 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1808 // if we are comparing against a constant zero
1809 if(CSDN->getValue()==0)
1810 Tmp2 = IA64::r0; // then we can just compare against r0
1811 else
1812 Tmp2 = SelectExpr(N.getOperand(1));
1813 } else // not comparing against a constant
1814 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinafb43ef72005-04-11 05:55:56 +00001815
Chris Lattner6ec77452005-08-09 20:21:10 +00001816 switch (CC) {
1817 default: assert(0 && "Unknown integer comparison!");
1818 case ISD::SETEQ:
1819 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 break;
1821 case ISD::SETGT:
1822 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 break;
1824 case ISD::SETGE:
1825 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1826 break;
1827 case ISD::SETLT:
1828 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 break;
1830 case ISD::SETLE:
1831 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1832 break;
1833 case ISD::SETNE:
1834 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1835 break;
1836 case ISD::SETULT:
1837 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1838 break;
1839 case ISD::SETUGT:
1840 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1841 break;
1842 case ISD::SETULE:
1843 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 break;
1845 case ISD::SETUGE:
1846 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1847 break;
1848 }
1849 } else { // if not integer, should be FP.
1850 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1851 "error: SETCC should have had incoming f32 promoted to f64!\n");
1852
1853 if(ConstantFPSDNode *CFPSDN =
1854 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1855
1856 // if we are comparing against a constant +0.0 or +1.0
1857 if(CFPSDN->isExactlyValue(+0.0))
1858 Tmp2 = IA64::F0; // then we can just compare against f0
1859 else if(CFPSDN->isExactlyValue(+1.0))
1860 Tmp2 = IA64::F1; // or f1
Misha Brukmane73e76d2005-04-22 17:54:37 +00001861 else
1862 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner6ec77452005-08-09 20:21:10 +00001863 } else // not comparing against a constant
1864 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001865
Chris Lattner6ec77452005-08-09 20:21:10 +00001866 switch (CC) {
1867 default: assert(0 && "Unknown FP comparison!");
1868 case ISD::SETEQ:
1869 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 break;
1871 case ISD::SETGT:
1872 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1873 break;
1874 case ISD::SETGE:
1875 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1876 break;
1877 case ISD::SETLT:
1878 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1879 break;
1880 case ISD::SETLE:
1881 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1882 break;
1883 case ISD::SETNE:
1884 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1885 break;
1886 case ISD::SETULT:
1887 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1888 break;
1889 case ISD::SETUGT:
1890 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1891 break;
1892 case ISD::SETULE:
1893 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1894 break;
1895 case ISD::SETUGE:
1896 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1897 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001898 }
1899 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00001900 return Result;
1901 }
1902
1903 case ISD::EXTLOAD:
1904 case ISD::ZEXTLOAD:
1905 case ISD::LOAD: {
1906 // Make sure we generate both values.
1907 if (Result != 1)
1908 ExprMap[N.getValue(1)] = 1; // Generate the token
1909 else
1910 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1911
1912 bool isBool=false;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001913
Duraid Madina91ed0a12005-03-17 18:17:03 +00001914 if(opcode == ISD::LOAD) { // this is a LOAD
1915 switch (Node->getValueType(0)) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001916 default: assert(0 && "Cannot load this type!");
1917 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1918 // FIXME: for now, we treat bool loads the same as i8 loads */
1919 case MVT::i8: Opc = IA64::LD1; break;
1920 case MVT::i16: Opc = IA64::LD2; break;
1921 case MVT::i32: Opc = IA64::LD4; break;
1922 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001923
Misha Brukmane73e76d2005-04-22 17:54:37 +00001924 case MVT::f32: Opc = IA64::LDF4; break;
1925 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001926 }
1927 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattner53676df2005-07-10 01:56:13 +00001928 MVT::ValueType TypeBeingLoaded =
1929 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina91ed0a12005-03-17 18:17:03 +00001930 switch (TypeBeingLoaded) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00001931 default: assert(0 && "Cannot extload/zextload this type!");
1932 // FIXME: bools?
1933 case MVT::i8: Opc = IA64::LD1; break;
1934 case MVT::i16: Opc = IA64::LD2; break;
1935 case MVT::i32: Opc = IA64::LD4; break;
1936 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00001937 }
1938 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001939
Duraid Madina91ed0a12005-03-17 18:17:03 +00001940 SDOperand Chain = N.getOperand(0);
1941 SDOperand Address = N.getOperand(1);
1942
1943 if(Address.getOpcode() == ISD::GlobalAddress) {
1944 Select(Chain);
1945 unsigned dummy = MakeReg(MVT::i64);
1946 unsigned dummy2 = MakeReg(MVT::i64);
1947 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001948 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1949 .addReg(IA64::r1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001950 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1951 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001952 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001953 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001954 // into a predicate register
1955 assert(Opc==IA64::LD1 && "problem loading a bool");
1956 unsigned dummy3 = MakeReg(MVT::i64);
1957 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1958 // we compare to 0. true? 0. false? 1.
1959 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001960 }
1961 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattnerc30405e2005-08-26 17:15:30 +00001962 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00001963 getConstantPoolIndex(CP->get());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001964 Select(Chain);
1965 IA64Lowering.restoreGP(BB);
1966 unsigned dummy = MakeReg(MVT::i64);
Duraid Madinaf221c262005-10-28 17:46:35 +00001967 unsigned dummy2 = MakeReg(MVT::i64);
1968 BuildMI(BB, IA64::MOVLIMM64, 1, dummy2).addConstantPoolIndex(CPIdx);
1969 BuildMI(BB, IA64::ADD, 2, dummy).addReg(dummy2).addReg(IA64::r1); //CPI+GP
1970
1971
1972 // OLD BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
1973 // (FIXME!) .addReg(IA64::r1); // CPI+GP
Duraid Madina91ed0a12005-03-17 18:17:03 +00001974 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001975 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001976 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001977 // into a predicate register
1978 assert(Opc==IA64::LD1 && "problem loading a bool");
1979 unsigned dummy3 = MakeReg(MVT::i64);
1980 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1981 // we compare to 0. true? 0. false? 1.
1982 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001983 }
1984 } else if(Address.getOpcode() == ISD::FrameIndex) {
1985 Select(Chain); // FIXME ? what about bools?
1986 unsigned dummy = MakeReg(MVT::i64);
1987 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001988 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina91ed0a12005-03-17 18:17:03 +00001989 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001990 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001991 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00001992 // into a predicate register
1993 assert(Opc==IA64::LD1 && "problem loading a bool");
1994 unsigned dummy3 = MakeReg(MVT::i64);
1995 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1996 // we compare to 0. true? 0. false? 1.
1997 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00001998 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00001999 } else { // none of the above...
Duraid Madina91ed0a12005-03-17 18:17:03 +00002000 Select(Chain);
2001 Tmp2 = SelectExpr(Address);
2002 if(!isBool)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002003 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002004 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002005 // into a predicate register
2006 assert(Opc==IA64::LD1 && "problem loading a bool");
2007 unsigned dummy = MakeReg(MVT::i64);
2008 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2009 // we compare to 0. true? 0. false? 1.
2010 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002011 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002012 }
2013
2014 return Result;
2015 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002016
Duraid Madina91ed0a12005-03-17 18:17:03 +00002017 case ISD::CopyFromReg: {
2018 if (Result == 1)
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002019 Result = ExprMap[N.getValue(0)] =
Misha Brukmane73e76d2005-04-22 17:54:37 +00002020 MakeReg(N.getValue(0).getValueType());
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002021
Duraid Madina91ed0a12005-03-17 18:17:03 +00002022 SDOperand Chain = N.getOperand(0);
2023
2024 Select(Chain);
Chris Lattner7c762782005-08-16 21:56:37 +00002025 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina91ed0a12005-03-17 18:17:03 +00002026
2027 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukmane73e76d2005-04-22 17:54:37 +00002028 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2029 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002030 // (r) Result =cmp.eq.unc(r0,r0)
2031 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00002032 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina91ed0a12005-03-17 18:17:03 +00002033 return Result;
2034 }
2035
Chris Lattner8abab9b2005-05-13 20:29:26 +00002036 case ISD::TAILCALL:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002037 case ISD::CALL: {
2038 Select(N.getOperand(0));
2039
2040 // The chain for this call is now lowered.
2041 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002042
Duraid Madina91ed0a12005-03-17 18:17:03 +00002043 //grab the arguments
2044 std::vector<unsigned> argvregs;
2045
2046 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002047 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002048
2049 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina91ed0a12005-03-17 18:17:03 +00002050 // Runtime Architecture Guide to see some examples of what's going
2051 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2052 // while FP args get mapped to F8->F15 as needed)
2053
2054 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002055
Duraid Madina91ed0a12005-03-17 18:17:03 +00002056 // in reg args
2057 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2058 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002059 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2060 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2061 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2062 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina91ed0a12005-03-17 18:17:03 +00002063
Misha Brukmane73e76d2005-04-22 17:54:37 +00002064 switch(N.getOperand(i+2).getValueType())
2065 {
2066 default: // XXX do we need to support MVT::i1 here?
2067 Node->dump();
2068 N.getOperand(i).Val->dump();
2069 std::cerr << "Type for " << i << " is: " <<
2070 N.getOperand(i+2).getValueType() << std::endl;
2071 assert(0 && "Unknown value type for call");
2072 case MVT::i64:
2073 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2074 break;
2075 case MVT::f64:
2076 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2077 .addReg(argvregs[i]);
2078 // FIXME: we don't need to do this _all_ the time:
2079 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2080 break;
2081 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002082 }
2083
2084 //in mem args
2085 for (int i = 8, e = argvregs.size(); i < e; ++i)
2086 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002087 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002088
Duraid Madina91ed0a12005-03-17 18:17:03 +00002089 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002090 default:
2091 Node->dump();
Duraid Madina91ed0a12005-03-17 18:17:03 +00002092 N.getOperand(i).Val->dump();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002093 std::cerr << "Type for " << i << " is: " <<
Duraid Madina91ed0a12005-03-17 18:17:03 +00002094 N.getOperand(i+2).getValueType() << "\n";
2095 assert(0 && "Unknown value type for call");
2096 case MVT::i1: // FIXME?
2097 case MVT::i8:
2098 case MVT::i16:
2099 case MVT::i32:
2100 case MVT::i64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002101 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2102 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2103 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002104 break;
2105 case MVT::f32:
2106 case MVT::f64:
2107 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002108 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2109 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002110 break;
2111 }
2112 }
Duraid Madina21ce5f72005-03-31 12:31:11 +00002113
Duraid Madina3a5eb612005-05-20 11:39:17 +00002114 // build the right kind of call. if we can branch directly, do so:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002115 if (GlobalAddressSDNode *GASD =
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002116 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina91ed0a12005-03-17 18:17:03 +00002117 {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002118 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2119 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina3a5eb612005-05-20 11:39:17 +00002120 } else
Duraid Madina21ce5f72005-03-31 12:31:11 +00002121 if (ExternalSymbolSDNode *ESSDN =
Misha Brukmane73e76d2005-04-22 17:54:37 +00002122 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madina21ce5f72005-03-31 12:31:11 +00002123 { // FIXME : currently need this case for correctness, to avoid
Misha Brukmane73e76d2005-04-22 17:54:37 +00002124 // "non-pic code with imm relocation against dynamic symbol" errors
2125 BuildMI(BB, IA64::BRCALL, 1)
2126 .addExternalSymbol(ESSDN->getSymbol(), true);
2127 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002128 }
Duraid Madina3a5eb612005-05-20 11:39:17 +00002129 else { // otherwise we need to get the function descriptor
2130 // load the branch target (function)'s entry point and
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002131 // GP, then branch
Duraid Madina91ed0a12005-03-17 18:17:03 +00002132 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina21ce5f72005-03-31 12:31:11 +00002133
2134 unsigned targetEntryPoint=MakeReg(MVT::i64);
2135 unsigned targetGPAddr=MakeReg(MVT::i64);
2136 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002137
Duraid Madina21ce5f72005-03-31 12:31:11 +00002138 // b6 is a scratch branch register, we load the target entry point
2139 // from the base of the function descriptor
2140 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2141 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2142
2143 // save the current GP:
2144 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002145
Duraid Madina21ce5f72005-03-31 12:31:11 +00002146 /* TODO: we need to make sure doing this never, ever loads a
2147 * bogus value into r1 (GP). */
2148 // load the target GP (which is at mem[functiondescriptor+8])
2149 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002150 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madina21ce5f72005-03-31 12:31:11 +00002151 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2152
Duraid Madina91ed0a12005-03-17 18:17:03 +00002153 // and then jump: (well, call)
2154 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madina21ce5f72005-03-31 12:31:11 +00002155 // and finally restore the old GP
2156 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2157 IA64Lowering.restoreSP_RP(BB);
2158 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002159
2160 switch (Node->getValueType(0)) {
2161 default: assert(0 && "Unknown value type for call result!");
2162 case MVT::Other: return 1;
2163 case MVT::i1:
2164 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002165 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002166 break;
2167 case MVT::i8:
2168 case MVT::i16:
2169 case MVT::i32:
2170 case MVT::i64:
2171 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2172 break;
2173 case MVT::f64:
2174 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2175 break;
2176 }
2177 return Result+N.ResNo;
2178 }
2179
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002180 } // <- uhhh XXX
Duraid Madina91ed0a12005-03-17 18:17:03 +00002181 return 0;
2182}
2183
2184void ISel::Select(SDOperand N) {
2185 unsigned Tmp1, Tmp2, Opc;
2186 unsigned opcode = N.getOpcode();
2187
Nate Begeman95210522005-03-24 04:39:54 +00002188 if (!LoweredTokens.insert(N).second)
Duraid Madina91ed0a12005-03-17 18:17:03 +00002189 return; // Already selected.
2190
2191 SDNode *Node = N.Val;
2192
2193 switch (Node->getOpcode()) {
2194 default:
2195 Node->dump(); std::cerr << "\n";
2196 assert(0 && "Node not handled yet!");
2197
2198 case ISD::EntryToken: return; // Noop
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002199
Duraid Madina91ed0a12005-03-17 18:17:03 +00002200 case ISD::TokenFactor: {
2201 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2202 Select(Node->getOperand(i));
2203 return;
2204 }
2205
2206 case ISD::CopyToReg: {
2207 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00002208 Tmp1 = SelectExpr(N.getOperand(2));
2209 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002210
Duraid Madina91ed0a12005-03-17 18:17:03 +00002211 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00002212 // if a bool, we use pseudocode
2213 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukmane73e76d2005-04-22 17:54:37 +00002214 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2215 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002216 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2217 else
Misha Brukmane73e76d2005-04-22 17:54:37 +00002218 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002219 // XXX is this the right way 'round? ;)
Chris Lattner7c762782005-08-16 21:56:37 +00002220 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina91ed0a12005-03-17 18:17:03 +00002221 }
2222 return;
2223 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002224
Duraid Madina91ed0a12005-03-17 18:17:03 +00002225 case ISD::RET: {
2226
2227 /* what the heck is going on here:
2228
2229<_sabre_> ret with two operands is obvious: chain and value
2230<camel_> yep
2231<_sabre_> ret with 3 values happens when 'expansion' occurs
2232<_sabre_> e.g. i64 gets split into 2x i32
2233<camel_> oh right
2234<_sabre_> you don't have this case on ia64
2235<camel_> yep
2236<_sabre_> so the two returned values go into EAX/EDX on ia32
2237<camel_> ahhh *memories*
2238<_sabre_> :)
2239<camel_> ok, thanks :)
2240<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2241<_sabre_> this is the first operand always
2242<_sabre_> these operand often define chains, they are the last operand
2243<_sabre_> they are printed as 'ch' if you do DAG.dump()
2244 */
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002245
Duraid Madina91ed0a12005-03-17 18:17:03 +00002246 switch (N.getNumOperands()) {
2247 default:
2248 assert(0 && "Unknown return instruction!");
2249 case 2:
2250 Select(N.getOperand(0));
2251 Tmp1 = SelectExpr(N.getOperand(1));
2252 switch (N.getOperand(1).getValueType()) {
2253 default: assert(0 && "All other types should have been promoted!!");
Misha Brukmane73e76d2005-04-22 17:54:37 +00002254 // FIXME: do I need to add support for bools here?
2255 // (return '0' or '1' r8, basically...)
2256 //
2257 // FIXME: need to round floats - 80 bits is bad, the tester
2258 // told me so
Duraid Madina91ed0a12005-03-17 18:17:03 +00002259 case MVT::i64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002260 // we mark r8 as live on exit up above in LowerArguments()
2261 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2262 break;
Duraid Madina91ed0a12005-03-17 18:17:03 +00002263 case MVT::f64:
Misha Brukmane73e76d2005-04-22 17:54:37 +00002264 // we mark F8 as live on exit up above in LowerArguments()
2265 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002266 }
2267 break;
2268 case 1:
2269 Select(N.getOperand(0));
2270 break;
2271 }
2272 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2273 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2274 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2275 return;
2276 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002277
Duraid Madina91ed0a12005-03-17 18:17:03 +00002278 case ISD::BR: {
2279 Select(N.getOperand(0));
2280 MachineBasicBlock *Dest =
2281 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2282 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2283 // XXX HACK! we do _not_ need long branches all the time
2284 return;
2285 }
2286
Duraid Madina91ed0a12005-03-17 18:17:03 +00002287 case ISD::BRCOND: {
2288 MachineBasicBlock *Dest =
2289 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2290
2291 Select(N.getOperand(0));
2292 Tmp1 = SelectExpr(N.getOperand(1));
2293 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2294 // XXX HACK! we do _not_ need long branches all the time
2295 return;
2296 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002297
Duraid Madina91ed0a12005-03-17 18:17:03 +00002298 case ISD::EXTLOAD:
2299 case ISD::ZEXTLOAD:
2300 case ISD::SEXTLOAD:
2301 case ISD::LOAD:
Chris Lattner8abab9b2005-05-13 20:29:26 +00002302 case ISD::TAILCALL:
Duraid Madina91ed0a12005-03-17 18:17:03 +00002303 case ISD::CALL:
2304 case ISD::CopyFromReg:
2305 case ISD::DYNAMIC_STACKALLOC:
2306 SelectExpr(N);
2307 return;
2308
2309 case ISD::TRUNCSTORE:
2310 case ISD::STORE: {
2311 Select(N.getOperand(0));
2312 Tmp1 = SelectExpr(N.getOperand(1)); // value
2313
2314 bool isBool=false;
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002315
Duraid Madina91ed0a12005-03-17 18:17:03 +00002316 if(opcode == ISD::STORE) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002317 switch (N.getOperand(1).getValueType()) {
2318 default: assert(0 && "Cannot store this type!");
2319 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2320 // FIXME?: for now, we treat bool loads the same as i8 stores */
2321 case MVT::i8: Opc = IA64::ST1; break;
2322 case MVT::i16: Opc = IA64::ST2; break;
2323 case MVT::i32: Opc = IA64::ST4; break;
2324 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002325
Misha Brukmane73e76d2005-04-22 17:54:37 +00002326 case MVT::f32: Opc = IA64::STF4; break;
2327 case MVT::f64: Opc = IA64::STF8; break;
2328 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002329 } else { // truncstore
Chris Lattner36db1ed2005-07-10 00:29:18 +00002330 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002331 default: assert(0 && "unknown type in truncstore");
2332 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2333 //FIXME: DAG does not promote this load?
2334 case MVT::i8: Opc = IA64::ST1; break;
2335 case MVT::i16: Opc = IA64::ST2; break;
2336 case MVT::i32: Opc = IA64::ST4; break;
2337 case MVT::f32: Opc = IA64::STF4; break;
2338 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002339 }
2340
2341 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukmane73e76d2005-04-22 17:54:37 +00002342 unsigned dummy = MakeReg(MVT::i64);
2343 unsigned dummy2 = MakeReg(MVT::i64);
2344 BuildMI(BB, IA64::ADD, 2, dummy)
2345 .addGlobalAddress(cast<GlobalAddressSDNode>
2346 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2347 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002348
Misha Brukmane73e76d2005-04-22 17:54:37 +00002349 if(!isBool)
2350 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2351 else { // we are storing a bool, so emit a little pseudocode
2352 // to store a predicate register as one byte
2353 assert(Opc==IA64::ST1);
2354 unsigned dummy3 = MakeReg(MVT::i64);
2355 unsigned dummy4 = MakeReg(MVT::i64);
2356 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2357 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2358 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2359 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2360 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002361 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2362
Misha Brukmane73e76d2005-04-22 17:54:37 +00002363 // FIXME? (what about bools?)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002364
Misha Brukmane73e76d2005-04-22 17:54:37 +00002365 unsigned dummy = MakeReg(MVT::i64);
2366 BuildMI(BB, IA64::MOV, 1, dummy)
2367 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2368 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002369 } else { // otherwise
Misha Brukmane73e76d2005-04-22 17:54:37 +00002370 Tmp2 = SelectExpr(N.getOperand(2)); //address
2371 if(!isBool)
2372 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2373 else { // we are storing a bool, so emit a little pseudocode
2374 // to store a predicate register as one byte
2375 assert(Opc==IA64::ST1);
2376 unsigned dummy3 = MakeReg(MVT::i64);
2377 unsigned dummy4 = MakeReg(MVT::i64);
2378 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2379 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2380 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2381 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2382 }
Duraid Madina91ed0a12005-03-17 18:17:03 +00002383 }
2384 return;
2385 }
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002386
Chris Lattner2dce7032005-05-12 23:24:06 +00002387 case ISD::CALLSEQ_START:
2388 case ISD::CALLSEQ_END: {
Duraid Madina91ed0a12005-03-17 18:17:03 +00002389 Select(N.getOperand(0));
2390 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002391
Chris Lattner2dce7032005-05-12 23:24:06 +00002392 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2393 IA64::ADJUSTCALLSTACKUP;
Duraid Madina91ed0a12005-03-17 18:17:03 +00002394 BuildMI(BB, Opc, 1).addImm(Tmp1);
2395 return;
2396 }
2397
2398 return;
2399 }
2400 assert(0 && "GAME OVER. INSERT COIN?");
2401}
2402
2403
2404/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2405/// into a machine code representation using pattern matching and a machine
2406/// description file.
2407///
2408FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman89b8c8d2005-04-21 23:13:11 +00002409 return new ISel(TM);
Duraid Madina91ed0a12005-03-17 18:17:03 +00002410}
2411
2412