blob: 20189e1999c0f1ac2725899d4f886c02ea371632 [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);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000060 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
61
Misha Brukman4633f1c2005-04-21 23:13:11 +000062 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000063 setShiftAmountType(MVT::i64);
64
65 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000066
67 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000068
69 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000073
74 setOperationAction(ISD::SREM , MVT::f32 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
76
77 setOperationAction(ISD::UREM , MVT::f32 , Expand);
78 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000079
Duraid Madina9b9d45f2005-03-17 18:17:03 +000080 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
81 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
82 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
83
Duraid Madina9b9d45f2005-03-17 18:17:03 +000084 computeRegisterProperties();
85
86 addLegalFPImmediate(+0.0);
87 addLegalFPImmediate(+1.0);
88 addLegalFPImmediate(-0.0);
89 addLegalFPImmediate(-1.0);
90 }
91
92 /// LowerArguments - This hook must be implemented to indicate how we should
93 /// lower the arguments for the specified function, into the specified DAG.
94 virtual std::vector<SDOperand>
95 LowerArguments(Function &F, SelectionDAG &DAG);
96
97 /// LowerCallTo - This hook lowers an abstract call to a function into an
98 /// actual call.
99 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000100 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
101 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000102
103 virtual std::pair<SDOperand, SDOperand>
104 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
105
106 virtual std::pair<SDOperand,SDOperand>
107 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
108 const Type *ArgTy, SelectionDAG &DAG);
109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
112 SelectionDAG &DAG);
113
114 void restoreGP_SP_RP(MachineBasicBlock* BB)
115 {
116 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
117 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
118 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
119 }
120
Duraid Madinabeeaab22005-03-31 12:31:11 +0000121 void restoreSP_RP(MachineBasicBlock* BB)
122 {
123 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
124 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
125 }
126
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000127 void restoreRP(MachineBasicBlock* BB)
128 {
129 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
130 }
131
132 void restoreGP(MachineBasicBlock* BB)
133 {
134 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
135 }
136
137 };
138}
139
140
141std::vector<SDOperand>
142IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
143 std::vector<SDOperand> ArgValues;
144
145 //
146 // add beautiful description of IA64 stack frame format
147 // here (from intel 24535803.pdf most likely)
148 //
149 MachineFunction &MF = DAG.getMachineFunction();
150 MachineFrameInfo *MFI = MF.getFrameInfo();
151
152 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
153 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
154 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
155
156 MachineBasicBlock& BB = MF.front();
157
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000159 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000160
161 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000162 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000163
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000164 unsigned argVreg[8];
165 unsigned argPreg[8];
166 unsigned argOpc[8];
167
Duraid Madinabeeaab22005-03-31 12:31:11 +0000168 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000169
Duraid Madinabeeaab22005-03-31 12:31:11 +0000170 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000173 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 {
175 SDOperand newroot, argt;
176 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000177
178 switch (getValueType(I->getType())) {
179 default:
180 std::cerr << "ERROR in LowerArgs: unknown type "
181 << getValueType(I->getType()) << "\n";
182 abort();
183 case MVT::f32:
184 // fixme? (well, will need to for weird FP structy stuff,
185 // see intel ABI docs)
186 case MVT::f64:
187//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
188 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
189 // floating point args go into f8..f15 as-needed, the increment
190 argVreg[count] = // is below..:
191 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
192 // FP args go into f8..f15 as needed: (hence the ++)
193 argPreg[count] = args_FP[used_FPArgs++];
194 argOpc[count] = IA64::FMOV;
195 argt = newroot = DAG.getCopyFromReg(argVreg[count],
196 getValueType(I->getType()), DAG.getRoot());
197 break;
198 case MVT::i1: // NOTE: as far as C abi stuff goes,
199 // bools are just boring old ints
200 case MVT::i8:
201 case MVT::i16:
202 case MVT::i32:
203 case MVT::i64:
204//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
205 MF.addLiveIn(args_int[count]); // mark this register as liveIn
206 argVreg[count] =
207 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
208 argPreg[count] = args_int[count];
209 argOpc[count] = IA64::MOV;
210 argt = newroot =
211 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
212 if ( getValueType(I->getType()) != MVT::i64)
213 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
214 newroot);
215 break;
216 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000217 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000218 // Create the frame index object for this incoming parameter...
219 ArgOffset = 16 + 8 * (count - 8);
220 int FI = MFI->CreateFixedObject(8, ArgOffset);
221
222 // Create the SelectionDAG nodes corresponding to a load
223 //from this parameter
224 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
225 argt = newroot = DAG.getLoad(getValueType(I->getType()),
226 DAG.getEntryNode(), FIN);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000227 }
228 ++count;
229 DAG.setRoot(newroot.getValue(1));
230 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000231 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000232
Misha Brukman4633f1c2005-04-21 23:13:11 +0000233
Duraid Madinabeeaab22005-03-31 12:31:11 +0000234 // Create a vreg to hold the output of (what will become)
235 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000236 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
237 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
238 // we create a PSEUDO_ALLOC (pseudo)instruction for now
239
240 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
241
242 // hmm:
243 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
244 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
245 // ..hmm.
246
247 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
248
249 // hmm:
250 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
251 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
252 // ..hmm.
253
Duraid Madinabeeaab22005-03-31 12:31:11 +0000254 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000255
Duraid Madinabeeaab22005-03-31 12:31:11 +0000256 // if this is a varargs function, we simply lower llvm.va_start by
257 // pointing to the first entry
258 if(F.isVarArg()) {
259 tempOffset=0;
260 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000261 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000262
Duraid Madinabeeaab22005-03-31 12:31:11 +0000263 // here we actually do the moving of args, and store them to the stack
264 // too if this is a varargs function:
265 for (int i = 0; i < count && i < 8; ++i) {
266 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
267 if(F.isVarArg()) {
268 // if this is a varargs function, we copy the input registers to the stack
269 int FI = MFI->CreateFixedObject(8, tempOffset);
270 tempOffset+=8; //XXX: is it safe to use r22 like this?
271 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
272 // FIXME: we should use st8.spill here, one day
273 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
274 }
275 }
276
Duraid Madinaca494fd2005-04-12 14:54:44 +0000277 // Finally, inform the code generator which regs we return values in.
278 // (see the ISD::RET: case down below)
279 switch (getValueType(F.getReturnType())) {
280 default: assert(0 && "i have no idea where to return this type!");
281 case MVT::isVoid: break;
282 case MVT::i1:
283 case MVT::i8:
284 case MVT::i16:
285 case MVT::i32:
286 case MVT::i64:
287 MF.addLiveOut(IA64::r8);
288 break;
289 case MVT::f32:
290 case MVT::f64:
291 MF.addLiveOut(IA64::F8);
292 break;
293 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000294
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000295 return ArgValues;
296}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000297
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000298std::pair<SDOperand, SDOperand>
299IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000300 const Type *RetTy, bool isVarArg,
301 SDOperand Callee, ArgListTy &Args,
302 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000303
304 MachineFunction &MF = DAG.getMachineFunction();
305
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000306 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000307 unsigned outRegsUsed = 0;
308
309 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000310 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000311 outRegsUsed = 8;
312 } else {
313 outRegsUsed = Args.size();
314 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000315
Duraid Madinabeeaab22005-03-31 12:31:11 +0000316 // FIXME? this WILL fail if we ever try to pass around an arg that
317 // consumes more than a single output slot (a 'real' double, int128
318 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
319 // registers we use. Hopefully, the assembler will notice.
320 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
321 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000322
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000323 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
324 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000325
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000326 std::vector<SDOperand> args_to_use;
327 for (unsigned i = 0, e = Args.size(); i != e; ++i)
328 {
329 switch (getValueType(Args[i].second)) {
330 default: assert(0 && "unexpected argument type!");
331 case MVT::i1:
332 case MVT::i8:
333 case MVT::i16:
334 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000335 //promote to 64-bits, sign/zero extending based on type
336 //of the argument
337 if(Args[i].second->isSigned())
338 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
339 Args[i].first);
340 else
341 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
342 Args[i].first);
343 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000344 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000345 //promote to 64-bits
346 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000347 case MVT::f64:
348 case MVT::i64:
349 break;
350 }
351 args_to_use.push_back(Args[i].first);
352 }
353
354 std::vector<MVT::ValueType> RetVals;
355 MVT::ValueType RetTyVT = getValueType(RetTy);
356 if (RetTyVT != MVT::isVoid)
357 RetVals.push_back(RetTyVT);
358 RetVals.push_back(MVT::Other);
359
360 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000361 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000362 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
363 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
364 DAG.getConstant(NumBytes, getPointerTy()));
365 return std::make_pair(TheCall, Chain);
366}
367
368std::pair<SDOperand, SDOperand>
369IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
370 // vastart just returns the address of the VarArgsFrameIndex slot.
371 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
372}
373
374std::pair<SDOperand,SDOperand> IA64TargetLowering::
375LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
376 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000377
378 MVT::ValueType ArgVT = getValueType(ArgTy);
379 SDOperand Result;
380 if (!isVANext) {
381 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
382 } else {
383 unsigned Amt;
384 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
385 Amt = 8;
386 else {
387 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
388 "Other types should have been promoted for varargs!");
389 Amt = 8;
390 }
391 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
392 DAG.getConstant(Amt, VAList.getValueType()));
393 }
394 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000395}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000396
397std::pair<SDOperand, SDOperand> IA64TargetLowering::
398LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
399 SelectionDAG &DAG) {
400
401 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000402 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000403}
404
405
406namespace {
407
408 //===--------------------------------------------------------------------===//
409 /// ISel - IA64 specific code to select IA64 machine instructions for
410 /// SelectionDAG operations.
411 ///
412 class ISel : public SelectionDAGISel {
413 /// IA64Lowering - This object fully describes how to lower LLVM code to an
414 /// IA64-specific SelectionDAG.
415 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000416 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
417 // for sdiv and udiv until it is put into the future
418 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000419
420 /// ExprMap - As shared expressions are codegen'd, we keep track of which
421 /// vreg the value is produced in, so we only emit one copy of each compiled
422 /// tree.
423 std::map<SDOperand, unsigned> ExprMap;
424 std::set<SDOperand> LoweredTokens;
425
426 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000427 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
428 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000429
430 /// InstructionSelectBasicBlock - This callback is invoked by
431 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
432 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
433
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000434 unsigned SelectExpr(SDOperand N);
435 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000436 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
437 SDOperand BuildConstmulSequence(SDOperand N);
438
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000439 };
440}
441
442/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
443/// when it has created a SelectionDAG for us to codegen.
444void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
445
446 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000447 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000448 Select(DAG.getRoot());
449
450 // Clear state used for selection.
451 ExprMap.clear();
452 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000453 ISelDAG = 0;
454}
455
456const char sign[2]={'+','-'};
457
458
459// strip leading '0' characters from a string
460void munchLeadingZeros(std::string& inString) {
461 while(inString.c_str()[0]=='0') {
462 inString.erase(0, 1);
463 }
464}
465
466// strip trailing '0' characters from a string
467void munchTrailingZeros(std::string& inString) {
468 int curPos=inString.length()-1;
469
470 while(inString.c_str()[curPos]=='0') {
471 inString.erase(curPos, 1);
472 curPos--;
473 }
474}
475
476// return how many consecutive '0' characters are at the end of a string
477unsigned int countTrailingZeros(std::string& inString) {
478 int curPos=inString.length()-1;
479 unsigned int zeroCount=0;
480 // assert goes here
481 while(inString.c_str()[curPos--]=='0') {
482 zeroCount++;
483 }
484 return zeroCount;
485}
486
487// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
488// '0' and 'N' (-1) characters)
489void boothEncode(std::string inString, std::string& boothEncodedString) {
490
491 int curpos=0;
492 int replacements=0;
493 int lim=inString.size();
494
495 while(curpos<lim) {
496 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
497 int runlength=0;
498 std::string replaceString="N";
499
500 // find the run length
501 for(;inString[curpos+runlength]=='1';runlength++) ;
502
503 for(int i=0; i<runlength-1; i++)
504 replaceString+="0";
505 replaceString+="1";
506
507 if(runlength>1) {
508 inString.replace(curpos, runlength+1, replaceString);
509 curpos+=runlength-1;
510 } else
511 curpos++;
512 } else { // a zero, we just keep chugging along
513 curpos++;
514 }
515 }
516
517 // clean up (trim the string, reverse it and turn '1's into 'P's)
518 munchTrailingZeros(inString);
519 boothEncodedString="";
520
521 for(int i=inString.size()-1;i>=0;i--)
522 if(inString[i]=='1')
523 boothEncodedString+="P";
524 else
525 boothEncodedString+=inString[i];
526
527}
528
529struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
530 unsigned firstVal; // A
531 unsigned firstShift; // B
532 unsigned secondVal; // C
533 unsigned secondShift; // D
534 bool isSub;
535};
536
537/* this implements Lefevre's "pattern-based" constant multiplication,
538 * see "Multiplication by an Integer Constant", INRIA report 1999-06
539 *
540 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
541 * to get better booth encodings - this does help in practice
542 * TODO: weight shifts appropriately (most architectures can't
543 * fuse a shift and an add for arbitrary shift amounts) */
544unsigned lefevre(const std::string inString,
545 std::vector<struct shiftaddblob> &ops) {
546 std::string retstring;
547 std::string s = inString;
548 munchTrailingZeros(s);
549
550 int length=s.length()-1;
551
552 if(length==0) {
553 return(0);
554 }
555
556 std::vector<int> p,n;
557
558 for(int i=0; i<=length; i++) {
559 if (s.c_str()[length-i]=='P') {
560 p.push_back(i);
561 } else if (s.c_str()[length-i]=='N') {
562 n.push_back(i);
563 }
564 }
565
566 std::string t, u;
567 int c,f;
568 std::map<const int, int> w;
569
570 for(int i=0; i<p.size(); i++) {
571 for(int j=0; j<i; j++) {
572 w[p[i]-p[j]]++;
573 }
574 }
575
576 for(int i=1; i<n.size(); i++) {
577 for(int j=0; j<i; j++) {
578 w[n[i]-n[j]]++;
579 }
580 }
581
582 for(int i=0; i<p.size(); i++) {
583 for(int j=0; j<n.size(); j++) {
584 w[-abs(p[i]-n[j])]++;
585 }
586 }
587
588 std::map<const int, int>::const_iterator ii;
589 std::vector<int> d;
590 std::multimap<int, int> sorted_by_value;
591
592 for(ii = w.begin(); ii!=w.end(); ii++)
593 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
594
595 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
596 it != sorted_by_value.end(); ++it) {
597 d.push_back((*it).second);
598 }
599
600 int int_W=0;
601 int int_d;
602
603 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
604 d.pop_back();
605 retstring=s; // hmmm
606 int x=0;
607 int z=abs(int_d)-1;
608
609 if(int_d>0) {
610
611 for(int base=0; base<retstring.size(); base++) {
612 if( ((base+z+1) < retstring.size()) &&
613 retstring.c_str()[base]=='P' &&
614 retstring.c_str()[base+z+1]=='P')
615 {
616 // match
617 x++;
618 retstring.replace(base, 1, "0");
619 retstring.replace(base+z+1, 1, "p");
620 }
621 }
622
623 for(int base=0; base<retstring.size(); base++) {
624 if( ((base+z+1) < retstring.size()) &&
625 retstring.c_str()[base]=='N' &&
626 retstring.c_str()[base+z+1]=='N')
627 {
628 // match
629 x++;
630 retstring.replace(base, 1, "0");
631 retstring.replace(base+z+1, 1, "n");
632 }
633 }
634
635 } else {
636 for(int base=0; base<retstring.size(); base++) {
637 if( ((base+z+1) < retstring.size()) &&
638 ((retstring.c_str()[base]=='P' &&
639 retstring.c_str()[base+z+1]=='N') ||
640 (retstring.c_str()[base]=='N' &&
641 retstring.c_str()[base+z+1]=='P')) ) {
642 // match
643 x++;
644
645 if(retstring.c_str()[base]=='P') {
646 retstring.replace(base, 1, "0");
647 retstring.replace(base+z+1, 1, "p");
648 } else { // retstring[base]=='N'
649 retstring.replace(base, 1, "0");
650 retstring.replace(base+z+1, 1, "n");
651 }
652 }
653 }
654 }
655
656 if(x>int_W) {
657 int_W = x;
658 t = retstring;
659 c = int_d; // tofix
660 }
661
662 } d.pop_back(); // hmm
663
664 u = t;
665
666 for(int i=0; i<t.length(); i++) {
667 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
668 t.replace(i, 1, "0");
669 }
670
671 /* and now for something completely different:
672
673 //\\\\\\ ` \`..(@)
674 : \\\\ (@)(@) / /(@)
675 \ ~L~ )\\\\ \ \ '\(__``',..
676 /\_~ / |||| \ , | ~~~--/
677 ////| |//// // /
678 ||||^ ~~~~~~--------~/ /
679 / ( ( _____---~~~~~\| /
680( )| / / / /
681 \^\ \____/ / \ /
682 \ \/ \ \ /
683 )) ) ' ~
684 | | ` ,
685 |______|
686 | || | ,
687 ( || |
688 \ | | ,
689 \| /
690 /_^||
691 */
692
693 for(int i=0; i<u.length(); i++) {
694 if(u.c_str()[i]=='P' || u.c_str()[i]=='N')
695 u.replace(i, 1, "0");
696 if(u.c_str()[i]=='p')
697 u.replace(i, 1, "P");
698 if(u.c_str()[i]=='n')
699 u.replace(i, 1, "N");
700 }
701
702 if( c<0 ) {
703 f=1;
704 c=-c;
705 } else
706 f=0;
707
708 bool hit=true;
709 for(int i=0; i<u.length()-1; i++) {
710 if(u.c_str()[i]!='0')
711 hit=false;
712 }
713 if(u.c_str()[u.length()-1]!='N')
714 hit=false;
715
716 int g=0;
717 if(hit) {
718 g=1;
719 for(int p=0; p<u.length(); p++) {
720 bool isP=(u.c_str()[p]=='P');
721 bool isN=(u.c_str()[p]=='N');
722
723 if(isP)
724 u.replace(p, 1, "N");
725 if(isN)
726 u.replace(p, 1, "P");
727 }
728 }
729
730 munchLeadingZeros(u);
731
732 int i = lefevre(u, ops);
733
734 shiftaddblob blob;
735
736 blob.firstVal=i; blob.firstShift=c;
737 blob.isSub=f;
738 blob.secondVal=i; blob.secondShift=0;
739
740 ops.push_back(blob);
741
742 i = ops.size();
743
744 munchLeadingZeros(t);
745
746 if(t.length()==0)
747 return i;
748
749 if(t.c_str()[0]!='P') {
750 g=2;
751 for(int p=0; p<t.length(); p++) {
752 bool isP=(t.c_str()[p]=='P');
753 bool isN=(t.c_str()[p]=='N');
754
755 if(isP)
756 t.replace(p, 1, "N");
757 if(isN)
758 t.replace(p, 1, "P");
759 }
760 }
761
762 int j = lefevre(t, ops);
763
764 int trail=countTrailingZeros(u);
765 blob.secondVal=i; blob.secondShift=trail;
766
767 trail=countTrailingZeros(t);
768 blob.firstVal=j; blob.firstShift=trail;
769
770 switch(g) {
771 case 0:
772 blob.isSub=false; // first + second
773 break;
774 case 1:
775 blob.isSub=true; // first - second
776 break;
777 case 2:
778 blob.isSub=true; // second - first
779 int tmpval, tmpshift;
780 tmpval=blob.firstVal;
781 tmpshift=blob.firstShift;
782 blob.firstVal=blob.secondVal;
783 blob.firstShift=blob.secondShift;
784 blob.secondVal=tmpval;
785 blob.secondShift=tmpshift;
786 break;
787 //assert
788 }
789
790 ops.push_back(blob);
791 return ops.size();
792}
793
794SDOperand ISel::BuildConstmulSequence(SDOperand N) {
795 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
796 // in particular, *3 is nicer as *2+1, not *4-1
797 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
798
799 bool flippedSign;
800 unsigned preliminaryShift=0;
801
802 assert(constant > 0 && "erk, don't multiply by zero or negative nums\n");
803
804 // first, we make the constant to multiply by positive
805 if(constant<0) {
806 constant=-constant;
807 flippedSign=true;
808 } else {
809 flippedSign=false;
810 }
811
812 // next, we make it odd.
813 for(; (constant%2==0); preliminaryShift++)
814 constant>>=1;
815
816 //OK, we have a positive, odd number of 64 bits or less. Convert it
817 //to a binary string, constantString[0] is the LSB
818 char constantString[65];
819 for(int i=0; i<64; i++)
820 constantString[i]='0'+((constant>>i)&0x1);
821 constantString[64]=0;
822
823 // now, Booth encode it
824 std::string boothEncodedString;
825 boothEncode(constantString, boothEncodedString);
826
827 std::vector<struct shiftaddblob> ops;
828 // do the transformation, filling out 'ops'
829 lefevre(boothEncodedString, ops);
830
831 SDOperand results[ops.size()]; // temporary results (of adds/subs of shifts)
832
833 // now turn 'ops' into DAG bits
834 for(int i=0; i<ops.size(); i++) {
835 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
836 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
837 results[ops[i].firstVal-1];
838 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
839 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
840 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
841 results[ops[i].secondVal-1];
842 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
843 if(ops[i].isSub)
844 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
845 else
846 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
847 }
848
849 // don't forget flippedSign and preliminaryShift!
850 SDOperand finalresult;
851 if(preliminaryShift) {
852 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
853 finalresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
854 results[ops.size()-1], finalshift);
855 } else { // there was no preliminary divide-by-power-of-2 required
856 finalresult = results[ops.size()-1];
857 }
858
859 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000860}
861
Duraid Madina4826a072005-04-06 09:55:17 +0000862/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
863/// returns zero when the input is not exactly a power of two.
Duraid Madinac02780e2005-04-13 04:50:54 +0000864static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000865 if (Val == 0 || (Val & (Val-1))) return 0;
866 unsigned Count = 0;
867 while (Val != 1) {
868 Val >>= 1;
869 ++Count;
870 }
871 return Count;
872}
873
Duraid Madinac02780e2005-04-13 04:50:54 +0000874/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
875/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
876static unsigned ExactLog2sub1(uint64_t Val) {
877 unsigned int n;
878 for(n=0; n<64; n++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000879 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000880 return n;
881 }
882 return 666;
883}
884
Duraid Madina4826a072005-04-06 09:55:17 +0000885/// ponderIntegerDivisionBy - When handling integer divides, if the divide
886/// is by a constant such that we can efficiently codegen it, this
887/// function says what to do. Currently, it returns 0 if the division must
888/// become a genuine divide, and 1 if the division can be turned into a
889/// right shift.
890static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
891 unsigned& Imm) {
892 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
893 // a constant, give up.
894
895 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
896
Misha Brukman4633f1c2005-04-21 23:13:11 +0000897 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
Duraid Madina4826a072005-04-06 09:55:17 +0000898 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000899 }
900
Duraid Madina4826a072005-04-06 09:55:17 +0000901 return 0; // fallthrough
902}
903
Duraid Madinac02780e2005-04-13 04:50:54 +0000904static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
905 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
906 // a constant, give up.
907
908 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
909
910 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
911 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000912 }
913
Duraid Madinac02780e2005-04-13 04:50:54 +0000914 return 0; // fallthrough
915}
916
Duraid Madinaf55e4032005-04-07 12:33:38 +0000917static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
918 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
919 // constant, give up.
920 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
921
922 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
923 Imm = v & 0x3FFF; // 14 bits
924 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000925 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000926 return 0; // fallthrough
927}
928
929static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
930 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
931 // constant, give up.
932 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
933
934 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
935 Imm = v & 0xFF; // 8 bits
936 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000937 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000938 return 0; // fallthrough
939}
940
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000941unsigned ISel::SelectExpr(SDOperand N) {
942 unsigned Result;
943 unsigned Tmp1, Tmp2, Tmp3;
944 unsigned Opc = 0;
945 MVT::ValueType DestType = N.getValueType();
946
947 unsigned opcode = N.getOpcode();
948
949 SDNode *Node = N.Val;
950 SDOperand Op0, Op1;
951
952 if (Node->getOpcode() == ISD::CopyFromReg)
953 // Just use the specified register as our input.
954 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000955
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000956 unsigned &Reg = ExprMap[N];
957 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000959 if (N.getOpcode() != ISD::CALL)
960 Reg = Result = (N.getValueType() != MVT::Other) ?
961 MakeReg(N.getValueType()) : 1;
962 else {
963 // If this is a call instruction, make sure to prepare ALL of the result
964 // values as well as the chain.
965 if (Node->getNumValues() == 1)
966 Reg = Result = 1; // Void call, just a chain.
967 else {
968 Result = MakeReg(Node->getValueType(0));
969 ExprMap[N.getValue(0)] = Result;
970 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
971 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
972 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
973 }
974 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000976 switch (N.getOpcode()) {
977 default:
978 Node->dump();
979 assert(0 && "Node not handled!\n");
980
981 case ISD::FrameIndex: {
982 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
983 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
984 return Result;
985 }
986
987 case ISD::ConstantPool: {
988 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
989 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
990 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
991 .addReg(IA64::r1);
992 return Result;
993 }
994
995 case ISD::ConstantFP: {
996 Tmp1 = Result; // Intermediate Register
997 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
998 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
999 Tmp1 = MakeReg(MVT::f64);
1000
1001 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1002 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1003 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
1004 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1005 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1006 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
1007 else
1008 assert(0 && "Unexpected FP constant!");
1009 if (Tmp1 != Result)
1010 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
1011 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001012 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001013 return Result;
1014 }
1015
1016 case ISD::DYNAMIC_STACKALLOC: {
1017 // Generate both result values.
1018 if (Result != 1)
1019 ExprMap[N.getValue(1)] = 1; // Generate the token
1020 else
1021 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1022
1023 // FIXME: We are currently ignoring the requested alignment for handling
1024 // greater than the stack alignment. This will need to be revisited at some
1025 // point. Align = N.getOperand(2);
1026
1027 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1028 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1029 std::cerr << "Cannot allocate stack object with greater alignment than"
1030 << " the stack alignment yet!";
1031 abort();
1032 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001033
1034/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001035 Select(N.getOperand(0));
1036 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1037 {
1038 if (CN->getValue() < 32000)
1039 {
1040 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001041 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001042 } else {
1043 Tmp1 = SelectExpr(N.getOperand(1));
1044 // Subtract size from stack pointer, thereby allocating some space.
1045 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1046 }
1047 } else {
1048 Tmp1 = SelectExpr(N.getOperand(1));
1049 // Subtract size from stack pointer, thereby allocating some space.
1050 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1051 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001052*/
1053 Select(N.getOperand(0));
1054 Tmp1 = SelectExpr(N.getOperand(1));
1055 // Subtract size from stack pointer, thereby allocating some space.
1056 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001057 // Put a pointer to the space into the result register, by copying the
1058 // stack pointer.
1059 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1060 return Result;
1061 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001062
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001063 case ISD::SELECT: {
1064 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1065 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1066 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1067
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001068 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001069
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001070 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001071 default: assert(0 &&
1072 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
1073 case MVT::i64:
1074 bogoResult=MakeReg(MVT::i64);
1075 break;
1076 case MVT::f64:
1077 bogoResult=MakeReg(MVT::f64);
1078 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001079 }
Duraid Madina69c8e202005-04-01 10:35:00 +00001080
1081 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1082 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001083 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
Duraid Madina69c8e202005-04-01 10:35:00 +00001084 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001085 return Result;
1086 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001087
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001088 case ISD::Constant: {
1089 unsigned depositPos=0;
1090 unsigned depositLen=0;
1091 switch (N.getValueType()) {
1092 default: assert(0 && "Cannot use constants of this type!");
1093 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001094 // the constant:
1095 if(cast<ConstantSDNode>(N)->getValue()) // true:
1096 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1097 else // false:
1098 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1099 return Result; // early exit
1100 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001101 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001102 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001103
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001104 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001105
1106 if(immediate==0) { // if the constant is just zero,
1107 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1108 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001109 }
1110
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001111 if (immediate <= 8191 && immediate >= -8192) {
1112 // if this constants fits in 14 bits, we use a mov the assembler will
1113 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1114 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1115 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001116 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001117
1118 if (immediate <= 2097151 && immediate >= -2097152) {
1119 // if this constants fits in 22 bits, we use a mov the assembler will
1120 // turn into: "addl rDest=imm,r0"
1121 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1122 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001123 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001124
1125 /* otherwise, our immediate is big, so we use movl */
1126 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001127 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001128 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001129 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001130
1131 case ISD::UNDEF: {
1132 BuildMI(BB, IA64::IDEF, 0, Result);
1133 return Result;
1134 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001135
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001136 case ISD::GlobalAddress: {
1137 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1138 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001139
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001140 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001141 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001142
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001143 return Result;
1144 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001145
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001146 case ISD::ExternalSymbol: {
1147 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001148// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1149 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001150 return Result;
1151 }
1152
1153 case ISD::FP_EXTEND: {
1154 Tmp1 = SelectExpr(N.getOperand(0));
1155 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1156 return Result;
1157 }
1158
1159 case ISD::ZERO_EXTEND: {
1160 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001161
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001162 switch (N.getOperand(0).getValueType()) {
1163 default: assert(0 && "Cannot zero-extend this type!");
1164 case MVT::i8: Opc = IA64::ZXT1; break;
1165 case MVT::i16: Opc = IA64::ZXT2; break;
1166 case MVT::i32: Opc = IA64::ZXT4; break;
1167
Misha Brukman4633f1c2005-04-21 23:13:11 +00001168 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001169 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001170 unsigned dummy = MakeReg(MVT::i64);
1171 // first load zero:
1172 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1173 // ...then conditionally (PR:Tmp1) add 1:
1174 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1175 .addImm(1).addReg(Tmp1);
1176 return Result; // XXX early exit!
1177 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001178 }
1179
1180 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1181 return Result;
1182 }
1183
1184 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1185
1186assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1187
1188 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001189
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001190 switch (N.getOperand(0).getValueType()) {
1191 default: assert(0 && "Cannot sign-extend this type!");
1192 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001193 Opc = IA64::SXT1; break;
1194 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001195 case MVT::i8: Opc = IA64::SXT1; break;
1196 case MVT::i16: Opc = IA64::SXT2; break;
1197 case MVT::i32: Opc = IA64::SXT4; break;
1198 }
1199
1200 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1201 return Result;
1202 }
1203
1204 case ISD::TRUNCATE: {
1205 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1206 // of R0 appropriately.
1207 switch (N.getOperand(0).getValueType()) {
1208 default: assert(0 && "Unknown truncate!");
1209 case MVT::i64: break;
1210 }
1211 Tmp1 = SelectExpr(N.getOperand(0));
1212 unsigned depositPos, depositLen;
1213
1214 switch (N.getValueType()) {
1215 default: assert(0 && "Unknown truncate!");
1216 case MVT::i1: {
1217 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001218 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1219 .addReg(IA64::r0);
1220 return Result; // XXX early exit!
1221 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001222 case MVT::i8: depositPos=0; depositLen=8; break;
1223 case MVT::i16: depositPos=0; depositLen=16; break;
1224 case MVT::i32: depositPos=0; depositLen=32; break;
1225 }
1226 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1227 .addImm(depositPos).addImm(depositLen);
1228 return Result;
1229 }
1230
Misha Brukman7847fca2005-04-22 17:54:37 +00001231/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001232 case ISD::FP_ROUND: {
1233 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001234 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001235 Tmp1 = SelectExpr(N.getOperand(0));
1236 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1237 // we add 0.0 using a single precision add to do rounding
1238 return Result;
1239 }
1240*/
1241
1242// FIXME: the following 4 cases need cleaning
1243 case ISD::SINT_TO_FP: {
1244 Tmp1 = SelectExpr(N.getOperand(0));
1245 Tmp2 = MakeReg(MVT::f64);
1246 unsigned dummy = MakeReg(MVT::f64);
1247 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1248 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1249 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1250 return Result;
1251 }
1252
1253 case ISD::UINT_TO_FP: {
1254 Tmp1 = SelectExpr(N.getOperand(0));
1255 Tmp2 = MakeReg(MVT::f64);
1256 unsigned dummy = MakeReg(MVT::f64);
1257 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1258 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1259 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1260 return Result;
1261 }
1262
1263 case ISD::FP_TO_SINT: {
1264 Tmp1 = SelectExpr(N.getOperand(0));
1265 Tmp2 = MakeReg(MVT::f64);
1266 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1267 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1268 return Result;
1269 }
1270
1271 case ISD::FP_TO_UINT: {
1272 Tmp1 = SelectExpr(N.getOperand(0));
1273 Tmp2 = MakeReg(MVT::f64);
1274 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1275 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1276 return Result;
1277 }
1278
1279 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001280 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1281 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1282 // into an fma, do so:
1283 // ++FusedFP; // Statistic
1284 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1285 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1286 Tmp3 = SelectExpr(N.getOperand(1));
1287 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1288 return Result; // early exit
1289 }
Duraid Madinaed095022005-04-13 06:12:04 +00001290
1291 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001292 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001293 // this add into a shladd, try:
1294 ConstantSDNode *CSD = NULL;
1295 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001296 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001297
Misha Brukman7847fca2005-04-22 17:54:37 +00001298 // ++FusedSHLADD; // Statistic
1299 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1300 int shl_amt = CSD->getValue();
1301 Tmp3 = SelectExpr(N.getOperand(1));
1302
1303 BuildMI(BB, IA64::SHLADD, 3, Result)
1304 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1305 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001306 }
1307 }
1308
1309 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001310 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001311 if(DestType != MVT::f64) { // integer addition:
1312 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001313 case 1: // adding a constant that's 14 bits
1314 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1315 return Result; // early exit
1316 } // fallthrough and emit a reg+reg ADD:
1317 Tmp2 = SelectExpr(N.getOperand(1));
1318 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001319 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001320 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001321 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1322 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001323 return Result;
1324 }
1325
1326 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001327
1328 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001329 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1330 // boring old integer multiply with xma
1331 Tmp1 = SelectExpr(N.getOperand(0));
1332 Tmp2 = SelectExpr(N.getOperand(1));
1333
1334 unsigned TempFR1=MakeReg(MVT::f64);
1335 unsigned TempFR2=MakeReg(MVT::f64);
1336 unsigned TempFR3=MakeReg(MVT::f64);
1337 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1338 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1339 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1340 .addReg(IA64::F0);
1341 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1342 return Result; // early exit
1343 } else { // we are multiplying by an integer constant! yay
1344 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1345 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001346 }
Duraid Madinab2322562005-04-26 07:23:02 +00001347 else { // floating point multiply
1348 Tmp1 = SelectExpr(N.getOperand(0));
1349 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001350 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001351 return Result;
1352 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001353 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001354
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001355 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001356 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1357 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1358 // into an fms, do so:
1359 // ++FusedFP; // Statistic
1360 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1361 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1362 Tmp3 = SelectExpr(N.getOperand(1));
1363 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1364 return Result; // early exit
1365 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001366 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001367 if(DestType != MVT::f64) { // integer subtraction:
1368 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001369 case 1: // subtracting *from* an 8 bit constant:
1370 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1371 return Result; // early exit
1372 } // fallthrough and emit a reg+reg SUB:
1373 Tmp1 = SelectExpr(N.getOperand(0));
1374 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001375 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001376 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001377 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001378 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001379 return Result;
1380 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001381
1382 case ISD::FABS: {
1383 Tmp1 = SelectExpr(N.getOperand(0));
1384 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1385 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1386 return Result;
1387 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001388
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001389 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001390 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001391
Misha Brukman4633f1c2005-04-21 23:13:11 +00001392 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001393 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1394 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1395 } else {
1396 Tmp1 = SelectExpr(N.getOperand(0));
1397 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1398 }
1399
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001400 return Result;
1401 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001402
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001403 case ISD::AND: {
1404 switch (N.getValueType()) {
1405 default: assert(0 && "Cannot AND this type!");
1406 case MVT::i1: { // if a bool, we emit a pseudocode AND
1407 unsigned pA = SelectExpr(N.getOperand(0));
1408 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001409
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001410/* our pseudocode for AND is:
1411 *
1412(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1413 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1414 ;;
1415(pB) cmp.ne pTemp,p0 = r0,r0
1416 ;;
1417(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1418
1419*/
1420 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001421
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001422 unsigned bogusTemp1 = MakeReg(MVT::i1);
1423 unsigned bogusTemp2 = MakeReg(MVT::i1);
1424 unsigned bogusTemp3 = MakeReg(MVT::i1);
1425 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001426
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001427 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001428 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001429 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001430 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001431 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001432 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001433 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001434 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001435 break;
1436 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001437
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001438 // if not a bool, we just AND away:
1439 case MVT::i8:
1440 case MVT::i16:
1441 case MVT::i32:
1442 case MVT::i64: {
1443 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001444 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1445 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001446 switch (Tmp3) {
1447 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1448 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1449 break;
1450 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1451 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1452 break;
1453 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1454 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1455 break;
1456 default: // otherwise, use dep.z to paste zeros
1457 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1458 .addImm(0).addImm(Tmp3);
1459 break;
1460 }
1461 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001462 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001463 Tmp2 = SelectExpr(N.getOperand(1));
1464 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001465 }
1466 }
1467 return Result;
1468 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001470 case ISD::OR: {
1471 switch (N.getValueType()) {
1472 default: assert(0 && "Cannot OR this type!");
1473 case MVT::i1: { // if a bool, we emit a pseudocode OR
1474 unsigned pA = SelectExpr(N.getOperand(0));
1475 unsigned pB = SelectExpr(N.getOperand(1));
1476
1477 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001479/* our pseudocode for OR is:
1480 *
1481
1482pC = pA OR pB
1483-------------
1484
Misha Brukman7847fca2005-04-22 17:54:37 +00001485(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1486 ;;
1487(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001488
1489*/
1490 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001491 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001492 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001493 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001494 break;
1495 }
1496 // if not a bool, we just OR away:
1497 case MVT::i8:
1498 case MVT::i16:
1499 case MVT::i32:
1500 case MVT::i64: {
1501 Tmp1 = SelectExpr(N.getOperand(0));
1502 Tmp2 = SelectExpr(N.getOperand(1));
1503 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1504 break;
1505 }
1506 }
1507 return Result;
1508 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001509
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001510 case ISD::XOR: {
1511 switch (N.getValueType()) {
1512 default: assert(0 && "Cannot XOR this type!");
1513 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1514 unsigned pY = SelectExpr(N.getOperand(0));
1515 unsigned pZ = SelectExpr(N.getOperand(1));
1516
1517/* one possible routine for XOR is:
1518
1519 // Compute px = py ^ pz
1520 // using sum of products: px = (py & !pz) | (pz & !py)
1521 // Uses 5 instructions in 3 cycles.
1522 // cycle 1
1523(pz) cmp.eq.unc px = r0, r0 // px = pz
1524(py) cmp.eq.unc pt = r0, r0 // pt = py
1525 ;;
1526 // cycle 2
1527(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1528(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1529 ;;
1530 } { .mmi
1531 // cycle 3
1532(pt) cmp.eq.or px = r0, r0 // px = px | pt
1533
1534*** Another, which we use here, requires one scratch GR. it is:
1535
1536 mov rt = 0 // initialize rt off critical path
1537 ;;
1538
1539 // cycle 1
1540(pz) cmp.eq.unc px = r0, r0 // px = pz
1541(pz) mov rt = 1 // rt = pz
1542 ;;
1543 // cycle 2
1544(py) cmp.ne px = 1, rt // if (py) px = !pz
1545
1546.. these routines kindly provided by Jim Hull
1547*/
1548 unsigned rt = MakeReg(MVT::i64);
1549
1550 // these two temporaries will never actually appear,
1551 // due to the two-address form of some of the instructions below
1552 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1553 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1554
1555 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1556 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001557 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001558 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001559 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001560 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001561 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001562 break;
1563 }
1564 // if not a bool, we just XOR away:
1565 case MVT::i8:
1566 case MVT::i16:
1567 case MVT::i32:
1568 case MVT::i64: {
1569 Tmp1 = SelectExpr(N.getOperand(0));
1570 Tmp2 = SelectExpr(N.getOperand(1));
1571 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1572 break;
1573 }
1574 }
1575 return Result;
1576 }
1577
1578 case ISD::SHL: {
1579 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001580 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1581 Tmp2 = CN->getValue();
1582 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1583 } else {
1584 Tmp2 = SelectExpr(N.getOperand(1));
1585 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1586 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001587 return Result;
1588 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001589
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001590 case ISD::SRL: {
1591 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001592 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1593 Tmp2 = CN->getValue();
1594 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1595 } else {
1596 Tmp2 = SelectExpr(N.getOperand(1));
1597 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1598 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001599 return Result;
1600 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001601
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001602 case ISD::SRA: {
1603 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001604 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1605 Tmp2 = CN->getValue();
1606 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1607 } else {
1608 Tmp2 = SelectExpr(N.getOperand(1));
1609 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1610 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001611 return Result;
1612 }
1613
1614 case ISD::SDIV:
1615 case ISD::UDIV:
1616 case ISD::SREM:
1617 case ISD::UREM: {
1618
1619 Tmp1 = SelectExpr(N.getOperand(0));
1620 Tmp2 = SelectExpr(N.getOperand(1));
1621
1622 bool isFP=false;
1623
1624 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1625 isFP=true;
1626
1627 bool isModulus=false; // is it a division or a modulus?
1628 bool isSigned=false;
1629
1630 switch(N.getOpcode()) {
1631 case ISD::SDIV: isModulus=false; isSigned=true; break;
1632 case ISD::UDIV: isModulus=false; isSigned=false; break;
1633 case ISD::SREM: isModulus=true; isSigned=true; break;
1634 case ISD::UREM: isModulus=true; isSigned=false; break;
1635 }
1636
Duraid Madina4826a072005-04-06 09:55:17 +00001637 if(!isModulus && !isFP) { // if this is an integer divide,
1638 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001639 case 1: // division by a constant that's a power of 2
1640 Tmp1 = SelectExpr(N.getOperand(0));
1641 if(isSigned) { // argument could be negative, so emit some code:
1642 unsigned divAmt=Tmp3;
1643 unsigned tempGR1=MakeReg(MVT::i64);
1644 unsigned tempGR2=MakeReg(MVT::i64);
1645 unsigned tempGR3=MakeReg(MVT::i64);
1646 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1647 .addReg(Tmp1).addImm(divAmt-1);
1648 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1649 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1650 BuildMI(BB, IA64::ADD, 2, tempGR3)
1651 .addReg(Tmp1).addReg(tempGR2);
1652 BuildMI(BB, IA64::SHRS, 2, Result)
1653 .addReg(tempGR3).addImm(divAmt);
1654 }
1655 else // unsigned div-by-power-of-2 becomes a simple shift right:
1656 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1657 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001658 }
1659 }
1660
Misha Brukman4633f1c2005-04-21 23:13:11 +00001661 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001662 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001663 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1664 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1665 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1666 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1667 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1668 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1669 unsigned TmpF7=MakeReg(MVT::f64);
1670 unsigned TmpF8=MakeReg(MVT::f64);
1671 unsigned TmpF9=MakeReg(MVT::f64);
1672 unsigned TmpF10=MakeReg(MVT::f64);
1673 unsigned TmpF11=MakeReg(MVT::f64);
1674 unsigned TmpF12=MakeReg(MVT::f64);
1675 unsigned TmpF13=MakeReg(MVT::f64);
1676 unsigned TmpF14=MakeReg(MVT::f64);
1677 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001678
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001679 // OK, emit some code:
1680
1681 if(!isFP) {
1682 // first, load the inputs into FP regs.
1683 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1684 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001685
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001686 // next, convert the inputs to FP
1687 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001688 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1689 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001690 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001691 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1692 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001693 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001694
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001695 } else { // this is an FP divide/remainder, so we 'leak' some temp
1696 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1697 TmpF3=Tmp1;
1698 TmpF4=Tmp2;
1699 }
1700
1701 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001702 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1703 BuildMI(BB, IA64::FRCPAS1, 4)
1704 .addReg(TmpF5, MachineOperand::Def)
1705 .addReg(TmpPR, MachineOperand::Def)
1706 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001707
Duraid Madinabeeaab22005-03-31 12:31:11 +00001708 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1709 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1710 // TPCMPNE below
1711 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1712 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001713 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001714 }
1715
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001716 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1717 // precision, don't need this much for f32/i32)
1718 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1719 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1720 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1721 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1722 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1723 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1724 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1725 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1726 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1727 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1728 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1729 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1730 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1731 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1732 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1733 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001734
1735 // FIXME: this is unfortunate :(
1736 // the story is that the dest reg of the fnma above and the fma below
1737 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1738 // be the same register, or this code breaks if the first argument is
1739 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001740 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1741 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1742
Duraid Madina6e02e682005-04-04 05:05:52 +00001743 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1744 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1745 }
1746
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001747 if(!isFP) {
1748 // round to an integer
1749 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001750 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001751 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001752 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001753 } else {
1754 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1755 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1756 // we really do need the above FMOV? ;)
1757 }
1758
1759 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001760 if(isFP) { // extra worrying about div-by-zero
1761 unsigned bogoResult=MakeReg(MVT::f64);
1762
1763 // we do a 'conditional fmov' (of the correct result, depending
1764 // on how the frcpa predicate turned out)
1765 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001767 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001768 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001769 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001770 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001771 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001772 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001773 } else { // this is a modulus
1774 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001775 // answer = q * (-b) + a
1776 unsigned ModulusResult = MakeReg(MVT::f64);
1777 unsigned TmpF = MakeReg(MVT::f64);
1778 unsigned TmpI = MakeReg(MVT::i64);
1779
1780 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1781 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1782 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1783 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1784 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001785 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001786 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001787 }
1788 }
1789
1790 return Result;
1791 }
1792
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001793 case ISD::SIGN_EXTEND_INREG: {
1794 Tmp1 = SelectExpr(N.getOperand(0));
1795 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1796 switch(MVN->getExtraValueType())
1797 {
1798 default:
1799 Node->dump();
1800 assert(0 && "don't know how to sign extend this type");
1801 break;
1802 case MVT::i8: Opc = IA64::SXT1; break;
1803 case MVT::i16: Opc = IA64::SXT2; break;
1804 case MVT::i32: Opc = IA64::SXT4; break;
1805 }
1806 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1807 return Result;
1808 }
1809
1810 case ISD::SETCC: {
1811 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001812
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001813 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1814 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001815
Misha Brukman7847fca2005-04-22 17:54:37 +00001816 if(ConstantSDNode *CSDN =
1817 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1818 // if we are comparing against a constant zero
1819 if(CSDN->getValue()==0)
1820 Tmp2 = IA64::r0; // then we can just compare against r0
1821 else
1822 Tmp2 = SelectExpr(N.getOperand(1));
1823 } else // not comparing against a constant
1824 Tmp2 = SelectExpr(N.getOperand(1));
1825
1826 switch (SetCC->getCondition()) {
1827 default: assert(0 && "Unknown integer comparison!");
1828 case ISD::SETEQ:
1829 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1830 break;
1831 case ISD::SETGT:
1832 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 break;
1834 case ISD::SETGE:
1835 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 break;
1837 case ISD::SETLT:
1838 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 break;
1840 case ISD::SETLE:
1841 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 break;
1843 case ISD::SETNE:
1844 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1845 break;
1846 case ISD::SETULT:
1847 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1848 break;
1849 case ISD::SETUGT:
1850 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1851 break;
1852 case ISD::SETULE:
1853 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1854 break;
1855 case ISD::SETUGE:
1856 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 break;
1858 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001859 }
1860 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001861 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1862 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001863
Misha Brukman7847fca2005-04-22 17:54:37 +00001864 if(ConstantFPSDNode *CFPSDN =
1865 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001866
Misha Brukman7847fca2005-04-22 17:54:37 +00001867 // if we are comparing against a constant +0.0 or +1.0
1868 if(CFPSDN->isExactlyValue(+0.0))
1869 Tmp2 = IA64::F0; // then we can just compare against f0
1870 else if(CFPSDN->isExactlyValue(+1.0))
1871 Tmp2 = IA64::F1; // or f1
1872 else
1873 Tmp2 = SelectExpr(N.getOperand(1));
1874 } else // not comparing against a constant
1875 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001876
Misha Brukman7847fca2005-04-22 17:54:37 +00001877 switch (SetCC->getCondition()) {
1878 default: assert(0 && "Unknown FP comparison!");
1879 case ISD::SETEQ:
1880 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETGT:
1883 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETGE:
1886 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
1888 case ISD::SETLT:
1889 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1890 break;
1891 case ISD::SETLE:
1892 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1893 break;
1894 case ISD::SETNE:
1895 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1896 break;
1897 case ISD::SETULT:
1898 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1899 break;
1900 case ISD::SETUGT:
1901 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1902 break;
1903 case ISD::SETULE:
1904 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1905 break;
1906 case ISD::SETUGE:
1907 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1908 break;
1909 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001910 }
1911 }
1912 else
1913 assert(0 && "this setcc not implemented yet");
1914
1915 return Result;
1916 }
1917
1918 case ISD::EXTLOAD:
1919 case ISD::ZEXTLOAD:
1920 case ISD::LOAD: {
1921 // Make sure we generate both values.
1922 if (Result != 1)
1923 ExprMap[N.getValue(1)] = 1; // Generate the token
1924 else
1925 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1926
1927 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001928
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001929 if(opcode == ISD::LOAD) { // this is a LOAD
1930 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001931 default: assert(0 && "Cannot load this type!");
1932 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1933 // FIXME: for now, we treat bool loads the same as i8 loads */
1934 case MVT::i8: Opc = IA64::LD1; break;
1935 case MVT::i16: Opc = IA64::LD2; break;
1936 case MVT::i32: Opc = IA64::LD4; break;
1937 case MVT::i64: Opc = IA64::LD8; break;
1938
1939 case MVT::f32: Opc = IA64::LDF4; break;
1940 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001941 }
1942 } else { // this is an EXTLOAD or ZEXTLOAD
1943 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1944 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001945 default: assert(0 && "Cannot extload/zextload this type!");
1946 // FIXME: bools?
1947 case MVT::i8: Opc = IA64::LD1; break;
1948 case MVT::i16: Opc = IA64::LD2; break;
1949 case MVT::i32: Opc = IA64::LD4; break;
1950 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001951 }
1952 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001953
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001954 SDOperand Chain = N.getOperand(0);
1955 SDOperand Address = N.getOperand(1);
1956
1957 if(Address.getOpcode() == ISD::GlobalAddress) {
1958 Select(Chain);
1959 unsigned dummy = MakeReg(MVT::i64);
1960 unsigned dummy2 = MakeReg(MVT::i64);
1961 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001962 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1963 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001964 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1965 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001966 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001967 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001968 // into a predicate register
1969 assert(Opc==IA64::LD1 && "problem loading a bool");
1970 unsigned dummy3 = MakeReg(MVT::i64);
1971 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1972 // we compare to 0. true? 0. false? 1.
1973 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001974 }
1975 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1976 Select(Chain);
1977 IA64Lowering.restoreGP(BB);
1978 unsigned dummy = MakeReg(MVT::i64);
1979 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001980 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001981 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001982 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001983 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001984 // into a predicate register
1985 assert(Opc==IA64::LD1 && "problem loading a bool");
1986 unsigned dummy3 = MakeReg(MVT::i64);
1987 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1988 // we compare to 0. true? 0. false? 1.
1989 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001990 }
1991 } else if(Address.getOpcode() == ISD::FrameIndex) {
1992 Select(Chain); // FIXME ? what about bools?
1993 unsigned dummy = MakeReg(MVT::i64);
1994 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001995 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001996 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001997 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001998 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001999 // into a predicate register
2000 assert(Opc==IA64::LD1 && "problem loading a bool");
2001 unsigned dummy3 = MakeReg(MVT::i64);
2002 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2003 // we compare to 0. true? 0. false? 1.
2004 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002005 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002006 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002007 Select(Chain);
2008 Tmp2 = SelectExpr(Address);
2009 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002010 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002011 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002012 // into a predicate register
2013 assert(Opc==IA64::LD1 && "problem loading a bool");
2014 unsigned dummy = MakeReg(MVT::i64);
2015 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2016 // we compare to 0. true? 0. false? 1.
2017 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
2018 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002019 }
2020
2021 return Result;
2022 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002023
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002024 case ISD::CopyFromReg: {
2025 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002026 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002027 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002028
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002029 SDOperand Chain = N.getOperand(0);
2030
2031 Select(Chain);
2032 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2033
2034 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002035 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2036 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002037 // (r) Result =cmp.eq.unc(r0,r0)
2038 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002039 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002040 return Result;
2041 }
2042
2043 case ISD::CALL: {
2044 Select(N.getOperand(0));
2045
2046 // The chain for this call is now lowered.
2047 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002048
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002049 //grab the arguments
2050 std::vector<unsigned> argvregs;
2051
2052 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002053 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002054
2055 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002056 // Runtime Architecture Guide to see some examples of what's going
2057 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2058 // while FP args get mapped to F8->F15 as needed)
2059
2060 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002061
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002062 // in reg args
2063 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2064 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002065 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2066 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2067 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2068 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002069
Misha Brukman7847fca2005-04-22 17:54:37 +00002070 switch(N.getOperand(i+2).getValueType())
2071 {
2072 default: // XXX do we need to support MVT::i1 here?
2073 Node->dump();
2074 N.getOperand(i).Val->dump();
2075 std::cerr << "Type for " << i << " is: " <<
2076 N.getOperand(i+2).getValueType() << std::endl;
2077 assert(0 && "Unknown value type for call");
2078 case MVT::i64:
2079 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2080 break;
2081 case MVT::f64:
2082 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2083 .addReg(argvregs[i]);
2084 // FIXME: we don't need to do this _all_ the time:
2085 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2086 break;
2087 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002088 }
2089
2090 //in mem args
2091 for (int i = 8, e = argvregs.size(); i < e; ++i)
2092 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002093 unsigned tempAddr = MakeReg(MVT::i64);
2094
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002095 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002096 default:
2097 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002098 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002099 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002100 N.getOperand(i+2).getValueType() << "\n";
2101 assert(0 && "Unknown value type for call");
2102 case MVT::i1: // FIXME?
2103 case MVT::i8:
2104 case MVT::i16:
2105 case MVT::i32:
2106 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002107 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2108 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2109 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002110 break;
2111 case MVT::f32:
2112 case MVT::f64:
2113 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002114 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2115 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002116 break;
2117 }
2118 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002119
2120 /* XXX we want to re-enable direct branches! crippling them now
Misha Brukman4633f1c2005-04-21 23:13:11 +00002121 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002122 //build the right kind of call
2123 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002124 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002125 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002126 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2127 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002128 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129 ^^^^^^^^^^^^^ we want this code one day XXX */
Duraid Madinabeeaab22005-03-31 12:31:11 +00002130 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002131 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002132 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002133 // "non-pic code with imm relocation against dynamic symbol" errors
2134 BuildMI(BB, IA64::BRCALL, 1)
2135 .addExternalSymbol(ESSDN->getSymbol(), true);
2136 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002137 }
2138 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002139 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002140
2141 unsigned targetEntryPoint=MakeReg(MVT::i64);
2142 unsigned targetGPAddr=MakeReg(MVT::i64);
2143 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002144
Duraid Madinabeeaab22005-03-31 12:31:11 +00002145 // b6 is a scratch branch register, we load the target entry point
2146 // from the base of the function descriptor
2147 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2148 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2149
2150 // save the current GP:
2151 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002152
Duraid Madinabeeaab22005-03-31 12:31:11 +00002153 /* TODO: we need to make sure doing this never, ever loads a
2154 * bogus value into r1 (GP). */
2155 // load the target GP (which is at mem[functiondescriptor+8])
2156 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002157 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002158 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2159
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002160 // and then jump: (well, call)
2161 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002162 // and finally restore the old GP
2163 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2164 IA64Lowering.restoreSP_RP(BB);
2165 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002166
2167 switch (Node->getValueType(0)) {
2168 default: assert(0 && "Unknown value type for call result!");
2169 case MVT::Other: return 1;
2170 case MVT::i1:
2171 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002172 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002173 break;
2174 case MVT::i8:
2175 case MVT::i16:
2176 case MVT::i32:
2177 case MVT::i64:
2178 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2179 break;
2180 case MVT::f64:
2181 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2182 break;
2183 }
2184 return Result+N.ResNo;
2185 }
2186
Misha Brukman4633f1c2005-04-21 23:13:11 +00002187 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002188 return 0;
2189}
2190
2191void ISel::Select(SDOperand N) {
2192 unsigned Tmp1, Tmp2, Opc;
2193 unsigned opcode = N.getOpcode();
2194
Nate Begeman85fdeb22005-03-24 04:39:54 +00002195 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002196 return; // Already selected.
2197
2198 SDNode *Node = N.Val;
2199
2200 switch (Node->getOpcode()) {
2201 default:
2202 Node->dump(); std::cerr << "\n";
2203 assert(0 && "Node not handled yet!");
2204
2205 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002206
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002207 case ISD::TokenFactor: {
2208 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2209 Select(Node->getOperand(i));
2210 return;
2211 }
2212
2213 case ISD::CopyToReg: {
2214 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002215 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002216 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002217
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002218 if (Tmp1 != Tmp2) {
2219 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002220 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2221 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002222 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2223 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002224 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002225 // XXX is this the right way 'round? ;)
2226 }
2227 return;
2228 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002229
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002230 case ISD::RET: {
2231
2232 /* what the heck is going on here:
2233
2234<_sabre_> ret with two operands is obvious: chain and value
2235<camel_> yep
2236<_sabre_> ret with 3 values happens when 'expansion' occurs
2237<_sabre_> e.g. i64 gets split into 2x i32
2238<camel_> oh right
2239<_sabre_> you don't have this case on ia64
2240<camel_> yep
2241<_sabre_> so the two returned values go into EAX/EDX on ia32
2242<camel_> ahhh *memories*
2243<_sabre_> :)
2244<camel_> ok, thanks :)
2245<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2246<_sabre_> this is the first operand always
2247<_sabre_> these operand often define chains, they are the last operand
2248<_sabre_> they are printed as 'ch' if you do DAG.dump()
2249 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002250
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002251 switch (N.getNumOperands()) {
2252 default:
2253 assert(0 && "Unknown return instruction!");
2254 case 2:
2255 Select(N.getOperand(0));
2256 Tmp1 = SelectExpr(N.getOperand(1));
2257 switch (N.getOperand(1).getValueType()) {
2258 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002259 // FIXME: do I need to add support for bools here?
2260 // (return '0' or '1' r8, basically...)
2261 //
2262 // FIXME: need to round floats - 80 bits is bad, the tester
2263 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002264 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002265 // we mark r8 as live on exit up above in LowerArguments()
2266 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2267 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002268 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002269 // we mark F8 as live on exit up above in LowerArguments()
2270 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002271 }
2272 break;
2273 case 1:
2274 Select(N.getOperand(0));
2275 break;
2276 }
2277 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2278 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2279 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2280 return;
2281 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002282
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002283 case ISD::BR: {
2284 Select(N.getOperand(0));
2285 MachineBasicBlock *Dest =
2286 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2287 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2288 // XXX HACK! we do _not_ need long branches all the time
2289 return;
2290 }
2291
2292 case ISD::ImplicitDef: {
2293 Select(N.getOperand(0));
2294 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2295 return;
2296 }
2297
2298 case ISD::BRCOND: {
2299 MachineBasicBlock *Dest =
2300 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2301
2302 Select(N.getOperand(0));
2303 Tmp1 = SelectExpr(N.getOperand(1));
2304 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2305 // XXX HACK! we do _not_ need long branches all the time
2306 return;
2307 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002308
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002309 case ISD::EXTLOAD:
2310 case ISD::ZEXTLOAD:
2311 case ISD::SEXTLOAD:
2312 case ISD::LOAD:
2313 case ISD::CALL:
2314 case ISD::CopyFromReg:
2315 case ISD::DYNAMIC_STACKALLOC:
2316 SelectExpr(N);
2317 return;
2318
2319 case ISD::TRUNCSTORE:
2320 case ISD::STORE: {
2321 Select(N.getOperand(0));
2322 Tmp1 = SelectExpr(N.getOperand(1)); // value
2323
2324 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002325
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002326 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002327 switch (N.getOperand(1).getValueType()) {
2328 default: assert(0 && "Cannot store this type!");
2329 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2330 // FIXME?: for now, we treat bool loads the same as i8 stores */
2331 case MVT::i8: Opc = IA64::ST1; break;
2332 case MVT::i16: Opc = IA64::ST2; break;
2333 case MVT::i32: Opc = IA64::ST4; break;
2334 case MVT::i64: Opc = IA64::ST8; break;
2335
2336 case MVT::f32: Opc = IA64::STF4; break;
2337 case MVT::f64: Opc = IA64::STF8; break;
2338 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002339 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002340 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2341 default: assert(0 && "unknown type in truncstore");
2342 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2343 //FIXME: DAG does not promote this load?
2344 case MVT::i8: Opc = IA64::ST1; break;
2345 case MVT::i16: Opc = IA64::ST2; break;
2346 case MVT::i32: Opc = IA64::ST4; break;
2347 case MVT::f32: Opc = IA64::STF4; break;
2348 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002349 }
2350
2351 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002352 unsigned dummy = MakeReg(MVT::i64);
2353 unsigned dummy2 = MakeReg(MVT::i64);
2354 BuildMI(BB, IA64::ADD, 2, dummy)
2355 .addGlobalAddress(cast<GlobalAddressSDNode>
2356 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2357 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002358
Misha Brukman7847fca2005-04-22 17:54:37 +00002359 if(!isBool)
2360 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2361 else { // we are storing a bool, so emit a little pseudocode
2362 // to store a predicate register as one byte
2363 assert(Opc==IA64::ST1);
2364 unsigned dummy3 = MakeReg(MVT::i64);
2365 unsigned dummy4 = MakeReg(MVT::i64);
2366 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2367 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2368 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2369 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2370 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002371 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2372
Misha Brukman7847fca2005-04-22 17:54:37 +00002373 // FIXME? (what about bools?)
2374
2375 unsigned dummy = MakeReg(MVT::i64);
2376 BuildMI(BB, IA64::MOV, 1, dummy)
2377 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2378 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002379 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002380 Tmp2 = SelectExpr(N.getOperand(2)); //address
2381 if(!isBool)
2382 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2383 else { // we are storing a bool, so emit a little pseudocode
2384 // to store a predicate register as one byte
2385 assert(Opc==IA64::ST1);
2386 unsigned dummy3 = MakeReg(MVT::i64);
2387 unsigned dummy4 = MakeReg(MVT::i64);
2388 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2389 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2390 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2391 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2392 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002393 }
2394 return;
2395 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002396
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002397 case ISD::ADJCALLSTACKDOWN:
2398 case ISD::ADJCALLSTACKUP: {
2399 Select(N.getOperand(0));
2400 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002401
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002402 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
2403 IA64::ADJUSTCALLSTACKUP;
2404 BuildMI(BB, Opc, 1).addImm(Tmp1);
2405 return;
2406 }
2407
2408 return;
2409 }
2410 assert(0 && "GAME OVER. INSERT COIN?");
2411}
2412
2413
2414/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2415/// into a machine code representation using pattern matching and a machine
2416/// description file.
2417///
2418FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002419 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002420}
2421
2422