blob: 5ac5df5c93a889eb86506b5c2e6749d27a883003 [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 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001057 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1058 // for i1, we load the condition into an integer register, then
1059 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1060 // of them will go through, since the integer register will hold
1061 // either 0 or 1)
1062 case MVT::i1: {
1063 bogoResult=MakeReg(MVT::i1);
1064
1065 // load the condition into an integer register
1066 unsigned condReg=MakeReg(MVT::i64);
1067 unsigned dummy=MakeReg(MVT::i64);
1068 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1069 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1070 .addImm(1).addReg(Tmp1);
1071
1072 // initialize Result (bool) to false (hence UNC) and if
1073 // the select condition (condReg) is false (0), copy Tmp3
1074 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1075 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1076
1077 // now, if the selection condition is true, write 1 to the
1078 // result if Tmp2 is 1
1079 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1080 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1081 break;
1082 }
1083 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1084 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001085 case MVT::i64:
1086 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001087 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1088 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1089 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001090 break;
1091 case MVT::f64:
1092 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001093 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1094 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1095 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001096 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001097 }
Duraid Madina4bd708d2005-05-02 06:41:13 +00001098
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001099 return Result;
1100 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001101
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001102 case ISD::Constant: {
1103 unsigned depositPos=0;
1104 unsigned depositLen=0;
1105 switch (N.getValueType()) {
1106 default: assert(0 && "Cannot use constants of this type!");
1107 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001108 // the constant:
1109 if(cast<ConstantSDNode>(N)->getValue()) // true:
1110 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1111 else // false:
1112 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1113 return Result; // early exit
1114 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001115 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001116 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001117
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001118 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001119
1120 if(immediate==0) { // if the constant is just zero,
1121 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1122 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001123 }
1124
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001125 if (immediate <= 8191 && immediate >= -8192) {
1126 // if this constants fits in 14 bits, we use a mov the assembler will
1127 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1128 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1129 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001131
1132 if (immediate <= 2097151 && immediate >= -2097152) {
1133 // if this constants fits in 22 bits, we use a mov the assembler will
1134 // turn into: "addl rDest=imm,r0"
1135 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1136 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001137 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001138
1139 /* otherwise, our immediate is big, so we use movl */
1140 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001141 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001142 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001143 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001144
1145 case ISD::UNDEF: {
1146 BuildMI(BB, IA64::IDEF, 0, Result);
1147 return Result;
1148 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001149
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001150 case ISD::GlobalAddress: {
1151 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1152 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001153
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001154 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001155 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001156
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001157 return Result;
1158 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001159
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001160 case ISD::ExternalSymbol: {
1161 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001162// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1163 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001164 return Result;
1165 }
1166
1167 case ISD::FP_EXTEND: {
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1170 return Result;
1171 }
1172
1173 case ISD::ZERO_EXTEND: {
1174 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001175
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001176 switch (N.getOperand(0).getValueType()) {
1177 default: assert(0 && "Cannot zero-extend this type!");
1178 case MVT::i8: Opc = IA64::ZXT1; break;
1179 case MVT::i16: Opc = IA64::ZXT2; break;
1180 case MVT::i32: Opc = IA64::ZXT4; break;
1181
Misha Brukman4633f1c2005-04-21 23:13:11 +00001182 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001183 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001184 unsigned dummy = MakeReg(MVT::i64);
1185 // first load zero:
1186 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1187 // ...then conditionally (PR:Tmp1) add 1:
1188 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1189 .addImm(1).addReg(Tmp1);
1190 return Result; // XXX early exit!
1191 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001192 }
1193
1194 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1195 return Result;
1196 }
1197
1198 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1199
1200assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1201
1202 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001203
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001204 switch (N.getOperand(0).getValueType()) {
1205 default: assert(0 && "Cannot sign-extend this type!");
1206 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001207 Opc = IA64::SXT1; break;
1208 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001209 case MVT::i8: Opc = IA64::SXT1; break;
1210 case MVT::i16: Opc = IA64::SXT2; break;
1211 case MVT::i32: Opc = IA64::SXT4; break;
1212 }
1213
1214 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1215 return Result;
1216 }
1217
1218 case ISD::TRUNCATE: {
1219 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1220 // of R0 appropriately.
1221 switch (N.getOperand(0).getValueType()) {
1222 default: assert(0 && "Unknown truncate!");
1223 case MVT::i64: break;
1224 }
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 unsigned depositPos, depositLen;
1227
1228 switch (N.getValueType()) {
1229 default: assert(0 && "Unknown truncate!");
1230 case MVT::i1: {
1231 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001232 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1233 .addReg(IA64::r0);
1234 return Result; // XXX early exit!
1235 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001236 case MVT::i8: depositPos=0; depositLen=8; break;
1237 case MVT::i16: depositPos=0; depositLen=16; break;
1238 case MVT::i32: depositPos=0; depositLen=32; break;
1239 }
1240 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1241 .addImm(depositPos).addImm(depositLen);
1242 return Result;
1243 }
1244
Misha Brukman7847fca2005-04-22 17:54:37 +00001245/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001246 case ISD::FP_ROUND: {
1247 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001248 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001249 Tmp1 = SelectExpr(N.getOperand(0));
1250 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1251 // we add 0.0 using a single precision add to do rounding
1252 return Result;
1253 }
1254*/
1255
1256// FIXME: the following 4 cases need cleaning
1257 case ISD::SINT_TO_FP: {
1258 Tmp1 = SelectExpr(N.getOperand(0));
1259 Tmp2 = MakeReg(MVT::f64);
1260 unsigned dummy = MakeReg(MVT::f64);
1261 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1262 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1263 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1264 return Result;
1265 }
1266
1267 case ISD::UINT_TO_FP: {
1268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = MakeReg(MVT::f64);
1270 unsigned dummy = MakeReg(MVT::f64);
1271 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1272 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1273 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1274 return Result;
1275 }
1276
1277 case ISD::FP_TO_SINT: {
1278 Tmp1 = SelectExpr(N.getOperand(0));
1279 Tmp2 = MakeReg(MVT::f64);
1280 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1281 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1282 return Result;
1283 }
1284
1285 case ISD::FP_TO_UINT: {
1286 Tmp1 = SelectExpr(N.getOperand(0));
1287 Tmp2 = MakeReg(MVT::f64);
1288 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1289 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1290 return Result;
1291 }
1292
1293 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001294 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1295 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1296 // into an fma, do so:
1297 // ++FusedFP; // Statistic
1298 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1299 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1300 Tmp3 = SelectExpr(N.getOperand(1));
1301 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1302 return Result; // early exit
1303 }
Duraid Madinaed095022005-04-13 06:12:04 +00001304
1305 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001306 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001307 // this add into a shladd, try:
1308 ConstantSDNode *CSD = NULL;
1309 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001310 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001311
Misha Brukman7847fca2005-04-22 17:54:37 +00001312 // ++FusedSHLADD; // Statistic
1313 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1314 int shl_amt = CSD->getValue();
1315 Tmp3 = SelectExpr(N.getOperand(1));
1316
1317 BuildMI(BB, IA64::SHLADD, 3, Result)
1318 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1319 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001320 }
1321 }
1322
1323 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001324 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001325 if(DestType != MVT::f64) { // integer addition:
1326 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001327 case 1: // adding a constant that's 14 bits
1328 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1329 return Result; // early exit
1330 } // fallthrough and emit a reg+reg ADD:
1331 Tmp2 = SelectExpr(N.getOperand(1));
1332 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001333 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001334 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001335 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1336 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001337 return Result;
1338 }
1339
1340 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001341
1342 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001343 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1344 // boring old integer multiply with xma
1345 Tmp1 = SelectExpr(N.getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(1));
1347
1348 unsigned TempFR1=MakeReg(MVT::f64);
1349 unsigned TempFR2=MakeReg(MVT::f64);
1350 unsigned TempFR3=MakeReg(MVT::f64);
1351 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1352 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1353 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1354 .addReg(IA64::F0);
1355 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1356 return Result; // early exit
1357 } else { // we are multiplying by an integer constant! yay
1358 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1359 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001360 }
Duraid Madinab2322562005-04-26 07:23:02 +00001361 else { // floating point multiply
1362 Tmp1 = SelectExpr(N.getOperand(0));
1363 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001364 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001365 return Result;
1366 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001367 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001369 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001370 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1371 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1372 // into an fms, do so:
1373 // ++FusedFP; // Statistic
1374 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1375 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1376 Tmp3 = SelectExpr(N.getOperand(1));
1377 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1378 return Result; // early exit
1379 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001380 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001381 if(DestType != MVT::f64) { // integer subtraction:
1382 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001383 case 1: // subtracting *from* an 8 bit constant:
1384 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1385 return Result; // early exit
1386 } // fallthrough and emit a reg+reg SUB:
1387 Tmp1 = SelectExpr(N.getOperand(0));
1388 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001389 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001390 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001391 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001392 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001393 return Result;
1394 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001395
1396 case ISD::FABS: {
1397 Tmp1 = SelectExpr(N.getOperand(0));
1398 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1399 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1400 return Result;
1401 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001402
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001403 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001404 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001405
Misha Brukman4633f1c2005-04-21 23:13:11 +00001406 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001407 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1408 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1409 } else {
1410 Tmp1 = SelectExpr(N.getOperand(0));
1411 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1412 }
1413
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001414 return Result;
1415 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001416
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001417 case ISD::AND: {
1418 switch (N.getValueType()) {
1419 default: assert(0 && "Cannot AND this type!");
1420 case MVT::i1: { // if a bool, we emit a pseudocode AND
1421 unsigned pA = SelectExpr(N.getOperand(0));
1422 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001423
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001424/* our pseudocode for AND is:
1425 *
1426(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1427 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1428 ;;
1429(pB) cmp.ne pTemp,p0 = r0,r0
1430 ;;
1431(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1432
1433*/
1434 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001435
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001436 unsigned bogusTemp1 = MakeReg(MVT::i1);
1437 unsigned bogusTemp2 = MakeReg(MVT::i1);
1438 unsigned bogusTemp3 = MakeReg(MVT::i1);
1439 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001440
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001441 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001442 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001443 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001444 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001445 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001446 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001447 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001448 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001449 break;
1450 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001451
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001452 // if not a bool, we just AND away:
1453 case MVT::i8:
1454 case MVT::i16:
1455 case MVT::i32:
1456 case MVT::i64: {
1457 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001458 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1459 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001460 switch (Tmp3) {
1461 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1462 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1463 break;
1464 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1465 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1466 break;
1467 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1468 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1469 break;
1470 default: // otherwise, use dep.z to paste zeros
1471 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1472 .addImm(0).addImm(Tmp3);
1473 break;
1474 }
1475 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001476 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001477 Tmp2 = SelectExpr(N.getOperand(1));
1478 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001479 }
1480 }
1481 return Result;
1482 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001483
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001484 case ISD::OR: {
1485 switch (N.getValueType()) {
1486 default: assert(0 && "Cannot OR this type!");
1487 case MVT::i1: { // if a bool, we emit a pseudocode OR
1488 unsigned pA = SelectExpr(N.getOperand(0));
1489 unsigned pB = SelectExpr(N.getOperand(1));
1490
1491 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001492
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001493/* our pseudocode for OR is:
1494 *
1495
1496pC = pA OR pB
1497-------------
1498
Misha Brukman7847fca2005-04-22 17:54:37 +00001499(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1500 ;;
1501(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001502
1503*/
1504 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001505 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001506 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001507 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001508 break;
1509 }
1510 // if not a bool, we just OR away:
1511 case MVT::i8:
1512 case MVT::i16:
1513 case MVT::i32:
1514 case MVT::i64: {
1515 Tmp1 = SelectExpr(N.getOperand(0));
1516 Tmp2 = SelectExpr(N.getOperand(1));
1517 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1518 break;
1519 }
1520 }
1521 return Result;
1522 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001523
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001524 case ISD::XOR: {
1525 switch (N.getValueType()) {
1526 default: assert(0 && "Cannot XOR this type!");
1527 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1528 unsigned pY = SelectExpr(N.getOperand(0));
1529 unsigned pZ = SelectExpr(N.getOperand(1));
1530
1531/* one possible routine for XOR is:
1532
1533 // Compute px = py ^ pz
1534 // using sum of products: px = (py & !pz) | (pz & !py)
1535 // Uses 5 instructions in 3 cycles.
1536 // cycle 1
1537(pz) cmp.eq.unc px = r0, r0 // px = pz
1538(py) cmp.eq.unc pt = r0, r0 // pt = py
1539 ;;
1540 // cycle 2
1541(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1542(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1543 ;;
1544 } { .mmi
1545 // cycle 3
1546(pt) cmp.eq.or px = r0, r0 // px = px | pt
1547
1548*** Another, which we use here, requires one scratch GR. it is:
1549
1550 mov rt = 0 // initialize rt off critical path
1551 ;;
1552
1553 // cycle 1
1554(pz) cmp.eq.unc px = r0, r0 // px = pz
1555(pz) mov rt = 1 // rt = pz
1556 ;;
1557 // cycle 2
1558(py) cmp.ne px = 1, rt // if (py) px = !pz
1559
1560.. these routines kindly provided by Jim Hull
1561*/
1562 unsigned rt = MakeReg(MVT::i64);
1563
1564 // these two temporaries will never actually appear,
1565 // due to the two-address form of some of the instructions below
1566 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1567 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1568
1569 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1570 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001571 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001572 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001573 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001574 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001575 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001576 break;
1577 }
1578 // if not a bool, we just XOR away:
1579 case MVT::i8:
1580 case MVT::i16:
1581 case MVT::i32:
1582 case MVT::i64: {
1583 Tmp1 = SelectExpr(N.getOperand(0));
1584 Tmp2 = SelectExpr(N.getOperand(1));
1585 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1586 break;
1587 }
1588 }
1589 return Result;
1590 }
1591
1592 case ISD::SHL: {
1593 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001594 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1595 Tmp2 = CN->getValue();
1596 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1597 } else {
1598 Tmp2 = SelectExpr(N.getOperand(1));
1599 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1600 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001601 return Result;
1602 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001603
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001604 case ISD::SRL: {
1605 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001606 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1607 Tmp2 = CN->getValue();
1608 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1609 } else {
1610 Tmp2 = SelectExpr(N.getOperand(1));
1611 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1612 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001613 return Result;
1614 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001615
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001616 case ISD::SRA: {
1617 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1619 Tmp2 = CN->getValue();
1620 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1621 } else {
1622 Tmp2 = SelectExpr(N.getOperand(1));
1623 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1624 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001625 return Result;
1626 }
1627
1628 case ISD::SDIV:
1629 case ISD::UDIV:
1630 case ISD::SREM:
1631 case ISD::UREM: {
1632
1633 Tmp1 = SelectExpr(N.getOperand(0));
1634 Tmp2 = SelectExpr(N.getOperand(1));
1635
1636 bool isFP=false;
1637
1638 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1639 isFP=true;
1640
1641 bool isModulus=false; // is it a division or a modulus?
1642 bool isSigned=false;
1643
1644 switch(N.getOpcode()) {
1645 case ISD::SDIV: isModulus=false; isSigned=true; break;
1646 case ISD::UDIV: isModulus=false; isSigned=false; break;
1647 case ISD::SREM: isModulus=true; isSigned=true; break;
1648 case ISD::UREM: isModulus=true; isSigned=false; break;
1649 }
1650
Duraid Madina4826a072005-04-06 09:55:17 +00001651 if(!isModulus && !isFP) { // if this is an integer divide,
1652 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001653 case 1: // division by a constant that's a power of 2
1654 Tmp1 = SelectExpr(N.getOperand(0));
1655 if(isSigned) { // argument could be negative, so emit some code:
1656 unsigned divAmt=Tmp3;
1657 unsigned tempGR1=MakeReg(MVT::i64);
1658 unsigned tempGR2=MakeReg(MVT::i64);
1659 unsigned tempGR3=MakeReg(MVT::i64);
1660 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1661 .addReg(Tmp1).addImm(divAmt-1);
1662 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1663 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1664 BuildMI(BB, IA64::ADD, 2, tempGR3)
1665 .addReg(Tmp1).addReg(tempGR2);
1666 BuildMI(BB, IA64::SHRS, 2, Result)
1667 .addReg(tempGR3).addImm(divAmt);
1668 }
1669 else // unsigned div-by-power-of-2 becomes a simple shift right:
1670 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1671 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001672 }
1673 }
1674
Misha Brukman4633f1c2005-04-21 23:13:11 +00001675 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001676 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001677 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1678 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1679 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1680 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1681 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1682 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1683 unsigned TmpF7=MakeReg(MVT::f64);
1684 unsigned TmpF8=MakeReg(MVT::f64);
1685 unsigned TmpF9=MakeReg(MVT::f64);
1686 unsigned TmpF10=MakeReg(MVT::f64);
1687 unsigned TmpF11=MakeReg(MVT::f64);
1688 unsigned TmpF12=MakeReg(MVT::f64);
1689 unsigned TmpF13=MakeReg(MVT::f64);
1690 unsigned TmpF14=MakeReg(MVT::f64);
1691 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001692
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001693 // OK, emit some code:
1694
1695 if(!isFP) {
1696 // first, load the inputs into FP regs.
1697 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1698 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001699
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001700 // next, convert the inputs to FP
1701 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001702 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1703 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001704 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001705 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1706 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001707 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001708
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001709 } else { // this is an FP divide/remainder, so we 'leak' some temp
1710 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1711 TmpF3=Tmp1;
1712 TmpF4=Tmp2;
1713 }
1714
1715 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001716 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1717 BuildMI(BB, IA64::FRCPAS1, 4)
1718 .addReg(TmpF5, MachineOperand::Def)
1719 .addReg(TmpPR, MachineOperand::Def)
1720 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001721
Duraid Madinabeeaab22005-03-31 12:31:11 +00001722 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1723 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1724 // TPCMPNE below
1725 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1726 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001727 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001728 }
1729
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001730 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1731 // precision, don't need this much for f32/i32)
1732 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1733 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1734 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1735 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1736 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1737 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1738 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1739 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1740 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1741 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1742 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1743 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1744 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1745 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1746 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1747 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001748
1749 // FIXME: this is unfortunate :(
1750 // the story is that the dest reg of the fnma above and the fma below
1751 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1752 // be the same register, or this code breaks if the first argument is
1753 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001754 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1755 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1756
Duraid Madina6e02e682005-04-04 05:05:52 +00001757 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1758 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1759 }
1760
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001761 if(!isFP) {
1762 // round to an integer
1763 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001764 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001765 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001767 } else {
1768 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1769 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1770 // we really do need the above FMOV? ;)
1771 }
1772
1773 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001774 if(isFP) { // extra worrying about div-by-zero
1775 unsigned bogoResult=MakeReg(MVT::f64);
1776
1777 // we do a 'conditional fmov' (of the correct result, depending
1778 // on how the frcpa predicate turned out)
1779 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001780 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001781 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001782 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001783 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001784 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001785 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001786 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001787 } else { // this is a modulus
1788 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001789 // answer = q * (-b) + a
1790 unsigned ModulusResult = MakeReg(MVT::f64);
1791 unsigned TmpF = MakeReg(MVT::f64);
1792 unsigned TmpI = MakeReg(MVT::i64);
1793
1794 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1795 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1796 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1797 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1798 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001799 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001800 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001801 }
1802 }
1803
1804 return Result;
1805 }
1806
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001807 case ISD::SIGN_EXTEND_INREG: {
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1810 switch(MVN->getExtraValueType())
1811 {
1812 default:
1813 Node->dump();
1814 assert(0 && "don't know how to sign extend this type");
1815 break;
1816 case MVT::i8: Opc = IA64::SXT1; break;
1817 case MVT::i16: Opc = IA64::SXT2; break;
1818 case MVT::i32: Opc = IA64::SXT4; break;
1819 }
1820 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1821 return Result;
1822 }
1823
1824 case ISD::SETCC: {
1825 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001826
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001827 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1828 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001829
Misha Brukman7847fca2005-04-22 17:54:37 +00001830 if(ConstantSDNode *CSDN =
1831 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1832 // if we are comparing against a constant zero
1833 if(CSDN->getValue()==0)
1834 Tmp2 = IA64::r0; // then we can just compare against r0
1835 else
1836 Tmp2 = SelectExpr(N.getOperand(1));
1837 } else // not comparing against a constant
1838 Tmp2 = SelectExpr(N.getOperand(1));
1839
1840 switch (SetCC->getCondition()) {
1841 default: assert(0 && "Unknown integer comparison!");
1842 case ISD::SETEQ:
1843 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 break;
1845 case ISD::SETGT:
1846 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1847 break;
1848 case ISD::SETGE:
1849 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1850 break;
1851 case ISD::SETLT:
1852 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1853 break;
1854 case ISD::SETLE:
1855 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1856 break;
1857 case ISD::SETNE:
1858 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1859 break;
1860 case ISD::SETULT:
1861 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1862 break;
1863 case ISD::SETUGT:
1864 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1865 break;
1866 case ISD::SETULE:
1867 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1868 break;
1869 case ISD::SETUGE:
1870 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 break;
1872 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001873 }
1874 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001875 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1876 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001877
Misha Brukman7847fca2005-04-22 17:54:37 +00001878 if(ConstantFPSDNode *CFPSDN =
1879 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001880
Misha Brukman7847fca2005-04-22 17:54:37 +00001881 // if we are comparing against a constant +0.0 or +1.0
1882 if(CFPSDN->isExactlyValue(+0.0))
1883 Tmp2 = IA64::F0; // then we can just compare against f0
1884 else if(CFPSDN->isExactlyValue(+1.0))
1885 Tmp2 = IA64::F1; // or f1
1886 else
1887 Tmp2 = SelectExpr(N.getOperand(1));
1888 } else // not comparing against a constant
1889 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001890
Misha Brukman7847fca2005-04-22 17:54:37 +00001891 switch (SetCC->getCondition()) {
1892 default: assert(0 && "Unknown FP comparison!");
1893 case ISD::SETEQ:
1894 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1895 break;
1896 case ISD::SETGT:
1897 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1898 break;
1899 case ISD::SETGE:
1900 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1901 break;
1902 case ISD::SETLT:
1903 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1904 break;
1905 case ISD::SETLE:
1906 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 break;
1908 case ISD::SETNE:
1909 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1910 break;
1911 case ISD::SETULT:
1912 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1913 break;
1914 case ISD::SETUGT:
1915 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1916 break;
1917 case ISD::SETULE:
1918 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1919 break;
1920 case ISD::SETUGE:
1921 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1922 break;
1923 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001924 }
1925 }
1926 else
1927 assert(0 && "this setcc not implemented yet");
1928
1929 return Result;
1930 }
1931
1932 case ISD::EXTLOAD:
1933 case ISD::ZEXTLOAD:
1934 case ISD::LOAD: {
1935 // Make sure we generate both values.
1936 if (Result != 1)
1937 ExprMap[N.getValue(1)] = 1; // Generate the token
1938 else
1939 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1940
1941 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001942
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001943 if(opcode == ISD::LOAD) { // this is a LOAD
1944 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001945 default: assert(0 && "Cannot load this type!");
1946 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1947 // FIXME: for now, we treat bool loads the same as i8 loads */
1948 case MVT::i8: Opc = IA64::LD1; break;
1949 case MVT::i16: Opc = IA64::LD2; break;
1950 case MVT::i32: Opc = IA64::LD4; break;
1951 case MVT::i64: Opc = IA64::LD8; break;
1952
1953 case MVT::f32: Opc = IA64::LDF4; break;
1954 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001955 }
1956 } else { // this is an EXTLOAD or ZEXTLOAD
1957 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1958 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001959 default: assert(0 && "Cannot extload/zextload this type!");
1960 // FIXME: bools?
1961 case MVT::i8: Opc = IA64::LD1; break;
1962 case MVT::i16: Opc = IA64::LD2; break;
1963 case MVT::i32: Opc = IA64::LD4; break;
1964 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001965 }
1966 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001967
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001968 SDOperand Chain = N.getOperand(0);
1969 SDOperand Address = N.getOperand(1);
1970
1971 if(Address.getOpcode() == ISD::GlobalAddress) {
1972 Select(Chain);
1973 unsigned dummy = MakeReg(MVT::i64);
1974 unsigned dummy2 = MakeReg(MVT::i64);
1975 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001976 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1977 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001978 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1979 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001980 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001981 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001982 // into a predicate register
1983 assert(Opc==IA64::LD1 && "problem loading a bool");
1984 unsigned dummy3 = MakeReg(MVT::i64);
1985 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1986 // we compare to 0. true? 0. false? 1.
1987 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001988 }
1989 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1990 Select(Chain);
1991 IA64Lowering.restoreGP(BB);
1992 unsigned dummy = MakeReg(MVT::i64);
1993 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001994 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001995 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 // into a predicate register
1999 assert(Opc==IA64::LD1 && "problem loading a bool");
2000 unsigned dummy3 = MakeReg(MVT::i64);
2001 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2002 // we compare to 0. true? 0. false? 1.
2003 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002004 }
2005 } else if(Address.getOpcode() == ISD::FrameIndex) {
2006 Select(Chain); // FIXME ? what about bools?
2007 unsigned dummy = MakeReg(MVT::i64);
2008 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00002009 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002010 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002011 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002012 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002013 // into a predicate register
2014 assert(Opc==IA64::LD1 && "problem loading a bool");
2015 unsigned dummy3 = MakeReg(MVT::i64);
2016 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
2017 // we compare to 0. true? 0. false? 1.
2018 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002019 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002020 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002021 Select(Chain);
2022 Tmp2 = SelectExpr(Address);
2023 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00002024 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002025 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00002026 // into a predicate register
2027 assert(Opc==IA64::LD1 && "problem loading a bool");
2028 unsigned dummy = MakeReg(MVT::i64);
2029 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2030 // we compare to 0. true? 0. false? 1.
2031 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
2032 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002033 }
2034
2035 return Result;
2036 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002037
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002038 case ISD::CopyFromReg: {
2039 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002040 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002041 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002042
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002043 SDOperand Chain = N.getOperand(0);
2044
2045 Select(Chain);
2046 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2047
2048 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002049 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2050 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002051 // (r) Result =cmp.eq.unc(r0,r0)
2052 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002053 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002054 return Result;
2055 }
2056
2057 case ISD::CALL: {
2058 Select(N.getOperand(0));
2059
2060 // The chain for this call is now lowered.
2061 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002062
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002063 //grab the arguments
2064 std::vector<unsigned> argvregs;
2065
2066 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002067 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002068
2069 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002070 // Runtime Architecture Guide to see some examples of what's going
2071 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2072 // while FP args get mapped to F8->F15 as needed)
2073
2074 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002075
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002076 // in reg args
2077 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2078 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002079 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2080 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2081 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2082 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002083
Misha Brukman7847fca2005-04-22 17:54:37 +00002084 switch(N.getOperand(i+2).getValueType())
2085 {
2086 default: // XXX do we need to support MVT::i1 here?
2087 Node->dump();
2088 N.getOperand(i).Val->dump();
2089 std::cerr << "Type for " << i << " is: " <<
2090 N.getOperand(i+2).getValueType() << std::endl;
2091 assert(0 && "Unknown value type for call");
2092 case MVT::i64:
2093 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2094 break;
2095 case MVT::f64:
2096 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2097 .addReg(argvregs[i]);
2098 // FIXME: we don't need to do this _all_ the time:
2099 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2100 break;
2101 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002102 }
2103
2104 //in mem args
2105 for (int i = 8, e = argvregs.size(); i < e; ++i)
2106 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002107 unsigned tempAddr = MakeReg(MVT::i64);
2108
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002109 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002110 default:
2111 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002112 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002113 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002114 N.getOperand(i+2).getValueType() << "\n";
2115 assert(0 && "Unknown value type for call");
2116 case MVT::i1: // FIXME?
2117 case MVT::i8:
2118 case MVT::i16:
2119 case MVT::i32:
2120 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002121 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2122 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2123 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002124 break;
2125 case MVT::f32:
2126 case MVT::f64:
2127 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002128 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2129 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002130 break;
2131 }
2132 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002133
2134 /* XXX we want to re-enable direct branches! crippling them now
Misha Brukman4633f1c2005-04-21 23:13:11 +00002135 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002136 //build the right kind of call
2137 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002138 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002139 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002140 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2141 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002142 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002143 ^^^^^^^^^^^^^ we want this code one day XXX */
Duraid Madinabeeaab22005-03-31 12:31:11 +00002144 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002145 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002146 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002147 // "non-pic code with imm relocation against dynamic symbol" errors
2148 BuildMI(BB, IA64::BRCALL, 1)
2149 .addExternalSymbol(ESSDN->getSymbol(), true);
2150 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002151 }
2152 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002153 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002154
2155 unsigned targetEntryPoint=MakeReg(MVT::i64);
2156 unsigned targetGPAddr=MakeReg(MVT::i64);
2157 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002158
Duraid Madinabeeaab22005-03-31 12:31:11 +00002159 // b6 is a scratch branch register, we load the target entry point
2160 // from the base of the function descriptor
2161 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2162 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2163
2164 // save the current GP:
2165 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002166
Duraid Madinabeeaab22005-03-31 12:31:11 +00002167 /* TODO: we need to make sure doing this never, ever loads a
2168 * bogus value into r1 (GP). */
2169 // load the target GP (which is at mem[functiondescriptor+8])
2170 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002171 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002172 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2173
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002174 // and then jump: (well, call)
2175 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002176 // and finally restore the old GP
2177 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2178 IA64Lowering.restoreSP_RP(BB);
2179 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002180
2181 switch (Node->getValueType(0)) {
2182 default: assert(0 && "Unknown value type for call result!");
2183 case MVT::Other: return 1;
2184 case MVT::i1:
2185 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002186 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002187 break;
2188 case MVT::i8:
2189 case MVT::i16:
2190 case MVT::i32:
2191 case MVT::i64:
2192 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2193 break;
2194 case MVT::f64:
2195 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2196 break;
2197 }
2198 return Result+N.ResNo;
2199 }
2200
Misha Brukman4633f1c2005-04-21 23:13:11 +00002201 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002202 return 0;
2203}
2204
2205void ISel::Select(SDOperand N) {
2206 unsigned Tmp1, Tmp2, Opc;
2207 unsigned opcode = N.getOpcode();
2208
Nate Begeman85fdeb22005-03-24 04:39:54 +00002209 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002210 return; // Already selected.
2211
2212 SDNode *Node = N.Val;
2213
2214 switch (Node->getOpcode()) {
2215 default:
2216 Node->dump(); std::cerr << "\n";
2217 assert(0 && "Node not handled yet!");
2218
2219 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002220
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002221 case ISD::TokenFactor: {
2222 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2223 Select(Node->getOperand(i));
2224 return;
2225 }
2226
2227 case ISD::CopyToReg: {
2228 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002229 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002230 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002231
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002232 if (Tmp1 != Tmp2) {
2233 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002234 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2235 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002236 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2237 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002238 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002239 // XXX is this the right way 'round? ;)
2240 }
2241 return;
2242 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002243
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002244 case ISD::RET: {
2245
2246 /* what the heck is going on here:
2247
2248<_sabre_> ret with two operands is obvious: chain and value
2249<camel_> yep
2250<_sabre_> ret with 3 values happens when 'expansion' occurs
2251<_sabre_> e.g. i64 gets split into 2x i32
2252<camel_> oh right
2253<_sabre_> you don't have this case on ia64
2254<camel_> yep
2255<_sabre_> so the two returned values go into EAX/EDX on ia32
2256<camel_> ahhh *memories*
2257<_sabre_> :)
2258<camel_> ok, thanks :)
2259<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2260<_sabre_> this is the first operand always
2261<_sabre_> these operand often define chains, they are the last operand
2262<_sabre_> they are printed as 'ch' if you do DAG.dump()
2263 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002264
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002265 switch (N.getNumOperands()) {
2266 default:
2267 assert(0 && "Unknown return instruction!");
2268 case 2:
2269 Select(N.getOperand(0));
2270 Tmp1 = SelectExpr(N.getOperand(1));
2271 switch (N.getOperand(1).getValueType()) {
2272 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002273 // FIXME: do I need to add support for bools here?
2274 // (return '0' or '1' r8, basically...)
2275 //
2276 // FIXME: need to round floats - 80 bits is bad, the tester
2277 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002278 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002279 // we mark r8 as live on exit up above in LowerArguments()
2280 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2281 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002282 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002283 // we mark F8 as live on exit up above in LowerArguments()
2284 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002285 }
2286 break;
2287 case 1:
2288 Select(N.getOperand(0));
2289 break;
2290 }
2291 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2292 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2293 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2294 return;
2295 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002296
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002297 case ISD::BR: {
2298 Select(N.getOperand(0));
2299 MachineBasicBlock *Dest =
2300 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2301 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2302 // XXX HACK! we do _not_ need long branches all the time
2303 return;
2304 }
2305
2306 case ISD::ImplicitDef: {
2307 Select(N.getOperand(0));
2308 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2309 return;
2310 }
2311
2312 case ISD::BRCOND: {
2313 MachineBasicBlock *Dest =
2314 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2315
2316 Select(N.getOperand(0));
2317 Tmp1 = SelectExpr(N.getOperand(1));
2318 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2319 // XXX HACK! we do _not_ need long branches all the time
2320 return;
2321 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002322
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002323 case ISD::EXTLOAD:
2324 case ISD::ZEXTLOAD:
2325 case ISD::SEXTLOAD:
2326 case ISD::LOAD:
2327 case ISD::CALL:
2328 case ISD::CopyFromReg:
2329 case ISD::DYNAMIC_STACKALLOC:
2330 SelectExpr(N);
2331 return;
2332
2333 case ISD::TRUNCSTORE:
2334 case ISD::STORE: {
2335 Select(N.getOperand(0));
2336 Tmp1 = SelectExpr(N.getOperand(1)); // value
2337
2338 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002339
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002340 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002341 switch (N.getOperand(1).getValueType()) {
2342 default: assert(0 && "Cannot store this type!");
2343 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2344 // FIXME?: for now, we treat bool loads the same as i8 stores */
2345 case MVT::i8: Opc = IA64::ST1; break;
2346 case MVT::i16: Opc = IA64::ST2; break;
2347 case MVT::i32: Opc = IA64::ST4; break;
2348 case MVT::i64: Opc = IA64::ST8; break;
2349
2350 case MVT::f32: Opc = IA64::STF4; break;
2351 case MVT::f64: Opc = IA64::STF8; break;
2352 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002353 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002354 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2355 default: assert(0 && "unknown type in truncstore");
2356 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2357 //FIXME: DAG does not promote this load?
2358 case MVT::i8: Opc = IA64::ST1; break;
2359 case MVT::i16: Opc = IA64::ST2; break;
2360 case MVT::i32: Opc = IA64::ST4; break;
2361 case MVT::f32: Opc = IA64::STF4; break;
2362 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002363 }
2364
2365 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002366 unsigned dummy = MakeReg(MVT::i64);
2367 unsigned dummy2 = MakeReg(MVT::i64);
2368 BuildMI(BB, IA64::ADD, 2, dummy)
2369 .addGlobalAddress(cast<GlobalAddressSDNode>
2370 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2371 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002372
Misha Brukman7847fca2005-04-22 17:54:37 +00002373 if(!isBool)
2374 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2375 else { // we are storing a bool, so emit a little pseudocode
2376 // to store a predicate register as one byte
2377 assert(Opc==IA64::ST1);
2378 unsigned dummy3 = MakeReg(MVT::i64);
2379 unsigned dummy4 = MakeReg(MVT::i64);
2380 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2381 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2382 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2383 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2384 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002385 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2386
Misha Brukman7847fca2005-04-22 17:54:37 +00002387 // FIXME? (what about bools?)
2388
2389 unsigned dummy = MakeReg(MVT::i64);
2390 BuildMI(BB, IA64::MOV, 1, dummy)
2391 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2392 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002393 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002394 Tmp2 = SelectExpr(N.getOperand(2)); //address
2395 if(!isBool)
2396 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2397 else { // we are storing a bool, so emit a little pseudocode
2398 // to store a predicate register as one byte
2399 assert(Opc==IA64::ST1);
2400 unsigned dummy3 = MakeReg(MVT::i64);
2401 unsigned dummy4 = MakeReg(MVT::i64);
2402 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2403 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2404 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2405 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2406 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002407 }
2408 return;
2409 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002410
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002411 case ISD::ADJCALLSTACKDOWN:
2412 case ISD::ADJCALLSTACKUP: {
2413 Select(N.getOperand(0));
2414 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002415
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002416 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
2417 IA64::ADJUSTCALLSTACKUP;
2418 BuildMI(BB, Opc, 1).addImm(Tmp1);
2419 return;
2420 }
2421
2422 return;
2423 }
2424 assert(0 && "GAME OVER. INSERT COIN?");
2425}
2426
2427
2428/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2429/// into a machine code representation using pattern matching and a machine
2430/// description file.
2431///
2432FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002433 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002434}
2435
2436