blob: faeaaf2e375efaa12bccb5f4b43b499979d69e0d [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman7cbd5252005-08-16 19:49:35 +000060 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000061 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
62
Misha Brukman4633f1c2005-04-21 23:13:11 +000063 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000064 setShiftAmountType(MVT::i64);
65
66 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067
68 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000069
70 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
72 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000073 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000074
Chris Lattner3e2bafd2005-09-28 22:29:17 +000075 setOperationAction(ISD::FREM , MVT::f32 , Expand);
76 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000077
78 setOperationAction(ISD::UREM , MVT::f32 , Expand);
79 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000080
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
82 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
83 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Chris Lattner7a7c9792005-10-07 04:50:48 +000084
85 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000087
Chris Lattner17234b72005-04-30 04:26:06 +000088 // We don't support sin/cos/sqrt
89 setOperationAction(ISD::FSIN , MVT::f64, Expand);
90 setOperationAction(ISD::FCOS , MVT::f64, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
92 setOperationAction(ISD::FSIN , MVT::f32, Expand);
93 setOperationAction(ISD::FCOS , MVT::f32, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
95
Andrew Lenharthb5884d32005-05-04 19:25:37 +000096 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000097 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
98 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000099
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000100 computeRegisterProperties();
101
102 addLegalFPImmediate(+0.0);
103 addLegalFPImmediate(+1.0);
104 addLegalFPImmediate(-0.0);
105 addLegalFPImmediate(-1.0);
106 }
107
108 /// LowerArguments - This hook must be implemented to indicate how we should
109 /// lower the arguments for the specified function, into the specified DAG.
110 virtual std::vector<SDOperand>
111 LowerArguments(Function &F, SelectionDAG &DAG);
112
113 /// LowerCallTo - This hook lowers an abstract call to a function into an
114 /// actual call.
115 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000116 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000117 bool isTailCall, SDOperand Callee, ArgListTy &Args,
118 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000119
Chris Lattnere0fe2252005-07-05 19:58:54 +0000120 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
121 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000122 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000123 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
124 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000125
126 void restoreGP_SP_RP(MachineBasicBlock* BB)
127 {
128 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
129 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
130 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
131 }
132
Duraid Madinabeeaab22005-03-31 12:31:11 +0000133 void restoreSP_RP(MachineBasicBlock* BB)
134 {
135 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
136 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
137 }
138
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000139 void restoreRP(MachineBasicBlock* BB)
140 {
141 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
142 }
143
144 void restoreGP(MachineBasicBlock* BB)
145 {
146 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
147 }
148
149 };
150}
151
152
153std::vector<SDOperand>
154IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
155 std::vector<SDOperand> ArgValues;
156
157 //
158 // add beautiful description of IA64 stack frame format
159 // here (from intel 24535803.pdf most likely)
160 //
161 MachineFunction &MF = DAG.getMachineFunction();
162 MachineFrameInfo *MFI = MF.getFrameInfo();
163
164 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
165 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
166 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167
168 MachineBasicBlock& BB = MF.front();
169
Misha Brukman4633f1c2005-04-21 23:13:11 +0000170 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
173 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000175
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000176 unsigned argVreg[8];
177 unsigned argPreg[8];
178 unsigned argOpc[8];
179
Duraid Madinabeeaab22005-03-31 12:31:11 +0000180 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000181
Duraid Madinabeeaab22005-03-31 12:31:11 +0000182 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000184
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000185 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000186 {
187 SDOperand newroot, argt;
188 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000189
190 switch (getValueType(I->getType())) {
191 default:
192 std::cerr << "ERROR in LowerArgs: unknown type "
193 << getValueType(I->getType()) << "\n";
194 abort();
195 case MVT::f32:
196 // fixme? (well, will need to for weird FP structy stuff,
197 // see intel ABI docs)
198 case MVT::f64:
199//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
200 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
201 // floating point args go into f8..f15 as-needed, the increment
202 argVreg[count] = // is below..:
203 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
204 // FP args go into f8..f15 as needed: (hence the ++)
205 argPreg[count] = args_FP[used_FPArgs++];
206 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000207 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattner0a00bec2005-08-22 21:33:11 +0000208 MVT::f64);
209 if (I->getType() == Type::FloatTy)
210 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukman7847fca2005-04-22 17:54:37 +0000211 break;
212 case MVT::i1: // NOTE: as far as C abi stuff goes,
213 // bools are just boring old ints
214 case MVT::i8:
215 case MVT::i16:
216 case MVT::i32:
217 case MVT::i64:
218//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
219 MF.addLiveIn(args_int[count]); // mark this register as liveIn
220 argVreg[count] =
221 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
222 argPreg[count] = args_int[count];
223 argOpc[count] = IA64::MOV;
224 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000225 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000226 if ( getValueType(I->getType()) != MVT::i64)
227 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
228 newroot);
229 break;
230 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000231 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000232 // Create the frame index object for this incoming parameter...
233 ArgOffset = 16 + 8 * (count - 8);
234 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000235
Misha Brukman7847fca2005-04-22 17:54:37 +0000236 // Create the SelectionDAG nodes corresponding to a load
237 //from this parameter
238 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
239 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000240 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000241 }
242 ++count;
243 DAG.setRoot(newroot.getValue(1));
244 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000245 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000246
Misha Brukman4633f1c2005-04-21 23:13:11 +0000247
Duraid Madinabeeaab22005-03-31 12:31:11 +0000248 // Create a vreg to hold the output of (what will become)
249 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000250 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
251 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
252 // we create a PSEUDO_ALLOC (pseudo)instruction for now
253
254 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
255
256 // hmm:
257 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
258 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
259 // ..hmm.
260
261 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
262
263 // hmm:
264 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
265 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
266 // ..hmm.
267
Duraid Madinabeeaab22005-03-31 12:31:11 +0000268 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269
Duraid Madinabeeaab22005-03-31 12:31:11 +0000270 // if this is a varargs function, we simply lower llvm.va_start by
271 // pointing to the first entry
272 if(F.isVarArg()) {
273 tempOffset=0;
274 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000275 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000276
Duraid Madinabeeaab22005-03-31 12:31:11 +0000277 // here we actually do the moving of args, and store them to the stack
278 // too if this is a varargs function:
279 for (int i = 0; i < count && i < 8; ++i) {
280 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
281 if(F.isVarArg()) {
282 // if this is a varargs function, we copy the input registers to the stack
283 int FI = MFI->CreateFixedObject(8, tempOffset);
284 tempOffset+=8; //XXX: is it safe to use r22 like this?
285 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
286 // FIXME: we should use st8.spill here, one day
287 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
288 }
289 }
290
Duraid Madinaca494fd2005-04-12 14:54:44 +0000291 // Finally, inform the code generator which regs we return values in.
292 // (see the ISD::RET: case down below)
293 switch (getValueType(F.getReturnType())) {
294 default: assert(0 && "i have no idea where to return this type!");
295 case MVT::isVoid: break;
296 case MVT::i1:
297 case MVT::i8:
298 case MVT::i16:
299 case MVT::i32:
300 case MVT::i64:
301 MF.addLiveOut(IA64::r8);
302 break;
303 case MVT::f32:
304 case MVT::f64:
305 MF.addLiveOut(IA64::F8);
306 break;
307 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000308
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000309 return ArgValues;
310}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000311
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000312std::pair<SDOperand, SDOperand>
313IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000314 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000315 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000316 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000317 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000318
319 MachineFunction &MF = DAG.getMachineFunction();
320
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000321 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000322 unsigned outRegsUsed = 0;
323
324 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000325 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000326 outRegsUsed = 8;
327 } else {
328 outRegsUsed = Args.size();
329 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000330
Duraid Madinabeeaab22005-03-31 12:31:11 +0000331 // FIXME? this WILL fail if we ever try to pass around an arg that
332 // consumes more than a single output slot (a 'real' double, int128
333 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
334 // registers we use. Hopefully, the assembler will notice.
335 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
336 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000337
Chris Lattner16cd04d2005-05-12 23:24:06 +0000338 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000339 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000340
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000341 std::vector<SDOperand> args_to_use;
342 for (unsigned i = 0, e = Args.size(); i != e; ++i)
343 {
344 switch (getValueType(Args[i].second)) {
345 default: assert(0 && "unexpected argument type!");
346 case MVT::i1:
347 case MVT::i8:
348 case MVT::i16:
349 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000350 //promote to 64-bits, sign/zero extending based on type
351 //of the argument
352 if(Args[i].second->isSigned())
353 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
354 Args[i].first);
355 else
356 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
357 Args[i].first);
358 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000359 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000360 //promote to 64-bits
361 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000362 case MVT::f64:
363 case MVT::i64:
364 break;
365 }
366 args_to_use.push_back(Args[i].first);
367 }
368
369 std::vector<MVT::ValueType> RetVals;
370 MVT::ValueType RetTyVT = getValueType(RetTy);
371 if (RetTyVT != MVT::isVoid)
372 RetVals.push_back(RetTyVT);
373 RetVals.push_back(MVT::Other);
374
375 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000376 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000377 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000378 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000379 DAG.getConstant(NumBytes, getPointerTy()));
380 return std::make_pair(TheCall, Chain);
381}
382
Chris Lattnere0fe2252005-07-05 19:58:54 +0000383SDOperand
384IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
385 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000386 // vastart just stores the address of the VarArgsFrameIndex slot.
387 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000388 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
389 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000390}
391
392std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000393LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
394 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000395
396 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000397 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
398 VAListP, DAG.getSrcValue(VAListV));
399 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
400 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000401 unsigned Amt;
402 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
403 Amt = 8;
404 else {
405 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
406 "Other types should have been promoted for varargs!");
407 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000408 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000409 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000410 DAG.getConstant(Amt, Val.getValueType()));
411 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000412 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000413 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000414}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000415
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000416namespace {
417
418 //===--------------------------------------------------------------------===//
419 /// ISel - IA64 specific code to select IA64 machine instructions for
420 /// SelectionDAG operations.
421 ///
422 class ISel : public SelectionDAGISel {
423 /// IA64Lowering - This object fully describes how to lower LLVM code to an
424 /// IA64-specific SelectionDAG.
425 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000426 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
427 // for sdiv and udiv until it is put into the future
428 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000429
430 /// ExprMap - As shared expressions are codegen'd, we keep track of which
431 /// vreg the value is produced in, so we only emit one copy of each compiled
432 /// tree.
433 std::map<SDOperand, unsigned> ExprMap;
434 std::set<SDOperand> LoweredTokens;
435
436 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000437 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
438 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000439
440 /// InstructionSelectBasicBlock - This callback is invoked by
441 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
442 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
443
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000444 unsigned SelectExpr(SDOperand N);
445 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000446 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
447 SDOperand BuildConstmulSequence(SDOperand N);
448
Chris Lattner47c08892005-08-22 18:28:09 +0000449 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000450 };
451}
452
453/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
454/// when it has created a SelectionDAG for us to codegen.
455void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
456
457 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000458 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000459 Select(DAG.getRoot());
460
461 // Clear state used for selection.
462 ExprMap.clear();
463 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000464 ISelDAG = 0;
465}
466
Duraid Madinab2322562005-04-26 07:23:02 +0000467// strip leading '0' characters from a string
468void munchLeadingZeros(std::string& inString) {
469 while(inString.c_str()[0]=='0') {
470 inString.erase(0, 1);
471 }
472}
473
474// strip trailing '0' characters from a string
475void munchTrailingZeros(std::string& inString) {
476 int curPos=inString.length()-1;
477
478 while(inString.c_str()[curPos]=='0') {
479 inString.erase(curPos, 1);
480 curPos--;
481 }
482}
483
484// return how many consecutive '0' characters are at the end of a string
485unsigned int countTrailingZeros(std::string& inString) {
486 int curPos=inString.length()-1;
487 unsigned int zeroCount=0;
488 // assert goes here
489 while(inString.c_str()[curPos--]=='0') {
490 zeroCount++;
491 }
492 return zeroCount;
493}
494
495// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
496// '0' and 'N' (-1) characters)
497void boothEncode(std::string inString, std::string& boothEncodedString) {
498
499 int curpos=0;
500 int replacements=0;
501 int lim=inString.size();
502
503 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000504 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000505 int runlength=0;
506 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000507
Duraid Madinab2322562005-04-26 07:23:02 +0000508 // find the run length
509 for(;inString[curpos+runlength]=='1';runlength++) ;
510
511 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000512 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000513 replaceString+="1";
514
515 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000516 inString.replace(curpos, runlength+1, replaceString);
517 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000518 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000519 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000520 } else { // a zero, we just keep chugging along
521 curpos++;
522 }
523 }
524
525 // clean up (trim the string, reverse it and turn '1's into 'P's)
526 munchTrailingZeros(inString);
527 boothEncodedString="";
528
529 for(int i=inString.size()-1;i>=0;i--)
530 if(inString[i]=='1')
531 boothEncodedString+="P";
532 else
533 boothEncodedString+=inString[i];
534
535}
536
537struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
538 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000539 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000540 unsigned secondVal; // C
541 unsigned secondShift; // D
542 bool isSub;
543};
544
545/* this implements Lefevre's "pattern-based" constant multiplication,
546 * see "Multiplication by an Integer Constant", INRIA report 1999-06
547 *
548 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
549 * to get better booth encodings - this does help in practice
550 * TODO: weight shifts appropriately (most architectures can't
551 * fuse a shift and an add for arbitrary shift amounts) */
552unsigned lefevre(const std::string inString,
553 std::vector<struct shiftaddblob> &ops) {
554 std::string retstring;
555 std::string s = inString;
556 munchTrailingZeros(s);
557
558 int length=s.length()-1;
559
560 if(length==0) {
561 return(0);
562 }
563
564 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000565
Duraid Madinab2322562005-04-26 07:23:02 +0000566 for(int i=0; i<=length; i++) {
567 if (s.c_str()[length-i]=='P') {
568 p.push_back(i);
569 } else if (s.c_str()[length-i]=='N') {
570 n.push_back(i);
571 }
572 }
573
574 std::string t, u;
Chris Lattner4a209972005-08-25 00:03:21 +0000575 int c = 0;
Duraid Madina4706c032005-04-26 09:42:50 +0000576 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000577 std::map<const int, int> w;
578
Duraid Madina85d5f602005-04-27 11:57:39 +0000579 for(unsigned i=0; i<p.size(); i++) {
580 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000581 w[p[i]-p[j]]++;
582 }
583 }
584
Duraid Madina85d5f602005-04-27 11:57:39 +0000585 for(unsigned i=1; i<n.size(); i++) {
586 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000587 w[n[i]-n[j]]++;
588 }
589 }
590
Duraid Madina85d5f602005-04-27 11:57:39 +0000591 for(unsigned i=0; i<p.size(); i++) {
592 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000593 w[-abs(p[i]-n[j])]++;
594 }
595 }
596
597 std::map<const int, int>::const_iterator ii;
598 std::vector<int> d;
599 std::multimap<int, int> sorted_by_value;
600
601 for(ii = w.begin(); ii!=w.end(); ii++)
602 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
603
604 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
605 it != sorted_by_value.end(); ++it) {
606 d.push_back((*it).second);
607 }
608
609 int int_W=0;
610 int int_d;
611
612 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
613 d.pop_back();
614 retstring=s; // hmmm
615 int x=0;
616 int z=abs(int_d)-1;
617
618 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000619
Duraid Madina85d5f602005-04-27 11:57:39 +0000620 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000621 if( ((base+z+1) < retstring.size()) &&
622 retstring.c_str()[base]=='P' &&
623 retstring.c_str()[base+z+1]=='P')
624 {
625 // match
626 x++;
627 retstring.replace(base, 1, "0");
628 retstring.replace(base+z+1, 1, "p");
629 }
Duraid Madinab2322562005-04-26 07:23:02 +0000630 }
631
Duraid Madina85d5f602005-04-27 11:57:39 +0000632 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000633 if( ((base+z+1) < retstring.size()) &&
634 retstring.c_str()[base]=='N' &&
635 retstring.c_str()[base+z+1]=='N')
636 {
637 // match
638 x++;
639 retstring.replace(base, 1, "0");
640 retstring.replace(base+z+1, 1, "n");
641 }
Duraid Madinab2322562005-04-26 07:23:02 +0000642 }
643
644 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000645 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000646 if( ((base+z+1) < retstring.size()) &&
647 ((retstring.c_str()[base]=='P' &&
648 retstring.c_str()[base+z+1]=='N') ||
649 (retstring.c_str()[base]=='N' &&
650 retstring.c_str()[base+z+1]=='P')) ) {
651 // match
652 x++;
653
654 if(retstring.c_str()[base]=='P') {
655 retstring.replace(base, 1, "0");
656 retstring.replace(base+z+1, 1, "p");
657 } else { // retstring[base]=='N'
658 retstring.replace(base, 1, "0");
659 retstring.replace(base+z+1, 1, "n");
660 }
661 }
Duraid Madinab2322562005-04-26 07:23:02 +0000662 }
663 }
664
665 if(x>int_W) {
666 int_W = x;
667 t = retstring;
668 c = int_d; // tofix
669 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000670
Duraid Madinab2322562005-04-26 07:23:02 +0000671 } d.pop_back(); // hmm
672
673 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000674
Duraid Madina85d5f602005-04-27 11:57:39 +0000675 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000676 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
677 t.replace(i, 1, "0");
678 }
679
Duraid Madina85d5f602005-04-27 11:57:39 +0000680 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000681 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000682 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000683 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000684 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000685 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000686 u.replace(i, 1, "N");
687 }
688
689 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000690 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000691 c=-c;
692 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000693 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000694
Duraid Madina8a3042c2005-05-09 13:18:34 +0000695 int pos=0;
696 while(u[pos]=='0')
697 pos++;
698
699 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000700
701 int g=0;
702 if(hit) {
703 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000704 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000705 bool isP=(u[p]=='P');
706 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000707
708 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000709 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000710 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000711 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000712 }
713 }
714
715 munchLeadingZeros(u);
716
717 int i = lefevre(u, ops);
718
719 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000720
Duraid Madinab2322562005-04-26 07:23:02 +0000721 blob.firstVal=i; blob.firstShift=c;
722 blob.isSub=f;
723 blob.secondVal=i; blob.secondShift=0;
724
725 ops.push_back(blob);
726
727 i = ops.size();
728
729 munchLeadingZeros(t);
730
731 if(t.length()==0)
732 return i;
733
734 if(t.c_str()[0]!='P') {
735 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000736 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000737 bool isP=(t.c_str()[p]=='P');
738 bool isN=(t.c_str()[p]=='N');
739
740 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000741 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000742 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000743 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000744 }
745 }
746
747 int j = lefevre(t, ops);
748
749 int trail=countTrailingZeros(u);
750 blob.secondVal=i; blob.secondShift=trail;
751
752 trail=countTrailingZeros(t);
753 blob.firstVal=j; blob.firstShift=trail;
754
755 switch(g) {
756 case 0:
757 blob.isSub=false; // first + second
758 break;
759 case 1:
760 blob.isSub=true; // first - second
761 break;
762 case 2:
763 blob.isSub=true; // second - first
764 int tmpval, tmpshift;
765 tmpval=blob.firstVal;
766 tmpshift=blob.firstShift;
767 blob.firstVal=blob.secondVal;
768 blob.firstShift=blob.secondShift;
769 blob.secondVal=tmpval;
770 blob.secondShift=tmpshift;
771 break;
772 //assert
773 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000774
Duraid Madinab2322562005-04-26 07:23:02 +0000775 ops.push_back(blob);
776 return ops.size();
777}
778
779SDOperand ISel::BuildConstmulSequence(SDOperand N) {
780 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
781 // in particular, *3 is nicer as *2+1, not *4-1
782 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
783
784 bool flippedSign;
785 unsigned preliminaryShift=0;
786
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000787 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000788
789 // first, we make the constant to multiply by positive
790 if(constant<0) {
791 constant=-constant;
792 flippedSign=true;
793 } else {
794 flippedSign=false;
795 }
796
797 // next, we make it odd.
798 for(; (constant%2==0); preliminaryShift++)
799 constant>>=1;
800
801 //OK, we have a positive, odd number of 64 bits or less. Convert it
802 //to a binary string, constantString[0] is the LSB
803 char constantString[65];
804 for(int i=0; i<64; i++)
805 constantString[i]='0'+((constant>>i)&0x1);
806 constantString[64]=0;
807
808 // now, Booth encode it
809 std::string boothEncodedString;
810 boothEncode(constantString, boothEncodedString);
811
812 std::vector<struct shiftaddblob> ops;
813 // do the transformation, filling out 'ops'
814 lefevre(boothEncodedString, ops);
815
Duraid Madinae75a24a2005-05-15 14:44:13 +0000816 assert(ops.size() < 80 && "constmul code has gone haywire\n");
817 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000818
Duraid Madinab2322562005-04-26 07:23:02 +0000819 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000820 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000821 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
822 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
823 results[ops[i].firstVal-1];
824 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
825 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
826 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
827 results[ops[i].secondVal-1];
828 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
829 if(ops[i].isSub)
830 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
831 else
832 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
833 }
834
835 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000836 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000837 if(preliminaryShift) {
838 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000839 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000840 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000841 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000842 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000843 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000844
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000845 SDOperand finalresult;
846 if(flippedSign) { // if we were multiplying by a negative constant:
847 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
848 // subtract the result from 0 to flip its sign
849 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
850 } else { // there was no preliminary multiply by -1 required
851 finalresult = shiftedresult;
852 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000853
854 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000855}
856
Duraid Madina4826a072005-04-06 09:55:17 +0000857/// ponderIntegerDivisionBy - When handling integer divides, if the divide
858/// is by a constant such that we can efficiently codegen it, this
859/// function says what to do. Currently, it returns 0 if the division must
860/// become a genuine divide, and 1 if the division can be turned into a
861/// right shift.
862static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
863 unsigned& Imm) {
864 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
865 // a constant, give up.
866
867 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
868
Chris Lattner0561b3f2005-08-02 19:26:06 +0000869 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
870 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000871 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000872 }
873
Duraid Madina4826a072005-04-06 09:55:17 +0000874 return 0; // fallthrough
875}
876
Duraid Madinac02780e2005-04-13 04:50:54 +0000877static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
878 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
879 // a constant, give up.
880
881 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
882
Chris Lattner0561b3f2005-08-02 19:26:06 +0000883 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000884 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000885 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000886 }
887
Duraid Madinac02780e2005-04-13 04:50:54 +0000888 return 0; // fallthrough
889}
890
Duraid Madinaf55e4032005-04-07 12:33:38 +0000891static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
892 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
893 // constant, give up.
894 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
895
896 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
897 Imm = v & 0x3FFF; // 14 bits
898 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000899 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000900 return 0; // fallthrough
901}
902
903static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
904 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
905 // constant, give up.
906 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
907
908 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
909 Imm = v & 0xFF; // 8 bits
910 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000911 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000912 return 0; // fallthrough
913}
914
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000915unsigned ISel::SelectExpr(SDOperand N) {
916 unsigned Result;
917 unsigned Tmp1, Tmp2, Tmp3;
918 unsigned Opc = 0;
919 MVT::ValueType DestType = N.getValueType();
920
921 unsigned opcode = N.getOpcode();
922
923 SDNode *Node = N.Val;
924 SDOperand Op0, Op1;
925
926 if (Node->getOpcode() == ISD::CopyFromReg)
927 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000928 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000929
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000930 unsigned &Reg = ExprMap[N];
931 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000932
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000933 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000934 Reg = Result = (N.getValueType() != MVT::Other) ?
935 MakeReg(N.getValueType()) : 1;
936 else {
937 // If this is a call instruction, make sure to prepare ALL of the result
938 // values as well as the chain.
939 if (Node->getNumValues() == 1)
940 Reg = Result = 1; // Void call, just a chain.
941 else {
942 Result = MakeReg(Node->getValueType(0));
943 ExprMap[N.getValue(0)] = Result;
944 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
945 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
946 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
947 }
948 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000949
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000950 switch (N.getOpcode()) {
951 default:
952 Node->dump();
953 assert(0 && "Node not handled!\n");
954
955 case ISD::FrameIndex: {
956 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
957 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
958 return Result;
959 }
960
961 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000962 Tmp1 = BB->getParent()->getConstantPool()->
963 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000964 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
965 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
966 .addReg(IA64::r1);
967 return Result;
968 }
969
970 case ISD::ConstantFP: {
971 Tmp1 = Result; // Intermediate Register
972 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
973 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
974 Tmp1 = MakeReg(MVT::f64);
975
976 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
977 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
978 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
979 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
980 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
981 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
982 else
983 assert(0 && "Unexpected FP constant!");
984 if (Tmp1 != Result)
985 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
986 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000987 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000988 return Result;
989 }
990
991 case ISD::DYNAMIC_STACKALLOC: {
992 // Generate both result values.
993 if (Result != 1)
994 ExprMap[N.getValue(1)] = 1; // Generate the token
995 else
996 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
997
998 // FIXME: We are currently ignoring the requested alignment for handling
999 // greater than the stack alignment. This will need to be revisited at some
1000 // point. Align = N.getOperand(2);
1001
1002 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1003 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1004 std::cerr << "Cannot allocate stack object with greater alignment than"
1005 << " the stack alignment yet!";
1006 abort();
1007 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001008
1009/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001010 Select(N.getOperand(0));
1011 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1012 {
1013 if (CN->getValue() < 32000)
1014 {
1015 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001016 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001017 } else {
1018 Tmp1 = SelectExpr(N.getOperand(1));
1019 // Subtract size from stack pointer, thereby allocating some space.
1020 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1021 }
1022 } else {
1023 Tmp1 = SelectExpr(N.getOperand(1));
1024 // Subtract size from stack pointer, thereby allocating some space.
1025 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1026 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001027*/
1028 Select(N.getOperand(0));
1029 Tmp1 = SelectExpr(N.getOperand(1));
1030 // Subtract size from stack pointer, thereby allocating some space.
1031 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001032 // Put a pointer to the space into the result register, by copying the
1033 // stack pointer.
1034 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1035 return Result;
1036 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001037
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001038 case ISD::SELECT: {
1039 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1040 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1041 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1042
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001043 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001044
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001045 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001046 default: assert(0 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001047 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1048 // for i1, we load the condition into an integer register, then
1049 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1050 // of them will go through, since the integer register will hold
1051 // either 0 or 1)
1052 case MVT::i1: {
1053 bogoResult=MakeReg(MVT::i1);
1054
1055 // load the condition into an integer register
1056 unsigned condReg=MakeReg(MVT::i64);
1057 unsigned dummy=MakeReg(MVT::i64);
1058 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1059 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1060 .addImm(1).addReg(Tmp1);
1061
1062 // initialize Result (bool) to false (hence UNC) and if
1063 // the select condition (condReg) is false (0), copy Tmp3
1064 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1065 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1066
1067 // now, if the selection condition is true, write 1 to the
1068 // result if Tmp2 is 1
1069 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1070 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1071 break;
1072 }
1073 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1074 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001075 case MVT::i64:
1076 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001077 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1078 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1079 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001080 break;
1081 case MVT::f64:
1082 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001083 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1084 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1085 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001086 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001087 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001088
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001089 return Result;
1090 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001091
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001092 case ISD::Constant: {
1093 unsigned depositPos=0;
1094 unsigned depositLen=0;
1095 switch (N.getValueType()) {
1096 default: assert(0 && "Cannot use constants of this type!");
1097 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001098 // the constant:
1099 if(cast<ConstantSDNode>(N)->getValue()) // true:
1100 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1101 else // false:
1102 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1103 return Result; // early exit
1104 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001105 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001106 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001107
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001108 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001109
1110 if(immediate==0) { // if the constant is just zero,
1111 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1112 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001113 }
1114
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001115 if (immediate <= 8191 && immediate >= -8192) {
1116 // if this constants fits in 14 bits, we use a mov the assembler will
1117 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1118 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1119 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001120 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001121
1122 if (immediate <= 2097151 && immediate >= -2097152) {
1123 // if this constants fits in 22 bits, we use a mov the assembler will
1124 // turn into: "addl rDest=imm,r0"
1125 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1126 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001128
1129 /* otherwise, our immediate is big, so we use movl */
1130 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001131 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001132 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001133 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001134
1135 case ISD::UNDEF: {
1136 BuildMI(BB, IA64::IDEF, 0, Result);
1137 return Result;
1138 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001140 case ISD::GlobalAddress: {
1141 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1142 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001143
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001144 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001145 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 return Result;
1148 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001149
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001150 case ISD::ExternalSymbol: {
1151 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001152// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1153 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001154 return Result;
1155 }
1156
1157 case ISD::FP_EXTEND: {
1158 Tmp1 = SelectExpr(N.getOperand(0));
1159 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1160 return Result;
1161 }
1162
Chris Lattner26e04bb2005-09-02 00:15:30 +00001163 case ISD::ANY_EXTEND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001164 case ISD::ZERO_EXTEND: {
1165 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001166
Chris Lattner26e04bb2005-09-02 00:15:30 +00001167 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1168 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001169
Chris Lattner26e04bb2005-09-02 00:15:30 +00001170 // if the predicate reg has 1, we want a '1' in our GR.
1171 unsigned dummy = MakeReg(MVT::i64);
1172 // first load zero:
1173 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1174 // ...then conditionally (PR:Tmp1) add 1:
1175 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1176 .addImm(1).addReg(Tmp1);
1177 return Result; // XXX early exit!
1178 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001179
Chris Lattner26e04bb2005-09-02 00:15:30 +00001180 case ISD::SIGN_EXTEND:
1181 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1182 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001183
1184 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner26e04bb2005-09-02 00:15:30 +00001185 assert(0 && "don't know how to sign_extend from bool yet!");
1186 abort();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001187
Chris Lattner26e04bb2005-09-02 00:15:30 +00001188 case ISD::TRUNCATE:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001189 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1190 // of R0 appropriately.
Chris Lattner26e04bb2005-09-02 00:15:30 +00001191 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1192 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001193 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001194
Chris Lattner26e04bb2005-09-02 00:15:30 +00001195 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1196 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1197 return Result; // XXX early exit!
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001198
Misha Brukman7847fca2005-04-22 17:54:37 +00001199/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001200 case ISD::FP_ROUND: {
1201 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001202 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
1204 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1205 // we add 0.0 using a single precision add to do rounding
1206 return Result;
1207 }
1208*/
1209
1210// FIXME: the following 4 cases need cleaning
1211 case ISD::SINT_TO_FP: {
1212 Tmp1 = SelectExpr(N.getOperand(0));
1213 Tmp2 = MakeReg(MVT::f64);
1214 unsigned dummy = MakeReg(MVT::f64);
1215 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1216 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1217 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1218 return Result;
1219 }
1220
1221 case ISD::UINT_TO_FP: {
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = MakeReg(MVT::f64);
1224 unsigned dummy = MakeReg(MVT::f64);
1225 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1226 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1227 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1228 return Result;
1229 }
1230
1231 case ISD::FP_TO_SINT: {
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = MakeReg(MVT::f64);
1234 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1235 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1236 return Result;
1237 }
1238
1239 case ISD::FP_TO_UINT: {
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = MakeReg(MVT::f64);
1242 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1243 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1244 return Result;
1245 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001246
1247 case ISD::FADD: {
1248 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1249 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1250 // into an fma, do so:
1251 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001252 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1253 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1254 Tmp3 = SelectExpr(N.getOperand(1));
1255 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1256 return Result; // early exit
1257 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001258
1259 //else, fallthrough:
1260 Tmp1 = SelectExpr(N.getOperand(0));
1261 Tmp2 = SelectExpr(N.getOperand(1));
1262 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1263 return Result;
1264 }
Duraid Madinaed095022005-04-13 06:12:04 +00001265
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001266 case ISD::ADD: {
1267 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001268 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001269 // this add into a shladd, try:
1270 ConstantSDNode *CSD = NULL;
1271 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001272 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001273
Misha Brukman7847fca2005-04-22 17:54:37 +00001274 // ++FusedSHLADD; // Statistic
1275 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1276 int shl_amt = CSD->getValue();
1277 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001278
Misha Brukman7847fca2005-04-22 17:54:37 +00001279 BuildMI(BB, IA64::SHLADD, 3, Result)
1280 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1281 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001282 }
1283 }
1284
1285 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001286 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001287 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1288 case 1: // adding a constant that's 14 bits
1289 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1290 return Result; // early exit
1291 } // fallthrough and emit a reg+reg ADD:
1292 Tmp2 = SelectExpr(N.getOperand(1));
1293 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001294 return Result;
1295 }
1296
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001297 case ISD::FMUL:
1298 Tmp1 = SelectExpr(N.getOperand(0));
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1301 return Result;
1302
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001303 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001304
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001305 // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001306/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1307 */
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001308 // boring old integer multiply with xma
Duraid Madinab2322562005-04-26 07:23:02 +00001309 Tmp1 = SelectExpr(N.getOperand(0));
1310 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001311
1312 unsigned TempFR1=MakeReg(MVT::f64);
1313 unsigned TempFR2=MakeReg(MVT::f64);
1314 unsigned TempFR3=MakeReg(MVT::f64);
1315 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1316 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1317 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1318 .addReg(IA64::F0);
1319 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1320 return Result; // early exit
1321 /* FIXME } else { // we are multiplying by an integer constant! yay
1322 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1323 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001324 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001325
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001326 case ISD::FSUB:
1327 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madina4826a072005-04-06 09:55:17 +00001328 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1329 // into an fms, do so:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001330 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001331 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1332 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1333 Tmp3 = SelectExpr(N.getOperand(1));
1334 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1335 return Result; // early exit
1336 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001337
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001338 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001339 Tmp1 = SelectExpr(N.getOperand(0));
1340 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1341 return Result;
1342
1343 case ISD::SUB: {
1344 Tmp2 = SelectExpr(N.getOperand(1));
1345 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1346 case 1: // subtracting *from* an 8 bit constant:
1347 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1348 return Result; // early exit
1349 } // fallthrough and emit a reg+reg SUB:
1350 Tmp1 = SelectExpr(N.getOperand(0));
1351 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001352 return Result;
1353 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001354
1355 case ISD::FABS: {
1356 Tmp1 = SelectExpr(N.getOperand(0));
1357 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1358 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1359 return Result;
1360 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001361
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001362 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001363 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001364
Misha Brukman4633f1c2005-04-21 23:13:11 +00001365 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001366 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1367 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1368 } else {
1369 Tmp1 = SelectExpr(N.getOperand(0));
1370 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1371 }
1372
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001373 return Result;
1374 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001375
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001376 case ISD::AND: {
1377 switch (N.getValueType()) {
1378 default: assert(0 && "Cannot AND this type!");
1379 case MVT::i1: { // if a bool, we emit a pseudocode AND
1380 unsigned pA = SelectExpr(N.getOperand(0));
1381 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001382
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001383/* our pseudocode for AND is:
1384 *
1385(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1386 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1387 ;;
1388(pB) cmp.ne pTemp,p0 = r0,r0
1389 ;;
1390(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1391
1392*/
1393 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001395 unsigned bogusTemp1 = MakeReg(MVT::i1);
1396 unsigned bogusTemp2 = MakeReg(MVT::i1);
1397 unsigned bogusTemp3 = MakeReg(MVT::i1);
1398 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001399
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001400 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001401 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001402 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001403 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001404 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001405 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001406 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001407 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001408 break;
1409 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001410
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001411 // if not a bool, we just AND away:
1412 case MVT::i8:
1413 case MVT::i16:
1414 case MVT::i32:
1415 case MVT::i64: {
1416 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001417 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001418 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001419 switch (Tmp3) {
1420 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1421 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1422 break;
1423 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1424 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1425 break;
1426 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1427 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1428 break;
1429 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001430 // FIXME: assert the dep.z is in bounds
1431 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001432 .addImm(0).addImm(Tmp3);
1433 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001434 }
1435 return Result; // early exit
1436 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001437 Tmp2 = SelectExpr(N.getOperand(1));
1438 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001439 }
1440 }
1441 return Result;
1442 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001443
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001444 case ISD::OR: {
1445 switch (N.getValueType()) {
1446 default: assert(0 && "Cannot OR this type!");
1447 case MVT::i1: { // if a bool, we emit a pseudocode OR
1448 unsigned pA = SelectExpr(N.getOperand(0));
1449 unsigned pB = SelectExpr(N.getOperand(1));
1450
1451 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001452
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001453/* our pseudocode for OR is:
1454 *
1455
1456pC = pA OR pB
1457-------------
1458
Misha Brukman7847fca2005-04-22 17:54:37 +00001459(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1460 ;;
1461(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001462
1463*/
1464 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001465 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001466 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001467 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001468 break;
1469 }
1470 // if not a bool, we just OR away:
1471 case MVT::i8:
1472 case MVT::i16:
1473 case MVT::i32:
1474 case MVT::i64: {
1475 Tmp1 = SelectExpr(N.getOperand(0));
1476 Tmp2 = SelectExpr(N.getOperand(1));
1477 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1478 break;
1479 }
1480 }
1481 return Result;
1482 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001483
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001484 case ISD::XOR: {
1485 switch (N.getValueType()) {
1486 default: assert(0 && "Cannot XOR this type!");
1487 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1488 unsigned pY = SelectExpr(N.getOperand(0));
1489 unsigned pZ = SelectExpr(N.getOperand(1));
1490
1491/* one possible routine for XOR is:
1492
1493 // Compute px = py ^ pz
1494 // using sum of products: px = (py & !pz) | (pz & !py)
1495 // Uses 5 instructions in 3 cycles.
1496 // cycle 1
1497(pz) cmp.eq.unc px = r0, r0 // px = pz
1498(py) cmp.eq.unc pt = r0, r0 // pt = py
1499 ;;
1500 // cycle 2
1501(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1502(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1503 ;;
1504 } { .mmi
1505 // cycle 3
1506(pt) cmp.eq.or px = r0, r0 // px = px | pt
1507
1508*** Another, which we use here, requires one scratch GR. it is:
1509
1510 mov rt = 0 // initialize rt off critical path
1511 ;;
1512
1513 // cycle 1
1514(pz) cmp.eq.unc px = r0, r0 // px = pz
1515(pz) mov rt = 1 // rt = pz
1516 ;;
1517 // cycle 2
1518(py) cmp.ne px = 1, rt // if (py) px = !pz
1519
1520.. these routines kindly provided by Jim Hull
1521*/
1522 unsigned rt = MakeReg(MVT::i64);
1523
1524 // these two temporaries will never actually appear,
1525 // due to the two-address form of some of the instructions below
1526 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1527 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1528
1529 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1530 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001531 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001532 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001533 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001534 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001535 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001536 break;
1537 }
1538 // if not a bool, we just XOR away:
1539 case MVT::i8:
1540 case MVT::i16:
1541 case MVT::i32:
1542 case MVT::i64: {
1543 Tmp1 = SelectExpr(N.getOperand(0));
1544 Tmp2 = SelectExpr(N.getOperand(1));
1545 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1546 break;
1547 }
1548 }
1549 return Result;
1550 }
1551
Duraid Madina63bbed52005-05-11 05:16:09 +00001552 case ISD::CTPOP: {
1553 Tmp1 = SelectExpr(N.getOperand(0));
1554 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1555 return Result;
1556 }
1557
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001558 case ISD::SHL: {
1559 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001560 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1561 Tmp2 = CN->getValue();
1562 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1563 } else {
1564 Tmp2 = SelectExpr(N.getOperand(1));
1565 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1566 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001567 return Result;
1568 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001569
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001570 case ISD::SRL: {
1571 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001572 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1573 Tmp2 = CN->getValue();
1574 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1575 } else {
1576 Tmp2 = SelectExpr(N.getOperand(1));
1577 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1578 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001579 return Result;
1580 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001581
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001582 case ISD::SRA: {
1583 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001584 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1585 Tmp2 = CN->getValue();
1586 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1587 } else {
1588 Tmp2 = SelectExpr(N.getOperand(1));
1589 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1590 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001591 return Result;
1592 }
1593
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001594 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001595 case ISD::SDIV:
1596 case ISD::UDIV:
1597 case ISD::SREM:
1598 case ISD::UREM: {
1599
1600 Tmp1 = SelectExpr(N.getOperand(0));
1601 Tmp2 = SelectExpr(N.getOperand(1));
1602
1603 bool isFP=false;
1604
1605 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1606 isFP=true;
1607
1608 bool isModulus=false; // is it a division or a modulus?
1609 bool isSigned=false;
1610
1611 switch(N.getOpcode()) {
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001612 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001613 case ISD::SDIV: isModulus=false; isSigned=true; break;
1614 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001615 case ISD::FREM:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001616 case ISD::SREM: isModulus=true; isSigned=true; break;
1617 case ISD::UREM: isModulus=true; isSigned=false; break;
1618 }
1619
Duraid Madina4826a072005-04-06 09:55:17 +00001620 if(!isModulus && !isFP) { // if this is an integer divide,
1621 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001622 case 1: // division by a constant that's a power of 2
1623 Tmp1 = SelectExpr(N.getOperand(0));
1624 if(isSigned) { // argument could be negative, so emit some code:
1625 unsigned divAmt=Tmp3;
1626 unsigned tempGR1=MakeReg(MVT::i64);
1627 unsigned tempGR2=MakeReg(MVT::i64);
1628 unsigned tempGR3=MakeReg(MVT::i64);
1629 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1630 .addReg(Tmp1).addImm(divAmt-1);
1631 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1632 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1633 BuildMI(BB, IA64::ADD, 2, tempGR3)
1634 .addReg(Tmp1).addReg(tempGR2);
1635 BuildMI(BB, IA64::SHRS, 2, Result)
1636 .addReg(tempGR3).addImm(divAmt);
1637 }
1638 else // unsigned div-by-power-of-2 becomes a simple shift right:
1639 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1640 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001641 }
1642 }
1643
Misha Brukman4633f1c2005-04-21 23:13:11 +00001644 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001645 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001646 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1647 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1648 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1649 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1650 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1651 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1652 unsigned TmpF7=MakeReg(MVT::f64);
1653 unsigned TmpF8=MakeReg(MVT::f64);
1654 unsigned TmpF9=MakeReg(MVT::f64);
1655 unsigned TmpF10=MakeReg(MVT::f64);
1656 unsigned TmpF11=MakeReg(MVT::f64);
1657 unsigned TmpF12=MakeReg(MVT::f64);
1658 unsigned TmpF13=MakeReg(MVT::f64);
1659 unsigned TmpF14=MakeReg(MVT::f64);
1660 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001661
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001662 // OK, emit some code:
1663
1664 if(!isFP) {
1665 // first, load the inputs into FP regs.
1666 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1667 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001668
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001669 // next, convert the inputs to FP
1670 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001671 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1672 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001673 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001674 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1675 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001676 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001677
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001678 } else { // this is an FP divide/remainder, so we 'leak' some temp
1679 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1680 TmpF3=Tmp1;
1681 TmpF4=Tmp2;
1682 }
1683
1684 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001685 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1686 BuildMI(BB, IA64::FRCPAS1, 4)
1687 .addReg(TmpF5, MachineOperand::Def)
1688 .addReg(TmpPR, MachineOperand::Def)
1689 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001690
Duraid Madinabeeaab22005-03-31 12:31:11 +00001691 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1692 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1693 // TPCMPNE below
1694 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1695 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001696 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001697 }
1698
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001699 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1700 // precision, don't need this much for f32/i32)
1701 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1702 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1703 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1704 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1705 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1706 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1707 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1708 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1709 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1710 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1711 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1712 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1713 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1714 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1715 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1716 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001717
1718 // FIXME: this is unfortunate :(
1719 // the story is that the dest reg of the fnma above and the fma below
1720 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1721 // be the same register, or this code breaks if the first argument is
1722 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001723 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1724 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1725
Duraid Madina6e02e682005-04-04 05:05:52 +00001726 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1727 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1728 }
1729
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001730 if(!isFP) {
1731 // round to an integer
1732 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001733 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001734 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001735 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001736 } else {
1737 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1738 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1739 // we really do need the above FMOV? ;)
1740 }
1741
1742 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001743 if(isFP) { // extra worrying about div-by-zero
1744 unsigned bogoResult=MakeReg(MVT::f64);
1745
1746 // we do a 'conditional fmov' (of the correct result, depending
1747 // on how the frcpa predicate turned out)
1748 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001749 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001750 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001751 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001752 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001753 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001754 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001755 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001756 } else { // this is a modulus
1757 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001758 // answer = q * (-b) + a
1759 unsigned ModulusResult = MakeReg(MVT::f64);
1760 unsigned TmpF = MakeReg(MVT::f64);
1761 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001762
Misha Brukman7847fca2005-04-22 17:54:37 +00001763 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1764 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1765 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1766 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1767 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001768 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001769 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001770 }
1771 }
1772
1773 return Result;
1774 }
1775
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001776 case ISD::SIGN_EXTEND_INREG: {
1777 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001778 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001779 default:
1780 Node->dump();
1781 assert(0 && "don't know how to sign extend this type");
1782 break;
1783 case MVT::i8: Opc = IA64::SXT1; break;
1784 case MVT::i16: Opc = IA64::SXT2; break;
1785 case MVT::i32: Opc = IA64::SXT4; break;
1786 }
1787 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1788 return Result;
1789 }
1790
1791 case ISD::SETCC: {
1792 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001793 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1794 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001795
Chris Lattner88ac32c2005-08-09 20:21:10 +00001796 if(ConstantSDNode *CSDN =
1797 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1798 // if we are comparing against a constant zero
1799 if(CSDN->getValue()==0)
1800 Tmp2 = IA64::r0; // then we can just compare against r0
1801 else
1802 Tmp2 = SelectExpr(N.getOperand(1));
1803 } else // not comparing against a constant
1804 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001805
Chris Lattner88ac32c2005-08-09 20:21:10 +00001806 switch (CC) {
1807 default: assert(0 && "Unknown integer comparison!");
1808 case ISD::SETEQ:
1809 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1810 break;
1811 case ISD::SETGT:
1812 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1813 break;
1814 case ISD::SETGE:
1815 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1816 break;
1817 case ISD::SETLT:
1818 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 break;
1820 case ISD::SETLE:
1821 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1822 break;
1823 case ISD::SETNE:
1824 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1825 break;
1826 case ISD::SETULT:
1827 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 break;
1829 case ISD::SETUGT:
1830 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1831 break;
1832 case ISD::SETULE:
1833 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1834 break;
1835 case ISD::SETUGE:
1836 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1837 break;
1838 }
1839 } else { // if not integer, should be FP.
1840 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1841 "error: SETCC should have had incoming f32 promoted to f64!\n");
1842
1843 if(ConstantFPSDNode *CFPSDN =
1844 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1845
1846 // if we are comparing against a constant +0.0 or +1.0
1847 if(CFPSDN->isExactlyValue(+0.0))
1848 Tmp2 = IA64::F0; // then we can just compare against f0
1849 else if(CFPSDN->isExactlyValue(+1.0))
1850 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001851 else
1852 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001853 } else // not comparing against a constant
1854 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001855
Chris Lattner88ac32c2005-08-09 20:21:10 +00001856 switch (CC) {
1857 default: assert(0 && "Unknown FP comparison!");
1858 case ISD::SETEQ:
1859 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1860 break;
1861 case ISD::SETGT:
1862 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 break;
1864 case ISD::SETGE:
1865 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETLT:
1868 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETLE:
1871 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETNE:
1874 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETULT:
1877 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETUGT:
1880 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETULE:
1883 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETUGE:
1886 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001888 }
1889 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001890 return Result;
1891 }
1892
1893 case ISD::EXTLOAD:
1894 case ISD::ZEXTLOAD:
1895 case ISD::LOAD: {
1896 // Make sure we generate both values.
1897 if (Result != 1)
1898 ExprMap[N.getValue(1)] = 1; // Generate the token
1899 else
1900 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1901
1902 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001903
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001904 if(opcode == ISD::LOAD) { // this is a LOAD
1905 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001906 default: assert(0 && "Cannot load this type!");
1907 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1908 // FIXME: for now, we treat bool loads the same as i8 loads */
1909 case MVT::i8: Opc = IA64::LD1; break;
1910 case MVT::i16: Opc = IA64::LD2; break;
1911 case MVT::i32: Opc = IA64::LD4; break;
1912 case MVT::i64: Opc = IA64::LD8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001913
Misha Brukman7847fca2005-04-22 17:54:37 +00001914 case MVT::f32: Opc = IA64::LDF4; break;
1915 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001916 }
1917 } else { // this is an EXTLOAD or ZEXTLOAD
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001918 MVT::ValueType TypeBeingLoaded =
1919 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001920 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001921 default: assert(0 && "Cannot extload/zextload this type!");
1922 // FIXME: bools?
1923 case MVT::i8: Opc = IA64::LD1; break;
1924 case MVT::i16: Opc = IA64::LD2; break;
1925 case MVT::i32: Opc = IA64::LD4; break;
1926 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001927 }
1928 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001929
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001930 SDOperand Chain = N.getOperand(0);
1931 SDOperand Address = N.getOperand(1);
1932
1933 if(Address.getOpcode() == ISD::GlobalAddress) {
1934 Select(Chain);
1935 unsigned dummy = MakeReg(MVT::i64);
1936 unsigned dummy2 = MakeReg(MVT::i64);
1937 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001938 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1939 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001940 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1941 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001942 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001943 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001944 // into a predicate register
1945 assert(Opc==IA64::LD1 && "problem loading a bool");
1946 unsigned dummy3 = MakeReg(MVT::i64);
1947 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1948 // we compare to 0. true? 0. false? 1.
1949 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001950 }
1951 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001952 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattner143b6752005-08-26 17:18:44 +00001953 getConstantPoolIndex(CP->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001954 Select(Chain);
1955 IA64Lowering.restoreGP(BB);
1956 unsigned dummy = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +00001957 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
Misha Brukman7847fca2005-04-22 17:54:37 +00001958 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001959 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001960 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001961 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001962 // into a predicate register
1963 assert(Opc==IA64::LD1 && "problem loading a bool");
1964 unsigned dummy3 = MakeReg(MVT::i64);
1965 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1966 // we compare to 0. true? 0. false? 1.
1967 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001968 }
1969 } else if(Address.getOpcode() == ISD::FrameIndex) {
1970 Select(Chain); // FIXME ? what about bools?
1971 unsigned dummy = MakeReg(MVT::i64);
1972 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001973 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001974 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001975 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001976 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001977 // into a predicate register
1978 assert(Opc==IA64::LD1 && "problem loading a bool");
1979 unsigned dummy3 = MakeReg(MVT::i64);
1980 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1981 // we compare to 0. true? 0. false? 1.
1982 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001983 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001984 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001985 Select(Chain);
1986 Tmp2 = SelectExpr(Address);
1987 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001988 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001989 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001990 // into a predicate register
1991 assert(Opc==IA64::LD1 && "problem loading a bool");
1992 unsigned dummy = MakeReg(MVT::i64);
1993 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1994 // we compare to 0. true? 0. false? 1.
1995 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00001996 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 }
1998
1999 return Result;
2000 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002001
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002002 case ISD::CopyFromReg: {
2003 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002004 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002005 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002006
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002007 SDOperand Chain = N.getOperand(0);
2008
2009 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002010 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002011
2012 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002013 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2014 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 // (r) Result =cmp.eq.unc(r0,r0)
2016 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002018 return Result;
2019 }
2020
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002021 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002022 case ISD::CALL: {
2023 Select(N.getOperand(0));
2024
2025 // The chain for this call is now lowered.
2026 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002027
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002028 //grab the arguments
2029 std::vector<unsigned> argvregs;
2030
2031 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002032 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002033
2034 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002035 // Runtime Architecture Guide to see some examples of what's going
2036 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2037 // while FP args get mapped to F8->F15 as needed)
2038
2039 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002040
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002041 // in reg args
2042 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2043 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2045 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2046 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2047 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002048
Misha Brukman7847fca2005-04-22 17:54:37 +00002049 switch(N.getOperand(i+2).getValueType())
2050 {
2051 default: // XXX do we need to support MVT::i1 here?
2052 Node->dump();
2053 N.getOperand(i).Val->dump();
2054 std::cerr << "Type for " << i << " is: " <<
2055 N.getOperand(i+2).getValueType() << std::endl;
2056 assert(0 && "Unknown value type for call");
2057 case MVT::i64:
2058 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2059 break;
2060 case MVT::f64:
2061 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2062 .addReg(argvregs[i]);
2063 // FIXME: we don't need to do this _all_ the time:
2064 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2065 break;
2066 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002067 }
2068
2069 //in mem args
2070 for (int i = 8, e = argvregs.size(); i < e; ++i)
2071 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002072 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002073
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002074 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002075 default:
2076 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002077 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002078 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002079 N.getOperand(i+2).getValueType() << "\n";
2080 assert(0 && "Unknown value type for call");
2081 case MVT::i1: // FIXME?
2082 case MVT::i8:
2083 case MVT::i16:
2084 case MVT::i32:
2085 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002086 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2087 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2088 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002089 break;
2090 case MVT::f32:
2091 case MVT::f64:
2092 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002093 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2094 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002095 break;
2096 }
2097 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002098
Duraid Madina04aa46d2005-05-20 11:39:17 +00002099 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002100 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002101 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002102 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002103 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2104 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002105 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002106 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002107 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002108 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002109 // "non-pic code with imm relocation against dynamic symbol" errors
2110 BuildMI(BB, IA64::BRCALL, 1)
2111 .addExternalSymbol(ESSDN->getSymbol(), true);
2112 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002113 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002114 else { // otherwise we need to get the function descriptor
2115 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002116 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002117 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002118
2119 unsigned targetEntryPoint=MakeReg(MVT::i64);
2120 unsigned targetGPAddr=MakeReg(MVT::i64);
2121 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002122
Duraid Madinabeeaab22005-03-31 12:31:11 +00002123 // b6 is a scratch branch register, we load the target entry point
2124 // from the base of the function descriptor
2125 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2126 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2127
2128 // save the current GP:
2129 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002130
Duraid Madinabeeaab22005-03-31 12:31:11 +00002131 /* TODO: we need to make sure doing this never, ever loads a
2132 * bogus value into r1 (GP). */
2133 // load the target GP (which is at mem[functiondescriptor+8])
2134 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002135 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002136 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2137
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002138 // and then jump: (well, call)
2139 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002140 // and finally restore the old GP
2141 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2142 IA64Lowering.restoreSP_RP(BB);
2143 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002144
2145 switch (Node->getValueType(0)) {
2146 default: assert(0 && "Unknown value type for call result!");
2147 case MVT::Other: return 1;
2148 case MVT::i1:
2149 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002150 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002151 break;
2152 case MVT::i8:
2153 case MVT::i16:
2154 case MVT::i32:
2155 case MVT::i64:
2156 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2157 break;
2158 case MVT::f64:
2159 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2160 break;
2161 }
2162 return Result+N.ResNo;
2163 }
2164
Misha Brukman4633f1c2005-04-21 23:13:11 +00002165 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002166 return 0;
2167}
2168
2169void ISel::Select(SDOperand N) {
2170 unsigned Tmp1, Tmp2, Opc;
2171 unsigned opcode = N.getOpcode();
2172
Nate Begeman85fdeb22005-03-24 04:39:54 +00002173 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002174 return; // Already selected.
2175
2176 SDNode *Node = N.Val;
2177
2178 switch (Node->getOpcode()) {
2179 default:
2180 Node->dump(); std::cerr << "\n";
2181 assert(0 && "Node not handled yet!");
2182
2183 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002184
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002185 case ISD::TokenFactor: {
2186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2187 Select(Node->getOperand(i));
2188 return;
2189 }
2190
2191 case ISD::CopyToReg: {
2192 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002193 Tmp1 = SelectExpr(N.getOperand(2));
2194 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002195
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002196 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002197 // if a bool, we use pseudocode
2198 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002199 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2200 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002201 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2202 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002203 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002204 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002205 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002206 }
2207 return;
2208 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002209
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002210 case ISD::RET: {
2211
2212 /* what the heck is going on here:
2213
2214<_sabre_> ret with two operands is obvious: chain and value
2215<camel_> yep
2216<_sabre_> ret with 3 values happens when 'expansion' occurs
2217<_sabre_> e.g. i64 gets split into 2x i32
2218<camel_> oh right
2219<_sabre_> you don't have this case on ia64
2220<camel_> yep
2221<_sabre_> so the two returned values go into EAX/EDX on ia32
2222<camel_> ahhh *memories*
2223<_sabre_> :)
2224<camel_> ok, thanks :)
2225<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2226<_sabre_> this is the first operand always
2227<_sabre_> these operand often define chains, they are the last operand
2228<_sabre_> they are printed as 'ch' if you do DAG.dump()
2229 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002230
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002231 switch (N.getNumOperands()) {
2232 default:
2233 assert(0 && "Unknown return instruction!");
2234 case 2:
2235 Select(N.getOperand(0));
2236 Tmp1 = SelectExpr(N.getOperand(1));
2237 switch (N.getOperand(1).getValueType()) {
2238 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002239 // FIXME: do I need to add support for bools here?
2240 // (return '0' or '1' r8, basically...)
2241 //
2242 // FIXME: need to round floats - 80 bits is bad, the tester
2243 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002244 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002245 // we mark r8 as live on exit up above in LowerArguments()
2246 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2247 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002248 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002249 // we mark F8 as live on exit up above in LowerArguments()
2250 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002251 }
2252 break;
2253 case 1:
2254 Select(N.getOperand(0));
2255 break;
2256 }
2257 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2258 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2259 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2260 return;
2261 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002262
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002263 case ISD::BR: {
2264 Select(N.getOperand(0));
2265 MachineBasicBlock *Dest =
2266 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2267 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2268 // XXX HACK! we do _not_ need long branches all the time
2269 return;
2270 }
2271
2272 case ISD::ImplicitDef: {
2273 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002274 BuildMI(BB, IA64::IDEF, 0,
2275 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002276 return;
2277 }
2278
2279 case ISD::BRCOND: {
2280 MachineBasicBlock *Dest =
2281 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2282
2283 Select(N.getOperand(0));
2284 Tmp1 = SelectExpr(N.getOperand(1));
2285 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2286 // XXX HACK! we do _not_ need long branches all the time
2287 return;
2288 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002289
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002290 case ISD::EXTLOAD:
2291 case ISD::ZEXTLOAD:
2292 case ISD::SEXTLOAD:
2293 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002294 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002295 case ISD::CALL:
2296 case ISD::CopyFromReg:
2297 case ISD::DYNAMIC_STACKALLOC:
2298 SelectExpr(N);
2299 return;
2300
2301 case ISD::TRUNCSTORE:
2302 case ISD::STORE: {
2303 Select(N.getOperand(0));
2304 Tmp1 = SelectExpr(N.getOperand(1)); // value
2305
2306 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002307
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002308 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002309 switch (N.getOperand(1).getValueType()) {
2310 default: assert(0 && "Cannot store this type!");
2311 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2312 // FIXME?: for now, we treat bool loads the same as i8 stores */
2313 case MVT::i8: Opc = IA64::ST1; break;
2314 case MVT::i16: Opc = IA64::ST2; break;
2315 case MVT::i32: Opc = IA64::ST4; break;
2316 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002317
Misha Brukman7847fca2005-04-22 17:54:37 +00002318 case MVT::f32: Opc = IA64::STF4; break;
2319 case MVT::f64: Opc = IA64::STF8; break;
2320 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002321 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002322 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002323 default: assert(0 && "unknown type in truncstore");
2324 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2325 //FIXME: DAG does not promote this load?
2326 case MVT::i8: Opc = IA64::ST1; break;
2327 case MVT::i16: Opc = IA64::ST2; break;
2328 case MVT::i32: Opc = IA64::ST4; break;
2329 case MVT::f32: Opc = IA64::STF4; break;
2330 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002331 }
2332
2333 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002334 unsigned dummy = MakeReg(MVT::i64);
2335 unsigned dummy2 = MakeReg(MVT::i64);
2336 BuildMI(BB, IA64::ADD, 2, dummy)
2337 .addGlobalAddress(cast<GlobalAddressSDNode>
2338 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2339 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002340
Misha Brukman7847fca2005-04-22 17:54:37 +00002341 if(!isBool)
2342 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2343 else { // we are storing a bool, so emit a little pseudocode
2344 // to store a predicate register as one byte
2345 assert(Opc==IA64::ST1);
2346 unsigned dummy3 = MakeReg(MVT::i64);
2347 unsigned dummy4 = MakeReg(MVT::i64);
2348 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2349 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2350 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2351 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2352 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002353 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2354
Misha Brukman7847fca2005-04-22 17:54:37 +00002355 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002356
Misha Brukman7847fca2005-04-22 17:54:37 +00002357 unsigned dummy = MakeReg(MVT::i64);
2358 BuildMI(BB, IA64::MOV, 1, dummy)
2359 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2360 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002361 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002362 Tmp2 = SelectExpr(N.getOperand(2)); //address
2363 if(!isBool)
2364 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2365 else { // we are storing a bool, so emit a little pseudocode
2366 // to store a predicate register as one byte
2367 assert(Opc==IA64::ST1);
2368 unsigned dummy3 = MakeReg(MVT::i64);
2369 unsigned dummy4 = MakeReg(MVT::i64);
2370 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2371 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2372 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2373 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2374 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002375 }
2376 return;
2377 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002378
Chris Lattner16cd04d2005-05-12 23:24:06 +00002379 case ISD::CALLSEQ_START:
2380 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002381 Select(N.getOperand(0));
2382 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002383
Chris Lattner16cd04d2005-05-12 23:24:06 +00002384 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2385 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002386 BuildMI(BB, Opc, 1).addImm(Tmp1);
2387 return;
2388 }
2389
2390 return;
2391 }
2392 assert(0 && "GAME OVER. INSERT COIN?");
2393}
2394
2395
2396/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2397/// into a machine code representation using pattern matching and a machine
2398/// description file.
2399///
2400FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002401 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002402}
2403
2404