blob: 7c2b38c51b9d83e282159588beb7827f176aca97 [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);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000105 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner18c778f2005-12-01 18:19:53 +0000106
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000107 computeRegisterProperties();
108
109 addLegalFPImmediate(+0.0);
110 addLegalFPImmediate(+1.0);
111 addLegalFPImmediate(-0.0);
112 addLegalFPImmediate(-1.0);
113 }
114
115 /// LowerArguments - This hook must be implemented to indicate how we should
116 /// lower the arguments for the specified function, into the specified DAG.
117 virtual std::vector<SDOperand>
118 LowerArguments(Function &F, SelectionDAG &DAG);
119
120 /// LowerCallTo - This hook lowers an abstract call to a function into an
121 /// actual call.
122 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000123 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000124 bool isTailCall, SDOperand Callee, ArgListTy &Args,
125 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000126
Chris Lattnere0fe2252005-07-05 19:58:54 +0000127 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
128 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000129 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000130 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
131 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000132
133 void restoreGP_SP_RP(MachineBasicBlock* BB)
134 {
135 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
136 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
137 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
138 }
139
Duraid Madinabeeaab22005-03-31 12:31:11 +0000140 void restoreSP_RP(MachineBasicBlock* BB)
141 {
142 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
143 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
144 }
145
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000146 void restoreRP(MachineBasicBlock* BB)
147 {
148 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
149 }
150
151 void restoreGP(MachineBasicBlock* BB)
152 {
153 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
154 }
155
156 };
157}
158
159
160std::vector<SDOperand>
161IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
162 std::vector<SDOperand> ArgValues;
163
164 //
165 // add beautiful description of IA64 stack frame format
166 // here (from intel 24535803.pdf most likely)
167 //
168 MachineFunction &MF = DAG.getMachineFunction();
169 MachineFrameInfo *MFI = MF.getFrameInfo();
170
171 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
173 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
174
175 MachineBasicBlock& BB = MF.front();
176
Misha Brukman4633f1c2005-04-21 23:13:11 +0000177 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000178 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000179
180 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000181 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000182
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 unsigned argVreg[8];
184 unsigned argPreg[8];
185 unsigned argOpc[8];
186
Duraid Madinabeeaab22005-03-31 12:31:11 +0000187 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000188
Duraid Madinabeeaab22005-03-31 12:31:11 +0000189 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000190 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000191
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000192 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000193 {
194 SDOperand newroot, argt;
195 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000196
197 switch (getValueType(I->getType())) {
198 default:
199 std::cerr << "ERROR in LowerArgs: unknown type "
200 << getValueType(I->getType()) << "\n";
201 abort();
202 case MVT::f32:
203 // fixme? (well, will need to for weird FP structy stuff,
204 // see intel ABI docs)
205 case MVT::f64:
206//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
207 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
208 // floating point args go into f8..f15 as-needed, the increment
209 argVreg[count] = // is below..:
210 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
211 // FP args go into f8..f15 as needed: (hence the ++)
212 argPreg[count] = args_FP[used_FPArgs++];
213 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000214 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattner0a00bec2005-08-22 21:33:11 +0000215 MVT::f64);
216 if (I->getType() == Type::FloatTy)
217 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukman7847fca2005-04-22 17:54:37 +0000218 break;
219 case MVT::i1: // NOTE: as far as C abi stuff goes,
220 // bools are just boring old ints
221 case MVT::i8:
222 case MVT::i16:
223 case MVT::i32:
224 case MVT::i64:
225//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
226 MF.addLiveIn(args_int[count]); // mark this register as liveIn
227 argVreg[count] =
228 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
229 argPreg[count] = args_int[count];
230 argOpc[count] = IA64::MOV;
231 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000232 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000233 if ( getValueType(I->getType()) != MVT::i64)
234 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
235 newroot);
236 break;
237 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000238 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000239 // Create the frame index object for this incoming parameter...
240 ArgOffset = 16 + 8 * (count - 8);
241 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000242
Misha Brukman7847fca2005-04-22 17:54:37 +0000243 // Create the SelectionDAG nodes corresponding to a load
244 //from this parameter
245 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
246 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000247 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000248 }
249 ++count;
250 DAG.setRoot(newroot.getValue(1));
251 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000252 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000253
Misha Brukman4633f1c2005-04-21 23:13:11 +0000254
Duraid Madinabeeaab22005-03-31 12:31:11 +0000255 // Create a vreg to hold the output of (what will become)
256 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000257 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
258 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
259 // we create a PSEUDO_ALLOC (pseudo)instruction for now
260
261 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
262
263 // hmm:
264 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
265 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
266 // ..hmm.
267
268 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
269
270 // hmm:
271 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
272 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
273 // ..hmm.
274
Duraid Madinabeeaab22005-03-31 12:31:11 +0000275 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000276
Duraid Madinabeeaab22005-03-31 12:31:11 +0000277 // if this is a varargs function, we simply lower llvm.va_start by
278 // pointing to the first entry
279 if(F.isVarArg()) {
280 tempOffset=0;
281 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000282 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000283
Duraid Madinabeeaab22005-03-31 12:31:11 +0000284 // here we actually do the moving of args, and store them to the stack
285 // too if this is a varargs function:
286 for (int i = 0; i < count && i < 8; ++i) {
287 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
288 if(F.isVarArg()) {
289 // if this is a varargs function, we copy the input registers to the stack
290 int FI = MFI->CreateFixedObject(8, tempOffset);
291 tempOffset+=8; //XXX: is it safe to use r22 like this?
292 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
293 // FIXME: we should use st8.spill here, one day
294 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
295 }
296 }
297
Duraid Madinaca494fd2005-04-12 14:54:44 +0000298 // Finally, inform the code generator which regs we return values in.
299 // (see the ISD::RET: case down below)
300 switch (getValueType(F.getReturnType())) {
301 default: assert(0 && "i have no idea where to return this type!");
302 case MVT::isVoid: break;
303 case MVT::i1:
304 case MVT::i8:
305 case MVT::i16:
306 case MVT::i32:
307 case MVT::i64:
308 MF.addLiveOut(IA64::r8);
309 break;
310 case MVT::f32:
311 case MVT::f64:
312 MF.addLiveOut(IA64::F8);
313 break;
314 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000315
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000316 return ArgValues;
317}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000318
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000319std::pair<SDOperand, SDOperand>
320IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000321 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000322 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000323 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000324 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000325
326 MachineFunction &MF = DAG.getMachineFunction();
327
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000328 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000329 unsigned outRegsUsed = 0;
330
331 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000332 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000333 outRegsUsed = 8;
334 } else {
335 outRegsUsed = Args.size();
336 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000337
Duraid Madinabeeaab22005-03-31 12:31:11 +0000338 // FIXME? this WILL fail if we ever try to pass around an arg that
339 // consumes more than a single output slot (a 'real' double, int128
340 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
341 // registers we use. Hopefully, the assembler will notice.
342 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
343 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000344
Chris Lattner16cd04d2005-05-12 23:24:06 +0000345 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000346 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000347
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000348 std::vector<SDOperand> args_to_use;
349 for (unsigned i = 0, e = Args.size(); i != e; ++i)
350 {
351 switch (getValueType(Args[i].second)) {
352 default: assert(0 && "unexpected argument type!");
353 case MVT::i1:
354 case MVT::i8:
355 case MVT::i16:
356 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000357 //promote to 64-bits, sign/zero extending based on type
358 //of the argument
359 if(Args[i].second->isSigned())
360 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
361 Args[i].first);
362 else
363 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
364 Args[i].first);
365 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000366 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000367 //promote to 64-bits
368 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000369 case MVT::f64:
370 case MVT::i64:
371 break;
372 }
373 args_to_use.push_back(Args[i].first);
374 }
375
376 std::vector<MVT::ValueType> RetVals;
377 MVT::ValueType RetTyVT = getValueType(RetTy);
378 if (RetTyVT != MVT::isVoid)
379 RetVals.push_back(RetTyVT);
380 RetVals.push_back(MVT::Other);
381
382 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000383 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000384 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000385 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000386 DAG.getConstant(NumBytes, getPointerTy()));
387 return std::make_pair(TheCall, Chain);
388}
389
Chris Lattnere0fe2252005-07-05 19:58:54 +0000390SDOperand
391IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
392 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000393 // vastart just stores the address of the VarArgsFrameIndex slot.
394 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000395 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
396 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000397}
398
399std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000400LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
401 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000402
403 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000404 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
405 VAListP, DAG.getSrcValue(VAListV));
406 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
407 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000408 unsigned Amt;
409 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
410 Amt = 8;
411 else {
412 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
413 "Other types should have been promoted for varargs!");
414 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000415 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000416 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000417 DAG.getConstant(Amt, Val.getValueType()));
418 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000419 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000420 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000421}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000422
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000423namespace {
424
425 //===--------------------------------------------------------------------===//
426 /// ISel - IA64 specific code to select IA64 machine instructions for
427 /// SelectionDAG operations.
428 ///
429 class ISel : public SelectionDAGISel {
430 /// IA64Lowering - This object fully describes how to lower LLVM code to an
431 /// IA64-specific SelectionDAG.
432 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000433 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
434 // for sdiv and udiv until it is put into the future
435 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000436
437 /// ExprMap - As shared expressions are codegen'd, we keep track of which
438 /// vreg the value is produced in, so we only emit one copy of each compiled
439 /// tree.
440 std::map<SDOperand, unsigned> ExprMap;
441 std::set<SDOperand> LoweredTokens;
442
443 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000444 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
445 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000446
447 /// InstructionSelectBasicBlock - This callback is invoked by
448 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
449 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
450
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000451 unsigned SelectExpr(SDOperand N);
452 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000453 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
454 SDOperand BuildConstmulSequence(SDOperand N);
455
Chris Lattner47c08892005-08-22 18:28:09 +0000456 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000457 };
458}
459
460/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
461/// when it has created a SelectionDAG for us to codegen.
462void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
463
464 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000465 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000466 Select(DAG.getRoot());
467
468 // Clear state used for selection.
469 ExprMap.clear();
470 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000471 ISelDAG = 0;
472}
473
Duraid Madinab2322562005-04-26 07:23:02 +0000474// strip leading '0' characters from a string
475void munchLeadingZeros(std::string& inString) {
476 while(inString.c_str()[0]=='0') {
477 inString.erase(0, 1);
478 }
479}
480
481// strip trailing '0' characters from a string
482void munchTrailingZeros(std::string& inString) {
483 int curPos=inString.length()-1;
484
485 while(inString.c_str()[curPos]=='0') {
486 inString.erase(curPos, 1);
487 curPos--;
488 }
489}
490
491// return how many consecutive '0' characters are at the end of a string
492unsigned int countTrailingZeros(std::string& inString) {
493 int curPos=inString.length()-1;
494 unsigned int zeroCount=0;
495 // assert goes here
496 while(inString.c_str()[curPos--]=='0') {
497 zeroCount++;
498 }
499 return zeroCount;
500}
501
502// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
503// '0' and 'N' (-1) characters)
504void boothEncode(std::string inString, std::string& boothEncodedString) {
505
506 int curpos=0;
507 int replacements=0;
508 int lim=inString.size();
509
510 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000511 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000512 int runlength=0;
513 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000514
Duraid Madinab2322562005-04-26 07:23:02 +0000515 // find the run length
516 for(;inString[curpos+runlength]=='1';runlength++) ;
517
518 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000519 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000520 replaceString+="1";
521
522 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000523 inString.replace(curpos, runlength+1, replaceString);
524 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000525 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000526 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000527 } else { // a zero, we just keep chugging along
528 curpos++;
529 }
530 }
531
532 // clean up (trim the string, reverse it and turn '1's into 'P's)
533 munchTrailingZeros(inString);
534 boothEncodedString="";
535
536 for(int i=inString.size()-1;i>=0;i--)
537 if(inString[i]=='1')
538 boothEncodedString+="P";
539 else
540 boothEncodedString+=inString[i];
541
542}
543
544struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
545 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000546 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000547 unsigned secondVal; // C
548 unsigned secondShift; // D
549 bool isSub;
550};
551
552/* this implements Lefevre's "pattern-based" constant multiplication,
553 * see "Multiplication by an Integer Constant", INRIA report 1999-06
554 *
555 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
556 * to get better booth encodings - this does help in practice
557 * TODO: weight shifts appropriately (most architectures can't
558 * fuse a shift and an add for arbitrary shift amounts) */
559unsigned lefevre(const std::string inString,
560 std::vector<struct shiftaddblob> &ops) {
561 std::string retstring;
562 std::string s = inString;
563 munchTrailingZeros(s);
564
565 int length=s.length()-1;
566
567 if(length==0) {
568 return(0);
569 }
570
571 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000572
Duraid Madinab2322562005-04-26 07:23:02 +0000573 for(int i=0; i<=length; i++) {
574 if (s.c_str()[length-i]=='P') {
575 p.push_back(i);
576 } else if (s.c_str()[length-i]=='N') {
577 n.push_back(i);
578 }
579 }
580
581 std::string t, u;
Chris Lattner4a209972005-08-25 00:03:21 +0000582 int c = 0;
Duraid Madina4706c032005-04-26 09:42:50 +0000583 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000584 std::map<const int, int> w;
585
Duraid Madina85d5f602005-04-27 11:57:39 +0000586 for(unsigned i=0; i<p.size(); i++) {
587 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000588 w[p[i]-p[j]]++;
589 }
590 }
591
Duraid Madina85d5f602005-04-27 11:57:39 +0000592 for(unsigned i=1; i<n.size(); i++) {
593 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000594 w[n[i]-n[j]]++;
595 }
596 }
597
Duraid Madina85d5f602005-04-27 11:57:39 +0000598 for(unsigned i=0; i<p.size(); i++) {
599 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000600 w[-abs(p[i]-n[j])]++;
601 }
602 }
603
604 std::map<const int, int>::const_iterator ii;
605 std::vector<int> d;
606 std::multimap<int, int> sorted_by_value;
607
608 for(ii = w.begin(); ii!=w.end(); ii++)
609 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
610
611 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
612 it != sorted_by_value.end(); ++it) {
613 d.push_back((*it).second);
614 }
615
616 int int_W=0;
617 int int_d;
618
619 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
620 d.pop_back();
621 retstring=s; // hmmm
622 int x=0;
623 int z=abs(int_d)-1;
624
625 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000626
Duraid Madina85d5f602005-04-27 11:57:39 +0000627 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000628 if( ((base+z+1) < retstring.size()) &&
629 retstring.c_str()[base]=='P' &&
630 retstring.c_str()[base+z+1]=='P')
631 {
632 // match
633 x++;
634 retstring.replace(base, 1, "0");
635 retstring.replace(base+z+1, 1, "p");
636 }
Duraid Madinab2322562005-04-26 07:23:02 +0000637 }
638
Duraid Madina85d5f602005-04-27 11:57:39 +0000639 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000640 if( ((base+z+1) < retstring.size()) &&
641 retstring.c_str()[base]=='N' &&
642 retstring.c_str()[base+z+1]=='N')
643 {
644 // match
645 x++;
646 retstring.replace(base, 1, "0");
647 retstring.replace(base+z+1, 1, "n");
648 }
Duraid Madinab2322562005-04-26 07:23:02 +0000649 }
650
651 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000652 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000653 if( ((base+z+1) < retstring.size()) &&
654 ((retstring.c_str()[base]=='P' &&
655 retstring.c_str()[base+z+1]=='N') ||
656 (retstring.c_str()[base]=='N' &&
657 retstring.c_str()[base+z+1]=='P')) ) {
658 // match
659 x++;
660
661 if(retstring.c_str()[base]=='P') {
662 retstring.replace(base, 1, "0");
663 retstring.replace(base+z+1, 1, "p");
664 } else { // retstring[base]=='N'
665 retstring.replace(base, 1, "0");
666 retstring.replace(base+z+1, 1, "n");
667 }
668 }
Duraid Madinab2322562005-04-26 07:23:02 +0000669 }
670 }
671
672 if(x>int_W) {
673 int_W = x;
674 t = retstring;
675 c = int_d; // tofix
676 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000677
Duraid Madinab2322562005-04-26 07:23:02 +0000678 } d.pop_back(); // hmm
679
680 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000681
Duraid Madina85d5f602005-04-27 11:57:39 +0000682 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000683 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
684 t.replace(i, 1, "0");
685 }
686
Duraid Madina85d5f602005-04-27 11:57:39 +0000687 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000688 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000689 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000690 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000691 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000692 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000693 u.replace(i, 1, "N");
694 }
695
696 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000697 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000698 c=-c;
699 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000700 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000701
Duraid Madina8a3042c2005-05-09 13:18:34 +0000702 int pos=0;
703 while(u[pos]=='0')
704 pos++;
705
706 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000707
708 int g=0;
709 if(hit) {
710 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000711 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000712 bool isP=(u[p]=='P');
713 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000714
715 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000716 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000717 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000718 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000719 }
720 }
721
722 munchLeadingZeros(u);
723
724 int i = lefevre(u, ops);
725
726 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000727
Duraid Madinab2322562005-04-26 07:23:02 +0000728 blob.firstVal=i; blob.firstShift=c;
729 blob.isSub=f;
730 blob.secondVal=i; blob.secondShift=0;
731
732 ops.push_back(blob);
733
734 i = ops.size();
735
736 munchLeadingZeros(t);
737
738 if(t.length()==0)
739 return i;
740
741 if(t.c_str()[0]!='P') {
742 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000743 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000744 bool isP=(t.c_str()[p]=='P');
745 bool isN=(t.c_str()[p]=='N');
746
747 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000748 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000749 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000750 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000751 }
752 }
753
754 int j = lefevre(t, ops);
755
756 int trail=countTrailingZeros(u);
757 blob.secondVal=i; blob.secondShift=trail;
758
759 trail=countTrailingZeros(t);
760 blob.firstVal=j; blob.firstShift=trail;
761
762 switch(g) {
763 case 0:
764 blob.isSub=false; // first + second
765 break;
766 case 1:
767 blob.isSub=true; // first - second
768 break;
769 case 2:
770 blob.isSub=true; // second - first
771 int tmpval, tmpshift;
772 tmpval=blob.firstVal;
773 tmpshift=blob.firstShift;
774 blob.firstVal=blob.secondVal;
775 blob.firstShift=blob.secondShift;
776 blob.secondVal=tmpval;
777 blob.secondShift=tmpshift;
778 break;
779 //assert
780 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000781
Duraid Madinab2322562005-04-26 07:23:02 +0000782 ops.push_back(blob);
783 return ops.size();
784}
785
786SDOperand ISel::BuildConstmulSequence(SDOperand N) {
787 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
788 // in particular, *3 is nicer as *2+1, not *4-1
789 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
790
791 bool flippedSign;
792 unsigned preliminaryShift=0;
793
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000794 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000795
796 // first, we make the constant to multiply by positive
797 if(constant<0) {
798 constant=-constant;
799 flippedSign=true;
800 } else {
801 flippedSign=false;
802 }
803
804 // next, we make it odd.
805 for(; (constant%2==0); preliminaryShift++)
806 constant>>=1;
807
808 //OK, we have a positive, odd number of 64 bits or less. Convert it
809 //to a binary string, constantString[0] is the LSB
810 char constantString[65];
811 for(int i=0; i<64; i++)
812 constantString[i]='0'+((constant>>i)&0x1);
813 constantString[64]=0;
814
815 // now, Booth encode it
816 std::string boothEncodedString;
817 boothEncode(constantString, boothEncodedString);
818
819 std::vector<struct shiftaddblob> ops;
820 // do the transformation, filling out 'ops'
821 lefevre(boothEncodedString, ops);
822
Duraid Madinae75a24a2005-05-15 14:44:13 +0000823 assert(ops.size() < 80 && "constmul code has gone haywire\n");
824 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000825
Duraid Madinab2322562005-04-26 07:23:02 +0000826 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000827 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000828 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
829 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
830 results[ops[i].firstVal-1];
831 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
832 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
833 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
834 results[ops[i].secondVal-1];
835 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
836 if(ops[i].isSub)
837 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
838 else
839 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
840 }
841
842 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000843 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000844 if(preliminaryShift) {
845 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000846 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000847 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000848 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000849 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000850 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000851
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000852 SDOperand finalresult;
853 if(flippedSign) { // if we were multiplying by a negative constant:
854 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
855 // subtract the result from 0 to flip its sign
856 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
857 } else { // there was no preliminary multiply by -1 required
858 finalresult = shiftedresult;
859 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000860
861 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000862}
863
Duraid Madina4826a072005-04-06 09:55:17 +0000864/// ponderIntegerDivisionBy - When handling integer divides, if the divide
865/// is by a constant such that we can efficiently codegen it, this
866/// function says what to do. Currently, it returns 0 if the division must
867/// become a genuine divide, and 1 if the division can be turned into a
868/// right shift.
869static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
870 unsigned& Imm) {
871 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
872 // a constant, give up.
873
874 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
875
Chris Lattner0561b3f2005-08-02 19:26:06 +0000876 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
877 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000878 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000879 }
880
Duraid Madina4826a072005-04-06 09:55:17 +0000881 return 0; // fallthrough
882}
883
Duraid Madinac02780e2005-04-13 04:50:54 +0000884static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
885 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
886 // a constant, give up.
887
888 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
889
Chris Lattner0561b3f2005-08-02 19:26:06 +0000890 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000891 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000892 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000893 }
894
Duraid Madinac02780e2005-04-13 04:50:54 +0000895 return 0; // fallthrough
896}
897
Duraid Madinaf55e4032005-04-07 12:33:38 +0000898static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
899 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
900 // constant, give up.
901 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
902
903 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
904 Imm = v & 0x3FFF; // 14 bits
905 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000906 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000907 return 0; // fallthrough
908}
909
910static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
911 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
912 // constant, give up.
913 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
914
915 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
916 Imm = v & 0xFF; // 8 bits
917 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000918 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000919 return 0; // fallthrough
920}
921
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000922unsigned ISel::SelectExpr(SDOperand N) {
923 unsigned Result;
924 unsigned Tmp1, Tmp2, Tmp3;
925 unsigned Opc = 0;
926 MVT::ValueType DestType = N.getValueType();
927
928 unsigned opcode = N.getOpcode();
929
930 SDNode *Node = N.Val;
931 SDOperand Op0, Op1;
932
933 if (Node->getOpcode() == ISD::CopyFromReg)
934 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000935 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000936
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000937 unsigned &Reg = ExprMap[N];
938 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000939
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000940 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000941 Reg = Result = (N.getValueType() != MVT::Other) ?
942 MakeReg(N.getValueType()) : 1;
943 else {
944 // If this is a call instruction, make sure to prepare ALL of the result
945 // values as well as the chain.
946 if (Node->getNumValues() == 1)
947 Reg = Result = 1; // Void call, just a chain.
948 else {
949 Result = MakeReg(Node->getValueType(0));
950 ExprMap[N.getValue(0)] = Result;
951 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
952 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
953 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
954 }
955 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000956
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000957 switch (N.getOpcode()) {
958 default:
959 Node->dump();
960 assert(0 && "Node not handled!\n");
961
962 case ISD::FrameIndex: {
963 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
964 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
965 return Result;
966 }
967
968 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000969 Tmp1 = BB->getParent()->getConstantPool()->
970 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000971 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
972 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
973 .addReg(IA64::r1);
974 return Result;
975 }
976
977 case ISD::ConstantFP: {
978 Tmp1 = Result; // Intermediate Register
979 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
980 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
981 Tmp1 = MakeReg(MVT::f64);
982
983 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
984 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
985 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
986 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
987 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
988 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
989 else
990 assert(0 && "Unexpected FP constant!");
991 if (Tmp1 != Result)
992 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
993 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000994 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000995 return Result;
996 }
997
998 case ISD::DYNAMIC_STACKALLOC: {
999 // Generate both result values.
1000 if (Result != 1)
1001 ExprMap[N.getValue(1)] = 1; // Generate the token
1002 else
1003 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1004
1005 // FIXME: We are currently ignoring the requested alignment for handling
1006 // greater than the stack alignment. This will need to be revisited at some
1007 // point. Align = N.getOperand(2);
1008
1009 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1010 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1011 std::cerr << "Cannot allocate stack object with greater alignment than"
1012 << " the stack alignment yet!";
1013 abort();
1014 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001015
1016/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001017 Select(N.getOperand(0));
1018 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1019 {
1020 if (CN->getValue() < 32000)
1021 {
1022 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001023 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001024 } else {
1025 Tmp1 = SelectExpr(N.getOperand(1));
1026 // Subtract size from stack pointer, thereby allocating some space.
1027 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1028 }
1029 } else {
1030 Tmp1 = SelectExpr(N.getOperand(1));
1031 // Subtract size from stack pointer, thereby allocating some space.
1032 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1033 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001034*/
1035 Select(N.getOperand(0));
1036 Tmp1 = SelectExpr(N.getOperand(1));
1037 // Subtract size from stack pointer, thereby allocating some space.
1038 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001039 // Put a pointer to the space into the result register, by copying the
1040 // stack pointer.
1041 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1042 return Result;
1043 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001044
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001045 case ISD::SELECT: {
1046 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1047 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1048 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1049
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001050 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001051
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001052 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001053 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001054 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1055 // for i1, we load the condition into an integer register, then
1056 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1057 // of them will go through, since the integer register will hold
1058 // either 0 or 1)
1059 case MVT::i1: {
1060 bogoResult=MakeReg(MVT::i1);
1061
1062 // load the condition into an integer register
1063 unsigned condReg=MakeReg(MVT::i64);
1064 unsigned dummy=MakeReg(MVT::i64);
1065 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1066 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1067 .addImm(1).addReg(Tmp1);
1068
1069 // initialize Result (bool) to false (hence UNC) and if
1070 // the select condition (condReg) is false (0), copy Tmp3
1071 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1072 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1073
1074 // now, if the selection condition is true, write 1 to the
1075 // result if Tmp2 is 1
1076 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1077 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1078 break;
1079 }
1080 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1081 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001082 case MVT::i64:
1083 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001084 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1085 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1086 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001087 break;
1088 case MVT::f64:
1089 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001090 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1091 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1092 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001093 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001094 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001095
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001096 return Result;
1097 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001098
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001099 case ISD::Constant: {
1100 unsigned depositPos=0;
1101 unsigned depositLen=0;
1102 switch (N.getValueType()) {
1103 default: assert(0 && "Cannot use constants of this type!");
1104 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001105 // the constant:
1106 if(cast<ConstantSDNode>(N)->getValue()) // true:
1107 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1108 else // false:
1109 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1110 return Result; // early exit
1111 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001112 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001113 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001114
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001115 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001116
1117 if(immediate==0) { // if the constant is just zero,
1118 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1119 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001120 }
1121
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001122 if (immediate <= 8191 && immediate >= -8192) {
1123 // if this constants fits in 14 bits, we use a mov the assembler will
1124 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1125 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1126 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001128
1129 if (immediate <= 2097151 && immediate >= -2097152) {
1130 // if this constants fits in 22 bits, we use a mov the assembler will
1131 // turn into: "addl rDest=imm,r0"
1132 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1133 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001134 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001135
1136 /* otherwise, our immediate is big, so we use movl */
1137 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001138 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001139 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001140 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001141
1142 case ISD::UNDEF: {
1143 BuildMI(BB, IA64::IDEF, 0, Result);
1144 return Result;
1145 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 case ISD::GlobalAddress: {
1148 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1149 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001150
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001151 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001152 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001153
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001154 return Result;
1155 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001156
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001157 case ISD::ExternalSymbol: {
1158 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001159// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1160 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001161 return Result;
1162 }
1163
1164 case ISD::FP_EXTEND: {
1165 Tmp1 = SelectExpr(N.getOperand(0));
1166 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1167 return Result;
1168 }
1169
Chris Lattner26e04bb2005-09-02 00:15:30 +00001170 case ISD::ANY_EXTEND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001171 case ISD::ZERO_EXTEND: {
1172 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001173
Chris Lattner26e04bb2005-09-02 00:15:30 +00001174 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1175 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001176
Chris Lattner26e04bb2005-09-02 00:15:30 +00001177 // if the predicate reg has 1, we want a '1' in our GR.
1178 unsigned dummy = MakeReg(MVT::i64);
1179 // first load zero:
1180 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1181 // ...then conditionally (PR:Tmp1) add 1:
1182 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1183 .addImm(1).addReg(Tmp1);
1184 return Result; // XXX early exit!
1185 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001186
Chris Lattner26e04bb2005-09-02 00:15:30 +00001187 case ISD::SIGN_EXTEND:
1188 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1189 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001190
1191 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner26e04bb2005-09-02 00:15:30 +00001192 assert(0 && "don't know how to sign_extend from bool yet!");
1193 abort();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001194
Chris Lattner26e04bb2005-09-02 00:15:30 +00001195 case ISD::TRUNCATE:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001196 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1197 // of R0 appropriately.
Chris Lattner26e04bb2005-09-02 00:15:30 +00001198 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1199 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001200 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001201
Chris Lattner26e04bb2005-09-02 00:15:30 +00001202 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1203 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1204 return Result; // XXX early exit!
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001205
Misha Brukman7847fca2005-04-22 17:54:37 +00001206/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001207 case ISD::FP_ROUND: {
1208 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001209 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001210 Tmp1 = SelectExpr(N.getOperand(0));
1211 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1212 // we add 0.0 using a single precision add to do rounding
1213 return Result;
1214 }
1215*/
1216
1217// FIXME: the following 4 cases need cleaning
1218 case ISD::SINT_TO_FP: {
1219 Tmp1 = SelectExpr(N.getOperand(0));
1220 Tmp2 = MakeReg(MVT::f64);
1221 unsigned dummy = MakeReg(MVT::f64);
1222 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1223 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1224 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1225 return Result;
1226 }
1227
1228 case ISD::UINT_TO_FP: {
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 Tmp2 = MakeReg(MVT::f64);
1231 unsigned dummy = MakeReg(MVT::f64);
1232 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1233 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1234 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1235 return Result;
1236 }
1237
1238 case ISD::FP_TO_SINT: {
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 Tmp2 = MakeReg(MVT::f64);
1241 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1242 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1243 return Result;
1244 }
1245
1246 case ISD::FP_TO_UINT: {
1247 Tmp1 = SelectExpr(N.getOperand(0));
1248 Tmp2 = MakeReg(MVT::f64);
1249 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1250 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1251 return Result;
1252 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001253
1254 case ISD::FADD: {
1255 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1256 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1257 // into an fma, do so:
1258 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001259 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1260 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1261 Tmp3 = SelectExpr(N.getOperand(1));
1262 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1263 return Result; // early exit
1264 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001265
1266 //else, fallthrough:
1267 Tmp1 = SelectExpr(N.getOperand(0));
1268 Tmp2 = SelectExpr(N.getOperand(1));
1269 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1270 return Result;
1271 }
Duraid Madinaed095022005-04-13 06:12:04 +00001272
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001273 case ISD::ADD: {
1274 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001275 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001276 // this add into a shladd, try:
1277 ConstantSDNode *CSD = NULL;
1278 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001279 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001280
Misha Brukman7847fca2005-04-22 17:54:37 +00001281 // ++FusedSHLADD; // Statistic
1282 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1283 int shl_amt = CSD->getValue();
1284 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001285
Misha Brukman7847fca2005-04-22 17:54:37 +00001286 BuildMI(BB, IA64::SHLADD, 3, Result)
1287 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1288 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001289 }
1290 }
1291
1292 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001293 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001294 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1295 case 1: // adding a constant that's 14 bits
1296 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1297 return Result; // early exit
1298 } // fallthrough and emit a reg+reg ADD:
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001301 return Result;
1302 }
1303
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001304 case ISD::FMUL:
1305 Tmp1 = SelectExpr(N.getOperand(0));
1306 Tmp2 = SelectExpr(N.getOperand(1));
1307 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1308 return Result;
1309
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001310 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001311
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001312 // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001313/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1314 */
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001315 // boring old integer multiply with xma
Duraid Madinab2322562005-04-26 07:23:02 +00001316 Tmp1 = SelectExpr(N.getOperand(0));
1317 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001318
1319 unsigned TempFR1=MakeReg(MVT::f64);
1320 unsigned TempFR2=MakeReg(MVT::f64);
1321 unsigned TempFR3=MakeReg(MVT::f64);
1322 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1323 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1324 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1325 .addReg(IA64::F0);
1326 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1327 return Result; // early exit
1328 /* FIXME } else { // we are multiplying by an integer constant! yay
1329 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1330 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001331 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001332
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001333 case ISD::FSUB:
1334 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madina4826a072005-04-06 09:55:17 +00001335 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1336 // into an fms, do so:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001337 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001338 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1339 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1340 Tmp3 = SelectExpr(N.getOperand(1));
1341 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1342 return Result; // early exit
1343 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001344
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001345 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001346 Tmp1 = SelectExpr(N.getOperand(0));
1347 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1348 return Result;
1349
1350 case ISD::SUB: {
1351 Tmp2 = SelectExpr(N.getOperand(1));
1352 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1353 case 1: // subtracting *from* an 8 bit constant:
1354 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1355 return Result; // early exit
1356 } // fallthrough and emit a reg+reg SUB:
1357 Tmp1 = SelectExpr(N.getOperand(0));
1358 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001359 return Result;
1360 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001361
1362 case ISD::FABS: {
1363 Tmp1 = SelectExpr(N.getOperand(0));
1364 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1365 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1366 return Result;
1367 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001369 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001370 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001371
Misha Brukman4633f1c2005-04-21 23:13:11 +00001372 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001373 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1374 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1375 } else {
1376 Tmp1 = SelectExpr(N.getOperand(0));
1377 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1378 }
1379
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001380 return Result;
1381 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001382
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001383 case ISD::AND: {
1384 switch (N.getValueType()) {
1385 default: assert(0 && "Cannot AND this type!");
1386 case MVT::i1: { // if a bool, we emit a pseudocode AND
1387 unsigned pA = SelectExpr(N.getOperand(0));
1388 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001389
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001390/* our pseudocode for AND is:
1391 *
1392(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1393 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1394 ;;
1395(pB) cmp.ne pTemp,p0 = r0,r0
1396 ;;
1397(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1398
1399*/
1400 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001401
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001402 unsigned bogusTemp1 = MakeReg(MVT::i1);
1403 unsigned bogusTemp2 = MakeReg(MVT::i1);
1404 unsigned bogusTemp3 = MakeReg(MVT::i1);
1405 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001406
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001408 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001409 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001410 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001411 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001412 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001413 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001414 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001415 break;
1416 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001417
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001418 // if not a bool, we just AND away:
1419 case MVT::i8:
1420 case MVT::i16:
1421 case MVT::i32:
1422 case MVT::i64: {
1423 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001424 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001425 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001426 switch (Tmp3) {
1427 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1428 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1429 break;
1430 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1431 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1432 break;
1433 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1434 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1435 break;
1436 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001437 // FIXME: assert the dep.z is in bounds
1438 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001439 .addImm(0).addImm(Tmp3);
1440 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001441 }
1442 return Result; // early exit
1443 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001444 Tmp2 = SelectExpr(N.getOperand(1));
1445 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001446 }
1447 }
1448 return Result;
1449 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001450
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001451 case ISD::OR: {
1452 switch (N.getValueType()) {
1453 default: assert(0 && "Cannot OR this type!");
1454 case MVT::i1: { // if a bool, we emit a pseudocode OR
1455 unsigned pA = SelectExpr(N.getOperand(0));
1456 unsigned pB = SelectExpr(N.getOperand(1));
1457
1458 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001459
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001460/* our pseudocode for OR is:
1461 *
1462
1463pC = pA OR pB
1464-------------
1465
Misha Brukman7847fca2005-04-22 17:54:37 +00001466(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1467 ;;
1468(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001469
1470*/
1471 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001472 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001473 BuildMI(BB, IA64::TPCMPEQ, 4, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001474 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001475 break;
1476 }
1477 // if not a bool, we just OR away:
1478 case MVT::i8:
1479 case MVT::i16:
1480 case MVT::i32:
1481 case MVT::i64: {
1482 Tmp1 = SelectExpr(N.getOperand(0));
1483 Tmp2 = SelectExpr(N.getOperand(1));
1484 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1485 break;
1486 }
1487 }
1488 return Result;
1489 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001490
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001491 case ISD::XOR: {
1492 switch (N.getValueType()) {
1493 default: assert(0 && "Cannot XOR this type!");
1494 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1495 unsigned pY = SelectExpr(N.getOperand(0));
1496 unsigned pZ = SelectExpr(N.getOperand(1));
1497
1498/* one possible routine for XOR is:
1499
1500 // Compute px = py ^ pz
1501 // using sum of products: px = (py & !pz) | (pz & !py)
1502 // Uses 5 instructions in 3 cycles.
1503 // cycle 1
1504(pz) cmp.eq.unc px = r0, r0 // px = pz
1505(py) cmp.eq.unc pt = r0, r0 // pt = py
1506 ;;
1507 // cycle 2
1508(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1509(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1510 ;;
1511 } { .mmi
1512 // cycle 3
1513(pt) cmp.eq.or px = r0, r0 // px = px | pt
1514
1515*** Another, which we use here, requires one scratch GR. it is:
1516
1517 mov rt = 0 // initialize rt off critical path
1518 ;;
1519
1520 // cycle 1
1521(pz) cmp.eq.unc px = r0, r0 // px = pz
1522(pz) mov rt = 1 // rt = pz
1523 ;;
1524 // cycle 2
1525(py) cmp.ne px = 1, rt // if (py) px = !pz
1526
1527.. these routines kindly provided by Jim Hull
1528*/
1529 unsigned rt = MakeReg(MVT::i64);
1530
1531 // these two temporaries will never actually appear,
1532 // due to the two-address form of some of the instructions below
1533 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1534 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1535
1536 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1537 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001538 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001539 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001540 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001541 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001542 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001543 break;
1544 }
1545 // if not a bool, we just XOR away:
1546 case MVT::i8:
1547 case MVT::i16:
1548 case MVT::i32:
1549 case MVT::i64: {
1550 Tmp1 = SelectExpr(N.getOperand(0));
1551 Tmp2 = SelectExpr(N.getOperand(1));
1552 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1553 break;
1554 }
1555 }
1556 return Result;
1557 }
1558
Duraid Madina63bbed52005-05-11 05:16:09 +00001559 case ISD::CTPOP: {
1560 Tmp1 = SelectExpr(N.getOperand(0));
1561 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1562 return Result;
1563 }
1564
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001565 case ISD::SHL: {
1566 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001567 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1568 Tmp2 = CN->getValue();
1569 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1570 } else {
1571 Tmp2 = SelectExpr(N.getOperand(1));
1572 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1573 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001574 return Result;
1575 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001576
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001577 case ISD::SRL: {
1578 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001579 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1580 Tmp2 = CN->getValue();
1581 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1582 } else {
1583 Tmp2 = SelectExpr(N.getOperand(1));
1584 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1585 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001586 return Result;
1587 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001588
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001589 case ISD::SRA: {
1590 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001591 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1592 Tmp2 = CN->getValue();
1593 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1594 } else {
1595 Tmp2 = SelectExpr(N.getOperand(1));
1596 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1597 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001598 return Result;
1599 }
1600
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001601 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001602 case ISD::SDIV:
1603 case ISD::UDIV:
1604 case ISD::SREM:
1605 case ISD::UREM: {
1606
1607 Tmp1 = SelectExpr(N.getOperand(0));
1608 Tmp2 = SelectExpr(N.getOperand(1));
1609
1610 bool isFP=false;
1611
1612 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1613 isFP=true;
1614
1615 bool isModulus=false; // is it a division or a modulus?
1616 bool isSigned=false;
1617
1618 switch(N.getOpcode()) {
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001619 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001620 case ISD::SDIV: isModulus=false; isSigned=true; break;
1621 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001622 case ISD::FREM:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001623 case ISD::SREM: isModulus=true; isSigned=true; break;
1624 case ISD::UREM: isModulus=true; isSigned=false; break;
1625 }
1626
Duraid Madina4826a072005-04-06 09:55:17 +00001627 if(!isModulus && !isFP) { // if this is an integer divide,
1628 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001629 case 1: // division by a constant that's a power of 2
1630 Tmp1 = SelectExpr(N.getOperand(0));
1631 if(isSigned) { // argument could be negative, so emit some code:
1632 unsigned divAmt=Tmp3;
1633 unsigned tempGR1=MakeReg(MVT::i64);
1634 unsigned tempGR2=MakeReg(MVT::i64);
1635 unsigned tempGR3=MakeReg(MVT::i64);
1636 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1637 .addReg(Tmp1).addImm(divAmt-1);
1638 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1639 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1640 BuildMI(BB, IA64::ADD, 2, tempGR3)
1641 .addReg(Tmp1).addReg(tempGR2);
1642 BuildMI(BB, IA64::SHRS, 2, Result)
1643 .addReg(tempGR3).addImm(divAmt);
1644 }
1645 else // unsigned div-by-power-of-2 becomes a simple shift right:
1646 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1647 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001648 }
1649 }
1650
Misha Brukman4633f1c2005-04-21 23:13:11 +00001651 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001652 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001653 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1654 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1655 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1656 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1657 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1658 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1659 unsigned TmpF7=MakeReg(MVT::f64);
1660 unsigned TmpF8=MakeReg(MVT::f64);
1661 unsigned TmpF9=MakeReg(MVT::f64);
1662 unsigned TmpF10=MakeReg(MVT::f64);
1663 unsigned TmpF11=MakeReg(MVT::f64);
1664 unsigned TmpF12=MakeReg(MVT::f64);
1665 unsigned TmpF13=MakeReg(MVT::f64);
1666 unsigned TmpF14=MakeReg(MVT::f64);
1667 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001668
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001669 // OK, emit some code:
1670
1671 if(!isFP) {
1672 // first, load the inputs into FP regs.
1673 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1674 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001675
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001676 // next, convert the inputs to FP
1677 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001678 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1679 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001680 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001681 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1682 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001683 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001684
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001685 } else { // this is an FP divide/remainder, so we 'leak' some temp
1686 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1687 TmpF3=Tmp1;
1688 TmpF4=Tmp2;
1689 }
1690
1691 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001692 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1693 BuildMI(BB, IA64::FRCPAS1, 4)
1694 .addReg(TmpF5, MachineOperand::Def)
1695 .addReg(TmpPR, MachineOperand::Def)
1696 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001697
Duraid Madinabeeaab22005-03-31 12:31:11 +00001698 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1699 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1700 // TPCMPNE below
1701 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1702 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001703 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001704 }
1705
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001706 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1707 // precision, don't need this much for f32/i32)
1708 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1709 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1710 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1711 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1712 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1713 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1714 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1715 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1716 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1717 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1718 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1719 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1720 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1721 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1722 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1723 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001724
1725 // FIXME: this is unfortunate :(
1726 // the story is that the dest reg of the fnma above and the fma below
1727 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1728 // be the same register, or this code breaks if the first argument is
1729 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001730 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1731 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1732
Duraid Madina6e02e682005-04-04 05:05:52 +00001733 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1734 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1735 }
1736
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001737 if(!isFP) {
1738 // round to an integer
1739 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001740 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001741 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001742 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001743 } else {
1744 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1745 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1746 // we really do need the above FMOV? ;)
1747 }
1748
1749 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001750 if(isFP) { // extra worrying about div-by-zero
1751 unsigned bogoResult=MakeReg(MVT::f64);
1752
1753 // we do a 'conditional fmov' (of the correct result, depending
1754 // on how the frcpa predicate turned out)
1755 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001756 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001757 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001758 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001759 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001760 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001761 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001762 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001763 } else { // this is a modulus
1764 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001765 // answer = q * (-b) + a
1766 unsigned ModulusResult = MakeReg(MVT::f64);
1767 unsigned TmpF = MakeReg(MVT::f64);
1768 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001769
Misha Brukman7847fca2005-04-22 17:54:37 +00001770 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1771 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1772 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1773 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1774 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001775 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001776 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001777 }
1778 }
1779
1780 return Result;
1781 }
1782
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001783 case ISD::SIGN_EXTEND_INREG: {
1784 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001785 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001786 default:
1787 Node->dump();
1788 assert(0 && "don't know how to sign extend this type");
1789 break;
1790 case MVT::i8: Opc = IA64::SXT1; break;
1791 case MVT::i16: Opc = IA64::SXT2; break;
1792 case MVT::i32: Opc = IA64::SXT4; break;
1793 }
1794 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1795 return Result;
1796 }
1797
1798 case ISD::SETCC: {
1799 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001800 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1801 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001802
Chris Lattner88ac32c2005-08-09 20:21:10 +00001803 if(ConstantSDNode *CSDN =
1804 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1805 // if we are comparing against a constant zero
1806 if(CSDN->getValue()==0)
1807 Tmp2 = IA64::r0; // then we can just compare against r0
1808 else
1809 Tmp2 = SelectExpr(N.getOperand(1));
1810 } else // not comparing against a constant
1811 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001812
Chris Lattner88ac32c2005-08-09 20:21:10 +00001813 switch (CC) {
1814 default: assert(0 && "Unknown integer comparison!");
1815 case ISD::SETEQ:
1816 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1817 break;
1818 case ISD::SETGT:
1819 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 break;
1821 case ISD::SETGE:
1822 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 break;
1824 case ISD::SETLT:
1825 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1826 break;
1827 case ISD::SETLE:
1828 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 break;
1830 case ISD::SETNE:
1831 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1832 break;
1833 case ISD::SETULT:
1834 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1835 break;
1836 case ISD::SETUGT:
1837 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1838 break;
1839 case ISD::SETULE:
1840 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1841 break;
1842 case ISD::SETUGE:
1843 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 break;
1845 }
1846 } else { // if not integer, should be FP.
1847 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1848 "error: SETCC should have had incoming f32 promoted to f64!\n");
1849
1850 if(ConstantFPSDNode *CFPSDN =
1851 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1852
1853 // if we are comparing against a constant +0.0 or +1.0
1854 if(CFPSDN->isExactlyValue(+0.0))
1855 Tmp2 = IA64::F0; // then we can just compare against f0
1856 else if(CFPSDN->isExactlyValue(+1.0))
1857 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001858 else
1859 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001860 } else // not comparing against a constant
1861 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001862
Chris Lattner88ac32c2005-08-09 20:21:10 +00001863 switch (CC) {
1864 default: assert(0 && "Unknown FP comparison!");
1865 case ISD::SETEQ:
1866 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1867 break;
1868 case ISD::SETGT:
1869 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 break;
1871 case ISD::SETGE:
1872 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1873 break;
1874 case ISD::SETLT:
1875 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1876 break;
1877 case ISD::SETLE:
1878 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1879 break;
1880 case ISD::SETNE:
1881 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1882 break;
1883 case ISD::SETULT:
1884 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1885 break;
1886 case ISD::SETUGT:
1887 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1888 break;
1889 case ISD::SETULE:
1890 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1891 break;
1892 case ISD::SETUGE:
1893 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1894 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001895 }
1896 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001897 return Result;
1898 }
1899
1900 case ISD::EXTLOAD:
1901 case ISD::ZEXTLOAD:
1902 case ISD::LOAD: {
1903 // Make sure we generate both values.
1904 if (Result != 1)
1905 ExprMap[N.getValue(1)] = 1; // Generate the token
1906 else
1907 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1908
1909 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001910
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001911 if(opcode == ISD::LOAD) { // this is a LOAD
1912 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001913 default: assert(0 && "Cannot load this type!");
1914 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1915 // FIXME: for now, we treat bool loads the same as i8 loads */
1916 case MVT::i8: Opc = IA64::LD1; break;
1917 case MVT::i16: Opc = IA64::LD2; break;
1918 case MVT::i32: Opc = IA64::LD4; break;
1919 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001920
Misha Brukman7847fca2005-04-22 17:54:37 +00001921 case MVT::f32: Opc = IA64::LDF4; break;
1922 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001923 }
1924 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001925 MVT::ValueType TypeBeingLoaded =
1926 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001927 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001928 default: assert(0 && "Cannot extload/zextload this type!");
1929 // FIXME: bools?
1930 case MVT::i8: Opc = IA64::LD1; break;
1931 case MVT::i16: Opc = IA64::LD2; break;
1932 case MVT::i32: Opc = IA64::LD4; break;
1933 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001934 }
1935 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001936
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001937 SDOperand Chain = N.getOperand(0);
1938 SDOperand Address = N.getOperand(1);
1939
1940 if(Address.getOpcode() == ISD::GlobalAddress) {
1941 Select(Chain);
1942 unsigned dummy = MakeReg(MVT::i64);
1943 unsigned dummy2 = MakeReg(MVT::i64);
1944 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001945 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1946 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001947 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1948 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001949 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001950 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001951 // into a predicate register
1952 assert(Opc==IA64::LD1 && "problem loading a bool");
1953 unsigned dummy3 = MakeReg(MVT::i64);
1954 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1955 // we compare to 0. true? 0. false? 1.
1956 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001957 }
1958 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001959 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattner143b6752005-08-26 17:18:44 +00001960 getConstantPoolIndex(CP->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001961 Select(Chain);
1962 IA64Lowering.restoreGP(BB);
1963 unsigned dummy = MakeReg(MVT::i64);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001964 unsigned dummy2 = MakeReg(MVT::i64);
1965 BuildMI(BB, IA64::MOVLIMM64, 1, dummy2).addConstantPoolIndex(CPIdx);
1966 BuildMI(BB, IA64::ADD, 2, dummy).addReg(dummy2).addReg(IA64::r1); //CPI+GP
1967
1968
1969 // OLD BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
1970 // (FIXME!) .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001971 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001972 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001973 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001974 // into a predicate register
1975 assert(Opc==IA64::LD1 && "problem loading a bool");
1976 unsigned dummy3 = MakeReg(MVT::i64);
1977 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1978 // we compare to 0. true? 0. false? 1.
1979 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001980 }
1981 } else if(Address.getOpcode() == ISD::FrameIndex) {
1982 Select(Chain); // FIXME ? what about bools?
1983 unsigned dummy = MakeReg(MVT::i64);
1984 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001985 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001986 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001987 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001988 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001989 // into a predicate register
1990 assert(Opc==IA64::LD1 && "problem loading a bool");
1991 unsigned dummy3 = MakeReg(MVT::i64);
1992 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1993 // we compare to 0. true? 0. false? 1.
1994 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001995 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001996 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 Select(Chain);
1998 Tmp2 = SelectExpr(Address);
1999 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002000 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002001 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002002 // into a predicate register
2003 assert(Opc==IA64::LD1 && "problem loading a bool");
2004 unsigned dummy = MakeReg(MVT::i64);
2005 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2006 // we compare to 0. true? 0. false? 1.
2007 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00002008 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002009 }
2010
2011 return Result;
2012 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002013
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002014 case ISD::CopyFromReg: {
2015 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002016 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002018
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002019 SDOperand Chain = N.getOperand(0);
2020
2021 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002022 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002023
2024 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002025 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2026 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002027 // (r) Result =cmp.eq.unc(r0,r0)
2028 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002029 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002030 return Result;
2031 }
2032
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002033 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002034 case ISD::CALL: {
2035 Select(N.getOperand(0));
2036
2037 // The chain for this call is now lowered.
2038 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002039
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002040 //grab the arguments
2041 std::vector<unsigned> argvregs;
2042
2043 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002045
2046 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002047 // Runtime Architecture Guide to see some examples of what's going
2048 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2049 // while FP args get mapped to F8->F15 as needed)
2050
2051 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002052
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002053 // in reg args
2054 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2055 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002056 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2057 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2058 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2059 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002060
Misha Brukman7847fca2005-04-22 17:54:37 +00002061 switch(N.getOperand(i+2).getValueType())
2062 {
2063 default: // XXX do we need to support MVT::i1 here?
2064 Node->dump();
2065 N.getOperand(i).Val->dump();
2066 std::cerr << "Type for " << i << " is: " <<
2067 N.getOperand(i+2).getValueType() << std::endl;
2068 assert(0 && "Unknown value type for call");
2069 case MVT::i64:
2070 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2071 break;
2072 case MVT::f64:
2073 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2074 .addReg(argvregs[i]);
2075 // FIXME: we don't need to do this _all_ the time:
2076 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2077 break;
2078 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002079 }
2080
2081 //in mem args
2082 for (int i = 8, e = argvregs.size(); i < e; ++i)
2083 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002084 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002085
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002086 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002087 default:
2088 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002089 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002090 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002091 N.getOperand(i+2).getValueType() << "\n";
2092 assert(0 && "Unknown value type for call");
2093 case MVT::i1: // FIXME?
2094 case MVT::i8:
2095 case MVT::i16:
2096 case MVT::i32:
2097 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002098 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2099 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2100 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002101 break;
2102 case MVT::f32:
2103 case MVT::f64:
2104 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002105 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2106 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002107 break;
2108 }
2109 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002110
Duraid Madina04aa46d2005-05-20 11:39:17 +00002111 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002112 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002113 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002114 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002115 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2116 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002117 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002118 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002119 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002120 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002121 // "non-pic code with imm relocation against dynamic symbol" errors
2122 BuildMI(BB, IA64::BRCALL, 1)
2123 .addExternalSymbol(ESSDN->getSymbol(), true);
2124 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002125 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002126 else { // otherwise we need to get the function descriptor
2127 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002128 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002129 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002130
2131 unsigned targetEntryPoint=MakeReg(MVT::i64);
2132 unsigned targetGPAddr=MakeReg(MVT::i64);
2133 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002134
Duraid Madinabeeaab22005-03-31 12:31:11 +00002135 // b6 is a scratch branch register, we load the target entry point
2136 // from the base of the function descriptor
2137 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2138 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2139
2140 // save the current GP:
2141 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002142
Duraid Madinabeeaab22005-03-31 12:31:11 +00002143 /* TODO: we need to make sure doing this never, ever loads a
2144 * bogus value into r1 (GP). */
2145 // load the target GP (which is at mem[functiondescriptor+8])
2146 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002147 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002148 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2149
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002150 // and then jump: (well, call)
2151 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002152 // and finally restore the old GP
2153 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2154 IA64Lowering.restoreSP_RP(BB);
2155 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002156
2157 switch (Node->getValueType(0)) {
2158 default: assert(0 && "Unknown value type for call result!");
2159 case MVT::Other: return 1;
2160 case MVT::i1:
2161 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002162 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002163 break;
2164 case MVT::i8:
2165 case MVT::i16:
2166 case MVT::i32:
2167 case MVT::i64:
2168 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2169 break;
2170 case MVT::f64:
2171 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2172 break;
2173 }
2174 return Result+N.ResNo;
2175 }
2176
Misha Brukman4633f1c2005-04-21 23:13:11 +00002177 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002178 return 0;
2179}
2180
2181void ISel::Select(SDOperand N) {
2182 unsigned Tmp1, Tmp2, Opc;
2183 unsigned opcode = N.getOpcode();
2184
Nate Begeman85fdeb22005-03-24 04:39:54 +00002185 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002186 return; // Already selected.
2187
2188 SDNode *Node = N.Val;
2189
2190 switch (Node->getOpcode()) {
2191 default:
2192 Node->dump(); std::cerr << "\n";
2193 assert(0 && "Node not handled yet!");
2194
2195 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002196
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002197 case ISD::TokenFactor: {
2198 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2199 Select(Node->getOperand(i));
2200 return;
2201 }
2202
2203 case ISD::CopyToReg: {
2204 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002205 Tmp1 = SelectExpr(N.getOperand(2));
2206 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002207
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002208 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002209 // if a bool, we use pseudocode
2210 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002211 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2212 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002213 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2214 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002215 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002216 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002217 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002218 }
2219 return;
2220 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002221
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002222 case ISD::RET: {
2223
2224 /* what the heck is going on here:
2225
2226<_sabre_> ret with two operands is obvious: chain and value
2227<camel_> yep
2228<_sabre_> ret with 3 values happens when 'expansion' occurs
2229<_sabre_> e.g. i64 gets split into 2x i32
2230<camel_> oh right
2231<_sabre_> you don't have this case on ia64
2232<camel_> yep
2233<_sabre_> so the two returned values go into EAX/EDX on ia32
2234<camel_> ahhh *memories*
2235<_sabre_> :)
2236<camel_> ok, thanks :)
2237<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2238<_sabre_> this is the first operand always
2239<_sabre_> these operand often define chains, they are the last operand
2240<_sabre_> they are printed as 'ch' if you do DAG.dump()
2241 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002242
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002243 switch (N.getNumOperands()) {
2244 default:
2245 assert(0 && "Unknown return instruction!");
2246 case 2:
2247 Select(N.getOperand(0));
2248 Tmp1 = SelectExpr(N.getOperand(1));
2249 switch (N.getOperand(1).getValueType()) {
2250 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002251 // FIXME: do I need to add support for bools here?
2252 // (return '0' or '1' r8, basically...)
2253 //
2254 // FIXME: need to round floats - 80 bits is bad, the tester
2255 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002256 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002257 // we mark r8 as live on exit up above in LowerArguments()
2258 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2259 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002260 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002261 // we mark F8 as live on exit up above in LowerArguments()
2262 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002263 }
2264 break;
2265 case 1:
2266 Select(N.getOperand(0));
2267 break;
2268 }
2269 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2270 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2271 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2272 return;
2273 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002275 case ISD::BR: {
2276 Select(N.getOperand(0));
2277 MachineBasicBlock *Dest =
2278 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2279 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2280 // XXX HACK! we do _not_ need long branches all the time
2281 return;
2282 }
2283
2284 case ISD::ImplicitDef: {
2285 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002286 BuildMI(BB, IA64::IDEF, 0,
2287 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002288 return;
2289 }
2290
2291 case ISD::BRCOND: {
2292 MachineBasicBlock *Dest =
2293 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2294
2295 Select(N.getOperand(0));
2296 Tmp1 = SelectExpr(N.getOperand(1));
2297 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2298 // XXX HACK! we do _not_ need long branches all the time
2299 return;
2300 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002301
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002302 case ISD::EXTLOAD:
2303 case ISD::ZEXTLOAD:
2304 case ISD::SEXTLOAD:
2305 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002306 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002307 case ISD::CALL:
2308 case ISD::CopyFromReg:
2309 case ISD::DYNAMIC_STACKALLOC:
2310 SelectExpr(N);
2311 return;
2312
2313 case ISD::TRUNCSTORE:
2314 case ISD::STORE: {
2315 Select(N.getOperand(0));
2316 Tmp1 = SelectExpr(N.getOperand(1)); // value
2317
2318 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002319
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002320 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002321 switch (N.getOperand(1).getValueType()) {
2322 default: assert(0 && "Cannot store this type!");
2323 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2324 // FIXME?: for now, we treat bool loads the same as i8 stores */
2325 case MVT::i8: Opc = IA64::ST1; break;
2326 case MVT::i16: Opc = IA64::ST2; break;
2327 case MVT::i32: Opc = IA64::ST4; break;
2328 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002329
Misha Brukman7847fca2005-04-22 17:54:37 +00002330 case MVT::f32: Opc = IA64::STF4; break;
2331 case MVT::f64: Opc = IA64::STF8; break;
2332 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002333 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002334 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002335 default: assert(0 && "unknown type in truncstore");
2336 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2337 //FIXME: DAG does not promote this load?
2338 case MVT::i8: Opc = IA64::ST1; break;
2339 case MVT::i16: Opc = IA64::ST2; break;
2340 case MVT::i32: Opc = IA64::ST4; break;
2341 case MVT::f32: Opc = IA64::STF4; break;
2342 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002343 }
2344
2345 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002346 unsigned dummy = MakeReg(MVT::i64);
2347 unsigned dummy2 = MakeReg(MVT::i64);
2348 BuildMI(BB, IA64::ADD, 2, dummy)
2349 .addGlobalAddress(cast<GlobalAddressSDNode>
2350 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2351 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002352
Misha Brukman7847fca2005-04-22 17:54:37 +00002353 if(!isBool)
2354 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2355 else { // we are storing a bool, so emit a little pseudocode
2356 // to store a predicate register as one byte
2357 assert(Opc==IA64::ST1);
2358 unsigned dummy3 = MakeReg(MVT::i64);
2359 unsigned dummy4 = MakeReg(MVT::i64);
2360 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2361 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2362 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2363 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2364 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002365 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2366
Misha Brukman7847fca2005-04-22 17:54:37 +00002367 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002368
Misha Brukman7847fca2005-04-22 17:54:37 +00002369 unsigned dummy = MakeReg(MVT::i64);
2370 BuildMI(BB, IA64::MOV, 1, dummy)
2371 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2372 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002373 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002374 Tmp2 = SelectExpr(N.getOperand(2)); //address
2375 if(!isBool)
2376 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2377 else { // we are storing a bool, so emit a little pseudocode
2378 // to store a predicate register as one byte
2379 assert(Opc==IA64::ST1);
2380 unsigned dummy3 = MakeReg(MVT::i64);
2381 unsigned dummy4 = MakeReg(MVT::i64);
2382 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2383 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2384 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2385 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2386 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002387 }
2388 return;
2389 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002390
Chris Lattner16cd04d2005-05-12 23:24:06 +00002391 case ISD::CALLSEQ_START:
2392 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002393 Select(N.getOperand(0));
2394 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002395
Chris Lattner16cd04d2005-05-12 23:24:06 +00002396 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2397 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002398 BuildMI(BB, Opc, 1).addImm(Tmp1);
2399 return;
2400 }
2401
2402 return;
2403 }
2404 assert(0 && "GAME OVER. INSERT COIN?");
2405}
2406
2407
2408/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2409/// into a machine code representation using pattern matching and a machine
2410/// description file.
2411///
2412FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002413 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002414}
2415
2416