blob: 04b331cff00009593cd4fda129938bd3864c419e [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);
84
Chris Lattner17234b72005-04-30 04:26:06 +000085 // We don't support sin/cos/sqrt
86 setOperationAction(ISD::FSIN , MVT::f64, Expand);
87 setOperationAction(ISD::FCOS , MVT::f64, Expand);
88 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
89 setOperationAction(ISD::FSIN , MVT::f32, Expand);
90 setOperationAction(ISD::FCOS , MVT::f32, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
92
Andrew Lenharthb5884d32005-05-04 19:25:37 +000093 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000094 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
95 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000096
Duraid Madina9b9d45f2005-03-17 18:17:03 +000097 computeRegisterProperties();
98
99 addLegalFPImmediate(+0.0);
100 addLegalFPImmediate(+1.0);
101 addLegalFPImmediate(-0.0);
102 addLegalFPImmediate(-1.0);
103 }
104
105 /// LowerArguments - This hook must be implemented to indicate how we should
106 /// lower the arguments for the specified function, into the specified DAG.
107 virtual std::vector<SDOperand>
108 LowerArguments(Function &F, SelectionDAG &DAG);
109
110 /// LowerCallTo - This hook lowers an abstract call to a function into an
111 /// actual call.
112 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000113 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000114 bool isTailCall, SDOperand Callee, ArgListTy &Args,
115 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000116
Chris Lattnere0fe2252005-07-05 19:58:54 +0000117 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
118 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000119 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000120 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
121 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000122
123 void restoreGP_SP_RP(MachineBasicBlock* BB)
124 {
125 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
126 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
127 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
128 }
129
Duraid Madinabeeaab22005-03-31 12:31:11 +0000130 void restoreSP_RP(MachineBasicBlock* BB)
131 {
132 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
133 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
134 }
135
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000136 void restoreRP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
139 }
140
141 void restoreGP(MachineBasicBlock* BB)
142 {
143 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
144 }
145
146 };
147}
148
149
150std::vector<SDOperand>
151IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
152 std::vector<SDOperand> ArgValues;
153
154 //
155 // add beautiful description of IA64 stack frame format
156 // here (from intel 24535803.pdf most likely)
157 //
158 MachineFunction &MF = DAG.getMachineFunction();
159 MachineFrameInfo *MFI = MF.getFrameInfo();
160
161 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
162 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
163 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
164
165 MachineBasicBlock& BB = MF.front();
166
Misha Brukman4633f1c2005-04-21 23:13:11 +0000167 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000168 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000169
170 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000173 unsigned argVreg[8];
174 unsigned argPreg[8];
175 unsigned argOpc[8];
176
Duraid Madinabeeaab22005-03-31 12:31:11 +0000177 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000178
Duraid Madinabeeaab22005-03-31 12:31:11 +0000179 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000180 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000181
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000182 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 {
184 SDOperand newroot, argt;
185 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000186
187 switch (getValueType(I->getType())) {
188 default:
189 std::cerr << "ERROR in LowerArgs: unknown type "
190 << getValueType(I->getType()) << "\n";
191 abort();
192 case MVT::f32:
193 // fixme? (well, will need to for weird FP structy stuff,
194 // see intel ABI docs)
195 case MVT::f64:
196//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
197 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
198 // floating point args go into f8..f15 as-needed, the increment
199 argVreg[count] = // is below..:
200 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
201 // FP args go into f8..f15 as needed: (hence the ++)
202 argPreg[count] = args_FP[used_FPArgs++];
203 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000204 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattner0a00bec2005-08-22 21:33:11 +0000205 MVT::f64);
206 if (I->getType() == Type::FloatTy)
207 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukman7847fca2005-04-22 17:54:37 +0000208 break;
209 case MVT::i1: // NOTE: as far as C abi stuff goes,
210 // bools are just boring old ints
211 case MVT::i8:
212 case MVT::i16:
213 case MVT::i32:
214 case MVT::i64:
215//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
216 MF.addLiveIn(args_int[count]); // mark this register as liveIn
217 argVreg[count] =
218 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
219 argPreg[count] = args_int[count];
220 argOpc[count] = IA64::MOV;
221 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000222 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000223 if ( getValueType(I->getType()) != MVT::i64)
224 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
225 newroot);
226 break;
227 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000228 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000229 // Create the frame index object for this incoming parameter...
230 ArgOffset = 16 + 8 * (count - 8);
231 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000232
Misha Brukman7847fca2005-04-22 17:54:37 +0000233 // Create the SelectionDAG nodes corresponding to a load
234 //from this parameter
235 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
236 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000237 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000238 }
239 ++count;
240 DAG.setRoot(newroot.getValue(1));
241 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000242 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000243
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
Duraid Madinabeeaab22005-03-31 12:31:11 +0000245 // Create a vreg to hold the output of (what will become)
246 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000247 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
248 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
249 // we create a PSEUDO_ALLOC (pseudo)instruction for now
250
251 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
252
253 // hmm:
254 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
255 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
256 // ..hmm.
257
258 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
259
260 // hmm:
261 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
262 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
263 // ..hmm.
264
Duraid Madinabeeaab22005-03-31 12:31:11 +0000265 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000266
Duraid Madinabeeaab22005-03-31 12:31:11 +0000267 // if this is a varargs function, we simply lower llvm.va_start by
268 // pointing to the first entry
269 if(F.isVarArg()) {
270 tempOffset=0;
271 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000272 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000273
Duraid Madinabeeaab22005-03-31 12:31:11 +0000274 // here we actually do the moving of args, and store them to the stack
275 // too if this is a varargs function:
276 for (int i = 0; i < count && i < 8; ++i) {
277 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
278 if(F.isVarArg()) {
279 // if this is a varargs function, we copy the input registers to the stack
280 int FI = MFI->CreateFixedObject(8, tempOffset);
281 tempOffset+=8; //XXX: is it safe to use r22 like this?
282 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
283 // FIXME: we should use st8.spill here, one day
284 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
285 }
286 }
287
Duraid Madinaca494fd2005-04-12 14:54:44 +0000288 // Finally, inform the code generator which regs we return values in.
289 // (see the ISD::RET: case down below)
290 switch (getValueType(F.getReturnType())) {
291 default: assert(0 && "i have no idea where to return this type!");
292 case MVT::isVoid: break;
293 case MVT::i1:
294 case MVT::i8:
295 case MVT::i16:
296 case MVT::i32:
297 case MVT::i64:
298 MF.addLiveOut(IA64::r8);
299 break;
300 case MVT::f32:
301 case MVT::f64:
302 MF.addLiveOut(IA64::F8);
303 break;
304 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000305
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000306 return ArgValues;
307}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000308
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000309std::pair<SDOperand, SDOperand>
310IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000311 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000312 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000313 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000314 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000315
316 MachineFunction &MF = DAG.getMachineFunction();
317
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000318 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000319 unsigned outRegsUsed = 0;
320
321 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000322 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000323 outRegsUsed = 8;
324 } else {
325 outRegsUsed = Args.size();
326 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000327
Duraid Madinabeeaab22005-03-31 12:31:11 +0000328 // FIXME? this WILL fail if we ever try to pass around an arg that
329 // consumes more than a single output slot (a 'real' double, int128
330 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
331 // registers we use. Hopefully, the assembler will notice.
332 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
333 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000334
Chris Lattner16cd04d2005-05-12 23:24:06 +0000335 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000336 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000337
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000338 std::vector<SDOperand> args_to_use;
339 for (unsigned i = 0, e = Args.size(); i != e; ++i)
340 {
341 switch (getValueType(Args[i].second)) {
342 default: assert(0 && "unexpected argument type!");
343 case MVT::i1:
344 case MVT::i8:
345 case MVT::i16:
346 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000347 //promote to 64-bits, sign/zero extending based on type
348 //of the argument
349 if(Args[i].second->isSigned())
350 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
351 Args[i].first);
352 else
353 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
354 Args[i].first);
355 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000357 //promote to 64-bits
358 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000359 case MVT::f64:
360 case MVT::i64:
361 break;
362 }
363 args_to_use.push_back(Args[i].first);
364 }
365
366 std::vector<MVT::ValueType> RetVals;
367 MVT::ValueType RetTyVT = getValueType(RetTy);
368 if (RetTyVT != MVT::isVoid)
369 RetVals.push_back(RetTyVT);
370 RetVals.push_back(MVT::Other);
371
372 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000373 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000374 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000375 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000376 DAG.getConstant(NumBytes, getPointerTy()));
377 return std::make_pair(TheCall, Chain);
378}
379
Chris Lattnere0fe2252005-07-05 19:58:54 +0000380SDOperand
381IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
382 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000383 // vastart just stores the address of the VarArgsFrameIndex slot.
384 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000385 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
386 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000387}
388
389std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000390LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
391 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000392
393 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000394 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
395 VAListP, DAG.getSrcValue(VAListV));
396 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
397 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000398 unsigned Amt;
399 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
400 Amt = 8;
401 else {
402 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
403 "Other types should have been promoted for varargs!");
404 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000405 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000406 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000407 DAG.getConstant(Amt, Val.getValueType()));
408 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000409 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000410 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000411}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000412
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000413namespace {
414
415 //===--------------------------------------------------------------------===//
416 /// ISel - IA64 specific code to select IA64 machine instructions for
417 /// SelectionDAG operations.
418 ///
419 class ISel : public SelectionDAGISel {
420 /// IA64Lowering - This object fully describes how to lower LLVM code to an
421 /// IA64-specific SelectionDAG.
422 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000423 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
424 // for sdiv and udiv until it is put into the future
425 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000426
427 /// ExprMap - As shared expressions are codegen'd, we keep track of which
428 /// vreg the value is produced in, so we only emit one copy of each compiled
429 /// tree.
430 std::map<SDOperand, unsigned> ExprMap;
431 std::set<SDOperand> LoweredTokens;
432
433 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000434 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
435 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000436
437 /// InstructionSelectBasicBlock - This callback is invoked by
438 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
439 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
440
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000441 unsigned SelectExpr(SDOperand N);
442 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000443 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
444 SDOperand BuildConstmulSequence(SDOperand N);
445
Chris Lattner47c08892005-08-22 18:28:09 +0000446 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000447 };
448}
449
450/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
451/// when it has created a SelectionDAG for us to codegen.
452void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
453
454 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000455 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000456 Select(DAG.getRoot());
457
458 // Clear state used for selection.
459 ExprMap.clear();
460 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000461 ISelDAG = 0;
462}
463
Duraid Madinab2322562005-04-26 07:23:02 +0000464// strip leading '0' characters from a string
465void munchLeadingZeros(std::string& inString) {
466 while(inString.c_str()[0]=='0') {
467 inString.erase(0, 1);
468 }
469}
470
471// strip trailing '0' characters from a string
472void munchTrailingZeros(std::string& inString) {
473 int curPos=inString.length()-1;
474
475 while(inString.c_str()[curPos]=='0') {
476 inString.erase(curPos, 1);
477 curPos--;
478 }
479}
480
481// return how many consecutive '0' characters are at the end of a string
482unsigned int countTrailingZeros(std::string& inString) {
483 int curPos=inString.length()-1;
484 unsigned int zeroCount=0;
485 // assert goes here
486 while(inString.c_str()[curPos--]=='0') {
487 zeroCount++;
488 }
489 return zeroCount;
490}
491
492// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
493// '0' and 'N' (-1) characters)
494void boothEncode(std::string inString, std::string& boothEncodedString) {
495
496 int curpos=0;
497 int replacements=0;
498 int lim=inString.size();
499
500 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000501 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000502 int runlength=0;
503 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000504
Duraid Madinab2322562005-04-26 07:23:02 +0000505 // find the run length
506 for(;inString[curpos+runlength]=='1';runlength++) ;
507
508 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000509 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000510 replaceString+="1";
511
512 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000513 inString.replace(curpos, runlength+1, replaceString);
514 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000515 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000516 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000517 } else { // a zero, we just keep chugging along
518 curpos++;
519 }
520 }
521
522 // clean up (trim the string, reverse it and turn '1's into 'P's)
523 munchTrailingZeros(inString);
524 boothEncodedString="";
525
526 for(int i=inString.size()-1;i>=0;i--)
527 if(inString[i]=='1')
528 boothEncodedString+="P";
529 else
530 boothEncodedString+=inString[i];
531
532}
533
534struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
535 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000536 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000537 unsigned secondVal; // C
538 unsigned secondShift; // D
539 bool isSub;
540};
541
542/* this implements Lefevre's "pattern-based" constant multiplication,
543 * see "Multiplication by an Integer Constant", INRIA report 1999-06
544 *
545 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
546 * to get better booth encodings - this does help in practice
547 * TODO: weight shifts appropriately (most architectures can't
548 * fuse a shift and an add for arbitrary shift amounts) */
549unsigned lefevre(const std::string inString,
550 std::vector<struct shiftaddblob> &ops) {
551 std::string retstring;
552 std::string s = inString;
553 munchTrailingZeros(s);
554
555 int length=s.length()-1;
556
557 if(length==0) {
558 return(0);
559 }
560
561 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000562
Duraid Madinab2322562005-04-26 07:23:02 +0000563 for(int i=0; i<=length; i++) {
564 if (s.c_str()[length-i]=='P') {
565 p.push_back(i);
566 } else if (s.c_str()[length-i]=='N') {
567 n.push_back(i);
568 }
569 }
570
571 std::string t, u;
Chris Lattner4a209972005-08-25 00:03:21 +0000572 int c = 0;
Duraid Madina4706c032005-04-26 09:42:50 +0000573 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000574 std::map<const int, int> w;
575
Duraid Madina85d5f602005-04-27 11:57:39 +0000576 for(unsigned i=0; i<p.size(); i++) {
577 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000578 w[p[i]-p[j]]++;
579 }
580 }
581
Duraid Madina85d5f602005-04-27 11:57:39 +0000582 for(unsigned i=1; i<n.size(); i++) {
583 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000584 w[n[i]-n[j]]++;
585 }
586 }
587
Duraid Madina85d5f602005-04-27 11:57:39 +0000588 for(unsigned i=0; i<p.size(); i++) {
589 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000590 w[-abs(p[i]-n[j])]++;
591 }
592 }
593
594 std::map<const int, int>::const_iterator ii;
595 std::vector<int> d;
596 std::multimap<int, int> sorted_by_value;
597
598 for(ii = w.begin(); ii!=w.end(); ii++)
599 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
600
601 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
602 it != sorted_by_value.end(); ++it) {
603 d.push_back((*it).second);
604 }
605
606 int int_W=0;
607 int int_d;
608
609 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
610 d.pop_back();
611 retstring=s; // hmmm
612 int x=0;
613 int z=abs(int_d)-1;
614
615 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000616
Duraid Madina85d5f602005-04-27 11:57:39 +0000617 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000618 if( ((base+z+1) < retstring.size()) &&
619 retstring.c_str()[base]=='P' &&
620 retstring.c_str()[base+z+1]=='P')
621 {
622 // match
623 x++;
624 retstring.replace(base, 1, "0");
625 retstring.replace(base+z+1, 1, "p");
626 }
Duraid Madinab2322562005-04-26 07:23:02 +0000627 }
628
Duraid Madina85d5f602005-04-27 11:57:39 +0000629 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000630 if( ((base+z+1) < retstring.size()) &&
631 retstring.c_str()[base]=='N' &&
632 retstring.c_str()[base+z+1]=='N')
633 {
634 // match
635 x++;
636 retstring.replace(base, 1, "0");
637 retstring.replace(base+z+1, 1, "n");
638 }
Duraid Madinab2322562005-04-26 07:23:02 +0000639 }
640
641 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000642 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000643 if( ((base+z+1) < retstring.size()) &&
644 ((retstring.c_str()[base]=='P' &&
645 retstring.c_str()[base+z+1]=='N') ||
646 (retstring.c_str()[base]=='N' &&
647 retstring.c_str()[base+z+1]=='P')) ) {
648 // match
649 x++;
650
651 if(retstring.c_str()[base]=='P') {
652 retstring.replace(base, 1, "0");
653 retstring.replace(base+z+1, 1, "p");
654 } else { // retstring[base]=='N'
655 retstring.replace(base, 1, "0");
656 retstring.replace(base+z+1, 1, "n");
657 }
658 }
Duraid Madinab2322562005-04-26 07:23:02 +0000659 }
660 }
661
662 if(x>int_W) {
663 int_W = x;
664 t = retstring;
665 c = int_d; // tofix
666 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000667
Duraid Madinab2322562005-04-26 07:23:02 +0000668 } d.pop_back(); // hmm
669
670 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000671
Duraid Madina85d5f602005-04-27 11:57:39 +0000672 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000673 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
674 t.replace(i, 1, "0");
675 }
676
Duraid Madina85d5f602005-04-27 11:57:39 +0000677 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000678 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000679 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000680 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000681 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000682 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000683 u.replace(i, 1, "N");
684 }
685
686 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000687 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000688 c=-c;
689 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000690 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000691
Duraid Madina8a3042c2005-05-09 13:18:34 +0000692 int pos=0;
693 while(u[pos]=='0')
694 pos++;
695
696 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000697
698 int g=0;
699 if(hit) {
700 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000701 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000702 bool isP=(u[p]=='P');
703 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000704
705 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000706 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000707 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000708 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000709 }
710 }
711
712 munchLeadingZeros(u);
713
714 int i = lefevre(u, ops);
715
716 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000717
Duraid Madinab2322562005-04-26 07:23:02 +0000718 blob.firstVal=i; blob.firstShift=c;
719 blob.isSub=f;
720 blob.secondVal=i; blob.secondShift=0;
721
722 ops.push_back(blob);
723
724 i = ops.size();
725
726 munchLeadingZeros(t);
727
728 if(t.length()==0)
729 return i;
730
731 if(t.c_str()[0]!='P') {
732 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000733 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000734 bool isP=(t.c_str()[p]=='P');
735 bool isN=(t.c_str()[p]=='N');
736
737 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000738 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000739 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000740 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000741 }
742 }
743
744 int j = lefevre(t, ops);
745
746 int trail=countTrailingZeros(u);
747 blob.secondVal=i; blob.secondShift=trail;
748
749 trail=countTrailingZeros(t);
750 blob.firstVal=j; blob.firstShift=trail;
751
752 switch(g) {
753 case 0:
754 blob.isSub=false; // first + second
755 break;
756 case 1:
757 blob.isSub=true; // first - second
758 break;
759 case 2:
760 blob.isSub=true; // second - first
761 int tmpval, tmpshift;
762 tmpval=blob.firstVal;
763 tmpshift=blob.firstShift;
764 blob.firstVal=blob.secondVal;
765 blob.firstShift=blob.secondShift;
766 blob.secondVal=tmpval;
767 blob.secondShift=tmpshift;
768 break;
769 //assert
770 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000771
Duraid Madinab2322562005-04-26 07:23:02 +0000772 ops.push_back(blob);
773 return ops.size();
774}
775
776SDOperand ISel::BuildConstmulSequence(SDOperand N) {
777 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
778 // in particular, *3 is nicer as *2+1, not *4-1
779 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
780
781 bool flippedSign;
782 unsigned preliminaryShift=0;
783
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000784 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000785
786 // first, we make the constant to multiply by positive
787 if(constant<0) {
788 constant=-constant;
789 flippedSign=true;
790 } else {
791 flippedSign=false;
792 }
793
794 // next, we make it odd.
795 for(; (constant%2==0); preliminaryShift++)
796 constant>>=1;
797
798 //OK, we have a positive, odd number of 64 bits or less. Convert it
799 //to a binary string, constantString[0] is the LSB
800 char constantString[65];
801 for(int i=0; i<64; i++)
802 constantString[i]='0'+((constant>>i)&0x1);
803 constantString[64]=0;
804
805 // now, Booth encode it
806 std::string boothEncodedString;
807 boothEncode(constantString, boothEncodedString);
808
809 std::vector<struct shiftaddblob> ops;
810 // do the transformation, filling out 'ops'
811 lefevre(boothEncodedString, ops);
812
Duraid Madinae75a24a2005-05-15 14:44:13 +0000813 assert(ops.size() < 80 && "constmul code has gone haywire\n");
814 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000815
Duraid Madinab2322562005-04-26 07:23:02 +0000816 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000817 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000818 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
819 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
820 results[ops[i].firstVal-1];
821 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
822 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
823 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
824 results[ops[i].secondVal-1];
825 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
826 if(ops[i].isSub)
827 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
828 else
829 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
830 }
831
832 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000833 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000834 if(preliminaryShift) {
835 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000836 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000837 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000838 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000839 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000840 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000841
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000842 SDOperand finalresult;
843 if(flippedSign) { // if we were multiplying by a negative constant:
844 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
845 // subtract the result from 0 to flip its sign
846 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
847 } else { // there was no preliminary multiply by -1 required
848 finalresult = shiftedresult;
849 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000850
851 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000852}
853
Duraid Madina4826a072005-04-06 09:55:17 +0000854/// ponderIntegerDivisionBy - When handling integer divides, if the divide
855/// is by a constant such that we can efficiently codegen it, this
856/// function says what to do. Currently, it returns 0 if the division must
857/// become a genuine divide, and 1 if the division can be turned into a
858/// right shift.
859static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
860 unsigned& Imm) {
861 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
862 // a constant, give up.
863
864 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
865
Chris Lattner0561b3f2005-08-02 19:26:06 +0000866 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
867 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000868 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000869 }
870
Duraid Madina4826a072005-04-06 09:55:17 +0000871 return 0; // fallthrough
872}
873
Duraid Madinac02780e2005-04-13 04:50:54 +0000874static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
875 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
876 // a constant, give up.
877
878 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
879
Chris Lattner0561b3f2005-08-02 19:26:06 +0000880 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000881 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000882 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000883 }
884
Duraid Madinac02780e2005-04-13 04:50:54 +0000885 return 0; // fallthrough
886}
887
Duraid Madinaf55e4032005-04-07 12:33:38 +0000888static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
889 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
890 // constant, give up.
891 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
892
893 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
894 Imm = v & 0x3FFF; // 14 bits
895 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000896 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000897 return 0; // fallthrough
898}
899
900static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
901 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
902 // constant, give up.
903 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
904
905 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
906 Imm = v & 0xFF; // 8 bits
907 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000908 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000909 return 0; // fallthrough
910}
911
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000912unsigned ISel::SelectExpr(SDOperand N) {
913 unsigned Result;
914 unsigned Tmp1, Tmp2, Tmp3;
915 unsigned Opc = 0;
916 MVT::ValueType DestType = N.getValueType();
917
918 unsigned opcode = N.getOpcode();
919
920 SDNode *Node = N.Val;
921 SDOperand Op0, Op1;
922
923 if (Node->getOpcode() == ISD::CopyFromReg)
924 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000925 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000926
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000927 unsigned &Reg = ExprMap[N];
928 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000929
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000930 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000931 Reg = Result = (N.getValueType() != MVT::Other) ?
932 MakeReg(N.getValueType()) : 1;
933 else {
934 // If this is a call instruction, make sure to prepare ALL of the result
935 // values as well as the chain.
936 if (Node->getNumValues() == 1)
937 Reg = Result = 1; // Void call, just a chain.
938 else {
939 Result = MakeReg(Node->getValueType(0));
940 ExprMap[N.getValue(0)] = Result;
941 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
942 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
943 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
944 }
945 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000947 switch (N.getOpcode()) {
948 default:
949 Node->dump();
950 assert(0 && "Node not handled!\n");
951
952 case ISD::FrameIndex: {
953 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
954 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
955 return Result;
956 }
957
958 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000959 Tmp1 = BB->getParent()->getConstantPool()->
960 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000961 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
962 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
963 .addReg(IA64::r1);
964 return Result;
965 }
966
967 case ISD::ConstantFP: {
968 Tmp1 = Result; // Intermediate Register
969 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
970 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
971 Tmp1 = MakeReg(MVT::f64);
972
973 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
974 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
975 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
976 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
977 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
978 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
979 else
980 assert(0 && "Unexpected FP constant!");
981 if (Tmp1 != Result)
982 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
983 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000984 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000985 return Result;
986 }
987
988 case ISD::DYNAMIC_STACKALLOC: {
989 // Generate both result values.
990 if (Result != 1)
991 ExprMap[N.getValue(1)] = 1; // Generate the token
992 else
993 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
994
995 // FIXME: We are currently ignoring the requested alignment for handling
996 // greater than the stack alignment. This will need to be revisited at some
997 // point. Align = N.getOperand(2);
998
999 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1000 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1001 std::cerr << "Cannot allocate stack object with greater alignment than"
1002 << " the stack alignment yet!";
1003 abort();
1004 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001005
1006/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001007 Select(N.getOperand(0));
1008 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1009 {
1010 if (CN->getValue() < 32000)
1011 {
1012 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001013 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001014 } else {
1015 Tmp1 = SelectExpr(N.getOperand(1));
1016 // Subtract size from stack pointer, thereby allocating some space.
1017 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1018 }
1019 } else {
1020 Tmp1 = SelectExpr(N.getOperand(1));
1021 // Subtract size from stack pointer, thereby allocating some space.
1022 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1023 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001024*/
1025 Select(N.getOperand(0));
1026 Tmp1 = SelectExpr(N.getOperand(1));
1027 // Subtract size from stack pointer, thereby allocating some space.
1028 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001029 // Put a pointer to the space into the result register, by copying the
1030 // stack pointer.
1031 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1032 return Result;
1033 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001034
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001035 case ISD::SELECT: {
1036 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1037 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1038 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1039
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001040 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001041
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001042 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001043 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001044 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1045 // for i1, we load the condition into an integer register, then
1046 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1047 // of them will go through, since the integer register will hold
1048 // either 0 or 1)
1049 case MVT::i1: {
1050 bogoResult=MakeReg(MVT::i1);
1051
1052 // load the condition into an integer register
1053 unsigned condReg=MakeReg(MVT::i64);
1054 unsigned dummy=MakeReg(MVT::i64);
1055 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1056 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1057 .addImm(1).addReg(Tmp1);
1058
1059 // initialize Result (bool) to false (hence UNC) and if
1060 // the select condition (condReg) is false (0), copy Tmp3
1061 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1062 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1063
1064 // now, if the selection condition is true, write 1 to the
1065 // result if Tmp2 is 1
1066 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1067 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1068 break;
1069 }
1070 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1071 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001072 case MVT::i64:
1073 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001074 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1075 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1076 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001077 break;
1078 case MVT::f64:
1079 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001080 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1081 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1082 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001083 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001084 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001085
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001086 return Result;
1087 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001088
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001089 case ISD::Constant: {
1090 unsigned depositPos=0;
1091 unsigned depositLen=0;
1092 switch (N.getValueType()) {
1093 default: assert(0 && "Cannot use constants of this type!");
1094 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001095 // the constant:
1096 if(cast<ConstantSDNode>(N)->getValue()) // true:
1097 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1098 else // false:
1099 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1100 return Result; // early exit
1101 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001102 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001103 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001104
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001105 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001106
1107 if(immediate==0) { // if the constant is just zero,
1108 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1109 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001110 }
1111
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001112 if (immediate <= 8191 && immediate >= -8192) {
1113 // if this constants fits in 14 bits, we use a mov the assembler will
1114 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1115 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1116 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001117 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001118
1119 if (immediate <= 2097151 && immediate >= -2097152) {
1120 // if this constants fits in 22 bits, we use a mov the assembler will
1121 // turn into: "addl rDest=imm,r0"
1122 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1123 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001124 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001125
1126 /* otherwise, our immediate is big, so we use movl */
1127 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001128 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001129 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001130 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001131
1132 case ISD::UNDEF: {
1133 BuildMI(BB, IA64::IDEF, 0, Result);
1134 return Result;
1135 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001136
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001137 case ISD::GlobalAddress: {
1138 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1139 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001140
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001141 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001142 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001143
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001144 return Result;
1145 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 case ISD::ExternalSymbol: {
1148 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001149// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1150 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001151 return Result;
1152 }
1153
1154 case ISD::FP_EXTEND: {
1155 Tmp1 = SelectExpr(N.getOperand(0));
1156 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1157 return Result;
1158 }
1159
Chris Lattner26e04bb2005-09-02 00:15:30 +00001160 case ISD::ANY_EXTEND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001161 case ISD::ZERO_EXTEND: {
1162 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001163
Chris Lattner26e04bb2005-09-02 00:15:30 +00001164 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1165 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001166
Chris Lattner26e04bb2005-09-02 00:15:30 +00001167 // if the predicate reg has 1, we want a '1' in our GR.
1168 unsigned dummy = MakeReg(MVT::i64);
1169 // first load zero:
1170 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1171 // ...then conditionally (PR:Tmp1) add 1:
1172 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1173 .addImm(1).addReg(Tmp1);
1174 return Result; // XXX early exit!
1175 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001176
Chris Lattner26e04bb2005-09-02 00:15:30 +00001177 case ISD::SIGN_EXTEND:
1178 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1179 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001180
1181 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner26e04bb2005-09-02 00:15:30 +00001182 assert(0 && "don't know how to sign_extend from bool yet!");
1183 abort();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001184
Chris Lattner26e04bb2005-09-02 00:15:30 +00001185 case ISD::TRUNCATE:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001186 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1187 // of R0 appropriately.
Chris Lattner26e04bb2005-09-02 00:15:30 +00001188 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1189 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001190 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001191
Chris Lattner26e04bb2005-09-02 00:15:30 +00001192 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1193 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1194 return Result; // XXX early exit!
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001195
Misha Brukman7847fca2005-04-22 17:54:37 +00001196/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001197 case ISD::FP_ROUND: {
1198 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001199 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001200 Tmp1 = SelectExpr(N.getOperand(0));
1201 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1202 // we add 0.0 using a single precision add to do rounding
1203 return Result;
1204 }
1205*/
1206
1207// FIXME: the following 4 cases need cleaning
1208 case ISD::SINT_TO_FP: {
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 Tmp2 = MakeReg(MVT::f64);
1211 unsigned dummy = MakeReg(MVT::f64);
1212 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1213 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1214 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1215 return Result;
1216 }
1217
1218 case ISD::UINT_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::FCVTXUF, 1, dummy).addReg(Tmp2);
1224 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1225 return Result;
1226 }
1227
1228 case ISD::FP_TO_SINT: {
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 Tmp2 = MakeReg(MVT::f64);
1231 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1232 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1233 return Result;
1234 }
1235
1236 case ISD::FP_TO_UINT: {
1237 Tmp1 = SelectExpr(N.getOperand(0));
1238 Tmp2 = MakeReg(MVT::f64);
1239 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1240 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1241 return Result;
1242 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001243
1244 case ISD::FADD: {
1245 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1246 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1247 // into an fma, do so:
1248 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001249 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1251 Tmp3 = SelectExpr(N.getOperand(1));
1252 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1253 return Result; // early exit
1254 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001255
1256 //else, fallthrough:
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = SelectExpr(N.getOperand(1));
1259 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1260 return Result;
1261 }
Duraid Madinaed095022005-04-13 06:12:04 +00001262
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001263 case ISD::ADD: {
1264 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001265 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001266 // this add into a shladd, try:
1267 ConstantSDNode *CSD = NULL;
1268 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001269 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001270
Misha Brukman7847fca2005-04-22 17:54:37 +00001271 // ++FusedSHLADD; // Statistic
1272 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1273 int shl_amt = CSD->getValue();
1274 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001275
Misha Brukman7847fca2005-04-22 17:54:37 +00001276 BuildMI(BB, IA64::SHLADD, 3, Result)
1277 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1278 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001279 }
1280 }
1281
1282 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001283 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001284 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1285 case 1: // adding a constant that's 14 bits
1286 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1287 return Result; // early exit
1288 } // fallthrough and emit a reg+reg ADD:
1289 Tmp2 = SelectExpr(N.getOperand(1));
1290 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001291 return Result;
1292 }
1293
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001294 case ISD::FMUL:
1295 Tmp1 = SelectExpr(N.getOperand(0));
1296 Tmp2 = SelectExpr(N.getOperand(1));
1297 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1298 return Result;
1299
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001300 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001301
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001302 // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001303/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1304 */
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001305 // boring old integer multiply with xma
Duraid Madinab2322562005-04-26 07:23:02 +00001306 Tmp1 = SelectExpr(N.getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001308
1309 unsigned TempFR1=MakeReg(MVT::f64);
1310 unsigned TempFR2=MakeReg(MVT::f64);
1311 unsigned TempFR3=MakeReg(MVT::f64);
1312 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1313 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1314 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1315 .addReg(IA64::F0);
1316 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1317 return Result; // early exit
1318 /* FIXME } else { // we are multiplying by an integer constant! yay
1319 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1320 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001321 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001322
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001323 case ISD::FSUB:
1324 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madina4826a072005-04-06 09:55:17 +00001325 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1326 // into an fms, do so:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001327 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001328 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1329 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1330 Tmp3 = SelectExpr(N.getOperand(1));
1331 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1332 return Result; // early exit
1333 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001334
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001335 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001336 Tmp1 = SelectExpr(N.getOperand(0));
1337 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1338 return Result;
1339
1340 case ISD::SUB: {
1341 Tmp2 = SelectExpr(N.getOperand(1));
1342 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1343 case 1: // subtracting *from* an 8 bit constant:
1344 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1345 return Result; // early exit
1346 } // fallthrough and emit a reg+reg SUB:
1347 Tmp1 = SelectExpr(N.getOperand(0));
1348 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001349 return Result;
1350 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001351
1352 case ISD::FABS: {
1353 Tmp1 = SelectExpr(N.getOperand(0));
1354 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1355 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1356 return Result;
1357 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001359 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001360 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001361
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001363 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1364 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1365 } else {
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1368 }
1369
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001370 return Result;
1371 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001372
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001373 case ISD::AND: {
1374 switch (N.getValueType()) {
1375 default: assert(0 && "Cannot AND this type!");
1376 case MVT::i1: { // if a bool, we emit a pseudocode AND
1377 unsigned pA = SelectExpr(N.getOperand(0));
1378 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001379
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001380/* our pseudocode for AND is:
1381 *
1382(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1383 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1384 ;;
1385(pB) cmp.ne pTemp,p0 = r0,r0
1386 ;;
1387(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1388
1389*/
1390 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001391
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001392 unsigned bogusTemp1 = MakeReg(MVT::i1);
1393 unsigned bogusTemp2 = MakeReg(MVT::i1);
1394 unsigned bogusTemp3 = MakeReg(MVT::i1);
1395 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001397 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001398 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001399 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001400 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001401 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001402 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001403 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001404 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001405 break;
1406 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001407
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001408 // if not a bool, we just AND away:
1409 case MVT::i8:
1410 case MVT::i16:
1411 case MVT::i32:
1412 case MVT::i64: {
1413 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001414 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001415 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001416 switch (Tmp3) {
1417 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1418 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1419 break;
1420 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1421 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1422 break;
1423 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1424 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1425 break;
1426 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001427 // FIXME: assert the dep.z is in bounds
1428 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001429 .addImm(0).addImm(Tmp3);
1430 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001431 }
1432 return Result; // early exit
1433 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001434 Tmp2 = SelectExpr(N.getOperand(1));
1435 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001436 }
1437 }
1438 return Result;
1439 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001440
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001441 case ISD::OR: {
1442 switch (N.getValueType()) {
1443 default: assert(0 && "Cannot OR this type!");
1444 case MVT::i1: { // if a bool, we emit a pseudocode OR
1445 unsigned pA = SelectExpr(N.getOperand(0));
1446 unsigned pB = SelectExpr(N.getOperand(1));
1447
1448 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001450/* our pseudocode for OR is:
1451 *
1452
1453pC = pA OR pB
1454-------------
1455
Misha Brukman7847fca2005-04-22 17:54:37 +00001456(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1457 ;;
1458(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001459
1460*/
1461 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001462 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001463 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001464 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001465 break;
1466 }
1467 // if not a bool, we just OR away:
1468 case MVT::i8:
1469 case MVT::i16:
1470 case MVT::i32:
1471 case MVT::i64: {
1472 Tmp1 = SelectExpr(N.getOperand(0));
1473 Tmp2 = SelectExpr(N.getOperand(1));
1474 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1475 break;
1476 }
1477 }
1478 return Result;
1479 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001480
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001481 case ISD::XOR: {
1482 switch (N.getValueType()) {
1483 default: assert(0 && "Cannot XOR this type!");
1484 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1485 unsigned pY = SelectExpr(N.getOperand(0));
1486 unsigned pZ = SelectExpr(N.getOperand(1));
1487
1488/* one possible routine for XOR is:
1489
1490 // Compute px = py ^ pz
1491 // using sum of products: px = (py & !pz) | (pz & !py)
1492 // Uses 5 instructions in 3 cycles.
1493 // cycle 1
1494(pz) cmp.eq.unc px = r0, r0 // px = pz
1495(py) cmp.eq.unc pt = r0, r0 // pt = py
1496 ;;
1497 // cycle 2
1498(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1499(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1500 ;;
1501 } { .mmi
1502 // cycle 3
1503(pt) cmp.eq.or px = r0, r0 // px = px | pt
1504
1505*** Another, which we use here, requires one scratch GR. it is:
1506
1507 mov rt = 0 // initialize rt off critical path
1508 ;;
1509
1510 // cycle 1
1511(pz) cmp.eq.unc px = r0, r0 // px = pz
1512(pz) mov rt = 1 // rt = pz
1513 ;;
1514 // cycle 2
1515(py) cmp.ne px = 1, rt // if (py) px = !pz
1516
1517.. these routines kindly provided by Jim Hull
1518*/
1519 unsigned rt = MakeReg(MVT::i64);
1520
1521 // these two temporaries will never actually appear,
1522 // due to the two-address form of some of the instructions below
1523 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1524 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1525
1526 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1527 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001528 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001529 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001530 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001531 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001532 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001533 break;
1534 }
1535 // if not a bool, we just XOR away:
1536 case MVT::i8:
1537 case MVT::i16:
1538 case MVT::i32:
1539 case MVT::i64: {
1540 Tmp1 = SelectExpr(N.getOperand(0));
1541 Tmp2 = SelectExpr(N.getOperand(1));
1542 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1543 break;
1544 }
1545 }
1546 return Result;
1547 }
1548
Duraid Madina63bbed52005-05-11 05:16:09 +00001549 case ISD::CTPOP: {
1550 Tmp1 = SelectExpr(N.getOperand(0));
1551 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1552 return Result;
1553 }
1554
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001555 case ISD::SHL: {
1556 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1558 Tmp2 = CN->getValue();
1559 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1560 } else {
1561 Tmp2 = SelectExpr(N.getOperand(1));
1562 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1563 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001564 return Result;
1565 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001566
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001567 case ISD::SRL: {
1568 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001569 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1570 Tmp2 = CN->getValue();
1571 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1572 } else {
1573 Tmp2 = SelectExpr(N.getOperand(1));
1574 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1575 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001576 return Result;
1577 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001578
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001579 case ISD::SRA: {
1580 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001581 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1582 Tmp2 = CN->getValue();
1583 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1584 } else {
1585 Tmp2 = SelectExpr(N.getOperand(1));
1586 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1587 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001588 return Result;
1589 }
1590
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001591 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001592 case ISD::SDIV:
1593 case ISD::UDIV:
1594 case ISD::SREM:
1595 case ISD::UREM: {
1596
1597 Tmp1 = SelectExpr(N.getOperand(0));
1598 Tmp2 = SelectExpr(N.getOperand(1));
1599
1600 bool isFP=false;
1601
1602 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1603 isFP=true;
1604
1605 bool isModulus=false; // is it a division or a modulus?
1606 bool isSigned=false;
1607
1608 switch(N.getOpcode()) {
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001609 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001610 case ISD::SDIV: isModulus=false; isSigned=true; break;
1611 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001612 case ISD::FREM:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001613 case ISD::SREM: isModulus=true; isSigned=true; break;
1614 case ISD::UREM: isModulus=true; isSigned=false; break;
1615 }
1616
Duraid Madina4826a072005-04-06 09:55:17 +00001617 if(!isModulus && !isFP) { // if this is an integer divide,
1618 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001619 case 1: // division by a constant that's a power of 2
1620 Tmp1 = SelectExpr(N.getOperand(0));
1621 if(isSigned) { // argument could be negative, so emit some code:
1622 unsigned divAmt=Tmp3;
1623 unsigned tempGR1=MakeReg(MVT::i64);
1624 unsigned tempGR2=MakeReg(MVT::i64);
1625 unsigned tempGR3=MakeReg(MVT::i64);
1626 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1627 .addReg(Tmp1).addImm(divAmt-1);
1628 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1629 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1630 BuildMI(BB, IA64::ADD, 2, tempGR3)
1631 .addReg(Tmp1).addReg(tempGR2);
1632 BuildMI(BB, IA64::SHRS, 2, Result)
1633 .addReg(tempGR3).addImm(divAmt);
1634 }
1635 else // unsigned div-by-power-of-2 becomes a simple shift right:
1636 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1637 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001638 }
1639 }
1640
Misha Brukman4633f1c2005-04-21 23:13:11 +00001641 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001642 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001643 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1644 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1645 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1646 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1647 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1648 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1649 unsigned TmpF7=MakeReg(MVT::f64);
1650 unsigned TmpF8=MakeReg(MVT::f64);
1651 unsigned TmpF9=MakeReg(MVT::f64);
1652 unsigned TmpF10=MakeReg(MVT::f64);
1653 unsigned TmpF11=MakeReg(MVT::f64);
1654 unsigned TmpF12=MakeReg(MVT::f64);
1655 unsigned TmpF13=MakeReg(MVT::f64);
1656 unsigned TmpF14=MakeReg(MVT::f64);
1657 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001658
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001659 // OK, emit some code:
1660
1661 if(!isFP) {
1662 // first, load the inputs into FP regs.
1663 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1664 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001665
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001666 // next, convert the inputs to FP
1667 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001668 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1669 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001670 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001671 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1672 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001673 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001674
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001675 } else { // this is an FP divide/remainder, so we 'leak' some temp
1676 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1677 TmpF3=Tmp1;
1678 TmpF4=Tmp2;
1679 }
1680
1681 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001682 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1683 BuildMI(BB, IA64::FRCPAS1, 4)
1684 .addReg(TmpF5, MachineOperand::Def)
1685 .addReg(TmpPR, MachineOperand::Def)
1686 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001687
Duraid Madinabeeaab22005-03-31 12:31:11 +00001688 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1689 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1690 // TPCMPNE below
1691 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1692 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001693 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001694 }
1695
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001696 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1697 // precision, don't need this much for f32/i32)
1698 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1699 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1700 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1701 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1702 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1703 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1704 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1705 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1706 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1707 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1708 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1709 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1710 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1711 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1712 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1713 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001714
1715 // FIXME: this is unfortunate :(
1716 // the story is that the dest reg of the fnma above and the fma below
1717 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1718 // be the same register, or this code breaks if the first argument is
1719 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001720 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1721 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1722
Duraid Madina6e02e682005-04-04 05:05:52 +00001723 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1724 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1725 }
1726
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001727 if(!isFP) {
1728 // round to an integer
1729 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001730 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001731 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001732 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001733 } else {
1734 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1735 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1736 // we really do need the above FMOV? ;)
1737 }
1738
1739 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001740 if(isFP) { // extra worrying about div-by-zero
1741 unsigned bogoResult=MakeReg(MVT::f64);
1742
1743 // we do a 'conditional fmov' (of the correct result, depending
1744 // on how the frcpa predicate turned out)
1745 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001746 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001747 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001748 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001749 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001750 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001751 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001752 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001753 } else { // this is a modulus
1754 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001755 // answer = q * (-b) + a
1756 unsigned ModulusResult = MakeReg(MVT::f64);
1757 unsigned TmpF = MakeReg(MVT::f64);
1758 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001759
Misha Brukman7847fca2005-04-22 17:54:37 +00001760 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1761 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1762 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1763 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1764 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001765 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001767 }
1768 }
1769
1770 return Result;
1771 }
1772
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001773 case ISD::SIGN_EXTEND_INREG: {
1774 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001775 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001776 default:
1777 Node->dump();
1778 assert(0 && "don't know how to sign extend this type");
1779 break;
1780 case MVT::i8: Opc = IA64::SXT1; break;
1781 case MVT::i16: Opc = IA64::SXT2; break;
1782 case MVT::i32: Opc = IA64::SXT4; break;
1783 }
1784 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1785 return Result;
1786 }
1787
1788 case ISD::SETCC: {
1789 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001790 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1791 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001792
Chris Lattner88ac32c2005-08-09 20:21:10 +00001793 if(ConstantSDNode *CSDN =
1794 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1795 // if we are comparing against a constant zero
1796 if(CSDN->getValue()==0)
1797 Tmp2 = IA64::r0; // then we can just compare against r0
1798 else
1799 Tmp2 = SelectExpr(N.getOperand(1));
1800 } else // not comparing against a constant
1801 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001802
Chris Lattner88ac32c2005-08-09 20:21:10 +00001803 switch (CC) {
1804 default: assert(0 && "Unknown integer comparison!");
1805 case ISD::SETEQ:
1806 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1807 break;
1808 case ISD::SETGT:
1809 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1810 break;
1811 case ISD::SETGE:
1812 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1813 break;
1814 case ISD::SETLT:
1815 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1816 break;
1817 case ISD::SETLE:
1818 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 break;
1820 case ISD::SETNE:
1821 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1822 break;
1823 case ISD::SETULT:
1824 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1825 break;
1826 case ISD::SETUGT:
1827 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 break;
1829 case ISD::SETULE:
1830 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1831 break;
1832 case ISD::SETUGE:
1833 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1834 break;
1835 }
1836 } else { // if not integer, should be FP.
1837 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1838 "error: SETCC should have had incoming f32 promoted to f64!\n");
1839
1840 if(ConstantFPSDNode *CFPSDN =
1841 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1842
1843 // if we are comparing against a constant +0.0 or +1.0
1844 if(CFPSDN->isExactlyValue(+0.0))
1845 Tmp2 = IA64::F0; // then we can just compare against f0
1846 else if(CFPSDN->isExactlyValue(+1.0))
1847 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001848 else
1849 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001850 } else // not comparing against a constant
1851 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001852
Chris Lattner88ac32c2005-08-09 20:21:10 +00001853 switch (CC) {
1854 default: assert(0 && "Unknown FP comparison!");
1855 case ISD::SETEQ:
1856 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 break;
1858 case ISD::SETGT:
1859 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1860 break;
1861 case ISD::SETGE:
1862 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 break;
1864 case ISD::SETLT:
1865 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETLE:
1868 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETNE:
1871 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETULT:
1874 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETUGT:
1877 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETULE:
1880 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETUGE:
1883 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001885 }
1886 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001887 return Result;
1888 }
1889
1890 case ISD::EXTLOAD:
1891 case ISD::ZEXTLOAD:
1892 case ISD::LOAD: {
1893 // Make sure we generate both values.
1894 if (Result != 1)
1895 ExprMap[N.getValue(1)] = 1; // Generate the token
1896 else
1897 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1898
1899 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001900
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001901 if(opcode == ISD::LOAD) { // this is a LOAD
1902 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001903 default: assert(0 && "Cannot load this type!");
1904 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1905 // FIXME: for now, we treat bool loads the same as i8 loads */
1906 case MVT::i8: Opc = IA64::LD1; break;
1907 case MVT::i16: Opc = IA64::LD2; break;
1908 case MVT::i32: Opc = IA64::LD4; break;
1909 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001910
Misha Brukman7847fca2005-04-22 17:54:37 +00001911 case MVT::f32: Opc = IA64::LDF4; break;
1912 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001913 }
1914 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001915 MVT::ValueType TypeBeingLoaded =
1916 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001917 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001918 default: assert(0 && "Cannot extload/zextload this type!");
1919 // FIXME: bools?
1920 case MVT::i8: Opc = IA64::LD1; break;
1921 case MVT::i16: Opc = IA64::LD2; break;
1922 case MVT::i32: Opc = IA64::LD4; break;
1923 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001924 }
1925 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001926
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001927 SDOperand Chain = N.getOperand(0);
1928 SDOperand Address = N.getOperand(1);
1929
1930 if(Address.getOpcode() == ISD::GlobalAddress) {
1931 Select(Chain);
1932 unsigned dummy = MakeReg(MVT::i64);
1933 unsigned dummy2 = MakeReg(MVT::i64);
1934 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001935 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1936 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001937 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1938 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001939 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001940 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001941 // into a predicate register
1942 assert(Opc==IA64::LD1 && "problem loading a bool");
1943 unsigned dummy3 = MakeReg(MVT::i64);
1944 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1945 // we compare to 0. true? 0. false? 1.
1946 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001947 }
1948 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001949 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattner143b6752005-08-26 17:18:44 +00001950 getConstantPoolIndex(CP->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001951 Select(Chain);
1952 IA64Lowering.restoreGP(BB);
1953 unsigned dummy = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +00001954 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
Misha Brukman7847fca2005-04-22 17:54:37 +00001955 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001956 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001957 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001958 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001959 // into a predicate register
1960 assert(Opc==IA64::LD1 && "problem loading a bool");
1961 unsigned dummy3 = MakeReg(MVT::i64);
1962 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1963 // we compare to 0. true? 0. false? 1.
1964 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001965 }
1966 } else if(Address.getOpcode() == ISD::FrameIndex) {
1967 Select(Chain); // FIXME ? what about bools?
1968 unsigned dummy = MakeReg(MVT::i64);
1969 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001970 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
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 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001981 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001982 Select(Chain);
1983 Tmp2 = SelectExpr(Address);
1984 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001985 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001986 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001987 // into a predicate register
1988 assert(Opc==IA64::LD1 && "problem loading a bool");
1989 unsigned dummy = MakeReg(MVT::i64);
1990 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1991 // we compare to 0. true? 0. false? 1.
1992 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00001993 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001994 }
1995
1996 return Result;
1997 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001998
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001999 case ISD::CopyFromReg: {
2000 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002001 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002002 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002003
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002004 SDOperand Chain = N.getOperand(0);
2005
2006 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002007 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002008
2009 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002010 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2011 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002012 // (r) Result =cmp.eq.unc(r0,r0)
2013 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002014 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 return Result;
2016 }
2017
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002018 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002019 case ISD::CALL: {
2020 Select(N.getOperand(0));
2021
2022 // The chain for this call is now lowered.
2023 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002024
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002025 //grab the arguments
2026 std::vector<unsigned> argvregs;
2027
2028 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002029 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002030
2031 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002032 // Runtime Architecture Guide to see some examples of what's going
2033 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2034 // while FP args get mapped to F8->F15 as needed)
2035
2036 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002037
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002038 // in reg args
2039 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2040 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002041 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2042 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2043 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2044 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002045
Misha Brukman7847fca2005-04-22 17:54:37 +00002046 switch(N.getOperand(i+2).getValueType())
2047 {
2048 default: // XXX do we need to support MVT::i1 here?
2049 Node->dump();
2050 N.getOperand(i).Val->dump();
2051 std::cerr << "Type for " << i << " is: " <<
2052 N.getOperand(i+2).getValueType() << std::endl;
2053 assert(0 && "Unknown value type for call");
2054 case MVT::i64:
2055 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2056 break;
2057 case MVT::f64:
2058 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2059 .addReg(argvregs[i]);
2060 // FIXME: we don't need to do this _all_ the time:
2061 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2062 break;
2063 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002064 }
2065
2066 //in mem args
2067 for (int i = 8, e = argvregs.size(); i < e; ++i)
2068 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002069 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002070
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002071 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002072 default:
2073 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002074 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002075 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002076 N.getOperand(i+2).getValueType() << "\n";
2077 assert(0 && "Unknown value type for call");
2078 case MVT::i1: // FIXME?
2079 case MVT::i8:
2080 case MVT::i16:
2081 case MVT::i32:
2082 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002083 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2084 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2085 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002086 break;
2087 case MVT::f32:
2088 case MVT::f64:
2089 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002090 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2091 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002092 break;
2093 }
2094 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002095
Duraid Madina04aa46d2005-05-20 11:39:17 +00002096 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002097 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002098 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002099 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002100 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2101 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002102 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002103 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002104 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002105 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002106 // "non-pic code with imm relocation against dynamic symbol" errors
2107 BuildMI(BB, IA64::BRCALL, 1)
2108 .addExternalSymbol(ESSDN->getSymbol(), true);
2109 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002110 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002111 else { // otherwise we need to get the function descriptor
2112 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002113 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002114 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002115
2116 unsigned targetEntryPoint=MakeReg(MVT::i64);
2117 unsigned targetGPAddr=MakeReg(MVT::i64);
2118 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002119
Duraid Madinabeeaab22005-03-31 12:31:11 +00002120 // b6 is a scratch branch register, we load the target entry point
2121 // from the base of the function descriptor
2122 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2123 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2124
2125 // save the current GP:
2126 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002127
Duraid Madinabeeaab22005-03-31 12:31:11 +00002128 /* TODO: we need to make sure doing this never, ever loads a
2129 * bogus value into r1 (GP). */
2130 // load the target GP (which is at mem[functiondescriptor+8])
2131 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002132 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002133 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2134
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002135 // and then jump: (well, call)
2136 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002137 // and finally restore the old GP
2138 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2139 IA64Lowering.restoreSP_RP(BB);
2140 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002141
2142 switch (Node->getValueType(0)) {
2143 default: assert(0 && "Unknown value type for call result!");
2144 case MVT::Other: return 1;
2145 case MVT::i1:
2146 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002147 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002148 break;
2149 case MVT::i8:
2150 case MVT::i16:
2151 case MVT::i32:
2152 case MVT::i64:
2153 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2154 break;
2155 case MVT::f64:
2156 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2157 break;
2158 }
2159 return Result+N.ResNo;
2160 }
2161
Misha Brukman4633f1c2005-04-21 23:13:11 +00002162 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002163 return 0;
2164}
2165
2166void ISel::Select(SDOperand N) {
2167 unsigned Tmp1, Tmp2, Opc;
2168 unsigned opcode = N.getOpcode();
2169
Nate Begeman85fdeb22005-03-24 04:39:54 +00002170 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002171 return; // Already selected.
2172
2173 SDNode *Node = N.Val;
2174
2175 switch (Node->getOpcode()) {
2176 default:
2177 Node->dump(); std::cerr << "\n";
2178 assert(0 && "Node not handled yet!");
2179
2180 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002181
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002182 case ISD::TokenFactor: {
2183 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2184 Select(Node->getOperand(i));
2185 return;
2186 }
2187
2188 case ISD::CopyToReg: {
2189 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002190 Tmp1 = SelectExpr(N.getOperand(2));
2191 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002193 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002194 // if a bool, we use pseudocode
2195 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002196 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2197 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002198 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2199 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002200 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002201 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002202 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002203 }
2204 return;
2205 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002206
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002207 case ISD::RET: {
2208
2209 /* what the heck is going on here:
2210
2211<_sabre_> ret with two operands is obvious: chain and value
2212<camel_> yep
2213<_sabre_> ret with 3 values happens when 'expansion' occurs
2214<_sabre_> e.g. i64 gets split into 2x i32
2215<camel_> oh right
2216<_sabre_> you don't have this case on ia64
2217<camel_> yep
2218<_sabre_> so the two returned values go into EAX/EDX on ia32
2219<camel_> ahhh *memories*
2220<_sabre_> :)
2221<camel_> ok, thanks :)
2222<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2223<_sabre_> this is the first operand always
2224<_sabre_> these operand often define chains, they are the last operand
2225<_sabre_> they are printed as 'ch' if you do DAG.dump()
2226 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002227
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002228 switch (N.getNumOperands()) {
2229 default:
2230 assert(0 && "Unknown return instruction!");
2231 case 2:
2232 Select(N.getOperand(0));
2233 Tmp1 = SelectExpr(N.getOperand(1));
2234 switch (N.getOperand(1).getValueType()) {
2235 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002236 // FIXME: do I need to add support for bools here?
2237 // (return '0' or '1' r8, basically...)
2238 //
2239 // FIXME: need to round floats - 80 bits is bad, the tester
2240 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002241 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002242 // we mark r8 as live on exit up above in LowerArguments()
2243 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2244 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002245 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002246 // we mark F8 as live on exit up above in LowerArguments()
2247 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002248 }
2249 break;
2250 case 1:
2251 Select(N.getOperand(0));
2252 break;
2253 }
2254 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2255 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2256 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2257 return;
2258 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002259
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002260 case ISD::BR: {
2261 Select(N.getOperand(0));
2262 MachineBasicBlock *Dest =
2263 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2264 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2265 // XXX HACK! we do _not_ need long branches all the time
2266 return;
2267 }
2268
2269 case ISD::ImplicitDef: {
2270 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002271 BuildMI(BB, IA64::IDEF, 0,
2272 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002273 return;
2274 }
2275
2276 case ISD::BRCOND: {
2277 MachineBasicBlock *Dest =
2278 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2279
2280 Select(N.getOperand(0));
2281 Tmp1 = SelectExpr(N.getOperand(1));
2282 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2283 // XXX HACK! we do _not_ need long branches all the time
2284 return;
2285 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002286
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002287 case ISD::EXTLOAD:
2288 case ISD::ZEXTLOAD:
2289 case ISD::SEXTLOAD:
2290 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002291 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002292 case ISD::CALL:
2293 case ISD::CopyFromReg:
2294 case ISD::DYNAMIC_STACKALLOC:
2295 SelectExpr(N);
2296 return;
2297
2298 case ISD::TRUNCSTORE:
2299 case ISD::STORE: {
2300 Select(N.getOperand(0));
2301 Tmp1 = SelectExpr(N.getOperand(1)); // value
2302
2303 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002304
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002305 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002306 switch (N.getOperand(1).getValueType()) {
2307 default: assert(0 && "Cannot store this type!");
2308 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2309 // FIXME?: for now, we treat bool loads the same as i8 stores */
2310 case MVT::i8: Opc = IA64::ST1; break;
2311 case MVT::i16: Opc = IA64::ST2; break;
2312 case MVT::i32: Opc = IA64::ST4; break;
2313 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002314
Misha Brukman7847fca2005-04-22 17:54:37 +00002315 case MVT::f32: Opc = IA64::STF4; break;
2316 case MVT::f64: Opc = IA64::STF8; break;
2317 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002318 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002319 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002320 default: assert(0 && "unknown type in truncstore");
2321 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2322 //FIXME: DAG does not promote this load?
2323 case MVT::i8: Opc = IA64::ST1; break;
2324 case MVT::i16: Opc = IA64::ST2; break;
2325 case MVT::i32: Opc = IA64::ST4; break;
2326 case MVT::f32: Opc = IA64::STF4; break;
2327 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002328 }
2329
2330 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002331 unsigned dummy = MakeReg(MVT::i64);
2332 unsigned dummy2 = MakeReg(MVT::i64);
2333 BuildMI(BB, IA64::ADD, 2, dummy)
2334 .addGlobalAddress(cast<GlobalAddressSDNode>
2335 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2336 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002337
Misha Brukman7847fca2005-04-22 17:54:37 +00002338 if(!isBool)
2339 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2340 else { // we are storing a bool, so emit a little pseudocode
2341 // to store a predicate register as one byte
2342 assert(Opc==IA64::ST1);
2343 unsigned dummy3 = MakeReg(MVT::i64);
2344 unsigned dummy4 = MakeReg(MVT::i64);
2345 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2346 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2347 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2348 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2349 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002350 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2351
Misha Brukman7847fca2005-04-22 17:54:37 +00002352 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002353
Misha Brukman7847fca2005-04-22 17:54:37 +00002354 unsigned dummy = MakeReg(MVT::i64);
2355 BuildMI(BB, IA64::MOV, 1, dummy)
2356 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2357 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002358 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002359 Tmp2 = SelectExpr(N.getOperand(2)); //address
2360 if(!isBool)
2361 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2362 else { // we are storing a bool, so emit a little pseudocode
2363 // to store a predicate register as one byte
2364 assert(Opc==IA64::ST1);
2365 unsigned dummy3 = MakeReg(MVT::i64);
2366 unsigned dummy4 = MakeReg(MVT::i64);
2367 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2368 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2369 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2370 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2371 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002372 }
2373 return;
2374 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002375
Chris Lattner16cd04d2005-05-12 23:24:06 +00002376 case ISD::CALLSEQ_START:
2377 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002378 Select(N.getOperand(0));
2379 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002380
Chris Lattner16cd04d2005-05-12 23:24:06 +00002381 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2382 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002383 BuildMI(BB, Opc, 1).addImm(Tmp1);
2384 return;
2385 }
2386
2387 return;
2388 }
2389 assert(0 && "GAME OVER. INSERT COIN?");
2390}
2391
2392
2393/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2394/// into a machine code representation using pattern matching and a machine
2395/// description file.
2396///
2397FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002398 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002399}
2400
2401