blob: b071a0894d57e9816df6262513a6e80b70de591c [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman7cbd5252005-08-16 19:49:35 +000060 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000061 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
62
Misha Brukman4633f1c2005-04-21 23:13:11 +000063 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000064 setShiftAmountType(MVT::i64);
65
66 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067
68 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000069
70 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
72 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000073 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000074
Chris Lattner3e2bafd2005-09-28 22:29:17 +000075 setOperationAction(ISD::FREM , MVT::f32 , Expand);
76 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000077
78 setOperationAction(ISD::UREM , MVT::f32 , Expand);
79 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000080
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
82 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
83 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Chris Lattner7a7c9792005-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 Begemand5ce2042005-10-21 01:52:45 +000087
Chris Lattner17234b72005-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 Lenharthb5884d32005-05-04 19:25:37 +000096 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000097 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
98 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Nate Begemand5ce2042005-10-21 01:52:45 +000099 // FIXME: implement mulhs (xma.h) and mulhu (xma.hu)
100 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
101 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000102
Chris Lattner18c778f2005-12-01 18:19:53 +0000103 // We don't have line number support yet.
104 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
105
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000106 computeRegisterProperties();
107
108 addLegalFPImmediate(+0.0);
109 addLegalFPImmediate(+1.0);
110 addLegalFPImmediate(-0.0);
111 addLegalFPImmediate(-1.0);
112 }
113
114 /// LowerArguments - This hook must be implemented to indicate how we should
115 /// lower the arguments for the specified function, into the specified DAG.
116 virtual std::vector<SDOperand>
117 LowerArguments(Function &F, SelectionDAG &DAG);
118
119 /// LowerCallTo - This hook lowers an abstract call to a function into an
120 /// actual call.
121 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000122 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000123 bool isTailCall, SDOperand Callee, ArgListTy &Args,
124 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000125
Chris Lattnere0fe2252005-07-05 19:58:54 +0000126 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
127 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000128 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000129 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
130 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000131
132 void restoreGP_SP_RP(MachineBasicBlock* BB)
133 {
134 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
135 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
136 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
137 }
138
Duraid Madinabeeaab22005-03-31 12:31:11 +0000139 void restoreSP_RP(MachineBasicBlock* BB)
140 {
141 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
142 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
143 }
144
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000145 void restoreRP(MachineBasicBlock* BB)
146 {
147 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
148 }
149
150 void restoreGP(MachineBasicBlock* BB)
151 {
152 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
153 }
154
155 };
156}
157
158
159std::vector<SDOperand>
160IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
161 std::vector<SDOperand> ArgValues;
162
163 //
164 // add beautiful description of IA64 stack frame format
165 // here (from intel 24535803.pdf most likely)
166 //
167 MachineFunction &MF = DAG.getMachineFunction();
168 MachineFrameInfo *MFI = MF.getFrameInfo();
169
170 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
171 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
173
174 MachineBasicBlock& BB = MF.front();
175
Misha Brukman4633f1c2005-04-21 23:13:11 +0000176 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000177 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000178
179 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000180 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000181
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000182 unsigned argVreg[8];
183 unsigned argPreg[8];
184 unsigned argOpc[8];
185
Duraid Madinabeeaab22005-03-31 12:31:11 +0000186 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000187
Duraid Madinabeeaab22005-03-31 12:31:11 +0000188 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000189 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000190
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000191 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000192 {
193 SDOperand newroot, argt;
194 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000195
196 switch (getValueType(I->getType())) {
197 default:
198 std::cerr << "ERROR in LowerArgs: unknown type "
199 << getValueType(I->getType()) << "\n";
200 abort();
201 case MVT::f32:
202 // fixme? (well, will need to for weird FP structy stuff,
203 // see intel ABI docs)
204 case MVT::f64:
205//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
206 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
207 // floating point args go into f8..f15 as-needed, the increment
208 argVreg[count] = // is below..:
209 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
210 // FP args go into f8..f15 as needed: (hence the ++)
211 argPreg[count] = args_FP[used_FPArgs++];
212 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000213 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattner0a00bec2005-08-22 21:33:11 +0000214 MVT::f64);
215 if (I->getType() == Type::FloatTy)
216 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukman7847fca2005-04-22 17:54:37 +0000217 break;
218 case MVT::i1: // NOTE: as far as C abi stuff goes,
219 // bools are just boring old ints
220 case MVT::i8:
221 case MVT::i16:
222 case MVT::i32:
223 case MVT::i64:
224//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
225 MF.addLiveIn(args_int[count]); // mark this register as liveIn
226 argVreg[count] =
227 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
228 argPreg[count] = args_int[count];
229 argOpc[count] = IA64::MOV;
230 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000231 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000232 if ( getValueType(I->getType()) != MVT::i64)
233 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
234 newroot);
235 break;
236 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000237 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000238 // Create the frame index object for this incoming parameter...
239 ArgOffset = 16 + 8 * (count - 8);
240 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000241
Misha Brukman7847fca2005-04-22 17:54:37 +0000242 // Create the SelectionDAG nodes corresponding to a load
243 //from this parameter
244 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
245 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000246 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000247 }
248 ++count;
249 DAG.setRoot(newroot.getValue(1));
250 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000251 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000252
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253
Duraid Madinabeeaab22005-03-31 12:31:11 +0000254 // Create a vreg to hold the output of (what will become)
255 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000256 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
257 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
258 // we create a PSEUDO_ALLOC (pseudo)instruction for now
259
260 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
261
262 // hmm:
263 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
264 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
265 // ..hmm.
266
267 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
268
269 // hmm:
270 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
271 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
272 // ..hmm.
273
Duraid Madinabeeaab22005-03-31 12:31:11 +0000274 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000275
Duraid Madinabeeaab22005-03-31 12:31:11 +0000276 // if this is a varargs function, we simply lower llvm.va_start by
277 // pointing to the first entry
278 if(F.isVarArg()) {
279 tempOffset=0;
280 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000281 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000282
Duraid Madinabeeaab22005-03-31 12:31:11 +0000283 // here we actually do the moving of args, and store them to the stack
284 // too if this is a varargs function:
285 for (int i = 0; i < count && i < 8; ++i) {
286 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
287 if(F.isVarArg()) {
288 // if this is a varargs function, we copy the input registers to the stack
289 int FI = MFI->CreateFixedObject(8, tempOffset);
290 tempOffset+=8; //XXX: is it safe to use r22 like this?
291 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
292 // FIXME: we should use st8.spill here, one day
293 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
294 }
295 }
296
Duraid Madinaca494fd2005-04-12 14:54:44 +0000297 // Finally, inform the code generator which regs we return values in.
298 // (see the ISD::RET: case down below)
299 switch (getValueType(F.getReturnType())) {
300 default: assert(0 && "i have no idea where to return this type!");
301 case MVT::isVoid: break;
302 case MVT::i1:
303 case MVT::i8:
304 case MVT::i16:
305 case MVT::i32:
306 case MVT::i64:
307 MF.addLiveOut(IA64::r8);
308 break;
309 case MVT::f32:
310 case MVT::f64:
311 MF.addLiveOut(IA64::F8);
312 break;
313 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000314
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000315 return ArgValues;
316}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000317
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000318std::pair<SDOperand, SDOperand>
319IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000320 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000321 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000322 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000323 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000324
325 MachineFunction &MF = DAG.getMachineFunction();
326
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000327 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000328 unsigned outRegsUsed = 0;
329
330 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000331 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000332 outRegsUsed = 8;
333 } else {
334 outRegsUsed = Args.size();
335 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000336
Duraid Madinabeeaab22005-03-31 12:31:11 +0000337 // FIXME? this WILL fail if we ever try to pass around an arg that
338 // consumes more than a single output slot (a 'real' double, int128
339 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
340 // registers we use. Hopefully, the assembler will notice.
341 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
342 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000343
Chris Lattner16cd04d2005-05-12 23:24:06 +0000344 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000345 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000346
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000347 std::vector<SDOperand> args_to_use;
348 for (unsigned i = 0, e = Args.size(); i != e; ++i)
349 {
350 switch (getValueType(Args[i].second)) {
351 default: assert(0 && "unexpected argument type!");
352 case MVT::i1:
353 case MVT::i8:
354 case MVT::i16:
355 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000356 //promote to 64-bits, sign/zero extending based on type
357 //of the argument
358 if(Args[i].second->isSigned())
359 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
360 Args[i].first);
361 else
362 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
363 Args[i].first);
364 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000365 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000366 //promote to 64-bits
367 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000368 case MVT::f64:
369 case MVT::i64:
370 break;
371 }
372 args_to_use.push_back(Args[i].first);
373 }
374
375 std::vector<MVT::ValueType> RetVals;
376 MVT::ValueType RetTyVT = getValueType(RetTy);
377 if (RetTyVT != MVT::isVoid)
378 RetVals.push_back(RetTyVT);
379 RetVals.push_back(MVT::Other);
380
381 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000382 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000383 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000384 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000385 DAG.getConstant(NumBytes, getPointerTy()));
386 return std::make_pair(TheCall, Chain);
387}
388
Chris Lattnere0fe2252005-07-05 19:58:54 +0000389SDOperand
390IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
391 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000392 // vastart just stores the address of the VarArgsFrameIndex slot.
393 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000394 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
395 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000396}
397
398std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000399LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
400 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000401
402 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000403 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
404 VAListP, DAG.getSrcValue(VAListV));
405 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
406 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000407 unsigned Amt;
408 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
409 Amt = 8;
410 else {
411 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
412 "Other types should have been promoted for varargs!");
413 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000414 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000415 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000416 DAG.getConstant(Amt, Val.getValueType()));
417 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000418 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000419 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000420}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000421
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000422namespace {
423
424 //===--------------------------------------------------------------------===//
425 /// ISel - IA64 specific code to select IA64 machine instructions for
426 /// SelectionDAG operations.
427 ///
428 class ISel : public SelectionDAGISel {
429 /// IA64Lowering - This object fully describes how to lower LLVM code to an
430 /// IA64-specific SelectionDAG.
431 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000432 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
433 // for sdiv and udiv until it is put into the future
434 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000435
436 /// ExprMap - As shared expressions are codegen'd, we keep track of which
437 /// vreg the value is produced in, so we only emit one copy of each compiled
438 /// tree.
439 std::map<SDOperand, unsigned> ExprMap;
440 std::set<SDOperand> LoweredTokens;
441
442 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000443 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
444 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000445
446 /// InstructionSelectBasicBlock - This callback is invoked by
447 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
448 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
449
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000450 unsigned SelectExpr(SDOperand N);
451 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000452 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
453 SDOperand BuildConstmulSequence(SDOperand N);
454
Chris Lattner47c08892005-08-22 18:28:09 +0000455 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000456 };
457}
458
459/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
460/// when it has created a SelectionDAG for us to codegen.
461void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
462
463 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000464 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000465 Select(DAG.getRoot());
466
467 // Clear state used for selection.
468 ExprMap.clear();
469 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000470 ISelDAG = 0;
471}
472
Duraid Madinab2322562005-04-26 07:23:02 +0000473// strip leading '0' characters from a string
474void munchLeadingZeros(std::string& inString) {
475 while(inString.c_str()[0]=='0') {
476 inString.erase(0, 1);
477 }
478}
479
480// strip trailing '0' characters from a string
481void munchTrailingZeros(std::string& inString) {
482 int curPos=inString.length()-1;
483
484 while(inString.c_str()[curPos]=='0') {
485 inString.erase(curPos, 1);
486 curPos--;
487 }
488}
489
490// return how many consecutive '0' characters are at the end of a string
491unsigned int countTrailingZeros(std::string& inString) {
492 int curPos=inString.length()-1;
493 unsigned int zeroCount=0;
494 // assert goes here
495 while(inString.c_str()[curPos--]=='0') {
496 zeroCount++;
497 }
498 return zeroCount;
499}
500
501// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
502// '0' and 'N' (-1) characters)
503void boothEncode(std::string inString, std::string& boothEncodedString) {
504
505 int curpos=0;
506 int replacements=0;
507 int lim=inString.size();
508
509 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000510 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000511 int runlength=0;
512 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000513
Duraid Madinab2322562005-04-26 07:23:02 +0000514 // find the run length
515 for(;inString[curpos+runlength]=='1';runlength++) ;
516
517 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000518 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000519 replaceString+="1";
520
521 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000522 inString.replace(curpos, runlength+1, replaceString);
523 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000524 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000525 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000526 } else { // a zero, we just keep chugging along
527 curpos++;
528 }
529 }
530
531 // clean up (trim the string, reverse it and turn '1's into 'P's)
532 munchTrailingZeros(inString);
533 boothEncodedString="";
534
535 for(int i=inString.size()-1;i>=0;i--)
536 if(inString[i]=='1')
537 boothEncodedString+="P";
538 else
539 boothEncodedString+=inString[i];
540
541}
542
543struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
544 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000545 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000546 unsigned secondVal; // C
547 unsigned secondShift; // D
548 bool isSub;
549};
550
551/* this implements Lefevre's "pattern-based" constant multiplication,
552 * see "Multiplication by an Integer Constant", INRIA report 1999-06
553 *
554 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
555 * to get better booth encodings - this does help in practice
556 * TODO: weight shifts appropriately (most architectures can't
557 * fuse a shift and an add for arbitrary shift amounts) */
558unsigned lefevre(const std::string inString,
559 std::vector<struct shiftaddblob> &ops) {
560 std::string retstring;
561 std::string s = inString;
562 munchTrailingZeros(s);
563
564 int length=s.length()-1;
565
566 if(length==0) {
567 return(0);
568 }
569
570 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000571
Duraid Madinab2322562005-04-26 07:23:02 +0000572 for(int i=0; i<=length; i++) {
573 if (s.c_str()[length-i]=='P') {
574 p.push_back(i);
575 } else if (s.c_str()[length-i]=='N') {
576 n.push_back(i);
577 }
578 }
579
580 std::string t, u;
Chris Lattner4a209972005-08-25 00:03:21 +0000581 int c = 0;
Duraid Madina4706c032005-04-26 09:42:50 +0000582 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000583 std::map<const int, int> w;
584
Duraid Madina85d5f602005-04-27 11:57:39 +0000585 for(unsigned i=0; i<p.size(); i++) {
586 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000587 w[p[i]-p[j]]++;
588 }
589 }
590
Duraid Madina85d5f602005-04-27 11:57:39 +0000591 for(unsigned i=1; i<n.size(); i++) {
592 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000593 w[n[i]-n[j]]++;
594 }
595 }
596
Duraid Madina85d5f602005-04-27 11:57:39 +0000597 for(unsigned i=0; i<p.size(); i++) {
598 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000599 w[-abs(p[i]-n[j])]++;
600 }
601 }
602
603 std::map<const int, int>::const_iterator ii;
604 std::vector<int> d;
605 std::multimap<int, int> sorted_by_value;
606
607 for(ii = w.begin(); ii!=w.end(); ii++)
608 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
609
610 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
611 it != sorted_by_value.end(); ++it) {
612 d.push_back((*it).second);
613 }
614
615 int int_W=0;
616 int int_d;
617
618 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
619 d.pop_back();
620 retstring=s; // hmmm
621 int x=0;
622 int z=abs(int_d)-1;
623
624 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000625
Duraid Madina85d5f602005-04-27 11:57:39 +0000626 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000627 if( ((base+z+1) < retstring.size()) &&
628 retstring.c_str()[base]=='P' &&
629 retstring.c_str()[base+z+1]=='P')
630 {
631 // match
632 x++;
633 retstring.replace(base, 1, "0");
634 retstring.replace(base+z+1, 1, "p");
635 }
Duraid Madinab2322562005-04-26 07:23:02 +0000636 }
637
Duraid Madina85d5f602005-04-27 11:57:39 +0000638 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000639 if( ((base+z+1) < retstring.size()) &&
640 retstring.c_str()[base]=='N' &&
641 retstring.c_str()[base+z+1]=='N')
642 {
643 // match
644 x++;
645 retstring.replace(base, 1, "0");
646 retstring.replace(base+z+1, 1, "n");
647 }
Duraid Madinab2322562005-04-26 07:23:02 +0000648 }
649
650 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000651 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000652 if( ((base+z+1) < retstring.size()) &&
653 ((retstring.c_str()[base]=='P' &&
654 retstring.c_str()[base+z+1]=='N') ||
655 (retstring.c_str()[base]=='N' &&
656 retstring.c_str()[base+z+1]=='P')) ) {
657 // match
658 x++;
659
660 if(retstring.c_str()[base]=='P') {
661 retstring.replace(base, 1, "0");
662 retstring.replace(base+z+1, 1, "p");
663 } else { // retstring[base]=='N'
664 retstring.replace(base, 1, "0");
665 retstring.replace(base+z+1, 1, "n");
666 }
667 }
Duraid Madinab2322562005-04-26 07:23:02 +0000668 }
669 }
670
671 if(x>int_W) {
672 int_W = x;
673 t = retstring;
674 c = int_d; // tofix
675 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000676
Duraid Madinab2322562005-04-26 07:23:02 +0000677 } d.pop_back(); // hmm
678
679 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000680
Duraid Madina85d5f602005-04-27 11:57:39 +0000681 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000682 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
683 t.replace(i, 1, "0");
684 }
685
Duraid Madina85d5f602005-04-27 11:57:39 +0000686 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000687 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000688 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000689 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000690 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000691 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000692 u.replace(i, 1, "N");
693 }
694
695 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000696 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000697 c=-c;
698 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000699 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000700
Duraid Madina8a3042c2005-05-09 13:18:34 +0000701 int pos=0;
702 while(u[pos]=='0')
703 pos++;
704
705 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000706
707 int g=0;
708 if(hit) {
709 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000710 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000711 bool isP=(u[p]=='P');
712 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000713
714 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000715 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000716 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000717 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000718 }
719 }
720
721 munchLeadingZeros(u);
722
723 int i = lefevre(u, ops);
724
725 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000726
Duraid Madinab2322562005-04-26 07:23:02 +0000727 blob.firstVal=i; blob.firstShift=c;
728 blob.isSub=f;
729 blob.secondVal=i; blob.secondShift=0;
730
731 ops.push_back(blob);
732
733 i = ops.size();
734
735 munchLeadingZeros(t);
736
737 if(t.length()==0)
738 return i;
739
740 if(t.c_str()[0]!='P') {
741 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000742 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000743 bool isP=(t.c_str()[p]=='P');
744 bool isN=(t.c_str()[p]=='N');
745
746 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000747 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000748 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000749 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000750 }
751 }
752
753 int j = lefevre(t, ops);
754
755 int trail=countTrailingZeros(u);
756 blob.secondVal=i; blob.secondShift=trail;
757
758 trail=countTrailingZeros(t);
759 blob.firstVal=j; blob.firstShift=trail;
760
761 switch(g) {
762 case 0:
763 blob.isSub=false; // first + second
764 break;
765 case 1:
766 blob.isSub=true; // first - second
767 break;
768 case 2:
769 blob.isSub=true; // second - first
770 int tmpval, tmpshift;
771 tmpval=blob.firstVal;
772 tmpshift=blob.firstShift;
773 blob.firstVal=blob.secondVal;
774 blob.firstShift=blob.secondShift;
775 blob.secondVal=tmpval;
776 blob.secondShift=tmpshift;
777 break;
778 //assert
779 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000780
Duraid Madinab2322562005-04-26 07:23:02 +0000781 ops.push_back(blob);
782 return ops.size();
783}
784
785SDOperand ISel::BuildConstmulSequence(SDOperand N) {
786 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
787 // in particular, *3 is nicer as *2+1, not *4-1
788 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
789
790 bool flippedSign;
791 unsigned preliminaryShift=0;
792
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000793 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000794
795 // first, we make the constant to multiply by positive
796 if(constant<0) {
797 constant=-constant;
798 flippedSign=true;
799 } else {
800 flippedSign=false;
801 }
802
803 // next, we make it odd.
804 for(; (constant%2==0); preliminaryShift++)
805 constant>>=1;
806
807 //OK, we have a positive, odd number of 64 bits or less. Convert it
808 //to a binary string, constantString[0] is the LSB
809 char constantString[65];
810 for(int i=0; i<64; i++)
811 constantString[i]='0'+((constant>>i)&0x1);
812 constantString[64]=0;
813
814 // now, Booth encode it
815 std::string boothEncodedString;
816 boothEncode(constantString, boothEncodedString);
817
818 std::vector<struct shiftaddblob> ops;
819 // do the transformation, filling out 'ops'
820 lefevre(boothEncodedString, ops);
821
Duraid Madinae75a24a2005-05-15 14:44:13 +0000822 assert(ops.size() < 80 && "constmul code has gone haywire\n");
823 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000824
Duraid Madinab2322562005-04-26 07:23:02 +0000825 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000826 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000827 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
828 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
829 results[ops[i].firstVal-1];
830 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
831 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
832 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
833 results[ops[i].secondVal-1];
834 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
835 if(ops[i].isSub)
836 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
837 else
838 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
839 }
840
841 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000842 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000843 if(preliminaryShift) {
844 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000845 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000846 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000847 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000848 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000849 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000850
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000851 SDOperand finalresult;
852 if(flippedSign) { // if we were multiplying by a negative constant:
853 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
854 // subtract the result from 0 to flip its sign
855 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
856 } else { // there was no preliminary multiply by -1 required
857 finalresult = shiftedresult;
858 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000859
860 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000861}
862
Duraid Madina4826a072005-04-06 09:55:17 +0000863/// ponderIntegerDivisionBy - When handling integer divides, if the divide
864/// is by a constant such that we can efficiently codegen it, this
865/// function says what to do. Currently, it returns 0 if the division must
866/// become a genuine divide, and 1 if the division can be turned into a
867/// right shift.
868static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
869 unsigned& Imm) {
870 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
871 // a constant, give up.
872
873 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
874
Chris Lattner0561b3f2005-08-02 19:26:06 +0000875 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
876 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000877 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000878 }
879
Duraid Madina4826a072005-04-06 09:55:17 +0000880 return 0; // fallthrough
881}
882
Duraid Madinac02780e2005-04-13 04:50:54 +0000883static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
884 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
885 // a constant, give up.
886
887 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
888
Chris Lattner0561b3f2005-08-02 19:26:06 +0000889 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000890 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000891 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000892 }
893
Duraid Madinac02780e2005-04-13 04:50:54 +0000894 return 0; // fallthrough
895}
896
Duraid Madinaf55e4032005-04-07 12:33:38 +0000897static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
898 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
899 // constant, give up.
900 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
901
902 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
903 Imm = v & 0x3FFF; // 14 bits
904 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000905 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000906 return 0; // fallthrough
907}
908
909static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
910 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
911 // constant, give up.
912 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
913
914 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
915 Imm = v & 0xFF; // 8 bits
916 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000917 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000918 return 0; // fallthrough
919}
920
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000921unsigned ISel::SelectExpr(SDOperand N) {
922 unsigned Result;
923 unsigned Tmp1, Tmp2, Tmp3;
924 unsigned Opc = 0;
925 MVT::ValueType DestType = N.getValueType();
926
927 unsigned opcode = N.getOpcode();
928
929 SDNode *Node = N.Val;
930 SDOperand Op0, Op1;
931
932 if (Node->getOpcode() == ISD::CopyFromReg)
933 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000934 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000935
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000936 unsigned &Reg = ExprMap[N];
937 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000938
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000939 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000940 Reg = Result = (N.getValueType() != MVT::Other) ?
941 MakeReg(N.getValueType()) : 1;
942 else {
943 // If this is a call instruction, make sure to prepare ALL of the result
944 // values as well as the chain.
945 if (Node->getNumValues() == 1)
946 Reg = Result = 1; // Void call, just a chain.
947 else {
948 Result = MakeReg(Node->getValueType(0));
949 ExprMap[N.getValue(0)] = Result;
950 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
951 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
952 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
953 }
954 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000955
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000956 switch (N.getOpcode()) {
957 default:
958 Node->dump();
959 assert(0 && "Node not handled!\n");
960
961 case ISD::FrameIndex: {
962 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
963 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
964 return Result;
965 }
966
967 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000968 Tmp1 = BB->getParent()->getConstantPool()->
969 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000970 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
971 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
972 .addReg(IA64::r1);
973 return Result;
974 }
975
976 case ISD::ConstantFP: {
977 Tmp1 = Result; // Intermediate Register
978 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
979 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
980 Tmp1 = MakeReg(MVT::f64);
981
982 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
983 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
984 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
985 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
986 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
987 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
988 else
989 assert(0 && "Unexpected FP constant!");
990 if (Tmp1 != Result)
991 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
992 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000993 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000994 return Result;
995 }
996
997 case ISD::DYNAMIC_STACKALLOC: {
998 // Generate both result values.
999 if (Result != 1)
1000 ExprMap[N.getValue(1)] = 1; // Generate the token
1001 else
1002 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1003
1004 // FIXME: We are currently ignoring the requested alignment for handling
1005 // greater than the stack alignment. This will need to be revisited at some
1006 // point. Align = N.getOperand(2);
1007
1008 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1009 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1010 std::cerr << "Cannot allocate stack object with greater alignment than"
1011 << " the stack alignment yet!";
1012 abort();
1013 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001014
1015/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001016 Select(N.getOperand(0));
1017 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1018 {
1019 if (CN->getValue() < 32000)
1020 {
1021 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001022 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001023 } else {
1024 Tmp1 = SelectExpr(N.getOperand(1));
1025 // Subtract size from stack pointer, thereby allocating some space.
1026 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1027 }
1028 } else {
1029 Tmp1 = SelectExpr(N.getOperand(1));
1030 // Subtract size from stack pointer, thereby allocating some space.
1031 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1032 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001033*/
1034 Select(N.getOperand(0));
1035 Tmp1 = SelectExpr(N.getOperand(1));
1036 // Subtract size from stack pointer, thereby allocating some space.
1037 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001038 // Put a pointer to the space into the result register, by copying the
1039 // stack pointer.
1040 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1041 return Result;
1042 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001043
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001044 case ISD::SELECT: {
1045 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1046 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1047 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1048
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001049 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001050
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001051 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001052 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001053 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1054 // for i1, we load the condition into an integer register, then
1055 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1056 // of them will go through, since the integer register will hold
1057 // either 0 or 1)
1058 case MVT::i1: {
1059 bogoResult=MakeReg(MVT::i1);
1060
1061 // load the condition into an integer register
1062 unsigned condReg=MakeReg(MVT::i64);
1063 unsigned dummy=MakeReg(MVT::i64);
1064 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1065 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1066 .addImm(1).addReg(Tmp1);
1067
1068 // initialize Result (bool) to false (hence UNC) and if
1069 // the select condition (condReg) is false (0), copy Tmp3
1070 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1071 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1072
1073 // now, if the selection condition is true, write 1 to the
1074 // result if Tmp2 is 1
1075 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1076 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1077 break;
1078 }
1079 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1080 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001081 case MVT::i64:
1082 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001083 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1084 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1085 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001086 break;
1087 case MVT::f64:
1088 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001089 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1090 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1091 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001092 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001093 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001094
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001095 return Result;
1096 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001097
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001098 case ISD::Constant: {
1099 unsigned depositPos=0;
1100 unsigned depositLen=0;
1101 switch (N.getValueType()) {
1102 default: assert(0 && "Cannot use constants of this type!");
1103 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001104 // the constant:
1105 if(cast<ConstantSDNode>(N)->getValue()) // true:
1106 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1107 else // false:
1108 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1109 return Result; // early exit
1110 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001111 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001112 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001113
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001114 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001115
1116 if(immediate==0) { // if the constant is just zero,
1117 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1118 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001119 }
1120
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001121 if (immediate <= 8191 && immediate >= -8192) {
1122 // if this constants fits in 14 bits, we use a mov the assembler will
1123 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1124 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1125 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001126 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001127
1128 if (immediate <= 2097151 && immediate >= -2097152) {
1129 // if this constants fits in 22 bits, we use a mov the assembler will
1130 // turn into: "addl rDest=imm,r0"
1131 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1132 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001133 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001134
1135 /* otherwise, our immediate is big, so we use movl */
1136 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001137 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001138 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001139 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001140
1141 case ISD::UNDEF: {
1142 BuildMI(BB, IA64::IDEF, 0, Result);
1143 return Result;
1144 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001145
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001146 case ISD::GlobalAddress: {
1147 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1148 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001149
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001150 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001151 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001152
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001153 return Result;
1154 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001155
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001156 case ISD::ExternalSymbol: {
1157 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001158// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1159 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001160 return Result;
1161 }
1162
1163 case ISD::FP_EXTEND: {
1164 Tmp1 = SelectExpr(N.getOperand(0));
1165 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1166 return Result;
1167 }
1168
Chris Lattner26e04bb2005-09-02 00:15:30 +00001169 case ISD::ANY_EXTEND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001170 case ISD::ZERO_EXTEND: {
1171 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001172
Chris Lattner26e04bb2005-09-02 00:15:30 +00001173 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1174 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001175
Chris Lattner26e04bb2005-09-02 00:15:30 +00001176 // if the predicate reg has 1, we want a '1' in our GR.
1177 unsigned dummy = MakeReg(MVT::i64);
1178 // first load zero:
1179 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1180 // ...then conditionally (PR:Tmp1) add 1:
1181 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1182 .addImm(1).addReg(Tmp1);
1183 return Result; // XXX early exit!
1184 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001185
Chris Lattner26e04bb2005-09-02 00:15:30 +00001186 case ISD::SIGN_EXTEND:
1187 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1188 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001189
1190 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner26e04bb2005-09-02 00:15:30 +00001191 assert(0 && "don't know how to sign_extend from bool yet!");
1192 abort();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001193
Chris Lattner26e04bb2005-09-02 00:15:30 +00001194 case ISD::TRUNCATE:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001195 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1196 // of R0 appropriately.
Chris Lattner26e04bb2005-09-02 00:15:30 +00001197 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1198 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001199 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001200
Chris Lattner26e04bb2005-09-02 00:15:30 +00001201 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1202 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1203 return Result; // XXX early exit!
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001204
Misha Brukman7847fca2005-04-22 17:54:37 +00001205/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001206 case ISD::FP_ROUND: {
1207 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001208 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001209 Tmp1 = SelectExpr(N.getOperand(0));
1210 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1211 // we add 0.0 using a single precision add to do rounding
1212 return Result;
1213 }
1214*/
1215
1216// FIXME: the following 4 cases need cleaning
1217 case ISD::SINT_TO_FP: {
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 Tmp2 = MakeReg(MVT::f64);
1220 unsigned dummy = MakeReg(MVT::f64);
1221 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1222 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1223 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1224 return Result;
1225 }
1226
1227 case ISD::UINT_TO_FP: {
1228 Tmp1 = SelectExpr(N.getOperand(0));
1229 Tmp2 = MakeReg(MVT::f64);
1230 unsigned dummy = MakeReg(MVT::f64);
1231 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1232 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1233 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1234 return Result;
1235 }
1236
1237 case ISD::FP_TO_SINT: {
1238 Tmp1 = SelectExpr(N.getOperand(0));
1239 Tmp2 = MakeReg(MVT::f64);
1240 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1241 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1242 return Result;
1243 }
1244
1245 case ISD::FP_TO_UINT: {
1246 Tmp1 = SelectExpr(N.getOperand(0));
1247 Tmp2 = MakeReg(MVT::f64);
1248 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1249 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1250 return Result;
1251 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001252
1253 case ISD::FADD: {
1254 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1255 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1256 // into an fma, do so:
1257 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001258 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1259 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1260 Tmp3 = SelectExpr(N.getOperand(1));
1261 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1262 return Result; // early exit
1263 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001264
1265 //else, fallthrough:
1266 Tmp1 = SelectExpr(N.getOperand(0));
1267 Tmp2 = SelectExpr(N.getOperand(1));
1268 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1269 return Result;
1270 }
Duraid Madinaed095022005-04-13 06:12:04 +00001271
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001272 case ISD::ADD: {
1273 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001274 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001275 // this add into a shladd, try:
1276 ConstantSDNode *CSD = NULL;
1277 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001278 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001279
Misha Brukman7847fca2005-04-22 17:54:37 +00001280 // ++FusedSHLADD; // Statistic
1281 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1282 int shl_amt = CSD->getValue();
1283 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001284
Misha Brukman7847fca2005-04-22 17:54:37 +00001285 BuildMI(BB, IA64::SHLADD, 3, Result)
1286 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1287 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001288 }
1289 }
1290
1291 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001292 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001293 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1294 case 1: // adding a constant that's 14 bits
1295 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1296 return Result; // early exit
1297 } // fallthrough and emit a reg+reg ADD:
1298 Tmp2 = SelectExpr(N.getOperand(1));
1299 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001300 return Result;
1301 }
1302
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001303 case ISD::FMUL:
1304 Tmp1 = SelectExpr(N.getOperand(0));
1305 Tmp2 = SelectExpr(N.getOperand(1));
1306 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1307 return Result;
1308
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001309 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001310
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001311 // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001312/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1313 */
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001314 // boring old integer multiply with xma
Duraid Madinab2322562005-04-26 07:23:02 +00001315 Tmp1 = SelectExpr(N.getOperand(0));
1316 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001317
1318 unsigned TempFR1=MakeReg(MVT::f64);
1319 unsigned TempFR2=MakeReg(MVT::f64);
1320 unsigned TempFR3=MakeReg(MVT::f64);
1321 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1322 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1323 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1324 .addReg(IA64::F0);
1325 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1326 return Result; // early exit
1327 /* FIXME } else { // we are multiplying by an integer constant! yay
1328 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1329 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001330 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001331
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001332 case ISD::FSUB:
1333 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madina4826a072005-04-06 09:55:17 +00001334 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1335 // into an fms, do so:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001336 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001337 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1338 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1339 Tmp3 = SelectExpr(N.getOperand(1));
1340 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1341 return Result; // early exit
1342 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001343
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001344 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001345 Tmp1 = SelectExpr(N.getOperand(0));
1346 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1347 return Result;
1348
1349 case ISD::SUB: {
1350 Tmp2 = SelectExpr(N.getOperand(1));
1351 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1352 case 1: // subtracting *from* an 8 bit constant:
1353 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1354 return Result; // early exit
1355 } // fallthrough and emit a reg+reg SUB:
1356 Tmp1 = SelectExpr(N.getOperand(0));
1357 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001358 return Result;
1359 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001360
1361 case ISD::FABS: {
1362 Tmp1 = SelectExpr(N.getOperand(0));
1363 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1364 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1365 return Result;
1366 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001367
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001368 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001369 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001370
Misha Brukman4633f1c2005-04-21 23:13:11 +00001371 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001372 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1373 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1374 } else {
1375 Tmp1 = SelectExpr(N.getOperand(0));
1376 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1377 }
1378
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001379 return Result;
1380 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001381
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001382 case ISD::AND: {
1383 switch (N.getValueType()) {
1384 default: assert(0 && "Cannot AND this type!");
1385 case MVT::i1: { // if a bool, we emit a pseudocode AND
1386 unsigned pA = SelectExpr(N.getOperand(0));
1387 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001388
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001389/* our pseudocode for AND is:
1390 *
1391(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1392 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1393 ;;
1394(pB) cmp.ne pTemp,p0 = r0,r0
1395 ;;
1396(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1397
1398*/
1399 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001401 unsigned bogusTemp1 = MakeReg(MVT::i1);
1402 unsigned bogusTemp2 = MakeReg(MVT::i1);
1403 unsigned bogusTemp3 = MakeReg(MVT::i1);
1404 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001405
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001406 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001407 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001408 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001409 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001410 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001411 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001412 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001413 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001414 break;
1415 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001416
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001417 // if not a bool, we just AND away:
1418 case MVT::i8:
1419 case MVT::i16:
1420 case MVT::i32:
1421 case MVT::i64: {
1422 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001423 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001424 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001425 switch (Tmp3) {
1426 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1427 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1428 break;
1429 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1430 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1431 break;
1432 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1433 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1434 break;
1435 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001436 // FIXME: assert the dep.z is in bounds
1437 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001438 .addImm(0).addImm(Tmp3);
1439 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001440 }
1441 return Result; // early exit
1442 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001443 Tmp2 = SelectExpr(N.getOperand(1));
1444 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001445 }
1446 }
1447 return Result;
1448 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001450 case ISD::OR: {
1451 switch (N.getValueType()) {
1452 default: assert(0 && "Cannot OR this type!");
1453 case MVT::i1: { // if a bool, we emit a pseudocode OR
1454 unsigned pA = SelectExpr(N.getOperand(0));
1455 unsigned pB = SelectExpr(N.getOperand(1));
1456
1457 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001458
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001459/* our pseudocode for OR is:
1460 *
1461
1462pC = pA OR pB
1463-------------
1464
Misha Brukman7847fca2005-04-22 17:54:37 +00001465(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1466 ;;
1467(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001468
1469*/
1470 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001471 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001472 BuildMI(BB, IA64::TPCMPEQ, 4, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001473 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001474 break;
1475 }
1476 // if not a bool, we just OR away:
1477 case MVT::i8:
1478 case MVT::i16:
1479 case MVT::i32:
1480 case MVT::i64: {
1481 Tmp1 = SelectExpr(N.getOperand(0));
1482 Tmp2 = SelectExpr(N.getOperand(1));
1483 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1484 break;
1485 }
1486 }
1487 return Result;
1488 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001489
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001490 case ISD::XOR: {
1491 switch (N.getValueType()) {
1492 default: assert(0 && "Cannot XOR this type!");
1493 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1494 unsigned pY = SelectExpr(N.getOperand(0));
1495 unsigned pZ = SelectExpr(N.getOperand(1));
1496
1497/* one possible routine for XOR is:
1498
1499 // Compute px = py ^ pz
1500 // using sum of products: px = (py & !pz) | (pz & !py)
1501 // Uses 5 instructions in 3 cycles.
1502 // cycle 1
1503(pz) cmp.eq.unc px = r0, r0 // px = pz
1504(py) cmp.eq.unc pt = r0, r0 // pt = py
1505 ;;
1506 // cycle 2
1507(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1508(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1509 ;;
1510 } { .mmi
1511 // cycle 3
1512(pt) cmp.eq.or px = r0, r0 // px = px | pt
1513
1514*** Another, which we use here, requires one scratch GR. it is:
1515
1516 mov rt = 0 // initialize rt off critical path
1517 ;;
1518
1519 // cycle 1
1520(pz) cmp.eq.unc px = r0, r0 // px = pz
1521(pz) mov rt = 1 // rt = pz
1522 ;;
1523 // cycle 2
1524(py) cmp.ne px = 1, rt // if (py) px = !pz
1525
1526.. these routines kindly provided by Jim Hull
1527*/
1528 unsigned rt = MakeReg(MVT::i64);
1529
1530 // these two temporaries will never actually appear,
1531 // due to the two-address form of some of the instructions below
1532 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1533 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1534
1535 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1536 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001537 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001538 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001539 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001540 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001541 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001542 break;
1543 }
1544 // if not a bool, we just XOR away:
1545 case MVT::i8:
1546 case MVT::i16:
1547 case MVT::i32:
1548 case MVT::i64: {
1549 Tmp1 = SelectExpr(N.getOperand(0));
1550 Tmp2 = SelectExpr(N.getOperand(1));
1551 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1552 break;
1553 }
1554 }
1555 return Result;
1556 }
1557
Duraid Madina63bbed52005-05-11 05:16:09 +00001558 case ISD::CTPOP: {
1559 Tmp1 = SelectExpr(N.getOperand(0));
1560 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1561 return Result;
1562 }
1563
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001564 case ISD::SHL: {
1565 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001566 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1567 Tmp2 = CN->getValue();
1568 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1569 } else {
1570 Tmp2 = SelectExpr(N.getOperand(1));
1571 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1572 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001573 return Result;
1574 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001575
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001576 case ISD::SRL: {
1577 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001578 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1579 Tmp2 = CN->getValue();
1580 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1581 } else {
1582 Tmp2 = SelectExpr(N.getOperand(1));
1583 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1584 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001585 return Result;
1586 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001587
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001588 case ISD::SRA: {
1589 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001590 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1591 Tmp2 = CN->getValue();
1592 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1593 } else {
1594 Tmp2 = SelectExpr(N.getOperand(1));
1595 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1596 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001597 return Result;
1598 }
1599
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001600 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001601 case ISD::SDIV:
1602 case ISD::UDIV:
1603 case ISD::SREM:
1604 case ISD::UREM: {
1605
1606 Tmp1 = SelectExpr(N.getOperand(0));
1607 Tmp2 = SelectExpr(N.getOperand(1));
1608
1609 bool isFP=false;
1610
1611 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1612 isFP=true;
1613
1614 bool isModulus=false; // is it a division or a modulus?
1615 bool isSigned=false;
1616
1617 switch(N.getOpcode()) {
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001618 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001619 case ISD::SDIV: isModulus=false; isSigned=true; break;
1620 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001621 case ISD::FREM:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001622 case ISD::SREM: isModulus=true; isSigned=true; break;
1623 case ISD::UREM: isModulus=true; isSigned=false; break;
1624 }
1625
Duraid Madina4826a072005-04-06 09:55:17 +00001626 if(!isModulus && !isFP) { // if this is an integer divide,
1627 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001628 case 1: // division by a constant that's a power of 2
1629 Tmp1 = SelectExpr(N.getOperand(0));
1630 if(isSigned) { // argument could be negative, so emit some code:
1631 unsigned divAmt=Tmp3;
1632 unsigned tempGR1=MakeReg(MVT::i64);
1633 unsigned tempGR2=MakeReg(MVT::i64);
1634 unsigned tempGR3=MakeReg(MVT::i64);
1635 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1636 .addReg(Tmp1).addImm(divAmt-1);
1637 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1638 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1639 BuildMI(BB, IA64::ADD, 2, tempGR3)
1640 .addReg(Tmp1).addReg(tempGR2);
1641 BuildMI(BB, IA64::SHRS, 2, Result)
1642 .addReg(tempGR3).addImm(divAmt);
1643 }
1644 else // unsigned div-by-power-of-2 becomes a simple shift right:
1645 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1646 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001647 }
1648 }
1649
Misha Brukman4633f1c2005-04-21 23:13:11 +00001650 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001651 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001652 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1653 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1654 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1655 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1656 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1657 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1658 unsigned TmpF7=MakeReg(MVT::f64);
1659 unsigned TmpF8=MakeReg(MVT::f64);
1660 unsigned TmpF9=MakeReg(MVT::f64);
1661 unsigned TmpF10=MakeReg(MVT::f64);
1662 unsigned TmpF11=MakeReg(MVT::f64);
1663 unsigned TmpF12=MakeReg(MVT::f64);
1664 unsigned TmpF13=MakeReg(MVT::f64);
1665 unsigned TmpF14=MakeReg(MVT::f64);
1666 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001667
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001668 // OK, emit some code:
1669
1670 if(!isFP) {
1671 // first, load the inputs into FP regs.
1672 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1673 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001674
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001675 // next, convert the inputs to FP
1676 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001677 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1678 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001679 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001680 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1681 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001682 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001683
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001684 } else { // this is an FP divide/remainder, so we 'leak' some temp
1685 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1686 TmpF3=Tmp1;
1687 TmpF4=Tmp2;
1688 }
1689
1690 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001691 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1692 BuildMI(BB, IA64::FRCPAS1, 4)
1693 .addReg(TmpF5, MachineOperand::Def)
1694 .addReg(TmpPR, MachineOperand::Def)
1695 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001696
Duraid Madinabeeaab22005-03-31 12:31:11 +00001697 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1698 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1699 // TPCMPNE below
1700 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1701 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001702 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001703 }
1704
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001705 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1706 // precision, don't need this much for f32/i32)
1707 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1708 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1709 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1710 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1711 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1712 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1713 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1714 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1715 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1716 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1717 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1718 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1719 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1720 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1721 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1722 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001723
1724 // FIXME: this is unfortunate :(
1725 // the story is that the dest reg of the fnma above and the fma below
1726 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1727 // be the same register, or this code breaks if the first argument is
1728 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001729 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1730 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1731
Duraid Madina6e02e682005-04-04 05:05:52 +00001732 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1733 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1734 }
1735
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001736 if(!isFP) {
1737 // round to an integer
1738 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001739 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001740 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001741 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001742 } else {
1743 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1744 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1745 // we really do need the above FMOV? ;)
1746 }
1747
1748 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001749 if(isFP) { // extra worrying about div-by-zero
1750 unsigned bogoResult=MakeReg(MVT::f64);
1751
1752 // we do a 'conditional fmov' (of the correct result, depending
1753 // on how the frcpa predicate turned out)
1754 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001755 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001756 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001757 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001758 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001759 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001760 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001761 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001762 } else { // this is a modulus
1763 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001764 // answer = q * (-b) + a
1765 unsigned ModulusResult = MakeReg(MVT::f64);
1766 unsigned TmpF = MakeReg(MVT::f64);
1767 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001768
Misha Brukman7847fca2005-04-22 17:54:37 +00001769 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1770 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1771 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1772 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1773 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001774 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001775 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001776 }
1777 }
1778
1779 return Result;
1780 }
1781
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001782 case ISD::SIGN_EXTEND_INREG: {
1783 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001784 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001785 default:
1786 Node->dump();
1787 assert(0 && "don't know how to sign extend this type");
1788 break;
1789 case MVT::i8: Opc = IA64::SXT1; break;
1790 case MVT::i16: Opc = IA64::SXT2; break;
1791 case MVT::i32: Opc = IA64::SXT4; break;
1792 }
1793 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1794 return Result;
1795 }
1796
1797 case ISD::SETCC: {
1798 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001799 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1800 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001801
Chris Lattner88ac32c2005-08-09 20:21:10 +00001802 if(ConstantSDNode *CSDN =
1803 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1804 // if we are comparing against a constant zero
1805 if(CSDN->getValue()==0)
1806 Tmp2 = IA64::r0; // then we can just compare against r0
1807 else
1808 Tmp2 = SelectExpr(N.getOperand(1));
1809 } else // not comparing against a constant
1810 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001811
Chris Lattner88ac32c2005-08-09 20:21:10 +00001812 switch (CC) {
1813 default: assert(0 && "Unknown integer comparison!");
1814 case ISD::SETEQ:
1815 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1816 break;
1817 case ISD::SETGT:
1818 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 break;
1820 case ISD::SETGE:
1821 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1822 break;
1823 case ISD::SETLT:
1824 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1825 break;
1826 case ISD::SETLE:
1827 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 break;
1829 case ISD::SETNE:
1830 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1831 break;
1832 case ISD::SETULT:
1833 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1834 break;
1835 case ISD::SETUGT:
1836 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1837 break;
1838 case ISD::SETULE:
1839 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1840 break;
1841 case ISD::SETUGE:
1842 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1843 break;
1844 }
1845 } else { // if not integer, should be FP.
1846 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1847 "error: SETCC should have had incoming f32 promoted to f64!\n");
1848
1849 if(ConstantFPSDNode *CFPSDN =
1850 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1851
1852 // if we are comparing against a constant +0.0 or +1.0
1853 if(CFPSDN->isExactlyValue(+0.0))
1854 Tmp2 = IA64::F0; // then we can just compare against f0
1855 else if(CFPSDN->isExactlyValue(+1.0))
1856 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001857 else
1858 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001859 } else // not comparing against a constant
1860 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001861
Chris Lattner88ac32c2005-08-09 20:21:10 +00001862 switch (CC) {
1863 default: assert(0 && "Unknown FP comparison!");
1864 case ISD::SETEQ:
1865 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETGT:
1868 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETGE:
1871 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETLT:
1874 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETLE:
1877 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETNE:
1880 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETULT:
1883 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETUGT:
1886 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
1888 case ISD::SETULE:
1889 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1890 break;
1891 case ISD::SETUGE:
1892 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1893 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001894 }
1895 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001896 return Result;
1897 }
1898
1899 case ISD::EXTLOAD:
1900 case ISD::ZEXTLOAD:
1901 case ISD::LOAD: {
1902 // Make sure we generate both values.
1903 if (Result != 1)
1904 ExprMap[N.getValue(1)] = 1; // Generate the token
1905 else
1906 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1907
1908 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001909
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001910 if(opcode == ISD::LOAD) { // this is a LOAD
1911 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001912 default: assert(0 && "Cannot load this type!");
1913 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1914 // FIXME: for now, we treat bool loads the same as i8 loads */
1915 case MVT::i8: Opc = IA64::LD1; break;
1916 case MVT::i16: Opc = IA64::LD2; break;
1917 case MVT::i32: Opc = IA64::LD4; break;
1918 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001919
Misha Brukman7847fca2005-04-22 17:54:37 +00001920 case MVT::f32: Opc = IA64::LDF4; break;
1921 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001922 }
1923 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001924 MVT::ValueType TypeBeingLoaded =
1925 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001926 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001927 default: assert(0 && "Cannot extload/zextload this type!");
1928 // FIXME: bools?
1929 case MVT::i8: Opc = IA64::LD1; break;
1930 case MVT::i16: Opc = IA64::LD2; break;
1931 case MVT::i32: Opc = IA64::LD4; break;
1932 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001933 }
1934 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001935
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001936 SDOperand Chain = N.getOperand(0);
1937 SDOperand Address = N.getOperand(1);
1938
1939 if(Address.getOpcode() == ISD::GlobalAddress) {
1940 Select(Chain);
1941 unsigned dummy = MakeReg(MVT::i64);
1942 unsigned dummy2 = MakeReg(MVT::i64);
1943 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001944 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1945 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001946 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1947 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001948 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001949 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001950 // into a predicate register
1951 assert(Opc==IA64::LD1 && "problem loading a bool");
1952 unsigned dummy3 = MakeReg(MVT::i64);
1953 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1954 // we compare to 0. true? 0. false? 1.
1955 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001956 }
1957 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001958 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattner143b6752005-08-26 17:18:44 +00001959 getConstantPoolIndex(CP->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001960 Select(Chain);
1961 IA64Lowering.restoreGP(BB);
1962 unsigned dummy = MakeReg(MVT::i64);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001963 unsigned dummy2 = MakeReg(MVT::i64);
1964 BuildMI(BB, IA64::MOVLIMM64, 1, dummy2).addConstantPoolIndex(CPIdx);
1965 BuildMI(BB, IA64::ADD, 2, dummy).addReg(dummy2).addReg(IA64::r1); //CPI+GP
1966
1967
1968 // OLD BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
1969 // (FIXME!) .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001970 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001971 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001972 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001973 // into a predicate register
1974 assert(Opc==IA64::LD1 && "problem loading a bool");
1975 unsigned dummy3 = MakeReg(MVT::i64);
1976 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1977 // we compare to 0. true? 0. false? 1.
1978 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001979 }
1980 } else if(Address.getOpcode() == ISD::FrameIndex) {
1981 Select(Chain); // FIXME ? what about bools?
1982 unsigned dummy = MakeReg(MVT::i64);
1983 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001984 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001985 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001986 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001987 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001988 // into a predicate register
1989 assert(Opc==IA64::LD1 && "problem loading a bool");
1990 unsigned dummy3 = MakeReg(MVT::i64);
1991 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1992 // we compare to 0. true? 0. false? 1.
1993 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001994 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001995 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001996 Select(Chain);
1997 Tmp2 = SelectExpr(Address);
1998 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001999 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002000 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002001 // into a predicate register
2002 assert(Opc==IA64::LD1 && "problem loading a bool");
2003 unsigned dummy = MakeReg(MVT::i64);
2004 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2005 // we compare to 0. true? 0. false? 1.
2006 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00002007 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002008 }
2009
2010 return Result;
2011 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002012
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002013 case ISD::CopyFromReg: {
2014 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002015 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002016 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002017
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002018 SDOperand Chain = N.getOperand(0);
2019
2020 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002021 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002022
2023 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002024 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2025 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002026 // (r) Result =cmp.eq.unc(r0,r0)
2027 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002028 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002029 return Result;
2030 }
2031
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002032 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002033 case ISD::CALL: {
2034 Select(N.getOperand(0));
2035
2036 // The chain for this call is now lowered.
2037 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002038
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002039 //grab the arguments
2040 std::vector<unsigned> argvregs;
2041
2042 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002043 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002044
2045 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002046 // Runtime Architecture Guide to see some examples of what's going
2047 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2048 // while FP args get mapped to F8->F15 as needed)
2049
2050 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002051
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002052 // in reg args
2053 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2054 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002055 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2056 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2057 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2058 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002059
Misha Brukman7847fca2005-04-22 17:54:37 +00002060 switch(N.getOperand(i+2).getValueType())
2061 {
2062 default: // XXX do we need to support MVT::i1 here?
2063 Node->dump();
2064 N.getOperand(i).Val->dump();
2065 std::cerr << "Type for " << i << " is: " <<
2066 N.getOperand(i+2).getValueType() << std::endl;
2067 assert(0 && "Unknown value type for call");
2068 case MVT::i64:
2069 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2070 break;
2071 case MVT::f64:
2072 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2073 .addReg(argvregs[i]);
2074 // FIXME: we don't need to do this _all_ the time:
2075 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2076 break;
2077 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002078 }
2079
2080 //in mem args
2081 for (int i = 8, e = argvregs.size(); i < e; ++i)
2082 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002083 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002084
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002085 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002086 default:
2087 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002088 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002089 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002090 N.getOperand(i+2).getValueType() << "\n";
2091 assert(0 && "Unknown value type for call");
2092 case MVT::i1: // FIXME?
2093 case MVT::i8:
2094 case MVT::i16:
2095 case MVT::i32:
2096 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002097 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2098 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2099 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002100 break;
2101 case MVT::f32:
2102 case MVT::f64:
2103 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002104 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2105 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002106 break;
2107 }
2108 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002109
Duraid Madina04aa46d2005-05-20 11:39:17 +00002110 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002111 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002112 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002113 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002114 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2115 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002116 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002117 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002118 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002119 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002120 // "non-pic code with imm relocation against dynamic symbol" errors
2121 BuildMI(BB, IA64::BRCALL, 1)
2122 .addExternalSymbol(ESSDN->getSymbol(), true);
2123 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002124 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002125 else { // otherwise we need to get the function descriptor
2126 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002127 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002128 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002129
2130 unsigned targetEntryPoint=MakeReg(MVT::i64);
2131 unsigned targetGPAddr=MakeReg(MVT::i64);
2132 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002133
Duraid Madinabeeaab22005-03-31 12:31:11 +00002134 // b6 is a scratch branch register, we load the target entry point
2135 // from the base of the function descriptor
2136 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2137 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2138
2139 // save the current GP:
2140 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002141
Duraid Madinabeeaab22005-03-31 12:31:11 +00002142 /* TODO: we need to make sure doing this never, ever loads a
2143 * bogus value into r1 (GP). */
2144 // load the target GP (which is at mem[functiondescriptor+8])
2145 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002146 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002147 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2148
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002149 // and then jump: (well, call)
2150 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002151 // and finally restore the old GP
2152 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2153 IA64Lowering.restoreSP_RP(BB);
2154 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002155
2156 switch (Node->getValueType(0)) {
2157 default: assert(0 && "Unknown value type for call result!");
2158 case MVT::Other: return 1;
2159 case MVT::i1:
2160 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002161 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002162 break;
2163 case MVT::i8:
2164 case MVT::i16:
2165 case MVT::i32:
2166 case MVT::i64:
2167 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2168 break;
2169 case MVT::f64:
2170 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2171 break;
2172 }
2173 return Result+N.ResNo;
2174 }
2175
Misha Brukman4633f1c2005-04-21 23:13:11 +00002176 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002177 return 0;
2178}
2179
2180void ISel::Select(SDOperand N) {
2181 unsigned Tmp1, Tmp2, Opc;
2182 unsigned opcode = N.getOpcode();
2183
Nate Begeman85fdeb22005-03-24 04:39:54 +00002184 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002185 return; // Already selected.
2186
2187 SDNode *Node = N.Val;
2188
2189 switch (Node->getOpcode()) {
2190 default:
2191 Node->dump(); std::cerr << "\n";
2192 assert(0 && "Node not handled yet!");
2193
2194 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002195
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002196 case ISD::TokenFactor: {
2197 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2198 Select(Node->getOperand(i));
2199 return;
2200 }
2201
2202 case ISD::CopyToReg: {
2203 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002204 Tmp1 = SelectExpr(N.getOperand(2));
2205 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002206
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002207 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002208 // if a bool, we use pseudocode
2209 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002210 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2211 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002212 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2213 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002214 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002215 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002216 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002217 }
2218 return;
2219 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002220
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002221 case ISD::RET: {
2222
2223 /* what the heck is going on here:
2224
2225<_sabre_> ret with two operands is obvious: chain and value
2226<camel_> yep
2227<_sabre_> ret with 3 values happens when 'expansion' occurs
2228<_sabre_> e.g. i64 gets split into 2x i32
2229<camel_> oh right
2230<_sabre_> you don't have this case on ia64
2231<camel_> yep
2232<_sabre_> so the two returned values go into EAX/EDX on ia32
2233<camel_> ahhh *memories*
2234<_sabre_> :)
2235<camel_> ok, thanks :)
2236<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2237<_sabre_> this is the first operand always
2238<_sabre_> these operand often define chains, they are the last operand
2239<_sabre_> they are printed as 'ch' if you do DAG.dump()
2240 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002241
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002242 switch (N.getNumOperands()) {
2243 default:
2244 assert(0 && "Unknown return instruction!");
2245 case 2:
2246 Select(N.getOperand(0));
2247 Tmp1 = SelectExpr(N.getOperand(1));
2248 switch (N.getOperand(1).getValueType()) {
2249 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002250 // FIXME: do I need to add support for bools here?
2251 // (return '0' or '1' r8, basically...)
2252 //
2253 // FIXME: need to round floats - 80 bits is bad, the tester
2254 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002255 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002256 // we mark r8 as live on exit up above in LowerArguments()
2257 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2258 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002259 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002260 // we mark F8 as live on exit up above in LowerArguments()
2261 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002262 }
2263 break;
2264 case 1:
2265 Select(N.getOperand(0));
2266 break;
2267 }
2268 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2269 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2270 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2271 return;
2272 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002273
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002274 case ISD::BR: {
2275 Select(N.getOperand(0));
2276 MachineBasicBlock *Dest =
2277 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2278 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2279 // XXX HACK! we do _not_ need long branches all the time
2280 return;
2281 }
2282
2283 case ISD::ImplicitDef: {
2284 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002285 BuildMI(BB, IA64::IDEF, 0,
2286 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002287 return;
2288 }
2289
2290 case ISD::BRCOND: {
2291 MachineBasicBlock *Dest =
2292 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2293
2294 Select(N.getOperand(0));
2295 Tmp1 = SelectExpr(N.getOperand(1));
2296 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2297 // XXX HACK! we do _not_ need long branches all the time
2298 return;
2299 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002300
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002301 case ISD::EXTLOAD:
2302 case ISD::ZEXTLOAD:
2303 case ISD::SEXTLOAD:
2304 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002305 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002306 case ISD::CALL:
2307 case ISD::CopyFromReg:
2308 case ISD::DYNAMIC_STACKALLOC:
2309 SelectExpr(N);
2310 return;
2311
2312 case ISD::TRUNCSTORE:
2313 case ISD::STORE: {
2314 Select(N.getOperand(0));
2315 Tmp1 = SelectExpr(N.getOperand(1)); // value
2316
2317 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002318
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002319 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002320 switch (N.getOperand(1).getValueType()) {
2321 default: assert(0 && "Cannot store this type!");
2322 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2323 // FIXME?: for now, we treat bool loads the same as i8 stores */
2324 case MVT::i8: Opc = IA64::ST1; break;
2325 case MVT::i16: Opc = IA64::ST2; break;
2326 case MVT::i32: Opc = IA64::ST4; break;
2327 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002328
Misha Brukman7847fca2005-04-22 17:54:37 +00002329 case MVT::f32: Opc = IA64::STF4; break;
2330 case MVT::f64: Opc = IA64::STF8; break;
2331 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002332 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002333 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002334 default: assert(0 && "unknown type in truncstore");
2335 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2336 //FIXME: DAG does not promote this load?
2337 case MVT::i8: Opc = IA64::ST1; break;
2338 case MVT::i16: Opc = IA64::ST2; break;
2339 case MVT::i32: Opc = IA64::ST4; break;
2340 case MVT::f32: Opc = IA64::STF4; break;
2341 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002342 }
2343
2344 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002345 unsigned dummy = MakeReg(MVT::i64);
2346 unsigned dummy2 = MakeReg(MVT::i64);
2347 BuildMI(BB, IA64::ADD, 2, dummy)
2348 .addGlobalAddress(cast<GlobalAddressSDNode>
2349 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2350 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002351
Misha Brukman7847fca2005-04-22 17:54:37 +00002352 if(!isBool)
2353 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2354 else { // we are storing a bool, so emit a little pseudocode
2355 // to store a predicate register as one byte
2356 assert(Opc==IA64::ST1);
2357 unsigned dummy3 = MakeReg(MVT::i64);
2358 unsigned dummy4 = MakeReg(MVT::i64);
2359 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2360 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2361 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2362 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2363 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002364 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2365
Misha Brukman7847fca2005-04-22 17:54:37 +00002366 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002367
Misha Brukman7847fca2005-04-22 17:54:37 +00002368 unsigned dummy = MakeReg(MVT::i64);
2369 BuildMI(BB, IA64::MOV, 1, dummy)
2370 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2371 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002372 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002373 Tmp2 = SelectExpr(N.getOperand(2)); //address
2374 if(!isBool)
2375 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2376 else { // we are storing a bool, so emit a little pseudocode
2377 // to store a predicate register as one byte
2378 assert(Opc==IA64::ST1);
2379 unsigned dummy3 = MakeReg(MVT::i64);
2380 unsigned dummy4 = MakeReg(MVT::i64);
2381 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2382 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2383 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2384 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2385 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002386 }
2387 return;
2388 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002389
Chris Lattner16cd04d2005-05-12 23:24:06 +00002390 case ISD::CALLSEQ_START:
2391 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002392 Select(N.getOperand(0));
2393 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002394
Chris Lattner16cd04d2005-05-12 23:24:06 +00002395 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2396 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002397 BuildMI(BB, Opc, 1).addImm(Tmp1);
2398 return;
2399 }
2400
2401 return;
2402 }
2403 assert(0 && "GAME OVER. INSERT COIN?");
2404}
2405
2406
2407/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2408/// into a machine code representation using pattern matching and a machine
2409/// description file.
2410///
2411FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002412 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002413}
2414
2415