blob: e2a5a988d6c0dec1eeb71313164f03c3976f49ab [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000060 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
61
Misha Brukman4633f1c2005-04-21 23:13:11 +000062 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000063 setShiftAmountType(MVT::i64);
64
65 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000066
67 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000068
69 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000073
74 setOperationAction(ISD::SREM , MVT::f32 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
76
77 setOperationAction(ISD::UREM , MVT::f32 , Expand);
78 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000079
Duraid Madina9b9d45f2005-03-17 18:17:03 +000080 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
81 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
82 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
83
Duraid Madina9b9d45f2005-03-17 18:17:03 +000084 computeRegisterProperties();
85
86 addLegalFPImmediate(+0.0);
87 addLegalFPImmediate(+1.0);
88 addLegalFPImmediate(-0.0);
89 addLegalFPImmediate(-1.0);
90 }
91
92 /// LowerArguments - This hook must be implemented to indicate how we should
93 /// lower the arguments for the specified function, into the specified DAG.
94 virtual std::vector<SDOperand>
95 LowerArguments(Function &F, SelectionDAG &DAG);
96
97 /// LowerCallTo - This hook lowers an abstract call to a function into an
98 /// actual call.
99 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000100 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
101 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000102
103 virtual std::pair<SDOperand, SDOperand>
104 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
105
106 virtual std::pair<SDOperand,SDOperand>
107 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
108 const Type *ArgTy, SelectionDAG &DAG);
109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
112 SelectionDAG &DAG);
113
114 void restoreGP_SP_RP(MachineBasicBlock* BB)
115 {
116 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
117 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
118 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
119 }
120
Duraid Madinabeeaab22005-03-31 12:31:11 +0000121 void restoreSP_RP(MachineBasicBlock* BB)
122 {
123 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
124 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
125 }
126
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000127 void restoreRP(MachineBasicBlock* BB)
128 {
129 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
130 }
131
132 void restoreGP(MachineBasicBlock* BB)
133 {
134 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
135 }
136
137 };
138}
139
140
141std::vector<SDOperand>
142IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
143 std::vector<SDOperand> ArgValues;
144
145 //
146 // add beautiful description of IA64 stack frame format
147 // here (from intel 24535803.pdf most likely)
148 //
149 MachineFunction &MF = DAG.getMachineFunction();
150 MachineFrameInfo *MFI = MF.getFrameInfo();
151
152 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
153 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
154 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
155
156 MachineBasicBlock& BB = MF.front();
157
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000159 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000160
161 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000162 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000163
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000164 unsigned argVreg[8];
165 unsigned argPreg[8];
166 unsigned argOpc[8];
167
Duraid Madinabeeaab22005-03-31 12:31:11 +0000168 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000169
Duraid Madinabeeaab22005-03-31 12:31:11 +0000170 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000171 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000173 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 {
175 SDOperand newroot, argt;
176 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000177
178 switch (getValueType(I->getType())) {
179 default:
180 std::cerr << "ERROR in LowerArgs: unknown type "
181 << getValueType(I->getType()) << "\n";
182 abort();
183 case MVT::f32:
184 // fixme? (well, will need to for weird FP structy stuff,
185 // see intel ABI docs)
186 case MVT::f64:
187//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
188 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
189 // floating point args go into f8..f15 as-needed, the increment
190 argVreg[count] = // is below..:
191 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
192 // FP args go into f8..f15 as needed: (hence the ++)
193 argPreg[count] = args_FP[used_FPArgs++];
194 argOpc[count] = IA64::FMOV;
195 argt = newroot = DAG.getCopyFromReg(argVreg[count],
196 getValueType(I->getType()), DAG.getRoot());
197 break;
198 case MVT::i1: // NOTE: as far as C abi stuff goes,
199 // bools are just boring old ints
200 case MVT::i8:
201 case MVT::i16:
202 case MVT::i32:
203 case MVT::i64:
204//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
205 MF.addLiveIn(args_int[count]); // mark this register as liveIn
206 argVreg[count] =
207 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
208 argPreg[count] = args_int[count];
209 argOpc[count] = IA64::MOV;
210 argt = newroot =
211 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
212 if ( getValueType(I->getType()) != MVT::i64)
213 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
214 newroot);
215 break;
216 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000217 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000218 // Create the frame index object for this incoming parameter...
219 ArgOffset = 16 + 8 * (count - 8);
220 int FI = MFI->CreateFixedObject(8, ArgOffset);
221
222 // Create the SelectionDAG nodes corresponding to a load
223 //from this parameter
224 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
225 argt = newroot = DAG.getLoad(getValueType(I->getType()),
226 DAG.getEntryNode(), FIN);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000227 }
228 ++count;
229 DAG.setRoot(newroot.getValue(1));
230 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000231 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000232
Misha Brukman4633f1c2005-04-21 23:13:11 +0000233
Duraid Madinabeeaab22005-03-31 12:31:11 +0000234 // Create a vreg to hold the output of (what will become)
235 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000236 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
237 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
238 // we create a PSEUDO_ALLOC (pseudo)instruction for now
239
240 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
241
242 // hmm:
243 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
244 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
245 // ..hmm.
246
247 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
248
249 // hmm:
250 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
251 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
252 // ..hmm.
253
Duraid Madinabeeaab22005-03-31 12:31:11 +0000254 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000255
Duraid Madinabeeaab22005-03-31 12:31:11 +0000256 // if this is a varargs function, we simply lower llvm.va_start by
257 // pointing to the first entry
258 if(F.isVarArg()) {
259 tempOffset=0;
260 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000261 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000262
Duraid Madinabeeaab22005-03-31 12:31:11 +0000263 // here we actually do the moving of args, and store them to the stack
264 // too if this is a varargs function:
265 for (int i = 0; i < count && i < 8; ++i) {
266 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
267 if(F.isVarArg()) {
268 // if this is a varargs function, we copy the input registers to the stack
269 int FI = MFI->CreateFixedObject(8, tempOffset);
270 tempOffset+=8; //XXX: is it safe to use r22 like this?
271 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
272 // FIXME: we should use st8.spill here, one day
273 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
274 }
275 }
276
Duraid Madinaca494fd2005-04-12 14:54:44 +0000277 // Finally, inform the code generator which regs we return values in.
278 // (see the ISD::RET: case down below)
279 switch (getValueType(F.getReturnType())) {
280 default: assert(0 && "i have no idea where to return this type!");
281 case MVT::isVoid: break;
282 case MVT::i1:
283 case MVT::i8:
284 case MVT::i16:
285 case MVT::i32:
286 case MVT::i64:
287 MF.addLiveOut(IA64::r8);
288 break;
289 case MVT::f32:
290 case MVT::f64:
291 MF.addLiveOut(IA64::F8);
292 break;
293 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000294
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000295 return ArgValues;
296}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000297
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000298std::pair<SDOperand, SDOperand>
299IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000300 const Type *RetTy, bool isVarArg,
301 SDOperand Callee, ArgListTy &Args,
302 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000303
304 MachineFunction &MF = DAG.getMachineFunction();
305
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000306 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000307 unsigned outRegsUsed = 0;
308
309 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000310 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000311 outRegsUsed = 8;
312 } else {
313 outRegsUsed = Args.size();
314 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000315
Duraid Madinabeeaab22005-03-31 12:31:11 +0000316 // FIXME? this WILL fail if we ever try to pass around an arg that
317 // consumes more than a single output slot (a 'real' double, int128
318 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
319 // registers we use. Hopefully, the assembler will notice.
320 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
321 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000322
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000323 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
324 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000325
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000326 std::vector<SDOperand> args_to_use;
327 for (unsigned i = 0, e = Args.size(); i != e; ++i)
328 {
329 switch (getValueType(Args[i].second)) {
330 default: assert(0 && "unexpected argument type!");
331 case MVT::i1:
332 case MVT::i8:
333 case MVT::i16:
334 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000335 //promote to 64-bits, sign/zero extending based on type
336 //of the argument
337 if(Args[i].second->isSigned())
338 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
339 Args[i].first);
340 else
341 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
342 Args[i].first);
343 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000344 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000345 //promote to 64-bits
346 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000347 case MVT::f64:
348 case MVT::i64:
349 break;
350 }
351 args_to_use.push_back(Args[i].first);
352 }
353
354 std::vector<MVT::ValueType> RetVals;
355 MVT::ValueType RetTyVT = getValueType(RetTy);
356 if (RetTyVT != MVT::isVoid)
357 RetVals.push_back(RetTyVT);
358 RetVals.push_back(MVT::Other);
359
360 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000361 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000362 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
363 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
364 DAG.getConstant(NumBytes, getPointerTy()));
365 return std::make_pair(TheCall, Chain);
366}
367
368std::pair<SDOperand, SDOperand>
369IA64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
370 // vastart just returns the address of the VarArgsFrameIndex slot.
371 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
372}
373
374std::pair<SDOperand,SDOperand> IA64TargetLowering::
375LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
376 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000377
378 MVT::ValueType ArgVT = getValueType(ArgTy);
379 SDOperand Result;
380 if (!isVANext) {
381 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
382 } else {
383 unsigned Amt;
384 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
385 Amt = 8;
386 else {
387 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
388 "Other types should have been promoted for varargs!");
389 Amt = 8;
390 }
391 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
392 DAG.getConstant(Amt, VAList.getValueType()));
393 }
394 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000395}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000396
397std::pair<SDOperand, SDOperand> IA64TargetLowering::
398LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
399 SelectionDAG &DAG) {
400
401 assert(0 && "LowerFrameReturnAddress not done yet\n");
Duraid Madina817aed42005-03-17 19:00:40 +0000402 abort();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000403}
404
405
406namespace {
407
408 //===--------------------------------------------------------------------===//
409 /// ISel - IA64 specific code to select IA64 machine instructions for
410 /// SelectionDAG operations.
411 ///
412 class ISel : public SelectionDAGISel {
413 /// IA64Lowering - This object fully describes how to lower LLVM code to an
414 /// IA64-specific SelectionDAG.
415 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000416 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
417 // for sdiv and udiv until it is put into the future
418 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000419
420 /// ExprMap - As shared expressions are codegen'd, we keep track of which
421 /// vreg the value is produced in, so we only emit one copy of each compiled
422 /// tree.
423 std::map<SDOperand, unsigned> ExprMap;
424 std::set<SDOperand> LoweredTokens;
425
426 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000427 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
428 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000429
430 /// InstructionSelectBasicBlock - This callback is invoked by
431 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
432 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
433
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000434 unsigned SelectExpr(SDOperand N);
435 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000436 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
437 SDOperand BuildConstmulSequence(SDOperand N);
438
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000439 };
440}
441
442/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
443/// when it has created a SelectionDAG for us to codegen.
444void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
445
446 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000447 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000448 Select(DAG.getRoot());
449
450 // Clear state used for selection.
451 ExprMap.clear();
452 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000453 ISelDAG = 0;
454}
455
456const char sign[2]={'+','-'};
457
458
459// strip leading '0' characters from a string
460void munchLeadingZeros(std::string& inString) {
461 while(inString.c_str()[0]=='0') {
462 inString.erase(0, 1);
463 }
464}
465
466// strip trailing '0' characters from a string
467void munchTrailingZeros(std::string& inString) {
468 int curPos=inString.length()-1;
469
470 while(inString.c_str()[curPos]=='0') {
471 inString.erase(curPos, 1);
472 curPos--;
473 }
474}
475
476// return how many consecutive '0' characters are at the end of a string
477unsigned int countTrailingZeros(std::string& inString) {
478 int curPos=inString.length()-1;
479 unsigned int zeroCount=0;
480 // assert goes here
481 while(inString.c_str()[curPos--]=='0') {
482 zeroCount++;
483 }
484 return zeroCount;
485}
486
487// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
488// '0' and 'N' (-1) characters)
489void boothEncode(std::string inString, std::string& boothEncodedString) {
490
491 int curpos=0;
492 int replacements=0;
493 int lim=inString.size();
494
495 while(curpos<lim) {
496 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
497 int runlength=0;
498 std::string replaceString="N";
499
500 // find the run length
501 for(;inString[curpos+runlength]=='1';runlength++) ;
502
503 for(int i=0; i<runlength-1; i++)
504 replaceString+="0";
505 replaceString+="1";
506
507 if(runlength>1) {
508 inString.replace(curpos, runlength+1, replaceString);
509 curpos+=runlength-1;
510 } else
511 curpos++;
512 } else { // a zero, we just keep chugging along
513 curpos++;
514 }
515 }
516
517 // clean up (trim the string, reverse it and turn '1's into 'P's)
518 munchTrailingZeros(inString);
519 boothEncodedString="";
520
521 for(int i=inString.size()-1;i>=0;i--)
522 if(inString[i]=='1')
523 boothEncodedString+="P";
524 else
525 boothEncodedString+=inString[i];
526
527}
528
529struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
530 unsigned firstVal; // A
531 unsigned firstShift; // B
532 unsigned secondVal; // C
533 unsigned secondShift; // D
534 bool isSub;
535};
536
537/* this implements Lefevre's "pattern-based" constant multiplication,
538 * see "Multiplication by an Integer Constant", INRIA report 1999-06
539 *
540 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
541 * to get better booth encodings - this does help in practice
542 * TODO: weight shifts appropriately (most architectures can't
543 * fuse a shift and an add for arbitrary shift amounts) */
544unsigned lefevre(const std::string inString,
545 std::vector<struct shiftaddblob> &ops) {
546 std::string retstring;
547 std::string s = inString;
548 munchTrailingZeros(s);
549
550 int length=s.length()-1;
551
552 if(length==0) {
553 return(0);
554 }
555
556 std::vector<int> p,n;
557
558 for(int i=0; i<=length; i++) {
559 if (s.c_str()[length-i]=='P') {
560 p.push_back(i);
561 } else if (s.c_str()[length-i]=='N') {
562 n.push_back(i);
563 }
564 }
565
566 std::string t, u;
567 int c,f;
568 std::map<const int, int> w;
569
570 for(int i=0; i<p.size(); i++) {
571 for(int j=0; j<i; j++) {
572 w[p[i]-p[j]]++;
573 }
574 }
575
576 for(int i=1; i<n.size(); i++) {
577 for(int j=0; j<i; j++) {
578 w[n[i]-n[j]]++;
579 }
580 }
581
582 for(int i=0; i<p.size(); i++) {
583 for(int j=0; j<n.size(); j++) {
584 w[-abs(p[i]-n[j])]++;
585 }
586 }
587
588 std::map<const int, int>::const_iterator ii;
589 std::vector<int> d;
590 std::multimap<int, int> sorted_by_value;
591
592 for(ii = w.begin(); ii!=w.end(); ii++)
593 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
594
595 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
596 it != sorted_by_value.end(); ++it) {
597 d.push_back((*it).second);
598 }
599
600 int int_W=0;
601 int int_d;
602
603 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
604 d.pop_back();
605 retstring=s; // hmmm
606 int x=0;
607 int z=abs(int_d)-1;
608
609 if(int_d>0) {
610
611 for(int base=0; base<retstring.size(); base++) {
612 if( ((base+z+1) < retstring.size()) &&
613 retstring.c_str()[base]=='P' &&
614 retstring.c_str()[base+z+1]=='P')
615 {
616 // match
617 x++;
618 retstring.replace(base, 1, "0");
619 retstring.replace(base+z+1, 1, "p");
620 }
621 }
622
623 for(int base=0; base<retstring.size(); base++) {
624 if( ((base+z+1) < retstring.size()) &&
625 retstring.c_str()[base]=='N' &&
626 retstring.c_str()[base+z+1]=='N')
627 {
628 // match
629 x++;
630 retstring.replace(base, 1, "0");
631 retstring.replace(base+z+1, 1, "n");
632 }
633 }
634
635 } else {
636 for(int base=0; base<retstring.size(); base++) {
637 if( ((base+z+1) < retstring.size()) &&
638 ((retstring.c_str()[base]=='P' &&
639 retstring.c_str()[base+z+1]=='N') ||
640 (retstring.c_str()[base]=='N' &&
641 retstring.c_str()[base+z+1]=='P')) ) {
642 // match
643 x++;
644
645 if(retstring.c_str()[base]=='P') {
646 retstring.replace(base, 1, "0");
647 retstring.replace(base+z+1, 1, "p");
648 } else { // retstring[base]=='N'
649 retstring.replace(base, 1, "0");
650 retstring.replace(base+z+1, 1, "n");
651 }
652 }
653 }
654 }
655
656 if(x>int_W) {
657 int_W = x;
658 t = retstring;
659 c = int_d; // tofix
660 }
661
662 } d.pop_back(); // hmm
663
664 u = t;
665
666 for(int i=0; i<t.length(); i++) {
667 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
668 t.replace(i, 1, "0");
669 }
670
Duraid Madinab2322562005-04-26 07:23:02 +0000671 for(int i=0; i<u.length(); i++) {
672 if(u.c_str()[i]=='P' || u.c_str()[i]=='N')
673 u.replace(i, 1, "0");
674 if(u.c_str()[i]=='p')
675 u.replace(i, 1, "P");
676 if(u.c_str()[i]=='n')
677 u.replace(i, 1, "N");
678 }
679
680 if( c<0 ) {
681 f=1;
682 c=-c;
683 } else
684 f=0;
685
686 bool hit=true;
687 for(int i=0; i<u.length()-1; i++) {
688 if(u.c_str()[i]!='0')
689 hit=false;
690 }
691 if(u.c_str()[u.length()-1]!='N')
692 hit=false;
693
694 int g=0;
695 if(hit) {
696 g=1;
697 for(int p=0; p<u.length(); p++) {
698 bool isP=(u.c_str()[p]=='P');
699 bool isN=(u.c_str()[p]=='N');
700
701 if(isP)
702 u.replace(p, 1, "N");
703 if(isN)
704 u.replace(p, 1, "P");
705 }
706 }
707
708 munchLeadingZeros(u);
709
710 int i = lefevre(u, ops);
711
712 shiftaddblob blob;
713
714 blob.firstVal=i; blob.firstShift=c;
715 blob.isSub=f;
716 blob.secondVal=i; blob.secondShift=0;
717
718 ops.push_back(blob);
719
720 i = ops.size();
721
722 munchLeadingZeros(t);
723
724 if(t.length()==0)
725 return i;
726
727 if(t.c_str()[0]!='P') {
728 g=2;
729 for(int p=0; p<t.length(); p++) {
730 bool isP=(t.c_str()[p]=='P');
731 bool isN=(t.c_str()[p]=='N');
732
733 if(isP)
734 t.replace(p, 1, "N");
735 if(isN)
736 t.replace(p, 1, "P");
737 }
738 }
739
740 int j = lefevre(t, ops);
741
742 int trail=countTrailingZeros(u);
743 blob.secondVal=i; blob.secondShift=trail;
744
745 trail=countTrailingZeros(t);
746 blob.firstVal=j; blob.firstShift=trail;
747
748 switch(g) {
749 case 0:
750 blob.isSub=false; // first + second
751 break;
752 case 1:
753 blob.isSub=true; // first - second
754 break;
755 case 2:
756 blob.isSub=true; // second - first
757 int tmpval, tmpshift;
758 tmpval=blob.firstVal;
759 tmpshift=blob.firstShift;
760 blob.firstVal=blob.secondVal;
761 blob.firstShift=blob.secondShift;
762 blob.secondVal=tmpval;
763 blob.secondShift=tmpshift;
764 break;
765 //assert
766 }
767
768 ops.push_back(blob);
769 return ops.size();
770}
771
772SDOperand ISel::BuildConstmulSequence(SDOperand N) {
773 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
774 // in particular, *3 is nicer as *2+1, not *4-1
775 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
776
777 bool flippedSign;
778 unsigned preliminaryShift=0;
779
780 assert(constant > 0 && "erk, don't multiply by zero or negative nums\n");
781
782 // first, we make the constant to multiply by positive
783 if(constant<0) {
784 constant=-constant;
785 flippedSign=true;
786 } else {
787 flippedSign=false;
788 }
789
790 // next, we make it odd.
791 for(; (constant%2==0); preliminaryShift++)
792 constant>>=1;
793
794 //OK, we have a positive, odd number of 64 bits or less. Convert it
795 //to a binary string, constantString[0] is the LSB
796 char constantString[65];
797 for(int i=0; i<64; i++)
798 constantString[i]='0'+((constant>>i)&0x1);
799 constantString[64]=0;
800
801 // now, Booth encode it
802 std::string boothEncodedString;
803 boothEncode(constantString, boothEncodedString);
804
805 std::vector<struct shiftaddblob> ops;
806 // do the transformation, filling out 'ops'
807 lefevre(boothEncodedString, ops);
808
809 SDOperand results[ops.size()]; // temporary results (of adds/subs of shifts)
810
811 // now turn 'ops' into DAG bits
812 for(int i=0; i<ops.size(); i++) {
813 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
814 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
815 results[ops[i].firstVal-1];
816 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
817 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
818 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
819 results[ops[i].secondVal-1];
820 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
821 if(ops[i].isSub)
822 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
823 else
824 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
825 }
826
827 // don't forget flippedSign and preliminaryShift!
828 SDOperand finalresult;
829 if(preliminaryShift) {
830 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
831 finalresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
832 results[ops.size()-1], finalshift);
833 } else { // there was no preliminary divide-by-power-of-2 required
834 finalresult = results[ops.size()-1];
835 }
836
837 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000838}
839
Duraid Madina4826a072005-04-06 09:55:17 +0000840/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
841/// returns zero when the input is not exactly a power of two.
Duraid Madinac02780e2005-04-13 04:50:54 +0000842static unsigned ExactLog2(uint64_t Val) {
Duraid Madina4826a072005-04-06 09:55:17 +0000843 if (Val == 0 || (Val & (Val-1))) return 0;
844 unsigned Count = 0;
845 while (Val != 1) {
846 Val >>= 1;
847 ++Count;
848 }
849 return Count;
850}
851
Duraid Madinac02780e2005-04-13 04:50:54 +0000852/// ExactLog2sub1 - This function solves for (Val == (1 << (N-1))-1)
853/// and returns N. It returns 666 if Val is not 2^n -1 for some n.
854static unsigned ExactLog2sub1(uint64_t Val) {
855 unsigned int n;
856 for(n=0; n<64; n++) {
Duraid Madina3eb71502005-04-14 10:06:35 +0000857 if(Val==(uint64_t)((1LL<<n)-1))
Duraid Madinac02780e2005-04-13 04:50:54 +0000858 return n;
859 }
860 return 666;
861}
862
Duraid Madina4826a072005-04-06 09:55:17 +0000863/// ponderIntegerDivisionBy - When handling integer divides, if the divide
864/// is by a constant such that we can efficiently codegen it, this
865/// function says what to do. Currently, it returns 0 if the division must
866/// become a genuine divide, and 1 if the division can be turned into a
867/// right shift.
868static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
869 unsigned& Imm) {
870 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
871 // a constant, give up.
872
873 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
874
Misha Brukman4633f1c2005-04-21 23:13:11 +0000875 if ((Imm = ExactLog2(v))) { // if a division by a power of two, say so
Duraid Madina4826a072005-04-06 09:55:17 +0000876 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000877 }
878
Duraid Madina4826a072005-04-06 09:55:17 +0000879 return 0; // fallthrough
880}
881
Duraid Madinac02780e2005-04-13 04:50:54 +0000882static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
883 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
884 // a constant, give up.
885
886 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
887
888 if ((Imm = ExactLog2sub1(v))!=666) { // if ANDing with ((2^n)-1) for some n
889 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000890 }
891
Duraid Madinac02780e2005-04-13 04:50:54 +0000892 return 0; // fallthrough
893}
894
Duraid Madinaf55e4032005-04-07 12:33:38 +0000895static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
896 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
897 // constant, give up.
898 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
899
900 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
901 Imm = v & 0x3FFF; // 14 bits
902 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000903 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000904 return 0; // fallthrough
905}
906
907static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
908 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
909 // constant, give up.
910 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
911
912 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
913 Imm = v & 0xFF; // 8 bits
914 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000915 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000916 return 0; // fallthrough
917}
918
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000919unsigned ISel::SelectExpr(SDOperand N) {
920 unsigned Result;
921 unsigned Tmp1, Tmp2, Tmp3;
922 unsigned Opc = 0;
923 MVT::ValueType DestType = N.getValueType();
924
925 unsigned opcode = N.getOpcode();
926
927 SDNode *Node = N.Val;
928 SDOperand Op0, Op1;
929
930 if (Node->getOpcode() == ISD::CopyFromReg)
931 // Just use the specified register as our input.
932 return dyn_cast<RegSDNode>(Node)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000933
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000934 unsigned &Reg = ExprMap[N];
935 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000936
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000937 if (N.getOpcode() != ISD::CALL)
938 Reg = Result = (N.getValueType() != MVT::Other) ?
939 MakeReg(N.getValueType()) : 1;
940 else {
941 // If this is a call instruction, make sure to prepare ALL of the result
942 // values as well as the chain.
943 if (Node->getNumValues() == 1)
944 Reg = Result = 1; // Void call, just a chain.
945 else {
946 Result = MakeReg(Node->getValueType(0));
947 ExprMap[N.getValue(0)] = Result;
948 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
949 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
950 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
951 }
952 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000953
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000954 switch (N.getOpcode()) {
955 default:
956 Node->dump();
957 assert(0 && "Node not handled!\n");
958
959 case ISD::FrameIndex: {
960 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
961 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
962 return Result;
963 }
964
965 case ISD::ConstantPool: {
966 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
967 IA64Lowering.restoreGP(BB); // FIXME: do i really need this?
968 BuildMI(BB, IA64::ADD, 2, Result).addConstantPoolIndex(Tmp1)
969 .addReg(IA64::r1);
970 return Result;
971 }
972
973 case ISD::ConstantFP: {
974 Tmp1 = Result; // Intermediate Register
975 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
976 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
977 Tmp1 = MakeReg(MVT::f64);
978
979 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
980 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
981 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F0); // load 0.0
982 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
983 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
984 BuildMI(BB, IA64::FMOV, 1, Tmp1).addReg(IA64::F1); // load 1.0
985 else
986 assert(0 && "Unexpected FP constant!");
987 if (Tmp1 != Result)
988 // we multiply by +1.0, negate (this is FNMA), and then add 0.0
989 BuildMI(BB, IA64::FNMA, 3, Result).addReg(Tmp1).addReg(IA64::F1)
Misha Brukman7847fca2005-04-22 17:54:37 +0000990 .addReg(IA64::F0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000991 return Result;
992 }
993
994 case ISD::DYNAMIC_STACKALLOC: {
995 // Generate both result values.
996 if (Result != 1)
997 ExprMap[N.getValue(1)] = 1; // Generate the token
998 else
999 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1000
1001 // FIXME: We are currently ignoring the requested alignment for handling
1002 // greater than the stack alignment. This will need to be revisited at some
1003 // point. Align = N.getOperand(2);
1004
1005 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1006 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1007 std::cerr << "Cannot allocate stack object with greater alignment than"
1008 << " the stack alignment yet!";
1009 abort();
1010 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001011
1012/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001013 Select(N.getOperand(0));
1014 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1015 {
1016 if (CN->getValue() < 32000)
1017 {
1018 BuildMI(BB, IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
Misha Brukman7847fca2005-04-22 17:54:37 +00001019 .addImm(-CN->getValue());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001020 } else {
1021 Tmp1 = SelectExpr(N.getOperand(1));
1022 // Subtract size from stack pointer, thereby allocating some space.
1023 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1024 }
1025 } else {
1026 Tmp1 = SelectExpr(N.getOperand(1));
1027 // Subtract size from stack pointer, thereby allocating some space.
1028 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
1029 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00001030*/
1031 Select(N.getOperand(0));
1032 Tmp1 = SelectExpr(N.getOperand(1));
1033 // Subtract size from stack pointer, thereby allocating some space.
1034 BuildMI(BB, IA64::SUB, 2, IA64::r12).addReg(IA64::r12).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001035 // Put a pointer to the space into the result register, by copying the
1036 // stack pointer.
1037 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r12);
1038 return Result;
1039 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001040
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001041 case ISD::SELECT: {
1042 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1043 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1044 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1045
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001046 unsigned bogoResult;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001047
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001048 switch (N.getOperand(1).getValueType()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001049 default: assert(0 &&
1050 "ISD::SELECT: 'select'ing something other than i64 or f64!\n");
1051 case MVT::i64:
1052 bogoResult=MakeReg(MVT::i64);
1053 break;
1054 case MVT::f64:
1055 bogoResult=MakeReg(MVT::f64);
1056 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001057 }
Duraid Madina69c8e202005-04-01 10:35:00 +00001058
1059 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1060 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001061 .addReg(Tmp1); // FIXME: should be FMOV/FCMOV sometimes,
Duraid Madina69c8e202005-04-01 10:35:00 +00001062 // though this will work for now (no JIT)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001063 return Result;
1064 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001065
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001066 case ISD::Constant: {
1067 unsigned depositPos=0;
1068 unsigned depositLen=0;
1069 switch (N.getValueType()) {
1070 default: assert(0 && "Cannot use constants of this type!");
1071 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001072 // the constant:
1073 if(cast<ConstantSDNode>(N)->getValue()) // true:
1074 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1075 else // false:
1076 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1077 return Result; // early exit
1078 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001079 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001080 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001081
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001082 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001083
1084 if(immediate==0) { // if the constant is just zero,
1085 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1086 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001087 }
1088
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001089 if (immediate <= 8191 && immediate >= -8192) {
1090 // if this constants fits in 14 bits, we use a mov the assembler will
1091 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1092 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1093 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001094 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001095
1096 if (immediate <= 2097151 && immediate >= -2097152) {
1097 // if this constants fits in 22 bits, we use a mov the assembler will
1098 // turn into: "addl rDest=imm,r0"
1099 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1100 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001101 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001102
1103 /* otherwise, our immediate is big, so we use movl */
1104 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001105 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001106 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001107 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001108
1109 case ISD::UNDEF: {
1110 BuildMI(BB, IA64::IDEF, 0, Result);
1111 return Result;
1112 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001113
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001114 case ISD::GlobalAddress: {
1115 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1116 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001117
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001118 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001119 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001120
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001121 return Result;
1122 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001123
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001124 case ISD::ExternalSymbol: {
1125 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001126// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1127 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001128 return Result;
1129 }
1130
1131 case ISD::FP_EXTEND: {
1132 Tmp1 = SelectExpr(N.getOperand(0));
1133 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1134 return Result;
1135 }
1136
1137 case ISD::ZERO_EXTEND: {
1138 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001140 switch (N.getOperand(0).getValueType()) {
1141 default: assert(0 && "Cannot zero-extend this type!");
1142 case MVT::i8: Opc = IA64::ZXT1; break;
1143 case MVT::i16: Opc = IA64::ZXT2; break;
1144 case MVT::i32: Opc = IA64::ZXT4; break;
1145
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146 // we handle bools differently! :
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 case MVT::i1: { // if the predicate reg has 1, we want a '1' in our GR.
Misha Brukman7847fca2005-04-22 17:54:37 +00001148 unsigned dummy = MakeReg(MVT::i64);
1149 // first load zero:
1150 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1151 // ...then conditionally (PR:Tmp1) add 1:
1152 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1153 .addImm(1).addReg(Tmp1);
1154 return Result; // XXX early exit!
1155 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001156 }
1157
1158 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1159 return Result;
1160 }
1161
1162 case ISD::SIGN_EXTEND: { // we should only have to handle i1 -> i64 here!!!
1163
1164assert(0 && "hmm, ISD::SIGN_EXTEND: shouldn't ever be reached. bad luck!\n");
1165
1166 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001167
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001168 switch (N.getOperand(0).getValueType()) {
1169 default: assert(0 && "Cannot sign-extend this type!");
1170 case MVT::i1: assert(0 && "trying to sign extend a bool? ow.\n");
Misha Brukman7847fca2005-04-22 17:54:37 +00001171 Opc = IA64::SXT1; break;
1172 // FIXME: for now, we treat bools the same as i8s
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001173 case MVT::i8: Opc = IA64::SXT1; break;
1174 case MVT::i16: Opc = IA64::SXT2; break;
1175 case MVT::i32: Opc = IA64::SXT4; break;
1176 }
1177
1178 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1179 return Result;
1180 }
1181
1182 case ISD::TRUNCATE: {
1183 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1184 // of R0 appropriately.
1185 switch (N.getOperand(0).getValueType()) {
1186 default: assert(0 && "Unknown truncate!");
1187 case MVT::i64: break;
1188 }
1189 Tmp1 = SelectExpr(N.getOperand(0));
1190 unsigned depositPos, depositLen;
1191
1192 switch (N.getValueType()) {
1193 default: assert(0 && "Unknown truncate!");
1194 case MVT::i1: {
1195 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
Misha Brukman7847fca2005-04-22 17:54:37 +00001196 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1)
1197 .addReg(IA64::r0);
1198 return Result; // XXX early exit!
1199 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001200 case MVT::i8: depositPos=0; depositLen=8; break;
1201 case MVT::i16: depositPos=0; depositLen=16; break;
1202 case MVT::i32: depositPos=0; depositLen=32; break;
1203 }
1204 BuildMI(BB, IA64::DEPZ, 1, Result).addReg(Tmp1)
1205 .addImm(depositPos).addImm(depositLen);
1206 return Result;
1207 }
1208
Misha Brukman7847fca2005-04-22 17:54:37 +00001209/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001210 case ISD::FP_ROUND: {
1211 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001212 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001213 Tmp1 = SelectExpr(N.getOperand(0));
1214 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1215 // we add 0.0 using a single precision add to do rounding
1216 return Result;
1217 }
1218*/
1219
1220// FIXME: the following 4 cases need cleaning
1221 case ISD::SINT_TO_FP: {
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = MakeReg(MVT::f64);
1224 unsigned dummy = MakeReg(MVT::f64);
1225 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1226 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1227 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1228 return Result;
1229 }
1230
1231 case ISD::UINT_TO_FP: {
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = MakeReg(MVT::f64);
1234 unsigned dummy = MakeReg(MVT::f64);
1235 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1236 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1237 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1238 return Result;
1239 }
1240
1241 case ISD::FP_TO_SINT: {
1242 Tmp1 = SelectExpr(N.getOperand(0));
1243 Tmp2 = MakeReg(MVT::f64);
1244 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1245 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1246 return Result;
1247 }
1248
1249 case ISD::FP_TO_UINT: {
1250 Tmp1 = SelectExpr(N.getOperand(0));
1251 Tmp2 = MakeReg(MVT::f64);
1252 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1253 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1254 return Result;
1255 }
1256
1257 case ISD::ADD: {
Duraid Madina4826a072005-04-06 09:55:17 +00001258 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1259 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1260 // into an fma, do so:
1261 // ++FusedFP; // Statistic
1262 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1263 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1264 Tmp3 = SelectExpr(N.getOperand(1));
1265 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1266 return Result; // early exit
1267 }
Duraid Madinaed095022005-04-13 06:12:04 +00001268
1269 if(DestType != MVT::f64 && N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001270 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001271 // this add into a shladd, try:
1272 ConstantSDNode *CSD = NULL;
1273 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001274 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001275
Misha Brukman7847fca2005-04-22 17:54:37 +00001276 // ++FusedSHLADD; // Statistic
1277 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1278 int shl_amt = CSD->getValue();
1279 Tmp3 = SelectExpr(N.getOperand(1));
1280
1281 BuildMI(BB, IA64::SHLADD, 3, Result)
1282 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1283 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001284 }
1285 }
1286
1287 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001288 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001289 if(DestType != MVT::f64) { // integer addition:
1290 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001291 case 1: // adding a constant that's 14 bits
1292 BuildMI(BB, IA64::ADDIMM14, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1293 return Result; // early exit
1294 } // fallthrough and emit a reg+reg ADD:
1295 Tmp2 = SelectExpr(N.getOperand(1));
1296 BuildMI(BB, IA64::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001297 } else { // this is a floating point addition
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001298 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001299 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1300 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001301 return Result;
1302 }
1303
1304 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001305
1306 if(DestType != MVT::f64) { // TODO: speed!
Duraid Madinab2322562005-04-26 07:23:02 +00001307 if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1308 // boring old integer multiply with xma
1309 Tmp1 = SelectExpr(N.getOperand(0));
1310 Tmp2 = SelectExpr(N.getOperand(1));
1311
1312 unsigned TempFR1=MakeReg(MVT::f64);
1313 unsigned TempFR2=MakeReg(MVT::f64);
1314 unsigned TempFR3=MakeReg(MVT::f64);
1315 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1316 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1317 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1318 .addReg(IA64::F0);
1319 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1320 return Result; // early exit
1321 } else { // we are multiplying by an integer constant! yay
1322 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1323 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001324 }
Duraid Madinab2322562005-04-26 07:23:02 +00001325 else { // floating point multiply
1326 Tmp1 = SelectExpr(N.getOperand(0));
1327 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001328 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinab2322562005-04-26 07:23:02 +00001329 return Result;
1330 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001331 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001332
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001333 case ISD::SUB: {
Duraid Madina4826a072005-04-06 09:55:17 +00001334 if(DestType == MVT::f64 && N.getOperand(0).getOpcode() == ISD::MUL &&
1335 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1336 // into an fms, do so:
1337 // ++FusedFP; // Statistic
1338 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1339 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1340 Tmp3 = SelectExpr(N.getOperand(1));
1341 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1342 return Result; // early exit
1343 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001344 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001345 if(DestType != MVT::f64) { // integer subtraction:
1346 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001347 case 1: // subtracting *from* an 8 bit constant:
1348 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1349 return Result; // early exit
1350 } // fallthrough and emit a reg+reg SUB:
1351 Tmp1 = SelectExpr(N.getOperand(0));
1352 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001353 } else { // this is a floating point subtraction
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001354 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001355 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madinaf55e4032005-04-07 12:33:38 +00001356 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001357 return Result;
1358 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001359
1360 case ISD::FABS: {
1361 Tmp1 = SelectExpr(N.getOperand(0));
1362 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1363 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1364 return Result;
1365 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001367 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001368 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001369
Misha Brukman4633f1c2005-04-21 23:13:11 +00001370 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001371 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1372 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1373 } else {
1374 Tmp1 = SelectExpr(N.getOperand(0));
1375 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1376 }
1377
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001378 return Result;
1379 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001380
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001381 case ISD::AND: {
1382 switch (N.getValueType()) {
1383 default: assert(0 && "Cannot AND this type!");
1384 case MVT::i1: { // if a bool, we emit a pseudocode AND
1385 unsigned pA = SelectExpr(N.getOperand(0));
1386 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001387
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001388/* our pseudocode for AND is:
1389 *
1390(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1391 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1392 ;;
1393(pB) cmp.ne pTemp,p0 = r0,r0
1394 ;;
1395(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1396
1397*/
1398 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001399
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001400 unsigned bogusTemp1 = MakeReg(MVT::i1);
1401 unsigned bogusTemp2 = MakeReg(MVT::i1);
1402 unsigned bogusTemp3 = MakeReg(MVT::i1);
1403 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001404
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001405 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001406 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001408 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001409 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001410 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001411 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001412 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001413 break;
1414 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001415
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001416 // if not a bool, we just AND away:
1417 case MVT::i8:
1418 case MVT::i16:
1419 case MVT::i32:
1420 case MVT::i64: {
1421 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinac02780e2005-04-13 04:50:54 +00001422 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
1423 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001424 switch (Tmp3) {
1425 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1426 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1427 break;
1428 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1429 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1430 break;
1431 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1432 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1433 break;
1434 default: // otherwise, use dep.z to paste zeros
1435 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
1436 .addImm(0).addImm(Tmp3);
1437 break;
1438 }
1439 return Result; // early exit
Duraid Madinac02780e2005-04-13 04:50:54 +00001440 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001441 Tmp2 = SelectExpr(N.getOperand(1));
1442 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001443 }
1444 }
1445 return Result;
1446 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001447
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001448 case ISD::OR: {
1449 switch (N.getValueType()) {
1450 default: assert(0 && "Cannot OR this type!");
1451 case MVT::i1: { // if a bool, we emit a pseudocode OR
1452 unsigned pA = SelectExpr(N.getOperand(0));
1453 unsigned pB = SelectExpr(N.getOperand(1));
1454
1455 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001456
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001457/* our pseudocode for OR is:
1458 *
1459
1460pC = pA OR pB
1461-------------
1462
Misha Brukman7847fca2005-04-22 17:54:37 +00001463(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1464 ;;
1465(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001466
1467*/
1468 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001469 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001470 BuildMI(BB, IA64::TPCMPEQ, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001471 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001472 break;
1473 }
1474 // if not a bool, we just OR away:
1475 case MVT::i8:
1476 case MVT::i16:
1477 case MVT::i32:
1478 case MVT::i64: {
1479 Tmp1 = SelectExpr(N.getOperand(0));
1480 Tmp2 = SelectExpr(N.getOperand(1));
1481 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1482 break;
1483 }
1484 }
1485 return Result;
1486 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001487
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001488 case ISD::XOR: {
1489 switch (N.getValueType()) {
1490 default: assert(0 && "Cannot XOR this type!");
1491 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1492 unsigned pY = SelectExpr(N.getOperand(0));
1493 unsigned pZ = SelectExpr(N.getOperand(1));
1494
1495/* one possible routine for XOR is:
1496
1497 // Compute px = py ^ pz
1498 // using sum of products: px = (py & !pz) | (pz & !py)
1499 // Uses 5 instructions in 3 cycles.
1500 // cycle 1
1501(pz) cmp.eq.unc px = r0, r0 // px = pz
1502(py) cmp.eq.unc pt = r0, r0 // pt = py
1503 ;;
1504 // cycle 2
1505(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1506(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1507 ;;
1508 } { .mmi
1509 // cycle 3
1510(pt) cmp.eq.or px = r0, r0 // px = px | pt
1511
1512*** Another, which we use here, requires one scratch GR. it is:
1513
1514 mov rt = 0 // initialize rt off critical path
1515 ;;
1516
1517 // cycle 1
1518(pz) cmp.eq.unc px = r0, r0 // px = pz
1519(pz) mov rt = 1 // rt = pz
1520 ;;
1521 // cycle 2
1522(py) cmp.ne px = 1, rt // if (py) px = !pz
1523
1524.. these routines kindly provided by Jim Hull
1525*/
1526 unsigned rt = MakeReg(MVT::i64);
1527
1528 // these two temporaries will never actually appear,
1529 // due to the two-address form of some of the instructions below
1530 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1531 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1532
1533 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1534 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001535 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001536 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001537 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001538 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001539 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001540 break;
1541 }
1542 // if not a bool, we just XOR away:
1543 case MVT::i8:
1544 case MVT::i16:
1545 case MVT::i32:
1546 case MVT::i64: {
1547 Tmp1 = SelectExpr(N.getOperand(0));
1548 Tmp2 = SelectExpr(N.getOperand(1));
1549 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1550 break;
1551 }
1552 }
1553 return Result;
1554 }
1555
1556 case ISD::SHL: {
1557 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001558 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1559 Tmp2 = CN->getValue();
1560 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1561 } else {
1562 Tmp2 = SelectExpr(N.getOperand(1));
1563 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1564 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001565 return Result;
1566 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001567
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001568 case ISD::SRL: {
1569 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001570 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1571 Tmp2 = CN->getValue();
1572 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1573 } else {
1574 Tmp2 = SelectExpr(N.getOperand(1));
1575 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1576 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001577 return Result;
1578 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001579
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001580 case ISD::SRA: {
1581 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001582 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1583 Tmp2 = CN->getValue();
1584 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1585 } else {
1586 Tmp2 = SelectExpr(N.getOperand(1));
1587 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1588 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001589 return Result;
1590 }
1591
1592 case ISD::SDIV:
1593 case ISD::UDIV:
1594 case ISD::SREM:
1595 case ISD::UREM: {
1596
1597 Tmp1 = SelectExpr(N.getOperand(0));
1598 Tmp2 = SelectExpr(N.getOperand(1));
1599
1600 bool isFP=false;
1601
1602 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1603 isFP=true;
1604
1605 bool isModulus=false; // is it a division or a modulus?
1606 bool isSigned=false;
1607
1608 switch(N.getOpcode()) {
1609 case ISD::SDIV: isModulus=false; isSigned=true; break;
1610 case ISD::UDIV: isModulus=false; isSigned=false; break;
1611 case ISD::SREM: isModulus=true; isSigned=true; break;
1612 case ISD::UREM: isModulus=true; isSigned=false; break;
1613 }
1614
Duraid Madina4826a072005-04-06 09:55:17 +00001615 if(!isModulus && !isFP) { // if this is an integer divide,
1616 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001617 case 1: // division by a constant that's a power of 2
1618 Tmp1 = SelectExpr(N.getOperand(0));
1619 if(isSigned) { // argument could be negative, so emit some code:
1620 unsigned divAmt=Tmp3;
1621 unsigned tempGR1=MakeReg(MVT::i64);
1622 unsigned tempGR2=MakeReg(MVT::i64);
1623 unsigned tempGR3=MakeReg(MVT::i64);
1624 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1625 .addReg(Tmp1).addImm(divAmt-1);
1626 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1627 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1628 BuildMI(BB, IA64::ADD, 2, tempGR3)
1629 .addReg(Tmp1).addReg(tempGR2);
1630 BuildMI(BB, IA64::SHRS, 2, Result)
1631 .addReg(tempGR3).addImm(divAmt);
1632 }
1633 else // unsigned div-by-power-of-2 becomes a simple shift right:
1634 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1635 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001636 }
1637 }
1638
Misha Brukman4633f1c2005-04-21 23:13:11 +00001639 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001640 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001641 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1642 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1643 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1644 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1645 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1646 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1647 unsigned TmpF7=MakeReg(MVT::f64);
1648 unsigned TmpF8=MakeReg(MVT::f64);
1649 unsigned TmpF9=MakeReg(MVT::f64);
1650 unsigned TmpF10=MakeReg(MVT::f64);
1651 unsigned TmpF11=MakeReg(MVT::f64);
1652 unsigned TmpF12=MakeReg(MVT::f64);
1653 unsigned TmpF13=MakeReg(MVT::f64);
1654 unsigned TmpF14=MakeReg(MVT::f64);
1655 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001656
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001657 // OK, emit some code:
1658
1659 if(!isFP) {
1660 // first, load the inputs into FP regs.
1661 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1662 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001663
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001664 // next, convert the inputs to FP
1665 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001666 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1667 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001668 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001669 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1670 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001671 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001672
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001673 } else { // this is an FP divide/remainder, so we 'leak' some temp
1674 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1675 TmpF3=Tmp1;
1676 TmpF4=Tmp2;
1677 }
1678
1679 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001680 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1681 BuildMI(BB, IA64::FRCPAS1, 4)
1682 .addReg(TmpF5, MachineOperand::Def)
1683 .addReg(TmpPR, MachineOperand::Def)
1684 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001685
Duraid Madinabeeaab22005-03-31 12:31:11 +00001686 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1687 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1688 // TPCMPNE below
1689 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1690 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001691 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001692 }
1693
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001694 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1695 // precision, don't need this much for f32/i32)
1696 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1697 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1698 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1699 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1700 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1701 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1702 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1703 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1704 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1705 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1706 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1707 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1708 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1709 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1710 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1711 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001712
1713 // FIXME: this is unfortunate :(
1714 // the story is that the dest reg of the fnma above and the fma below
1715 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1716 // be the same register, or this code breaks if the first argument is
1717 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001718 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1719 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1720
Duraid Madina6e02e682005-04-04 05:05:52 +00001721 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1722 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1723 }
1724
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001725 if(!isFP) {
1726 // round to an integer
1727 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001728 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001729 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001730 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001731 } else {
1732 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1733 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1734 // we really do need the above FMOV? ;)
1735 }
1736
1737 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001738 if(isFP) { // extra worrying about div-by-zero
1739 unsigned bogoResult=MakeReg(MVT::f64);
1740
1741 // we do a 'conditional fmov' (of the correct result, depending
1742 // on how the frcpa predicate turned out)
1743 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001744 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001745 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001746 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001747 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001748 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001749 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001750 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001751 } else { // this is a modulus
1752 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001753 // answer = q * (-b) + a
1754 unsigned ModulusResult = MakeReg(MVT::f64);
1755 unsigned TmpF = MakeReg(MVT::f64);
1756 unsigned TmpI = MakeReg(MVT::i64);
1757
1758 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1759 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1760 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1761 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1762 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001763 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001764 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001765 }
1766 }
1767
1768 return Result;
1769 }
1770
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001771 case ISD::SIGN_EXTEND_INREG: {
1772 Tmp1 = SelectExpr(N.getOperand(0));
1773 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
1774 switch(MVN->getExtraValueType())
1775 {
1776 default:
1777 Node->dump();
1778 assert(0 && "don't know how to sign extend this type");
1779 break;
1780 case MVT::i8: Opc = IA64::SXT1; break;
1781 case MVT::i16: Opc = IA64::SXT2; break;
1782 case MVT::i32: Opc = IA64::SXT4; break;
1783 }
1784 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1785 return Result;
1786 }
1787
1788 case ISD::SETCC: {
1789 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001790
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001791 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1792 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001793
Misha Brukman7847fca2005-04-22 17:54:37 +00001794 if(ConstantSDNode *CSDN =
1795 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1796 // if we are comparing against a constant zero
1797 if(CSDN->getValue()==0)
1798 Tmp2 = IA64::r0; // then we can just compare against r0
1799 else
1800 Tmp2 = SelectExpr(N.getOperand(1));
1801 } else // not comparing against a constant
1802 Tmp2 = SelectExpr(N.getOperand(1));
1803
1804 switch (SetCC->getCondition()) {
1805 default: assert(0 && "Unknown integer comparison!");
1806 case ISD::SETEQ:
1807 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1808 break;
1809 case ISD::SETGT:
1810 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1811 break;
1812 case ISD::SETGE:
1813 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1814 break;
1815 case ISD::SETLT:
1816 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1817 break;
1818 case ISD::SETLE:
1819 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 break;
1821 case ISD::SETNE:
1822 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 break;
1824 case ISD::SETULT:
1825 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1826 break;
1827 case ISD::SETUGT:
1828 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 break;
1830 case ISD::SETULE:
1831 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1832 break;
1833 case ISD::SETUGE:
1834 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1835 break;
1836 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001837 }
1838 else { // if not integer, should be FP. FIXME: what about bools? ;)
Misha Brukman7847fca2005-04-22 17:54:37 +00001839 assert(SetCC->getOperand(0).getValueType() != MVT::f32 &&
1840 "error: SETCC should have had incoming f32 promoted to f64!\n");
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001841
Misha Brukman7847fca2005-04-22 17:54:37 +00001842 if(ConstantFPSDNode *CFPSDN =
1843 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001844
Misha Brukman7847fca2005-04-22 17:54:37 +00001845 // if we are comparing against a constant +0.0 or +1.0
1846 if(CFPSDN->isExactlyValue(+0.0))
1847 Tmp2 = IA64::F0; // then we can just compare against f0
1848 else if(CFPSDN->isExactlyValue(+1.0))
1849 Tmp2 = IA64::F1; // or f1
1850 else
1851 Tmp2 = SelectExpr(N.getOperand(1));
1852 } else // not comparing against a constant
1853 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001854
Misha Brukman7847fca2005-04-22 17:54:37 +00001855 switch (SetCC->getCondition()) {
1856 default: assert(0 && "Unknown FP comparison!");
1857 case ISD::SETEQ:
1858 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1859 break;
1860 case ISD::SETGT:
1861 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1862 break;
1863 case ISD::SETGE:
1864 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1865 break;
1866 case ISD::SETLT:
1867 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1868 break;
1869 case ISD::SETLE:
1870 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 break;
1872 case ISD::SETNE:
1873 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1874 break;
1875 case ISD::SETULT:
1876 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1877 break;
1878 case ISD::SETUGT:
1879 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1880 break;
1881 case ISD::SETULE:
1882 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1883 break;
1884 case ISD::SETUGE:
1885 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1886 break;
1887 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001888 }
1889 }
1890 else
1891 assert(0 && "this setcc not implemented yet");
1892
1893 return Result;
1894 }
1895
1896 case ISD::EXTLOAD:
1897 case ISD::ZEXTLOAD:
1898 case ISD::LOAD: {
1899 // Make sure we generate both values.
1900 if (Result != 1)
1901 ExprMap[N.getValue(1)] = 1; // Generate the token
1902 else
1903 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1904
1905 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001906
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001907 if(opcode == ISD::LOAD) { // this is a LOAD
1908 switch (Node->getValueType(0)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001909 default: assert(0 && "Cannot load this type!");
1910 case MVT::i1: Opc = IA64::LD1; isBool=true; break;
1911 // FIXME: for now, we treat bool loads the same as i8 loads */
1912 case MVT::i8: Opc = IA64::LD1; break;
1913 case MVT::i16: Opc = IA64::LD2; break;
1914 case MVT::i32: Opc = IA64::LD4; break;
1915 case MVT::i64: Opc = IA64::LD8; break;
1916
1917 case MVT::f32: Opc = IA64::LDF4; break;
1918 case MVT::f64: Opc = IA64::LDF8; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001919 }
1920 } else { // this is an EXTLOAD or ZEXTLOAD
1921 MVT::ValueType TypeBeingLoaded = cast<MVTSDNode>(Node)->getExtraValueType();
1922 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001923 default: assert(0 && "Cannot extload/zextload this type!");
1924 // FIXME: bools?
1925 case MVT::i8: Opc = IA64::LD1; break;
1926 case MVT::i16: Opc = IA64::LD2; break;
1927 case MVT::i32: Opc = IA64::LD4; break;
1928 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001929 }
1930 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001931
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001932 SDOperand Chain = N.getOperand(0);
1933 SDOperand Address = N.getOperand(1);
1934
1935 if(Address.getOpcode() == ISD::GlobalAddress) {
1936 Select(Chain);
1937 unsigned dummy = MakeReg(MVT::i64);
1938 unsigned dummy2 = MakeReg(MVT::i64);
1939 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001940 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1941 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001942 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1943 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001944 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001945 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001946 // into a predicate register
1947 assert(Opc==IA64::LD1 && "problem loading a bool");
1948 unsigned dummy3 = MakeReg(MVT::i64);
1949 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1950 // we compare to 0. true? 0. false? 1.
1951 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001952 }
1953 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1954 Select(Chain);
1955 IA64Lowering.restoreGP(BB);
1956 unsigned dummy = MakeReg(MVT::i64);
1957 BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CP->getIndex())
Misha Brukman7847fca2005-04-22 17:54:37 +00001958 .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001959 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001960 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001961 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001962 // into a predicate register
1963 assert(Opc==IA64::LD1 && "problem loading a bool");
1964 unsigned dummy3 = MakeReg(MVT::i64);
1965 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1966 // we compare to 0. true? 0. false? 1.
1967 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001968 }
1969 } else if(Address.getOpcode() == ISD::FrameIndex) {
1970 Select(Chain); // FIXME ? what about bools?
1971 unsigned dummy = MakeReg(MVT::i64);
1972 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001973 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001974 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001975 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001976 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001977 // into a predicate register
1978 assert(Opc==IA64::LD1 && "problem loading a bool");
1979 unsigned dummy3 = MakeReg(MVT::i64);
1980 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1981 // we compare to 0. true? 0. false? 1.
1982 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001983 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001984 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001985 Select(Chain);
1986 Tmp2 = SelectExpr(Address);
1987 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001988 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001989 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001990 // into a predicate register
1991 assert(Opc==IA64::LD1 && "problem loading a bool");
1992 unsigned dummy = MakeReg(MVT::i64);
1993 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
1994 // we compare to 0. true? 0. false? 1.
1995 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
1996 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 }
1998
1999 return Result;
2000 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002001
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002002 case ISD::CopyFromReg: {
2003 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002004 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002005 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002006
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002007 SDOperand Chain = N.getOperand(0);
2008
2009 Select(Chain);
2010 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
2011
2012 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002013 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2014 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 // (r) Result =cmp.eq.unc(r0,r0)
2016 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002018 return Result;
2019 }
2020
2021 case ISD::CALL: {
2022 Select(N.getOperand(0));
2023
2024 // The chain for this call is now lowered.
2025 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002026
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002027 //grab the arguments
2028 std::vector<unsigned> argvregs;
2029
2030 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002031 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002032
2033 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002034 // Runtime Architecture Guide to see some examples of what's going
2035 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2036 // while FP args get mapped to F8->F15 as needed)
2037
2038 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002039
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002040 // in reg args
2041 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2042 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002043 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2044 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2045 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2046 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002047
Misha Brukman7847fca2005-04-22 17:54:37 +00002048 switch(N.getOperand(i+2).getValueType())
2049 {
2050 default: // XXX do we need to support MVT::i1 here?
2051 Node->dump();
2052 N.getOperand(i).Val->dump();
2053 std::cerr << "Type for " << i << " is: " <<
2054 N.getOperand(i+2).getValueType() << std::endl;
2055 assert(0 && "Unknown value type for call");
2056 case MVT::i64:
2057 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2058 break;
2059 case MVT::f64:
2060 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2061 .addReg(argvregs[i]);
2062 // FIXME: we don't need to do this _all_ the time:
2063 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2064 break;
2065 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002066 }
2067
2068 //in mem args
2069 for (int i = 8, e = argvregs.size(); i < e; ++i)
2070 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002071 unsigned tempAddr = MakeReg(MVT::i64);
2072
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002073 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002074 default:
2075 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002076 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002077 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002078 N.getOperand(i+2).getValueType() << "\n";
2079 assert(0 && "Unknown value type for call");
2080 case MVT::i1: // FIXME?
2081 case MVT::i8:
2082 case MVT::i16:
2083 case MVT::i32:
2084 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002085 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2086 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2087 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002088 break;
2089 case MVT::f32:
2090 case MVT::f64:
2091 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002092 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2093 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002094 break;
2095 }
2096 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002097
2098 /* XXX we want to re-enable direct branches! crippling them now
Misha Brukman4633f1c2005-04-21 23:13:11 +00002099 * to stress-test indirect branches.:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002100 //build the right kind of call
2101 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002102 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002103 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002104 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2105 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002106 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002107 ^^^^^^^^^^^^^ we want this code one day XXX */
Duraid Madinabeeaab22005-03-31 12:31:11 +00002108 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002109 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002110 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002111 // "non-pic code with imm relocation against dynamic symbol" errors
2112 BuildMI(BB, IA64::BRCALL, 1)
2113 .addExternalSymbol(ESSDN->getSymbol(), true);
2114 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002115 }
2116 else {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002117 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002118
2119 unsigned targetEntryPoint=MakeReg(MVT::i64);
2120 unsigned targetGPAddr=MakeReg(MVT::i64);
2121 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002122
Duraid Madinabeeaab22005-03-31 12:31:11 +00002123 // b6 is a scratch branch register, we load the target entry point
2124 // from the base of the function descriptor
2125 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2126 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2127
2128 // save the current GP:
2129 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002130
Duraid Madinabeeaab22005-03-31 12:31:11 +00002131 /* TODO: we need to make sure doing this never, ever loads a
2132 * bogus value into r1 (GP). */
2133 // load the target GP (which is at mem[functiondescriptor+8])
2134 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002135 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002136 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2137
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002138 // and then jump: (well, call)
2139 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002140 // and finally restore the old GP
2141 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2142 IA64Lowering.restoreSP_RP(BB);
2143 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002144
2145 switch (Node->getValueType(0)) {
2146 default: assert(0 && "Unknown value type for call result!");
2147 case MVT::Other: return 1;
2148 case MVT::i1:
2149 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002150 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002151 break;
2152 case MVT::i8:
2153 case MVT::i16:
2154 case MVT::i32:
2155 case MVT::i64:
2156 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2157 break;
2158 case MVT::f64:
2159 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2160 break;
2161 }
2162 return Result+N.ResNo;
2163 }
2164
Misha Brukman4633f1c2005-04-21 23:13:11 +00002165 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002166 return 0;
2167}
2168
2169void ISel::Select(SDOperand N) {
2170 unsigned Tmp1, Tmp2, Opc;
2171 unsigned opcode = N.getOpcode();
2172
Nate Begeman85fdeb22005-03-24 04:39:54 +00002173 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002174 return; // Already selected.
2175
2176 SDNode *Node = N.Val;
2177
2178 switch (Node->getOpcode()) {
2179 default:
2180 Node->dump(); std::cerr << "\n";
2181 assert(0 && "Node not handled yet!");
2182
2183 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002184
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002185 case ISD::TokenFactor: {
2186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2187 Select(Node->getOperand(i));
2188 return;
2189 }
2190
2191 case ISD::CopyToReg: {
2192 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002193 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002194 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002195
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002196 if (Tmp1 != Tmp2) {
2197 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002198 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2199 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002200 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2201 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002202 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002203 // XXX is this the right way 'round? ;)
2204 }
2205 return;
2206 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002207
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002208 case ISD::RET: {
2209
2210 /* what the heck is going on here:
2211
2212<_sabre_> ret with two operands is obvious: chain and value
2213<camel_> yep
2214<_sabre_> ret with 3 values happens when 'expansion' occurs
2215<_sabre_> e.g. i64 gets split into 2x i32
2216<camel_> oh right
2217<_sabre_> you don't have this case on ia64
2218<camel_> yep
2219<_sabre_> so the two returned values go into EAX/EDX on ia32
2220<camel_> ahhh *memories*
2221<_sabre_> :)
2222<camel_> ok, thanks :)
2223<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2224<_sabre_> this is the first operand always
2225<_sabre_> these operand often define chains, they are the last operand
2226<_sabre_> they are printed as 'ch' if you do DAG.dump()
2227 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002228
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002229 switch (N.getNumOperands()) {
2230 default:
2231 assert(0 && "Unknown return instruction!");
2232 case 2:
2233 Select(N.getOperand(0));
2234 Tmp1 = SelectExpr(N.getOperand(1));
2235 switch (N.getOperand(1).getValueType()) {
2236 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002237 // FIXME: do I need to add support for bools here?
2238 // (return '0' or '1' r8, basically...)
2239 //
2240 // FIXME: need to round floats - 80 bits is bad, the tester
2241 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002242 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002243 // we mark r8 as live on exit up above in LowerArguments()
2244 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2245 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002246 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002247 // we mark F8 as live on exit up above in LowerArguments()
2248 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002249 }
2250 break;
2251 case 1:
2252 Select(N.getOperand(0));
2253 break;
2254 }
2255 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2256 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2257 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2258 return;
2259 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002260
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002261 case ISD::BR: {
2262 Select(N.getOperand(0));
2263 MachineBasicBlock *Dest =
2264 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2265 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2266 // XXX HACK! we do _not_ need long branches all the time
2267 return;
2268 }
2269
2270 case ISD::ImplicitDef: {
2271 Select(N.getOperand(0));
2272 BuildMI(BB, IA64::IDEF, 0, cast<RegSDNode>(N)->getReg());
2273 return;
2274 }
2275
2276 case ISD::BRCOND: {
2277 MachineBasicBlock *Dest =
2278 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2279
2280 Select(N.getOperand(0));
2281 Tmp1 = SelectExpr(N.getOperand(1));
2282 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2283 // XXX HACK! we do _not_ need long branches all the time
2284 return;
2285 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002286
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002287 case ISD::EXTLOAD:
2288 case ISD::ZEXTLOAD:
2289 case ISD::SEXTLOAD:
2290 case ISD::LOAD:
2291 case ISD::CALL:
2292 case ISD::CopyFromReg:
2293 case ISD::DYNAMIC_STACKALLOC:
2294 SelectExpr(N);
2295 return;
2296
2297 case ISD::TRUNCSTORE:
2298 case ISD::STORE: {
2299 Select(N.getOperand(0));
2300 Tmp1 = SelectExpr(N.getOperand(1)); // value
2301
2302 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002303
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002304 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002305 switch (N.getOperand(1).getValueType()) {
2306 default: assert(0 && "Cannot store this type!");
2307 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2308 // FIXME?: for now, we treat bool loads the same as i8 stores */
2309 case MVT::i8: Opc = IA64::ST1; break;
2310 case MVT::i16: Opc = IA64::ST2; break;
2311 case MVT::i32: Opc = IA64::ST4; break;
2312 case MVT::i64: Opc = IA64::ST8; break;
2313
2314 case MVT::f32: Opc = IA64::STF4; break;
2315 case MVT::f64: Opc = IA64::STF8; break;
2316 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002317 } else { // truncstore
Misha Brukman7847fca2005-04-22 17:54:37 +00002318 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2319 default: assert(0 && "unknown type in truncstore");
2320 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2321 //FIXME: DAG does not promote this load?
2322 case MVT::i8: Opc = IA64::ST1; break;
2323 case MVT::i16: Opc = IA64::ST2; break;
2324 case MVT::i32: Opc = IA64::ST4; break;
2325 case MVT::f32: Opc = IA64::STF4; break;
2326 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002327 }
2328
2329 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002330 unsigned dummy = MakeReg(MVT::i64);
2331 unsigned dummy2 = MakeReg(MVT::i64);
2332 BuildMI(BB, IA64::ADD, 2, dummy)
2333 .addGlobalAddress(cast<GlobalAddressSDNode>
2334 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2335 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002336
Misha Brukman7847fca2005-04-22 17:54:37 +00002337 if(!isBool)
2338 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2339 else { // we are storing a bool, so emit a little pseudocode
2340 // to store a predicate register as one byte
2341 assert(Opc==IA64::ST1);
2342 unsigned dummy3 = MakeReg(MVT::i64);
2343 unsigned dummy4 = MakeReg(MVT::i64);
2344 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2345 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2346 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2347 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2348 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002349 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2350
Misha Brukman7847fca2005-04-22 17:54:37 +00002351 // FIXME? (what about bools?)
2352
2353 unsigned dummy = MakeReg(MVT::i64);
2354 BuildMI(BB, IA64::MOV, 1, dummy)
2355 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2356 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002357 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002358 Tmp2 = SelectExpr(N.getOperand(2)); //address
2359 if(!isBool)
2360 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2361 else { // we are storing a bool, so emit a little pseudocode
2362 // to store a predicate register as one byte
2363 assert(Opc==IA64::ST1);
2364 unsigned dummy3 = MakeReg(MVT::i64);
2365 unsigned dummy4 = MakeReg(MVT::i64);
2366 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2367 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2368 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2369 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2370 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002371 }
2372 return;
2373 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002374
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002375 case ISD::ADJCALLSTACKDOWN:
2376 case ISD::ADJCALLSTACKUP: {
2377 Select(N.getOperand(0));
2378 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002379
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002380 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? IA64::ADJUSTCALLSTACKDOWN :
2381 IA64::ADJUSTCALLSTACKUP;
2382 BuildMI(BB, Opc, 1).addImm(Tmp1);
2383 return;
2384 }
2385
2386 return;
2387 }
2388 assert(0 && "GAME OVER. INSERT COIN?");
2389}
2390
2391
2392/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2393/// into a machine code representation using pattern matching and a machine
2394/// description file.
2395///
2396FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002397 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002398}
2399
2400