blob: 1fe9026ed0fa4ffa3b12a0c58538429e97ce066e [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
Chris Lattner17234b72005-04-30 04:26:06 +000084 // We don't support sin/cos/sqrt
85 setOperationAction(ISD::FSIN , MVT::f64, Expand);
86 setOperationAction(ISD::FCOS , MVT::f64, Expand);
87 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
88 setOperationAction(ISD::FSIN , MVT::f32, Expand);
89 setOperationAction(ISD::FCOS , MVT::f32, Expand);
90 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
91
Duraid Madina9b9d45f2005-03-17 18:17:03 +000092 computeRegisterProperties();
93
94 addLegalFPImmediate(+0.0);
95 addLegalFPImmediate(+1.0);
96 addLegalFPImmediate(-0.0);
97 addLegalFPImmediate(-1.0);
98 }
99
100 /// LowerArguments - This hook must be implemented to indicate how we should
101 /// lower the arguments for the specified function, into the specified DAG.
102 virtual std::vector<SDOperand>
103 LowerArguments(Function &F, SelectionDAG &DAG);
104
105 /// LowerCallTo - This hook lowers an abstract call to a function into an
106 /// actual call.
107 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000108 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
109 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000110
111 virtual std::pair<SDOperand, SDOperand>
112 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
113
114 virtual std::pair<SDOperand,SDOperand>
115 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
116 const Type *ArgTy, SelectionDAG &DAG);
117
118 virtual std::pair<SDOperand, SDOperand>
119 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
120 SelectionDAG &DAG);
121
122 void restoreGP_SP_RP(MachineBasicBlock* BB)
123 {
124 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
125 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
126 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
127 }
128
Duraid Madinabeeaab22005-03-31 12:31:11 +0000129 void restoreSP_RP(MachineBasicBlock* BB)
130 {
131 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
132 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
133 }
134
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000135 void restoreRP(MachineBasicBlock* BB)
136 {
137 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
138 }
139
140 void restoreGP(MachineBasicBlock* BB)
141 {
142 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
143 }
144
145 };
146}
147
148
149std::vector<SDOperand>
150IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
151 std::vector<SDOperand> ArgValues;
152
153 //
154 // add beautiful description of IA64 stack frame format
155 // here (from intel 24535803.pdf most likely)
156 //
157 MachineFunction &MF = DAG.getMachineFunction();
158 MachineFrameInfo *MFI = MF.getFrameInfo();
159
160 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
161 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
162 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
163
164 MachineBasicBlock& BB = MF.front();
165
Misha Brukman4633f1c2005-04-21 23:13:11 +0000166 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000167 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000168
169 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000170 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000171
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000172 unsigned argVreg[8];
173 unsigned argPreg[8];
174 unsigned argOpc[8];
175
Duraid Madinabeeaab22005-03-31 12:31:11 +0000176 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000177
Duraid Madinabeeaab22005-03-31 12:31:11 +0000178 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000179 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000180
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000181 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000182 {
183 SDOperand newroot, argt;
184 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000185
186 switch (getValueType(I->getType())) {
187 default:
188 std::cerr << "ERROR in LowerArgs: unknown type "
189 << getValueType(I->getType()) << "\n";
190 abort();
191 case MVT::f32:
192 // fixme? (well, will need to for weird FP structy stuff,
193 // see intel ABI docs)
194 case MVT::f64:
195//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
196 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
197 // floating point args go into f8..f15 as-needed, the increment
198 argVreg[count] = // is below..:
199 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
200 // FP args go into f8..f15 as needed: (hence the ++)
201 argPreg[count] = args_FP[used_FPArgs++];
202 argOpc[count] = IA64::FMOV;
203 argt = newroot = DAG.getCopyFromReg(argVreg[count],
204 getValueType(I->getType()), DAG.getRoot());
205 break;
206 case MVT::i1: // NOTE: as far as C abi stuff goes,
207 // bools are just boring old ints
208 case MVT::i8:
209 case MVT::i16:
210 case MVT::i32:
211 case MVT::i64:
212//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
213 MF.addLiveIn(args_int[count]); // mark this register as liveIn
214 argVreg[count] =
215 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
216 argPreg[count] = args_int[count];
217 argOpc[count] = IA64::MOV;
218 argt = newroot =
219 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
220 if ( getValueType(I->getType()) != MVT::i64)
221 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
222 newroot);
223 break;
224 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000225 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000226 // Create the frame index object for this incoming parameter...
227 ArgOffset = 16 + 8 * (count - 8);
228 int FI = MFI->CreateFixedObject(8, ArgOffset);
229
230 // Create the SelectionDAG nodes corresponding to a load
231 //from this parameter
232 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
233 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000234 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000235 }
236 ++count;
237 DAG.setRoot(newroot.getValue(1));
238 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000240
Misha Brukman4633f1c2005-04-21 23:13:11 +0000241
Duraid Madinabeeaab22005-03-31 12:31:11 +0000242 // Create a vreg to hold the output of (what will become)
243 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000244 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
245 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
246 // we create a PSEUDO_ALLOC (pseudo)instruction for now
247
248 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
249
250 // hmm:
251 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
252 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
253 // ..hmm.
254
255 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
256
257 // hmm:
258 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
259 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
260 // ..hmm.
261
Duraid Madinabeeaab22005-03-31 12:31:11 +0000262 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000263
Duraid Madinabeeaab22005-03-31 12:31:11 +0000264 // if this is a varargs function, we simply lower llvm.va_start by
265 // pointing to the first entry
266 if(F.isVarArg()) {
267 tempOffset=0;
268 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000269 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000270
Duraid Madinabeeaab22005-03-31 12:31:11 +0000271 // here we actually do the moving of args, and store them to the stack
272 // too if this is a varargs function:
273 for (int i = 0; i < count && i < 8; ++i) {
274 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
275 if(F.isVarArg()) {
276 // if this is a varargs function, we copy the input registers to the stack
277 int FI = MFI->CreateFixedObject(8, tempOffset);
278 tempOffset+=8; //XXX: is it safe to use r22 like this?
279 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
280 // FIXME: we should use st8.spill here, one day
281 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
282 }
283 }
284
Duraid Madinaca494fd2005-04-12 14:54:44 +0000285 // Finally, inform the code generator which regs we return values in.
286 // (see the ISD::RET: case down below)
287 switch (getValueType(F.getReturnType())) {
288 default: assert(0 && "i have no idea where to return this type!");
289 case MVT::isVoid: break;
290 case MVT::i1:
291 case MVT::i8:
292 case MVT::i16:
293 case MVT::i32:
294 case MVT::i64:
295 MF.addLiveOut(IA64::r8);
296 break;
297 case MVT::f32:
298 case MVT::f64:
299 MF.addLiveOut(IA64::F8);
300 break;
301 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000302
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000303 return ArgValues;
304}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000305
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000306std::pair<SDOperand, SDOperand>
307IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000308 const Type *RetTy, bool isVarArg,
309 SDOperand Callee, ArgListTy &Args,
310 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000311
312 MachineFunction &MF = DAG.getMachineFunction();
313
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000314 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000315 unsigned outRegsUsed = 0;
316
317 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000318 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000319 outRegsUsed = 8;
320 } else {
321 outRegsUsed = Args.size();
322 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000323
Duraid Madinabeeaab22005-03-31 12:31:11 +0000324 // FIXME? this WILL fail if we ever try to pass around an arg that
325 // consumes more than a single output slot (a 'real' double, int128
326 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
327 // registers we use. Hopefully, the assembler will notice.
328 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
329 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000330
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000331 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
332 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000333
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000334 std::vector<SDOperand> args_to_use;
335 for (unsigned i = 0, e = Args.size(); i != e; ++i)
336 {
337 switch (getValueType(Args[i].second)) {
338 default: assert(0 && "unexpected argument type!");
339 case MVT::i1:
340 case MVT::i8:
341 case MVT::i16:
342 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000343 //promote to 64-bits, sign/zero extending based on type
344 //of the argument
345 if(Args[i].second->isSigned())
346 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
347 Args[i].first);
348 else
349 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
350 Args[i].first);
351 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000352 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000353 //promote to 64-bits
354 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000355 case MVT::f64:
356 case MVT::i64:
357 break;
358 }
359 args_to_use.push_back(Args[i].first);
360 }
361
362 std::vector<MVT::ValueType> RetVals;
363 MVT::ValueType RetTyVT = getValueType(RetTy);
364 if (RetTyVT != MVT::isVoid)
365 RetVals.push_back(RetTyVT);
366 RetVals.push_back(MVT::Other);
367
368 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000369 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000370 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
371 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
372 DAG.getConstant(NumBytes, getPointerTy()));
373 return std::make_pair(TheCall, Chain);
374}
375
376std::pair<SDOperand, SDOperand>
377IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
378 // vastart just returns the address of the VarArgsFrameIndex slot.
379 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
380}
381
382std::pair<SDOperand,SDOperand> IA64TargetLowering::
383LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
384 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000385
386 MVT::ValueType ArgVT = getValueType(ArgTy);
387 SDOperand Result;
388 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000389 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000390 } else {
391 unsigned Amt;
392 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
393 Amt = 8;
394 else {
395 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
396 "Other types should have been promoted for varargs!");
397 Amt = 8;
398 }
399 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
400 DAG.getConstant(Amt, VAList.getValueType()));
401 }
402 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000403}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000404
405std::pair<SDOperand, SDOperand> IA64TargetLowering::
406LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
407 SelectionDAG &DAG) {
408
409 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000410 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000411}
412
413
414namespace {
415
416 //===--------------------------------------------------------------------===//
417 /// ISel - IA64 specific code to select IA64 machine instructions for
418 /// SelectionDAG operations.
419 ///
420 class ISel : public SelectionDAGISel {
421 /// IA64Lowering - This object fully describes how to lower LLVM code to an
422 /// IA64-specific SelectionDAG.
423 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000424 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
425 // for sdiv and udiv until it is put into the future
426 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000427
428 /// ExprMap - As shared expressions are codegen'd, we keep track of which
429 /// vreg the value is produced in, so we only emit one copy of each compiled
430 /// tree.
431 std::map<SDOperand, unsigned> ExprMap;
432 std::set<SDOperand> LoweredTokens;
433
434 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000435 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
436 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000437
438 /// InstructionSelectBasicBlock - This callback is invoked by
439 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
440 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
441
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000442 unsigned SelectExpr(SDOperand N);
443 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000444 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
445 SDOperand BuildConstmulSequence(SDOperand N);
446
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000447 };
448}
449
450/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
451/// when it has created a SelectionDAG for us to codegen.
452void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
453
454 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000455 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000456 Select(DAG.getRoot());
457
458 // Clear state used for selection.
459 ExprMap.clear();
460 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000461 ISelDAG = 0;
462}
463
Duraid Madinab2322562005-04-26 07:23:02 +0000464// strip leading '0' characters from a string
465void munchLeadingZeros(std::string& inString) {
466 while(inString.c_str()[0]=='0') {
467 inString.erase(0, 1);
468 }
469}
470
471// strip trailing '0' characters from a string
472void munchTrailingZeros(std::string& inString) {
473 int curPos=inString.length()-1;
474
475 while(inString.c_str()[curPos]=='0') {
476 inString.erase(curPos, 1);
477 curPos--;
478 }
479}
480
481// return how many consecutive '0' characters are at the end of a string
482unsigned int countTrailingZeros(std::string& inString) {
483 int curPos=inString.length()-1;
484 unsigned int zeroCount=0;
485 // assert goes here
486 while(inString.c_str()[curPos--]=='0') {
487 zeroCount++;
488 }
489 return zeroCount;
490}
491
492// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
493// '0' and 'N' (-1) characters)
494void boothEncode(std::string inString, std::string& boothEncodedString) {
495
496 int curpos=0;
497 int replacements=0;
498 int lim=inString.size();
499
500 while(curpos<lim) {
501 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
502 int runlength=0;
503 std::string replaceString="N";
504
505 // find the run length
506 for(;inString[curpos+runlength]=='1';runlength++) ;
507
508 for(int i=0; i<runlength-1; i++)
509 replaceString+="0";
510 replaceString+="1";
511
512 if(runlength>1) {
513 inString.replace(curpos, runlength+1, replaceString);
514 curpos+=runlength-1;
515 } else
516 curpos++;
517 } else { // a zero, we just keep chugging along
518 curpos++;
519 }
520 }
521
522 // clean up (trim the string, reverse it and turn '1's into 'P's)
523 munchTrailingZeros(inString);
524 boothEncodedString="";
525
526 for(int i=inString.size()-1;i>=0;i--)
527 if(inString[i]=='1')
528 boothEncodedString+="P";
529 else
530 boothEncodedString+=inString[i];
531
532}
533
534struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
535 unsigned firstVal; // A
536 unsigned firstShift; // B
537 unsigned secondVal; // C
538 unsigned secondShift; // D
539 bool isSub;
540};
541
542/* this implements Lefevre's "pattern-based" constant multiplication,
543 * see "Multiplication by an Integer Constant", INRIA report 1999-06
544 *
545 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
546 * to get better booth encodings - this does help in practice
547 * TODO: weight shifts appropriately (most architectures can't
548 * fuse a shift and an add for arbitrary shift amounts) */
549unsigned lefevre(const std::string inString,
550 std::vector<struct shiftaddblob> &ops) {
551 std::string retstring;
552 std::string s = inString;
553 munchTrailingZeros(s);
554
555 int length=s.length()-1;
556
557 if(length==0) {
558 return(0);
559 }
560
561 std::vector<int> p,n;
562
563 for(int i=0; i<=length; i++) {
564 if (s.c_str()[length-i]=='P') {
565 p.push_back(i);
566 } else if (s.c_str()[length-i]=='N') {
567 n.push_back(i);
568 }
569 }
570
571 std::string t, u;
Duraid Madina4706c032005-04-26 09:42:50 +0000572 int c;
573 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000574 std::map<const int, int> w;
575
Duraid Madina85d5f602005-04-27 11:57:39 +0000576 for(unsigned i=0; i<p.size(); i++) {
577 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000578 w[p[i]-p[j]]++;
579 }
580 }
581
Duraid Madina85d5f602005-04-27 11:57:39 +0000582 for(unsigned i=1; i<n.size(); i++) {
583 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000584 w[n[i]-n[j]]++;
585 }
586 }
587
Duraid Madina85d5f602005-04-27 11:57:39 +0000588 for(unsigned i=0; i<p.size(); i++) {
589 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000590 w[-abs(p[i]-n[j])]++;
591 }
592 }
593
594 std::map<const int, int>::const_iterator ii;
595 std::vector<int> d;
596 std::multimap<int, int> sorted_by_value;
597
598 for(ii = w.begin(); ii!=w.end(); ii++)
599 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
600
601 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
602 it != sorted_by_value.end(); ++it) {
603 d.push_back((*it).second);
604 }
605
606 int int_W=0;
607 int int_d;
608
609 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
610 d.pop_back();
611 retstring=s; // hmmm
612 int x=0;
613 int z=abs(int_d)-1;
614
615 if(int_d>0) {
616
Duraid Madina85d5f602005-04-27 11:57:39 +0000617 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000618 if( ((base+z+1) < retstring.size()) &&
619 retstring.c_str()[base]=='P' &&
620 retstring.c_str()[base+z+1]=='P')
621 {
622 // match
623 x++;
624 retstring.replace(base, 1, "0");
625 retstring.replace(base+z+1, 1, "p");
626 }
627 }
628
Duraid Madina85d5f602005-04-27 11:57:39 +0000629 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000630 if( ((base+z+1) < retstring.size()) &&
631 retstring.c_str()[base]=='N' &&
632 retstring.c_str()[base+z+1]=='N')
633 {
634 // match
635 x++;
636 retstring.replace(base, 1, "0");
637 retstring.replace(base+z+1, 1, "n");
638 }
639 }
640
641 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000642 for(unsigned base=0; base<retstring.size(); base++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000643 if( ((base+z+1) < retstring.size()) &&
644 ((retstring.c_str()[base]=='P' &&
645 retstring.c_str()[base+z+1]=='N') ||
646 (retstring.c_str()[base]=='N' &&
647 retstring.c_str()[base+z+1]=='P')) ) {
648 // match
649 x++;
650
651 if(retstring.c_str()[base]=='P') {
652 retstring.replace(base, 1, "0");
653 retstring.replace(base+z+1, 1, "p");
654 } else { // retstring[base]=='N'
655 retstring.replace(base, 1, "0");
656 retstring.replace(base+z+1, 1, "n");
657 }
658 }
659 }
660 }
661
662 if(x>int_W) {
663 int_W = x;
664 t = retstring;
665 c = int_d; // tofix
666 }
667
668 } d.pop_back(); // hmm
669
670 u = t;
671
Duraid Madina85d5f602005-04-27 11:57:39 +0000672 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000673 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
674 t.replace(i, 1, "0");
675 }
676
Duraid Madina85d5f602005-04-27 11:57:39 +0000677 for(unsigned i=0; i<u.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000678 if(u.c_str()[i]=='P' || u.c_str()[i]=='N')
679 u.replace(i, 1, "0");
680 if(u.c_str()[i]=='p')
681 u.replace(i, 1, "P");
682 if(u.c_str()[i]=='n')
683 u.replace(i, 1, "N");
684 }
685
686 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000687 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000688 c=-c;
689 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000690 f=false;
Duraid Madinab2322562005-04-26 07:23:02 +0000691
692 bool hit=true;
Duraid Madina85d5f602005-04-27 11:57:39 +0000693 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina4706c032005-04-26 09:42:50 +0000694 if(u[i]!='0')
695 if(u[i]!='N') {
696 hit=false;
697 break;
698 }
Duraid Madinab2322562005-04-26 07:23:02 +0000699 }
Duraid Madinab2322562005-04-26 07:23:02 +0000700
701 int g=0;
702 if(hit) {
703 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000704 for(unsigned p=0; p<u.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000705 bool isP=(u.c_str()[p]=='P');
706 bool isN=(u.c_str()[p]=='N');
707
708 if(isP)
709 u.replace(p, 1, "N");
710 if(isN)
711 u.replace(p, 1, "P");
712 }
713 }
714
715 munchLeadingZeros(u);
716
717 int i = lefevre(u, ops);
718
719 shiftaddblob blob;
720
721 blob.firstVal=i; blob.firstShift=c;
722 blob.isSub=f;
723 blob.secondVal=i; blob.secondShift=0;
724
725 ops.push_back(blob);
726
727 i = ops.size();
728
729 munchLeadingZeros(t);
730
731 if(t.length()==0)
732 return i;
733
734 if(t.c_str()[0]!='P') {
735 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000736 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000737 bool isP=(t.c_str()[p]=='P');
738 bool isN=(t.c_str()[p]=='N');
739
740 if(isP)
741 t.replace(p, 1, "N");
742 if(isN)
743 t.replace(p, 1, "P");
744 }
745 }
746
747 int j = lefevre(t, ops);
748
749 int trail=countTrailingZeros(u);
750 blob.secondVal=i; blob.secondShift=trail;
751
752 trail=countTrailingZeros(t);
753 blob.firstVal=j; blob.firstShift=trail;
754
755 switch(g) {
756 case 0:
757 blob.isSub=false; // first + second
758 break;
759 case 1:
760 blob.isSub=true; // first - second
761 break;
762 case 2:
763 blob.isSub=true; // second - first
764 int tmpval, tmpshift;
765 tmpval=blob.firstVal;
766 tmpshift=blob.firstShift;
767 blob.firstVal=blob.secondVal;
768 blob.firstShift=blob.secondShift;
769 blob.secondVal=tmpval;
770 blob.secondShift=tmpshift;
771 break;
772 //assert
773 }
774
775 ops.push_back(blob);
776 return ops.size();
777}
778
779SDOperand ISel::BuildConstmulSequence(SDOperand N) {
780 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
781 // in particular, *3 is nicer as *2+1, not *4-1
782 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
783
784 bool flippedSign;
785 unsigned preliminaryShift=0;
786
787 assert(constant > 0 && "erk, don't multiply by zero or negative nums\n");
788
789 // first, we make the constant to multiply by positive
790 if(constant<0) {
791 constant=-constant;
792 flippedSign=true;
793 } else {
794 flippedSign=false;
795 }
796
797 // next, we make it odd.
798 for(; (constant%2==0); preliminaryShift++)
799 constant>>=1;
800
801 //OK, we have a positive, odd number of 64 bits or less. Convert it
802 //to a binary string, constantString[0] is the LSB
803 char constantString[65];
804 for(int i=0; i<64; i++)
805 constantString[i]='0'+((constant>>i)&0x1);
806 constantString[64]=0;
807
808 // now, Booth encode it
809 std::string boothEncodedString;
810 boothEncode(constantString, boothEncodedString);
811
812 std::vector<struct shiftaddblob> ops;
813 // do the transformation, filling out 'ops'
814 lefevre(boothEncodedString, ops);
815
816 SDOperand results[ops.size()]; // temporary results (of adds/subs of shifts)
817
818 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000819 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000820 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
821 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
822 results[ops[i].firstVal-1];
823 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
824 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
825 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
826 results[ops[i].secondVal-1];
827 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
828 if(ops[i].isSub)
829 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
830 else
831 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
832 }
833
834 // don't forget flippedSign and preliminaryShift!
835 SDOperand finalresult;
836 if(preliminaryShift) {
837 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
838 finalresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
839 results[ops.size()-1], finalshift);
840 } else { // there was no preliminary divide-by-power-of-2 required
841 finalresult = results[ops.size()-1];
842 }
843
844 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000845}
846
Duraid Madina4826a072005-04-06 09:55:17 +0000847/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
848/// returns zero when the input is not exactly a power of two.
Duraid Madinac02780e2005-04-13 04:50:54 +0000849static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000850 if (Val == 0 || (Val & (Val-1))) return 0;
851 unsigned Count = 0;
852 while (Val != 1) {
853 Val >>= 1;
854 ++Count;
855 }
856 return Count;
857}
858
Duraid Madinac02780e2005-04-13 04:50:54 +0000859/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
860/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
861static unsigned ExactLog2sub1(uint64_t Val) {
862 unsigned int n;
863 for(n=0; n<64; n++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000864 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000865 return n;
866 }
867 return 666;
868}
869
Duraid Madina4826a072005-04-06 09:55:17 +0000870/// ponderIntegerDivisionBy - When handling integer divides, if the divide
871/// is by a constant such that we can efficiently codegen it, this
872/// function says what to do. Currently, it returns 0 if the division must
873/// become a genuine divide, and 1 if the division can be turned into a
874/// right shift.
875static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
876 unsigned& Imm) {
877 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
878 // a constant, give up.
879
880 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
881
Misha Brukman4633f1c2005-04-21 23:13:11 +0000882 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
Duraid Madina4826a072005-04-06 09:55:17 +0000883 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000884 }
885
Duraid Madina4826a072005-04-06 09:55:17 +0000886 return 0; // fallthrough
887}
888
Duraid Madinac02780e2005-04-13 04:50:54 +0000889static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
890 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
891 // a constant, give up.
892
893 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
894
895 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
896 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000897 }
898
Duraid Madinac02780e2005-04-13 04:50:54 +0000899 return 0; // fallthrough
900}
901
Duraid Madinaf55e4032005-04-07 12:33:38 +0000902static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
903 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
904 // constant, give up.
905 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
906
907 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
908 Imm = v & 0x3FFF; // 14 bits
909 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000910 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000911 return 0; // fallthrough
912}
913
914static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
915 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
916 // constant, give up.
917 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
918
919 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
920 Imm = v & 0xFF; // 8 bits
921 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000922 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000923 return 0; // fallthrough
924}
925
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000926unsigned ISel::SelectExpr(SDOperand N) {
927 unsigned Result;
928 unsigned Tmp1, Tmp2, Tmp3;
929 unsigned Opc = 0;
930 MVT::ValueType DestType = N.getValueType();
931
932 unsigned opcode = N.getOpcode();
933
934 SDNode *Node = N.Val;
935 SDOperand Op0, Op1;
936
937 if (Node->getOpcode() == ISD::CopyFromReg)
938 // Just use the specified register as our input.
939 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000940
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000941 unsigned &Reg = ExprMap[N];
942 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000943
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000944 if (N.getOpcode() != ISD::CALL)
945 Reg = Result = (N.getValueType() != MVT::Other) ?
946 MakeReg(N.getValueType()) : 1;
947 else {
948 // If this is a call instruction, make sure to prepare ALL of the result
949 // values as well as the chain.
950 if (Node->getNumValues() == 1)
951 Reg = Result = 1; // Void call, just a chain.
952 else {
953 Result = MakeReg(Node->getValueType(0));
954 ExprMap[N.getValue(0)] = Result;
955 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
956 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
957 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
958 }
959 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000960
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000961 switch (N.getOpcode()) {
962 default:
963 Node->dump();
964 assert(0 && "Node not handled!\n");
965
966 case ISD::FrameIndex: {
967 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
968 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
969 return Result;
970 }
971
972 case ISD::ConstantPool: {
973 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
974 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
975 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
976 .addReg(IA64::r1);
977 return Result;
978 }
979
980 case ISD::ConstantFP: {
981 Tmp1 = Result; // Intermediate Register
982 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
983 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
984 Tmp1 = MakeReg(MVT::f64);
985
986 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
987 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
988 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
989 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
990 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
991 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
992 else
993 assert(0 && "Unexpected FP constant!");
994 if (Tmp1 != Result)
995 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
996 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000997 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000998 return Result;
999 }
1000
1001 case ISD::DYNAMIC_STACKALLOC: {
1002 // Generate both result values.
1003 if (Result != 1)
1004 ExprMap[N.getValue(1)] = 1; // Generate the token
1005 else
1006 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1007
1008 // FIXME: We are currently ignoring the requested alignment for handling
1009 // greater than the stack alignment. This will need to be revisited at some
1010 // point. Align = N.getOperand(2);
1011
1012 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1013 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1014 std::cerr << "Cannot allocate stack object with greater alignment than"
1015 << " the stack alignment yet!";
1016 abort();
1017 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001018
1019/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001020 Select(N.getOperand(0));
1021 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1022 {
1023 if (CN->getValue() < 32000)
1024 {
1025 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001026 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001027 } else {
1028 Tmp1 = SelectExpr(N.getOperand(1));
1029 // Subtract size from stack pointer, thereby allocating some space.
1030 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1031 }
1032 } else {
1033 Tmp1 = SelectExpr(N.getOperand(1));
1034 // Subtract size from stack pointer, thereby allocating some space.
1035 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1036 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001037*/
1038 Select(N.getOperand(0));
1039 Tmp1 = SelectExpr(N.getOperand(1));
1040 // Subtract size from stack pointer, thereby allocating some space.
1041 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001042 // Put a pointer to the space into the result register, by copying the
1043 // stack pointer.
1044 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1045 return Result;
1046 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001047
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001048 case ISD::SELECT: {
1049 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1050 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1051 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1052
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001053 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001054
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001055 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001056 default: assert(0 &&
1057 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
1058 case MVT::i64:
1059 bogoResult=MakeReg(MVT::i64);
1060 break;
1061 case MVT::f64:
1062 bogoResult=MakeReg(MVT::f64);
1063 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001064 }
Duraid Madina69c8e202005-04-01 10:35:00 +00001065
1066 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1067 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001068 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
Duraid Madina69c8e202005-04-01 10:35:00 +00001069 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001070 return Result;
1071 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001072
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001073 case ISD::Constant: {
1074 unsigned depositPos=0;
1075 unsigned depositLen=0;
1076 switch (N.getValueType()) {
1077 default: assert(0 && "Cannot use constants of this type!");
1078 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001079 // the constant:
1080 if(cast<ConstantSDNode>(N)->getValue()) // true:
1081 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1082 else // false:
1083 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1084 return Result; // early exit
1085 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001086 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001087 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001088
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001089 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001090
1091 if(immediate==0) { // if the constant is just zero,
1092 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1093 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001094 }
1095
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001096 if (immediate <= 8191 && immediate >= -8192) {
1097 // if this constants fits in 14 bits, we use a mov the assembler will
1098 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1099 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1100 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001101 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001102
1103 if (immediate <= 2097151 && immediate >= -2097152) {
1104 // if this constants fits in 22 bits, we use a mov the assembler will
1105 // turn into: "addl rDest=imm,r0"
1106 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1107 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001108 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001109
1110 /* otherwise, our immediate is big, so we use movl */
1111 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001112 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001113 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001114 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001115
1116 case ISD::UNDEF: {
1117 BuildMI(BB, IA64::IDEF, 0, Result);
1118 return Result;
1119 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001120
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001121 case ISD::GlobalAddress: {
1122 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1123 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001124
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001125 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001126 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001127
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001128 return Result;
1129 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001131 case ISD::ExternalSymbol: {
1132 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001133// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1134 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001135 return Result;
1136 }
1137
1138 case ISD::FP_EXTEND: {
1139 Tmp1 = SelectExpr(N.getOperand(0));
1140 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1141 return Result;
1142 }
1143
1144 case ISD::ZERO_EXTEND: {
1145 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 switch (N.getOperand(0).getValueType()) {
1148 default: assert(0 && "Cannot zero-extend this type!");
1149 case MVT::i8: Opc = IA64::ZXT1; break;
1150 case MVT::i16: Opc = IA64::ZXT2; break;
1151 case MVT::i32: Opc = IA64::ZXT4; break;
1152
Misha Brukman4633f1c2005-04-21 23:13:11 +00001153 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001154 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001155 unsigned dummy = MakeReg(MVT::i64);
1156 // first load zero:
1157 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1158 // ...then conditionally (PR:Tmp1) add 1:
1159 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1160 .addImm(1).addReg(Tmp1);
1161 return Result; // XXX early exit!
1162 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001163 }
1164
1165 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1166 return Result;
1167 }
1168
1169 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1170
1171assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1172
1173 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001174
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001175 switch (N.getOperand(0).getValueType()) {
1176 default: assert(0 && "Cannot sign-extend this type!");
1177 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001178 Opc = IA64::SXT1; break;
1179 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001180 case MVT::i8: Opc = IA64::SXT1; break;
1181 case MVT::i16: Opc = IA64::SXT2; break;
1182 case MVT::i32: Opc = IA64::SXT4; break;
1183 }
1184
1185 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1186 return Result;
1187 }
1188
1189 case ISD::TRUNCATE: {
1190 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1191 // of R0 appropriately.
1192 switch (N.getOperand(0).getValueType()) {
1193 default: assert(0 && "Unknown truncate!");
1194 case MVT::i64: break;
1195 }
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 unsigned depositPos, depositLen;
1198
1199 switch (N.getValueType()) {
1200 default: assert(0 && "Unknown truncate!");
1201 case MVT::i1: {
1202 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001203 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1204 .addReg(IA64::r0);
1205 return Result; // XXX early exit!
1206 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001207 case MVT::i8: depositPos=0; depositLen=8; break;
1208 case MVT::i16: depositPos=0; depositLen=16; break;
1209 case MVT::i32: depositPos=0; depositLen=32; break;
1210 }
1211 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1212 .addImm(depositPos).addImm(depositLen);
1213 return Result;
1214 }
1215
Misha Brukman7847fca2005-04-22 17:54:37 +00001216/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001217 case ISD::FP_ROUND: {
1218 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001219 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001220 Tmp1 = SelectExpr(N.getOperand(0));
1221 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1222 // we add 0.0 using a single precision add to do rounding
1223 return Result;
1224 }
1225*/
1226
1227// FIXME: the following 4 cases need cleaning
1228 case ISD::SINT_TO_FP: {
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 Tmp2 = MakeReg(MVT::f64);
1231 unsigned dummy = MakeReg(MVT::f64);
1232 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1233 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1234 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1235 return Result;
1236 }
1237
1238 case ISD::UINT_TO_FP: {
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 Tmp2 = MakeReg(MVT::f64);
1241 unsigned dummy = MakeReg(MVT::f64);
1242 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1243 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1244 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1245 return Result;
1246 }
1247
1248 case ISD::FP_TO_SINT: {
1249 Tmp1 = SelectExpr(N.getOperand(0));
1250 Tmp2 = MakeReg(MVT::f64);
1251 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1252 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1253 return Result;
1254 }
1255
1256 case ISD::FP_TO_UINT: {
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = MakeReg(MVT::f64);
1259 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1260 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1261 return Result;
1262 }
1263
1264 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001265 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1266 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1267 // into an fma, do so:
1268 // ++FusedFP; // Statistic
1269 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1270 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1271 Tmp3 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1273 return Result; // early exit
1274 }
Duraid Madinaed095022005-04-13 06:12:04 +00001275
1276 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001277 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001278 // this add into a shladd, try:
1279 ConstantSDNode *CSD = NULL;
1280 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001281 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001282
Misha Brukman7847fca2005-04-22 17:54:37 +00001283 // ++FusedSHLADD; // Statistic
1284 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1285 int shl_amt = CSD->getValue();
1286 Tmp3 = SelectExpr(N.getOperand(1));
1287
1288 BuildMI(BB, IA64::SHLADD, 3, Result)
1289 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1290 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001291 }
1292 }
1293
1294 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001295 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001296 if(DestType != MVT::f64) { // integer addition:
1297 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001298 case 1: // adding a constant that's 14 bits
1299 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1300 return Result; // early exit
1301 } // fallthrough and emit a reg+reg ADD:
1302 Tmp2 = SelectExpr(N.getOperand(1));
1303 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001304 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001305 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001306 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1307 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001308 return Result;
1309 }
1310
1311 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001312
1313 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001314 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1315 // boring old integer multiply with xma
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 Tmp2 = SelectExpr(N.getOperand(1));
1318
1319 unsigned TempFR1=MakeReg(MVT::f64);
1320 unsigned TempFR2=MakeReg(MVT::f64);
1321 unsigned TempFR3=MakeReg(MVT::f64);
1322 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1323 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1324 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1325 .addReg(IA64::F0);
1326 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1327 return Result; // early exit
1328 } else { // we are multiplying by an integer constant! yay
1329 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1330 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001331 }
Duraid Madinab2322562005-04-26 07:23:02 +00001332 else { // floating point multiply
1333 Tmp1 = SelectExpr(N.getOperand(0));
1334 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001335 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001336 return Result;
1337 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001338 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001339
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001340 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001341 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1342 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1343 // into an fms, do so:
1344 // ++FusedFP; // Statistic
1345 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1347 Tmp3 = SelectExpr(N.getOperand(1));
1348 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1349 return Result; // early exit
1350 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001351 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001352 if(DestType != MVT::f64) { // integer subtraction:
1353 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001354 case 1: // subtracting *from* an 8 bit constant:
1355 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1356 return Result; // early exit
1357 } // fallthrough and emit a reg+reg SUB:
1358 Tmp1 = SelectExpr(N.getOperand(0));
1359 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001360 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001361 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001362 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001363 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001364 return Result;
1365 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001366
1367 case ISD::FABS: {
1368 Tmp1 = SelectExpr(N.getOperand(0));
1369 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1370 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1371 return Result;
1372 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001373
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001374 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001375 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001376
Misha Brukman4633f1c2005-04-21 23:13:11 +00001377 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001378 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1379 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1380 } else {
1381 Tmp1 = SelectExpr(N.getOperand(0));
1382 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1383 }
1384
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001385 return Result;
1386 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001387
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001388 case ISD::AND: {
1389 switch (N.getValueType()) {
1390 default: assert(0 && "Cannot AND this type!");
1391 case MVT::i1: { // if a bool, we emit a pseudocode AND
1392 unsigned pA = SelectExpr(N.getOperand(0));
1393 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001395/* our pseudocode for AND is:
1396 *
1397(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1398 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1399 ;;
1400(pB) cmp.ne pTemp,p0 = r0,r0
1401 ;;
1402(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1403
1404*/
1405 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001406
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407 unsigned bogusTemp1 = MakeReg(MVT::i1);
1408 unsigned bogusTemp2 = MakeReg(MVT::i1);
1409 unsigned bogusTemp3 = MakeReg(MVT::i1);
1410 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001411
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001412 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001413 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001414 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001415 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001416 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001417 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001418 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001419 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001420 break;
1421 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001423 // if not a bool, we just AND away:
1424 case MVT::i8:
1425 case MVT::i16:
1426 case MVT::i32:
1427 case MVT::i64: {
1428 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001429 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1430 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001431 switch (Tmp3) {
1432 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1433 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1434 break;
1435 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1436 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1437 break;
1438 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1439 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1440 break;
1441 default: // otherwise, use dep.z to paste zeros
1442 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1443 .addImm(0).addImm(Tmp3);
1444 break;
1445 }
1446 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001447 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001448 Tmp2 = SelectExpr(N.getOperand(1));
1449 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001450 }
1451 }
1452 return Result;
1453 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001454
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001455 case ISD::OR: {
1456 switch (N.getValueType()) {
1457 default: assert(0 && "Cannot OR this type!");
1458 case MVT::i1: { // if a bool, we emit a pseudocode OR
1459 unsigned pA = SelectExpr(N.getOperand(0));
1460 unsigned pB = SelectExpr(N.getOperand(1));
1461
1462 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001463
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001464/* our pseudocode for OR is:
1465 *
1466
1467pC = pA OR pB
1468-------------
1469
Misha Brukman7847fca2005-04-22 17:54:37 +00001470(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1471 ;;
1472(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001473
1474*/
1475 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001476 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001477 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001478 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001479 break;
1480 }
1481 // if not a bool, we just OR away:
1482 case MVT::i8:
1483 case MVT::i16:
1484 case MVT::i32:
1485 case MVT::i64: {
1486 Tmp1 = SelectExpr(N.getOperand(0));
1487 Tmp2 = SelectExpr(N.getOperand(1));
1488 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1489 break;
1490 }
1491 }
1492 return Result;
1493 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001494
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001495 case ISD::XOR: {
1496 switch (N.getValueType()) {
1497 default: assert(0 && "Cannot XOR this type!");
1498 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1499 unsigned pY = SelectExpr(N.getOperand(0));
1500 unsigned pZ = SelectExpr(N.getOperand(1));
1501
1502/* one possible routine for XOR is:
1503
1504 // Compute px = py ^ pz
1505 // using sum of products: px = (py & !pz) | (pz & !py)
1506 // Uses 5 instructions in 3 cycles.
1507 // cycle 1
1508(pz) cmp.eq.unc px = r0, r0 // px = pz
1509(py) cmp.eq.unc pt = r0, r0 // pt = py
1510 ;;
1511 // cycle 2
1512(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1513(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1514 ;;
1515 } { .mmi
1516 // cycle 3
1517(pt) cmp.eq.or px = r0, r0 // px = px | pt
1518
1519*** Another, which we use here, requires one scratch GR. it is:
1520
1521 mov rt = 0 // initialize rt off critical path
1522 ;;
1523
1524 // cycle 1
1525(pz) cmp.eq.unc px = r0, r0 // px = pz
1526(pz) mov rt = 1 // rt = pz
1527 ;;
1528 // cycle 2
1529(py) cmp.ne px = 1, rt // if (py) px = !pz
1530
1531.. these routines kindly provided by Jim Hull
1532*/
1533 unsigned rt = MakeReg(MVT::i64);
1534
1535 // these two temporaries will never actually appear,
1536 // due to the two-address form of some of the instructions below
1537 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1538 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1539
1540 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1541 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001542 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001543 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001544 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001545 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001546 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001547 break;
1548 }
1549 // if not a bool, we just XOR away:
1550 case MVT::i8:
1551 case MVT::i16:
1552 case MVT::i32:
1553 case MVT::i64: {
1554 Tmp1 = SelectExpr(N.getOperand(0));
1555 Tmp2 = SelectExpr(N.getOperand(1));
1556 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1557 break;
1558 }
1559 }
1560 return Result;
1561 }
1562
1563 case ISD::SHL: {
1564 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001565 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1566 Tmp2 = CN->getValue();
1567 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1568 } else {
1569 Tmp2 = SelectExpr(N.getOperand(1));
1570 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1571 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001572 return Result;
1573 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001574
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001575 case ISD::SRL: {
1576 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001577 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1578 Tmp2 = CN->getValue();
1579 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1580 } else {
1581 Tmp2 = SelectExpr(N.getOperand(1));
1582 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1583 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001584 return Result;
1585 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001586
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001587 case ISD::SRA: {
1588 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001589 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1590 Tmp2 = CN->getValue();
1591 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1592 } else {
1593 Tmp2 = SelectExpr(N.getOperand(1));
1594 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1595 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001596 return Result;
1597 }
1598
1599 case ISD::SDIV:
1600 case ISD::UDIV:
1601 case ISD::SREM:
1602 case ISD::UREM: {
1603
1604 Tmp1 = SelectExpr(N.getOperand(0));
1605 Tmp2 = SelectExpr(N.getOperand(1));
1606
1607 bool isFP=false;
1608
1609 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1610 isFP=true;
1611
1612 bool isModulus=false; // is it a division or a modulus?
1613 bool isSigned=false;
1614
1615 switch(N.getOpcode()) {
1616 case ISD::SDIV: isModulus=false; isSigned=true; break;
1617 case ISD::UDIV: isModulus=false; isSigned=false; break;
1618 case ISD::SREM: isModulus=true; isSigned=true; break;
1619 case ISD::UREM: isModulus=true; isSigned=false; break;
1620 }
1621
Duraid Madina4826a072005-04-06 09:55:17 +00001622 if(!isModulus && !isFP) { // if this is an integer divide,
1623 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001624 case 1: // division by a constant that's a power of 2
1625 Tmp1 = SelectExpr(N.getOperand(0));
1626 if(isSigned) { // argument could be negative, so emit some code:
1627 unsigned divAmt=Tmp3;
1628 unsigned tempGR1=MakeReg(MVT::i64);
1629 unsigned tempGR2=MakeReg(MVT::i64);
1630 unsigned tempGR3=MakeReg(MVT::i64);
1631 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1632 .addReg(Tmp1).addImm(divAmt-1);
1633 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1634 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1635 BuildMI(BB, IA64::ADD, 2, tempGR3)
1636 .addReg(Tmp1).addReg(tempGR2);
1637 BuildMI(BB, IA64::SHRS, 2, Result)
1638 .addReg(tempGR3).addImm(divAmt);
1639 }
1640 else // unsigned div-by-power-of-2 becomes a simple shift right:
1641 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1642 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001643 }
1644 }
1645
Misha Brukman4633f1c2005-04-21 23:13:11 +00001646 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001647 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001648 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1649 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1650 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1651 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1652 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1653 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1654 unsigned TmpF7=MakeReg(MVT::f64);
1655 unsigned TmpF8=MakeReg(MVT::f64);
1656 unsigned TmpF9=MakeReg(MVT::f64);
1657 unsigned TmpF10=MakeReg(MVT::f64);
1658 unsigned TmpF11=MakeReg(MVT::f64);
1659 unsigned TmpF12=MakeReg(MVT::f64);
1660 unsigned TmpF13=MakeReg(MVT::f64);
1661 unsigned TmpF14=MakeReg(MVT::f64);
1662 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001663
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001664 // OK, emit some code:
1665
1666 if(!isFP) {
1667 // first, load the inputs into FP regs.
1668 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1669 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001670
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001671 // next, convert the inputs to FP
1672 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001673 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1674 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001675 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001676 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1677 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001678 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001679
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001680 } else { // this is an FP divide/remainder, so we 'leak' some temp
1681 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1682 TmpF3=Tmp1;
1683 TmpF4=Tmp2;
1684 }
1685
1686 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001687 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1688 BuildMI(BB, IA64::FRCPAS1, 4)
1689 .addReg(TmpF5, MachineOperand::Def)
1690 .addReg(TmpPR, MachineOperand::Def)
1691 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001692
Duraid Madinabeeaab22005-03-31 12:31:11 +00001693 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1694 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1695 // TPCMPNE below
1696 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1697 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001698 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001699 }
1700
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001701 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1702 // precision, don't need this much for f32/i32)
1703 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1704 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1705 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1706 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1707 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1708 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1709 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1710 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1711 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1712 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1713 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1714 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1715 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1716 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1717 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1718 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001719
1720 // FIXME: this is unfortunate :(
1721 // the story is that the dest reg of the fnma above and the fma below
1722 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1723 // be the same register, or this code breaks if the first argument is
1724 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001725 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1726 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1727
Duraid Madina6e02e682005-04-04 05:05:52 +00001728 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1729 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1730 }
1731
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001732 if(!isFP) {
1733 // round to an integer
1734 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001735 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001736 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001737 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001738 } else {
1739 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1740 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1741 // we really do need the above FMOV? ;)
1742 }
1743
1744 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001745 if(isFP) { // extra worrying about div-by-zero
1746 unsigned bogoResult=MakeReg(MVT::f64);
1747
1748 // we do a 'conditional fmov' (of the correct result, depending
1749 // on how the frcpa predicate turned out)
1750 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001751 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001752 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001753 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001754 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001755 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001756 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001757 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001758 } else { // this is a modulus
1759 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001760 // answer = q * (-b) + a
1761 unsigned ModulusResult = MakeReg(MVT::f64);
1762 unsigned TmpF = MakeReg(MVT::f64);
1763 unsigned TmpI = MakeReg(MVT::i64);
1764
1765 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1766 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1767 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1768 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1769 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001770 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001771 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001772 }
1773 }
1774
1775 return Result;
1776 }
1777
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001778 case ISD::SIGN_EXTEND_INREG: {
1779 Tmp1 = SelectExpr(N.getOperand(0));
1780 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1781 switch(MVN->getExtraValueType())
1782 {
1783 default:
1784 Node->dump();
1785 assert(0 && "don't know how to sign extend this type");
1786 break;
1787 case MVT::i8: Opc = IA64::SXT1; break;
1788 case MVT::i16: Opc = IA64::SXT2; break;
1789 case MVT::i32: Opc = IA64::SXT4; break;
1790 }
1791 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1792 return Result;
1793 }
1794
1795 case ISD::SETCC: {
1796 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001797
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001798 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1799 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001800
Misha Brukman7847fca2005-04-22 17:54:37 +00001801 if(ConstantSDNode *CSDN =
1802 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1803 // if we are comparing against a constant zero
1804 if(CSDN->getValue()==0)
1805 Tmp2 = IA64::r0; // then we can just compare against r0
1806 else
1807 Tmp2 = SelectExpr(N.getOperand(1));
1808 } else // not comparing against a constant
1809 Tmp2 = SelectExpr(N.getOperand(1));
1810
1811 switch (SetCC->getCondition()) {
1812 default: assert(0 && "Unknown integer comparison!");
1813 case ISD::SETEQ:
1814 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1815 break;
1816 case ISD::SETGT:
1817 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1818 break;
1819 case ISD::SETGE:
1820 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1821 break;
1822 case ISD::SETLT:
1823 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1824 break;
1825 case ISD::SETLE:
1826 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1827 break;
1828 case ISD::SETNE:
1829 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1830 break;
1831 case ISD::SETULT:
1832 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 break;
1834 case ISD::SETUGT:
1835 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 break;
1837 case ISD::SETULE:
1838 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 break;
1840 case ISD::SETUGE:
1841 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 break;
1843 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001844 }
1845 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001846 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1847 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001848
Misha Brukman7847fca2005-04-22 17:54:37 +00001849 if(ConstantFPSDNode *CFPSDN =
1850 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001851
Misha Brukman7847fca2005-04-22 17:54:37 +00001852 // if we are comparing against a constant +0.0 or +1.0
1853 if(CFPSDN->isExactlyValue(+0.0))
1854 Tmp2 = IA64::F0; // then we can just compare against f0
1855 else if(CFPSDN->isExactlyValue(+1.0))
1856 Tmp2 = IA64::F1; // or f1
1857 else
1858 Tmp2 = SelectExpr(N.getOperand(1));
1859 } else // not comparing against a constant
1860 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001861
Misha Brukman7847fca2005-04-22 17:54:37 +00001862 switch (SetCC->getCondition()) {
1863 default: assert(0 && "Unknown FP comparison!");
1864 case ISD::SETEQ:
1865 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETGT:
1868 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETGE:
1871 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETLT:
1874 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETLE:
1877 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETNE:
1880 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETULT:
1883 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETUGT:
1886 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
1888 case ISD::SETULE:
1889 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1890 break;
1891 case ISD::SETUGE:
1892 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1893 break;
1894 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001895 }
1896 }
1897 else
1898 assert(0 && "this setcc not implemented yet");
1899
1900 return Result;
1901 }
1902
1903 case ISD::EXTLOAD:
1904 case ISD::ZEXTLOAD:
1905 case ISD::LOAD: {
1906 // Make sure we generate both values.
1907 if (Result != 1)
1908 ExprMap[N.getValue(1)] = 1; // Generate the token
1909 else
1910 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1911
1912 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001913
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001914 if(opcode == ISD::LOAD) { // this is a LOAD
1915 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001916 default: assert(0 && "Cannot load this type!");
1917 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1918 // FIXME: for now, we treat bool loads the same as i8 loads */
1919 case MVT::i8: Opc = IA64::LD1; break;
1920 case MVT::i16: Opc = IA64::LD2; break;
1921 case MVT::i32: Opc = IA64::LD4; break;
1922 case MVT::i64: Opc = IA64::LD8; break;
1923
1924 case MVT::f32: Opc = IA64::LDF4; break;
1925 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001926 }
1927 } else { // this is an EXTLOAD or ZEXTLOAD
1928 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1929 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001930 default: assert(0 && "Cannot extload/zextload this type!");
1931 // FIXME: bools?
1932 case MVT::i8: Opc = IA64::LD1; break;
1933 case MVT::i16: Opc = IA64::LD2; break;
1934 case MVT::i32: Opc = IA64::LD4; break;
1935 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001936 }
1937 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001938
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001939 SDOperand Chain = N.getOperand(0);
1940 SDOperand Address = N.getOperand(1);
1941
1942 if(Address.getOpcode() == ISD::GlobalAddress) {
1943 Select(Chain);
1944 unsigned dummy = MakeReg(MVT::i64);
1945 unsigned dummy2 = MakeReg(MVT::i64);
1946 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001947 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1948 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001949 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1950 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001951 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001952 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001953 // into a predicate register
1954 assert(Opc==IA64::LD1 && "problem loading a bool");
1955 unsigned dummy3 = MakeReg(MVT::i64);
1956 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1957 // we compare to 0. true? 0. false? 1.
1958 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001959 }
1960 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1961 Select(Chain);
1962 IA64Lowering.restoreGP(BB);
1963 unsigned dummy = MakeReg(MVT::i64);
1964 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001965 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001966 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001967 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001968 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001969 // into a predicate register
1970 assert(Opc==IA64::LD1 && "problem loading a bool");
1971 unsigned dummy3 = MakeReg(MVT::i64);
1972 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1973 // we compare to 0. true? 0. false? 1.
1974 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001975 }
1976 } else if(Address.getOpcode() == ISD::FrameIndex) {
1977 Select(Chain); // FIXME ? what about bools?
1978 unsigned dummy = MakeReg(MVT::i64);
1979 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001980 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
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 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001991 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001992 Select(Chain);
1993 Tmp2 = SelectExpr(Address);
1994 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001995 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001996 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001997 // into a predicate register
1998 assert(Opc==IA64::LD1 && "problem loading a bool");
1999 unsigned dummy = MakeReg(MVT::i64);
2000 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2001 // we compare to 0. true? 0. false? 1.
2002 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
2003 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002004 }
2005
2006 return Result;
2007 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002008
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002009 case ISD::CopyFromReg: {
2010 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002011 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002012 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002013
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002014 SDOperand Chain = N.getOperand(0);
2015
2016 Select(Chain);
2017 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2018
2019 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002020 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2021 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002022 // (r) Result =cmp.eq.unc(r0,r0)
2023 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002024 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002025 return Result;
2026 }
2027
2028 case ISD::CALL: {
2029 Select(N.getOperand(0));
2030
2031 // The chain for this call is now lowered.
2032 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002033
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002034 //grab the arguments
2035 std::vector<unsigned> argvregs;
2036
2037 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002038 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002039
2040 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002041 // Runtime Architecture Guide to see some examples of what's going
2042 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2043 // while FP args get mapped to F8->F15 as needed)
2044
2045 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002046
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002047 // in reg args
2048 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2049 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002050 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2051 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2052 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2053 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002054
Misha Brukman7847fca2005-04-22 17:54:37 +00002055 switch(N.getOperand(i+2).getValueType())
2056 {
2057 default: // XXX do we need to support MVT::i1 here?
2058 Node->dump();
2059 N.getOperand(i).Val->dump();
2060 std::cerr << "Type for " << i << " is: " <<
2061 N.getOperand(i+2).getValueType() << std::endl;
2062 assert(0 && "Unknown value type for call");
2063 case MVT::i64:
2064 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2065 break;
2066 case MVT::f64:
2067 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2068 .addReg(argvregs[i]);
2069 // FIXME: we don't need to do this _all_ the time:
2070 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2071 break;
2072 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002073 }
2074
2075 //in mem args
2076 for (int i = 8, e = argvregs.size(); i < e; ++i)
2077 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002078 unsigned tempAddr = MakeReg(MVT::i64);
2079
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002080 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002081 default:
2082 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002083 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002084 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002085 N.getOperand(i+2).getValueType() << "\n";
2086 assert(0 && "Unknown value type for call");
2087 case MVT::i1: // FIXME?
2088 case MVT::i8:
2089 case MVT::i16:
2090 case MVT::i32:
2091 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002092 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2093 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2094 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002095 break;
2096 case MVT::f32:
2097 case MVT::f64:
2098 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002099 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2100 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002101 break;
2102 }
2103 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002104
2105 /* XXX we want to re-enable direct branches! crippling them now
Misha Brukman4633f1c2005-04-21 23:13:11 +00002106 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002107 //build the right kind of call
2108 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002109 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002110 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002111 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2112 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002113 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002114 ^^^^^^^^^^^^^ we want this code one day XXX */
Duraid Madinabeeaab22005-03-31 12:31:11 +00002115 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002116 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002117 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002118 // "non-pic code with imm relocation against dynamic symbol" errors
2119 BuildMI(BB, IA64::BRCALL, 1)
2120 .addExternalSymbol(ESSDN->getSymbol(), true);
2121 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002122 }
2123 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002124 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002125
2126 unsigned targetEntryPoint=MakeReg(MVT::i64);
2127 unsigned targetGPAddr=MakeReg(MVT::i64);
2128 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129
Duraid Madinabeeaab22005-03-31 12:31:11 +00002130 // b6 is a scratch branch register, we load the target entry point
2131 // from the base of the function descriptor
2132 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2133 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2134
2135 // save the current GP:
2136 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002137
Duraid Madinabeeaab22005-03-31 12:31:11 +00002138 /* TODO: we need to make sure doing this never, ever loads a
2139 * bogus value into r1 (GP). */
2140 // load the target GP (which is at mem[functiondescriptor+8])
2141 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002142 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002143 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2144
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002145 // and then jump: (well, call)
2146 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002147 // and finally restore the old GP
2148 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2149 IA64Lowering.restoreSP_RP(BB);
2150 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002151
2152 switch (Node->getValueType(0)) {
2153 default: assert(0 && "Unknown value type for call result!");
2154 case MVT::Other: return 1;
2155 case MVT::i1:
2156 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002157 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002158 break;
2159 case MVT::i8:
2160 case MVT::i16:
2161 case MVT::i32:
2162 case MVT::i64:
2163 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2164 break;
2165 case MVT::f64:
2166 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2167 break;
2168 }
2169 return Result+N.ResNo;
2170 }
2171
Misha Brukman4633f1c2005-04-21 23:13:11 +00002172 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002173 return 0;
2174}
2175
2176void ISel::Select(SDOperand N) {
2177 unsigned Tmp1, Tmp2, Opc;
2178 unsigned opcode = N.getOpcode();
2179
Nate Begeman85fdeb22005-03-24 04:39:54 +00002180 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002181 return; // Already selected.
2182
2183 SDNode *Node = N.Val;
2184
2185 switch (Node->getOpcode()) {
2186 default:
2187 Node->dump(); std::cerr << "\n";
2188 assert(0 && "Node not handled yet!");
2189
2190 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002191
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002192 case ISD::TokenFactor: {
2193 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2194 Select(Node->getOperand(i));
2195 return;
2196 }
2197
2198 case ISD::CopyToReg: {
2199 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002200 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002201 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002202
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002203 if (Tmp1 != Tmp2) {
2204 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002205 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2206 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002207 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2208 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002209 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002210 // XXX is this the right way 'round? ;)
2211 }
2212 return;
2213 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002214
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002215 case ISD::RET: {
2216
2217 /* what the heck is going on here:
2218
2219<_sabre_> ret with two operands is obvious: chain and value
2220<camel_> yep
2221<_sabre_> ret with 3 values happens when 'expansion' occurs
2222<_sabre_> e.g. i64 gets split into 2x i32
2223<camel_> oh right
2224<_sabre_> you don't have this case on ia64
2225<camel_> yep
2226<_sabre_> so the two returned values go into EAX/EDX on ia32
2227<camel_> ahhh *memories*
2228<_sabre_> :)
2229<camel_> ok, thanks :)
2230<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2231<_sabre_> this is the first operand always
2232<_sabre_> these operand often define chains, they are the last operand
2233<_sabre_> they are printed as 'ch' if you do DAG.dump()
2234 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002235
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002236 switch (N.getNumOperands()) {
2237 default:
2238 assert(0 && "Unknown return instruction!");
2239 case 2:
2240 Select(N.getOperand(0));
2241 Tmp1 = SelectExpr(N.getOperand(1));
2242 switch (N.getOperand(1).getValueType()) {
2243 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002244 // FIXME: do I need to add support for bools here?
2245 // (return '0' or '1' r8, basically...)
2246 //
2247 // FIXME: need to round floats - 80 bits is bad, the tester
2248 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002249 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002250 // we mark r8 as live on exit up above in LowerArguments()
2251 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2252 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002253 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002254 // we mark F8 as live on exit up above in LowerArguments()
2255 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002256 }
2257 break;
2258 case 1:
2259 Select(N.getOperand(0));
2260 break;
2261 }
2262 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2263 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2264 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2265 return;
2266 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002267
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002268 case ISD::BR: {
2269 Select(N.getOperand(0));
2270 MachineBasicBlock *Dest =
2271 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2272 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2273 // XXX HACK! we do _not_ need long branches all the time
2274 return;
2275 }
2276
2277 case ISD::ImplicitDef: {
2278 Select(N.getOperand(0));
2279 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2280 return;
2281 }
2282
2283 case ISD::BRCOND: {
2284 MachineBasicBlock *Dest =
2285 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2286
2287 Select(N.getOperand(0));
2288 Tmp1 = SelectExpr(N.getOperand(1));
2289 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2290 // XXX HACK! we do _not_ need long branches all the time
2291 return;
2292 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002293
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002294 case ISD::EXTLOAD:
2295 case ISD::ZEXTLOAD:
2296 case ISD::SEXTLOAD:
2297 case ISD::LOAD:
2298 case ISD::CALL:
2299 case ISD::CopyFromReg:
2300 case ISD::DYNAMIC_STACKALLOC:
2301 SelectExpr(N);
2302 return;
2303
2304 case ISD::TRUNCSTORE:
2305 case ISD::STORE: {
2306 Select(N.getOperand(0));
2307 Tmp1 = SelectExpr(N.getOperand(1)); // value
2308
2309 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002310
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002311 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002312 switch (N.getOperand(1).getValueType()) {
2313 default: assert(0 && "Cannot store this type!");
2314 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2315 // FIXME?: for now, we treat bool loads the same as i8 stores */
2316 case MVT::i8: Opc = IA64::ST1; break;
2317 case MVT::i16: Opc = IA64::ST2; break;
2318 case MVT::i32: Opc = IA64::ST4; break;
2319 case MVT::i64: Opc = IA64::ST8; break;
2320
2321 case MVT::f32: Opc = IA64::STF4; break;
2322 case MVT::f64: Opc = IA64::STF8; break;
2323 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002324 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002325 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2326 default: assert(0 && "unknown type in truncstore");
2327 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2328 //FIXME: DAG does not promote this load?
2329 case MVT::i8: Opc = IA64::ST1; break;
2330 case MVT::i16: Opc = IA64::ST2; break;
2331 case MVT::i32: Opc = IA64::ST4; break;
2332 case MVT::f32: Opc = IA64::STF4; break;
2333 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002334 }
2335
2336 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002337 unsigned dummy = MakeReg(MVT::i64);
2338 unsigned dummy2 = MakeReg(MVT::i64);
2339 BuildMI(BB, IA64::ADD, 2, dummy)
2340 .addGlobalAddress(cast<GlobalAddressSDNode>
2341 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2342 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002343
Misha Brukman7847fca2005-04-22 17:54:37 +00002344 if(!isBool)
2345 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2346 else { // we are storing a bool, so emit a little pseudocode
2347 // to store a predicate register as one byte
2348 assert(Opc==IA64::ST1);
2349 unsigned dummy3 = MakeReg(MVT::i64);
2350 unsigned dummy4 = MakeReg(MVT::i64);
2351 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2352 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2353 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2354 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2355 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002356 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2357
Misha Brukman7847fca2005-04-22 17:54:37 +00002358 // FIXME? (what about bools?)
2359
2360 unsigned dummy = MakeReg(MVT::i64);
2361 BuildMI(BB, IA64::MOV, 1, dummy)
2362 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2363 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002364 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002365 Tmp2 = SelectExpr(N.getOperand(2)); //address
2366 if(!isBool)
2367 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2368 else { // we are storing a bool, so emit a little pseudocode
2369 // to store a predicate register as one byte
2370 assert(Opc==IA64::ST1);
2371 unsigned dummy3 = MakeReg(MVT::i64);
2372 unsigned dummy4 = MakeReg(MVT::i64);
2373 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2374 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2375 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2376 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2377 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002378 }
2379 return;
2380 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002381
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002382 case ISD::ADJCALLSTACKDOWN:
2383 case ISD::ADJCALLSTACKUP: {
2384 Select(N.getOperand(0));
2385 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002386
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002387 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
2388 IA64::ADJUSTCALLSTACKUP;
2389 BuildMI(BB, Opc, 1).addImm(Tmp1);
2390 return;
2391 }
2392
2393 return;
2394 }
2395 assert(0 && "GAME OVER. INSERT COIN?");
2396}
2397
2398
2399/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2400/// into a machine code representation using pattern matching and a machine
2401/// description file.
2402///
2403FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002404 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002405}
2406
2407