blob: d2351d0fbd7678b3700fada5ba4cb6d1a9bfe9fc [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64ISelPattern.cpp - A pattern matching inst selector for IA64 ---===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IA64.h"
15#include "IA64InstrBuilder.h"
16#include "IA64RegisterInfo.h"
17#include "IA64MachineFunctionInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Duraid Madinab2322562005-04-26 07:23:02 +000031#include <map>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000032#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// IA64TargetLowering - IA64 Implementation of the TargetLowering interface
37namespace {
38 class IA64TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 //int ReturnAddrIndex; // FrameIndex for return slot.
42 unsigned GP, SP, RP; // FIXME - clean this mess up
43 public:
44
45 unsigned VirtGPR; // this is public so it can be accessed in the selector
46 // for ISD::RET down below. add an accessor instead? FIXME
47
48 IA64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +000049
Duraid Madina9b9d45f2005-03-17 18:17:03 +000050 // register class for general registers
51 addRegisterClass(MVT::i64, IA64::GRRegisterClass);
52
53 // register class for FP registers
54 addRegisterClass(MVT::f64, IA64::FPRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000055
56 // register class for predicate registers
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman7cbd5252005-08-16 19:49:35 +000060 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000061 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
62
Misha Brukman4633f1c2005-04-21 23:13:11 +000063 setSetCCResultType(MVT::i1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000064 setShiftAmountType(MVT::i64);
65
66 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067
68 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000069
70 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
71 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
72 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Duraid Madinac4ccc2d2005-04-14 08:37:32 +000073 setOperationAction(ISD::SEXTLOAD , MVT::i32 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000074
Chris Lattner3e2bafd2005-09-28 22:29:17 +000075 setOperationAction(ISD::FREM , MVT::f32 , Expand);
76 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000077
78 setOperationAction(ISD::UREM , MVT::f32 , Expand);
79 setOperationAction(ISD::UREM , MVT::f64 , Expand);
Misha Brukman4633f1c2005-04-21 23:13:11 +000080
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
82 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
83 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Chris Lattner7a7c9792005-10-07 04:50:48 +000084
85 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
Nate Begemand5ce2042005-10-21 01:52:45 +000087
Chris Lattner17234b72005-04-30 04:26:06 +000088 // We don't support sin/cos/sqrt
89 setOperationAction(ISD::FSIN , MVT::f64, Expand);
90 setOperationAction(ISD::FCOS , MVT::f64, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
92 setOperationAction(ISD::FSIN , MVT::f32, Expand);
93 setOperationAction(ISD::FCOS , MVT::f32, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
95
Andrew Lenharthb5884d32005-05-04 19:25:37 +000096 //IA64 has these, but they are not implemented
Chris Lattner1f38e5c2005-05-11 05:03:56 +000097 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
98 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Nate Begemand5ce2042005-10-21 01:52:45 +000099 // FIXME: implement mulhs (xma.h) and mulhu (xma.hu)
100 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
101 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000102
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000103 computeRegisterProperties();
104
105 addLegalFPImmediate(+0.0);
106 addLegalFPImmediate(+1.0);
107 addLegalFPImmediate(-0.0);
108 addLegalFPImmediate(-1.0);
109 }
110
111 /// LowerArguments - This hook must be implemented to indicate how we should
112 /// lower the arguments for the specified function, into the specified DAG.
113 virtual std::vector<SDOperand>
114 LowerArguments(Function &F, SelectionDAG &DAG);
115
116 /// LowerCallTo - This hook lowers an abstract call to a function into an
117 /// actual call.
118 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000119 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000120 bool isTailCall, SDOperand Callee, ArgListTy &Args,
121 SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000122
Chris Lattnere0fe2252005-07-05 19:58:54 +0000123 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
124 Value *VAListV, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000125 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000126 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
127 const Type *ArgTy, SelectionDAG &DAG);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000128
129 void restoreGP_SP_RP(MachineBasicBlock* BB)
130 {
131 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
132 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
133 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
134 }
135
Duraid Madinabeeaab22005-03-31 12:31:11 +0000136 void restoreSP_RP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, IA64::MOV, 1, IA64::r12).addReg(SP);
139 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
140 }
141
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000142 void restoreRP(MachineBasicBlock* BB)
143 {
144 BuildMI(BB, IA64::MOV, 1, IA64::rp).addReg(RP);
145 }
146
147 void restoreGP(MachineBasicBlock* BB)
148 {
149 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(GP);
150 }
151
152 };
153}
154
155
156std::vector<SDOperand>
157IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
158 std::vector<SDOperand> ArgValues;
159
160 //
161 // add beautiful description of IA64 stack frame format
162 // here (from intel 24535803.pdf most likely)
163 //
164 MachineFunction &MF = DAG.getMachineFunction();
165 MachineFrameInfo *MFI = MF.getFrameInfo();
166
167 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
168 SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
169 RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
170
171 MachineBasicBlock& BB = MF.front();
172
Misha Brukman4633f1c2005-04-21 23:13:11 +0000173 unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 IA64::r36, IA64::r37, IA64::r38, IA64::r39};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000175
176 unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000177 IA64::F12,IA64::F13,IA64::F14, IA64::F15};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000178
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000179 unsigned argVreg[8];
180 unsigned argPreg[8];
181 unsigned argOpc[8];
182
Duraid Madinabeeaab22005-03-31 12:31:11 +0000183 unsigned used_FPArgs = 0; // how many FP args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +0000184
Duraid Madinabeeaab22005-03-31 12:31:11 +0000185 unsigned ArgOffset = 0;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000186 int count = 0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000187
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000188 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000189 {
190 SDOperand newroot, argt;
191 if(count < 8) { // need to fix this logic? maybe.
Misha Brukman7847fca2005-04-22 17:54:37 +0000192
193 switch (getValueType(I->getType())) {
194 default:
195 std::cerr << "ERROR in LowerArgs: unknown type "
196 << getValueType(I->getType()) << "\n";
197 abort();
198 case MVT::f32:
199 // fixme? (well, will need to for weird FP structy stuff,
200 // see intel ABI docs)
201 case MVT::f64:
202//XXX BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
203 MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
204 // floating point args go into f8..f15 as-needed, the increment
205 argVreg[count] = // is below..:
206 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
207 // FP args go into f8..f15 as needed: (hence the ++)
208 argPreg[count] = args_FP[used_FPArgs++];
209 argOpc[count] = IA64::FMOV;
Chris Lattner707ebc52005-08-16 21:56:37 +0000210 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
Chris Lattner0a00bec2005-08-22 21:33:11 +0000211 MVT::f64);
212 if (I->getType() == Type::FloatTy)
213 argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
Misha Brukman7847fca2005-04-22 17:54:37 +0000214 break;
215 case MVT::i1: // NOTE: as far as C abi stuff goes,
216 // bools are just boring old ints
217 case MVT::i8:
218 case MVT::i16:
219 case MVT::i32:
220 case MVT::i64:
221//XXX BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
222 MF.addLiveIn(args_int[count]); // mark this register as liveIn
223 argVreg[count] =
224 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
225 argPreg[count] = args_int[count];
226 argOpc[count] = IA64::MOV;
227 argt = newroot =
Chris Lattner707ebc52005-08-16 21:56:37 +0000228 DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
Misha Brukman7847fca2005-04-22 17:54:37 +0000229 if ( getValueType(I->getType()) != MVT::i64)
230 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
231 newroot);
232 break;
233 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000234 } else { // more than 8 args go into the frame
Misha Brukman7847fca2005-04-22 17:54:37 +0000235 // Create the frame index object for this incoming parameter...
236 ArgOffset = 16 + 8 * (count - 8);
237 int FI = MFI->CreateFixedObject(8, ArgOffset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000238
Misha Brukman7847fca2005-04-22 17:54:37 +0000239 // Create the SelectionDAG nodes corresponding to a load
240 //from this parameter
241 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
242 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000243 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000244 }
245 ++count;
246 DAG.setRoot(newroot.getValue(1));
247 ArgValues.push_back(argt);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000248 }
Duraid Madinabeeaab22005-03-31 12:31:11 +0000249
Misha Brukman4633f1c2005-04-21 23:13:11 +0000250
Duraid Madinabeeaab22005-03-31 12:31:11 +0000251 // Create a vreg to hold the output of (what will become)
252 // the "alloc" instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000253 VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
254 BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
255 // we create a PSEUDO_ALLOC (pseudo)instruction for now
256
257 BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
258
259 // hmm:
260 BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
261 BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
262 // ..hmm.
263
264 BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
265
266 // hmm:
267 BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
268 BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
269 // ..hmm.
270
Duraid Madinabeeaab22005-03-31 12:31:11 +0000271 unsigned tempOffset=0;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000272
Duraid Madinabeeaab22005-03-31 12:31:11 +0000273 // if this is a varargs function, we simply lower llvm.va_start by
274 // pointing to the first entry
275 if(F.isVarArg()) {
276 tempOffset=0;
277 VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000278 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000279
Duraid Madinabeeaab22005-03-31 12:31:11 +0000280 // here we actually do the moving of args, and store them to the stack
281 // too if this is a varargs function:
282 for (int i = 0; i < count && i < 8; ++i) {
283 BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
284 if(F.isVarArg()) {
285 // if this is a varargs function, we copy the input registers to the stack
286 int FI = MFI->CreateFixedObject(8, tempOffset);
287 tempOffset+=8; //XXX: is it safe to use r22 like this?
288 BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
289 // FIXME: we should use st8.spill here, one day
290 BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
291 }
292 }
293
Duraid Madinaca494fd2005-04-12 14:54:44 +0000294 // Finally, inform the code generator which regs we return values in.
295 // (see the ISD::RET: case down below)
296 switch (getValueType(F.getReturnType())) {
297 default: assert(0 && "i have no idea where to return this type!");
298 case MVT::isVoid: break;
299 case MVT::i1:
300 case MVT::i8:
301 case MVT::i16:
302 case MVT::i32:
303 case MVT::i64:
304 MF.addLiveOut(IA64::r8);
305 break;
306 case MVT::f32:
307 case MVT::f64:
308 MF.addLiveOut(IA64::F8);
309 break;
310 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000311
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000312 return ArgValues;
313}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000314
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000315std::pair<SDOperand, SDOperand>
316IA64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000317 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000318 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000319 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000320 SelectionDAG &DAG) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000321
322 MachineFunction &MF = DAG.getMachineFunction();
323
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000324 unsigned NumBytes = 16;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000325 unsigned outRegsUsed = 0;
326
327 if (Args.size() > 8) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000328 NumBytes += (Args.size() - 8) * 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000329 outRegsUsed = 8;
330 } else {
331 outRegsUsed = Args.size();
332 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000333
Duraid Madinabeeaab22005-03-31 12:31:11 +0000334 // FIXME? this WILL fail if we ever try to pass around an arg that
335 // consumes more than a single output slot (a 'real' double, int128
336 // some sort of aggregate etc.), as we'll underestimate how many 'outX'
337 // registers we use. Hopefully, the assembler will notice.
338 MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
339 std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000340
Chris Lattner16cd04d2005-05-12 23:24:06 +0000341 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000342 DAG.getConstant(NumBytes, getPointerTy()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000343
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000344 std::vector<SDOperand> args_to_use;
345 for (unsigned i = 0, e = Args.size(); i != e; ++i)
346 {
347 switch (getValueType(Args[i].second)) {
348 default: assert(0 && "unexpected argument type!");
349 case MVT::i1:
350 case MVT::i8:
351 case MVT::i16:
352 case MVT::i32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000353 //promote to 64-bits, sign/zero extending based on type
354 //of the argument
355 if(Args[i].second->isSigned())
356 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
357 Args[i].first);
358 else
359 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
360 Args[i].first);
361 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000362 case MVT::f32:
Misha Brukman7847fca2005-04-22 17:54:37 +0000363 //promote to 64-bits
364 Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000365 case MVT::f64:
366 case MVT::i64:
367 break;
368 }
369 args_to_use.push_back(Args[i].first);
370 }
371
372 std::vector<MVT::ValueType> RetVals;
373 MVT::ValueType RetTyVT = getValueType(RetTy);
374 if (RetTyVT != MVT::isVoid)
375 RetVals.push_back(RetTyVT);
376 RetVals.push_back(MVT::Other);
377
378 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000379 Callee, args_to_use), 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000380 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000381 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000382 DAG.getConstant(NumBytes, getPointerTy()));
383 return std::make_pair(TheCall, Chain);
384}
385
Chris Lattnere0fe2252005-07-05 19:58:54 +0000386SDOperand
387IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
388 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000389 // vastart just stores the address of the VarArgsFrameIndex slot.
390 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000391 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
392 VAListP, DAG.getSrcValue(VAListV));
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000393}
394
395std::pair<SDOperand,SDOperand> IA64TargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000396LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
397 const Type *ArgTy, SelectionDAG &DAG) {
Duraid Madinabeeaab22005-03-31 12:31:11 +0000398
399 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000400 SDOperand Val = DAG.getLoad(MVT::i64, Chain,
401 VAListP, DAG.getSrcValue(VAListV));
402 SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
403 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000404 unsigned Amt;
405 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
406 Amt = 8;
407 else {
408 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
409 "Other types should have been promoted for varargs!");
410 Amt = 8;
Duraid Madinabeeaab22005-03-31 12:31:11 +0000411 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000412 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000413 DAG.getConstant(Amt, Val.getValueType()));
414 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000415 Val, VAListP, DAG.getSrcValue(VAListV));
Duraid Madinabeeaab22005-03-31 12:31:11 +0000416 return std::make_pair(Result, Chain);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000417}
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000418
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000419namespace {
420
421 //===--------------------------------------------------------------------===//
422 /// ISel - IA64 specific code to select IA64 machine instructions for
423 /// SelectionDAG operations.
424 ///
425 class ISel : public SelectionDAGISel {
426 /// IA64Lowering - This object fully describes how to lower LLVM code to an
427 /// IA64-specific SelectionDAG.
428 IA64TargetLowering IA64Lowering;
Duraid Madinab2322562005-04-26 07:23:02 +0000429 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
430 // for sdiv and udiv until it is put into the future
431 // dag combiner
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000432
433 /// ExprMap - As shared expressions are codegen'd, we keep track of which
434 /// vreg the value is produced in, so we only emit one copy of each compiled
435 /// tree.
436 std::map<SDOperand, unsigned> ExprMap;
437 std::set<SDOperand> LoweredTokens;
438
439 public:
Duraid Madinab2322562005-04-26 07:23:02 +0000440 ISel(TargetMachine &TM) : SelectionDAGISel(IA64Lowering), IA64Lowering(TM),
441 ISelDAG(0) { }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000442
443 /// InstructionSelectBasicBlock - This callback is invoked by
444 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
445 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
446
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000447 unsigned SelectExpr(SDOperand N);
448 void Select(SDOperand N);
Duraid Madinab2322562005-04-26 07:23:02 +0000449 // a dag->dag to transform mul-by-constant-int to shifts+adds/subs
450 SDOperand BuildConstmulSequence(SDOperand N);
451
Chris Lattner47c08892005-08-22 18:28:09 +0000452 const char *getPassName() const { return "IA64 Instruction Selector"; }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000453 };
454}
455
456/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
457/// when it has created a SelectionDAG for us to codegen.
458void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
459
460 // Codegen the basic block.
Duraid Madinab2322562005-04-26 07:23:02 +0000461 ISelDAG = &DAG;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000462 Select(DAG.getRoot());
463
464 // Clear state used for selection.
465 ExprMap.clear();
466 LoweredTokens.clear();
Duraid Madinab2322562005-04-26 07:23:02 +0000467 ISelDAG = 0;
468}
469
Duraid Madinab2322562005-04-26 07:23:02 +0000470// strip leading '0' characters from a string
471void munchLeadingZeros(std::string& inString) {
472 while(inString.c_str()[0]=='0') {
473 inString.erase(0, 1);
474 }
475}
476
477// strip trailing '0' characters from a string
478void munchTrailingZeros(std::string& inString) {
479 int curPos=inString.length()-1;
480
481 while(inString.c_str()[curPos]=='0') {
482 inString.erase(curPos, 1);
483 curPos--;
484 }
485}
486
487// return how many consecutive '0' characters are at the end of a string
488unsigned int countTrailingZeros(std::string& inString) {
489 int curPos=inString.length()-1;
490 unsigned int zeroCount=0;
491 // assert goes here
492 while(inString.c_str()[curPos--]=='0') {
493 zeroCount++;
494 }
495 return zeroCount;
496}
497
498// booth encode a string of '1' and '0' characters (returns string of 'P' (+1)
499// '0' and 'N' (-1) characters)
500void boothEncode(std::string inString, std::string& boothEncodedString) {
501
502 int curpos=0;
503 int replacements=0;
504 int lim=inString.size();
505
506 while(curpos<lim) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000507 if(inString[curpos]=='1') { // if we see a '1', look for a run of them
Duraid Madinab2322562005-04-26 07:23:02 +0000508 int runlength=0;
509 std::string replaceString="N";
Jeff Cohen00b168892005-07-27 06:12:32 +0000510
Duraid Madinab2322562005-04-26 07:23:02 +0000511 // find the run length
512 for(;inString[curpos+runlength]=='1';runlength++) ;
513
514 for(int i=0; i<runlength-1; i++)
Jeff Cohen00b168892005-07-27 06:12:32 +0000515 replaceString+="0";
Duraid Madinab2322562005-04-26 07:23:02 +0000516 replaceString+="1";
517
518 if(runlength>1) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000519 inString.replace(curpos, runlength+1, replaceString);
520 curpos+=runlength-1;
Duraid Madinab2322562005-04-26 07:23:02 +0000521 } else
Jeff Cohen00b168892005-07-27 06:12:32 +0000522 curpos++;
Duraid Madinab2322562005-04-26 07:23:02 +0000523 } else { // a zero, we just keep chugging along
524 curpos++;
525 }
526 }
527
528 // clean up (trim the string, reverse it and turn '1's into 'P's)
529 munchTrailingZeros(inString);
530 boothEncodedString="";
531
532 for(int i=inString.size()-1;i>=0;i--)
533 if(inString[i]=='1')
534 boothEncodedString+="P";
535 else
536 boothEncodedString+=inString[i];
537
538}
539
540struct shiftaddblob { // this encodes stuff like (x=) "A << B [+-] C << D"
541 unsigned firstVal; // A
Jeff Cohen00b168892005-07-27 06:12:32 +0000542 unsigned firstShift; // B
Duraid Madinab2322562005-04-26 07:23:02 +0000543 unsigned secondVal; // C
544 unsigned secondShift; // D
545 bool isSub;
546};
547
548/* this implements Lefevre's "pattern-based" constant multiplication,
549 * see "Multiplication by an Integer Constant", INRIA report 1999-06
550 *
551 * TODO: implement a method to try rewriting P0N<->0PP / N0P<->0NN
552 * to get better booth encodings - this does help in practice
553 * TODO: weight shifts appropriately (most architectures can't
554 * fuse a shift and an add for arbitrary shift amounts) */
555unsigned lefevre(const std::string inString,
556 std::vector<struct shiftaddblob> &ops) {
557 std::string retstring;
558 std::string s = inString;
559 munchTrailingZeros(s);
560
561 int length=s.length()-1;
562
563 if(length==0) {
564 return(0);
565 }
566
567 std::vector<int> p,n;
Jeff Cohen00b168892005-07-27 06:12:32 +0000568
Duraid Madinab2322562005-04-26 07:23:02 +0000569 for(int i=0; i<=length; i++) {
570 if (s.c_str()[length-i]=='P') {
571 p.push_back(i);
572 } else if (s.c_str()[length-i]=='N') {
573 n.push_back(i);
574 }
575 }
576
577 std::string t, u;
Chris Lattner4a209972005-08-25 00:03:21 +0000578 int c = 0;
Duraid Madina4706c032005-04-26 09:42:50 +0000579 bool f;
Duraid Madinab2322562005-04-26 07:23:02 +0000580 std::map<const int, int> w;
581
Duraid Madina85d5f602005-04-27 11:57:39 +0000582 for(unsigned i=0; i<p.size(); i++) {
583 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000584 w[p[i]-p[j]]++;
585 }
586 }
587
Duraid Madina85d5f602005-04-27 11:57:39 +0000588 for(unsigned i=1; i<n.size(); i++) {
589 for(unsigned j=0; j<i; j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000590 w[n[i]-n[j]]++;
591 }
592 }
593
Duraid Madina85d5f602005-04-27 11:57:39 +0000594 for(unsigned i=0; i<p.size(); i++) {
595 for(unsigned j=0; j<n.size(); j++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000596 w[-abs(p[i]-n[j])]++;
597 }
598 }
599
600 std::map<const int, int>::const_iterator ii;
601 std::vector<int> d;
602 std::multimap<int, int> sorted_by_value;
603
604 for(ii = w.begin(); ii!=w.end(); ii++)
605 sorted_by_value.insert(std::pair<int, int>((*ii).second,(*ii).first));
606
607 for (std::multimap<int, int>::iterator it = sorted_by_value.begin();
608 it != sorted_by_value.end(); ++it) {
609 d.push_back((*it).second);
610 }
611
612 int int_W=0;
613 int int_d;
614
615 while(d.size()>0 && (w[int_d=d.back()] > int_W)) {
616 d.pop_back();
617 retstring=s; // hmmm
618 int x=0;
619 int z=abs(int_d)-1;
620
621 if(int_d>0) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000622
Duraid Madina85d5f602005-04-27 11:57:39 +0000623 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000624 if( ((base+z+1) < retstring.size()) &&
625 retstring.c_str()[base]=='P' &&
626 retstring.c_str()[base+z+1]=='P')
627 {
628 // match
629 x++;
630 retstring.replace(base, 1, "0");
631 retstring.replace(base+z+1, 1, "p");
632 }
Duraid Madinab2322562005-04-26 07:23:02 +0000633 }
634
Duraid Madina85d5f602005-04-27 11:57:39 +0000635 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000636 if( ((base+z+1) < retstring.size()) &&
637 retstring.c_str()[base]=='N' &&
638 retstring.c_str()[base+z+1]=='N')
639 {
640 // match
641 x++;
642 retstring.replace(base, 1, "0");
643 retstring.replace(base+z+1, 1, "n");
644 }
Duraid Madinab2322562005-04-26 07:23:02 +0000645 }
646
647 } else {
Duraid Madina85d5f602005-04-27 11:57:39 +0000648 for(unsigned base=0; base<retstring.size(); base++) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000649 if( ((base+z+1) < retstring.size()) &&
650 ((retstring.c_str()[base]=='P' &&
651 retstring.c_str()[base+z+1]=='N') ||
652 (retstring.c_str()[base]=='N' &&
653 retstring.c_str()[base+z+1]=='P')) ) {
654 // match
655 x++;
656
657 if(retstring.c_str()[base]=='P') {
658 retstring.replace(base, 1, "0");
659 retstring.replace(base+z+1, 1, "p");
660 } else { // retstring[base]=='N'
661 retstring.replace(base, 1, "0");
662 retstring.replace(base+z+1, 1, "n");
663 }
664 }
Duraid Madinab2322562005-04-26 07:23:02 +0000665 }
666 }
667
668 if(x>int_W) {
669 int_W = x;
670 t = retstring;
671 c = int_d; // tofix
672 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000673
Duraid Madinab2322562005-04-26 07:23:02 +0000674 } d.pop_back(); // hmm
675
676 u = t;
Jeff Cohen00b168892005-07-27 06:12:32 +0000677
Duraid Madina85d5f602005-04-27 11:57:39 +0000678 for(unsigned i=0; i<t.length(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000679 if(t.c_str()[i]=='p' || t.c_str()[i]=='n')
680 t.replace(i, 1, "0");
681 }
682
Duraid Madina85d5f602005-04-27 11:57:39 +0000683 for(unsigned i=0; i<u.length(); i++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000684 if(u[i]=='P' || u[i]=='N')
Duraid Madinab2322562005-04-26 07:23:02 +0000685 u.replace(i, 1, "0");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000686 if(u[i]=='p')
Duraid Madinab2322562005-04-26 07:23:02 +0000687 u.replace(i, 1, "P");
Duraid Madina8a3042c2005-05-09 13:18:34 +0000688 if(u[i]=='n')
Duraid Madinab2322562005-04-26 07:23:02 +0000689 u.replace(i, 1, "N");
690 }
691
692 if( c<0 ) {
Duraid Madina4706c032005-04-26 09:42:50 +0000693 f=true;
Duraid Madinab2322562005-04-26 07:23:02 +0000694 c=-c;
695 } else
Duraid Madina4706c032005-04-26 09:42:50 +0000696 f=false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000697
Duraid Madina8a3042c2005-05-09 13:18:34 +0000698 int pos=0;
699 while(u[pos]=='0')
700 pos++;
701
702 bool hit=(u[pos]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000703
704 int g=0;
705 if(hit) {
706 g=1;
Duraid Madina85d5f602005-04-27 11:57:39 +0000707 for(unsigned p=0; p<u.length(); p++) {
Duraid Madina8a3042c2005-05-09 13:18:34 +0000708 bool isP=(u[p]=='P');
709 bool isN=(u[p]=='N');
Duraid Madinab2322562005-04-26 07:23:02 +0000710
711 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000712 u.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000713 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000714 u.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000715 }
716 }
717
718 munchLeadingZeros(u);
719
720 int i = lefevre(u, ops);
721
722 shiftaddblob blob;
Jeff Cohen00b168892005-07-27 06:12:32 +0000723
Duraid Madinab2322562005-04-26 07:23:02 +0000724 blob.firstVal=i; blob.firstShift=c;
725 blob.isSub=f;
726 blob.secondVal=i; blob.secondShift=0;
727
728 ops.push_back(blob);
729
730 i = ops.size();
731
732 munchLeadingZeros(t);
733
734 if(t.length()==0)
735 return i;
736
737 if(t.c_str()[0]!='P') {
738 g=2;
Duraid Madina85d5f602005-04-27 11:57:39 +0000739 for(unsigned p=0; p<t.length(); p++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000740 bool isP=(t.c_str()[p]=='P');
741 bool isN=(t.c_str()[p]=='N');
742
743 if(isP)
Jeff Cohen00b168892005-07-27 06:12:32 +0000744 t.replace(p, 1, "N");
Duraid Madinab2322562005-04-26 07:23:02 +0000745 if(isN)
Jeff Cohen00b168892005-07-27 06:12:32 +0000746 t.replace(p, 1, "P");
Duraid Madinab2322562005-04-26 07:23:02 +0000747 }
748 }
749
750 int j = lefevre(t, ops);
751
752 int trail=countTrailingZeros(u);
753 blob.secondVal=i; blob.secondShift=trail;
754
755 trail=countTrailingZeros(t);
756 blob.firstVal=j; blob.firstShift=trail;
757
758 switch(g) {
759 case 0:
760 blob.isSub=false; // first + second
761 break;
762 case 1:
763 blob.isSub=true; // first - second
764 break;
765 case 2:
766 blob.isSub=true; // second - first
767 int tmpval, tmpshift;
768 tmpval=blob.firstVal;
769 tmpshift=blob.firstShift;
770 blob.firstVal=blob.secondVal;
771 blob.firstShift=blob.secondShift;
772 blob.secondVal=tmpval;
773 blob.secondShift=tmpshift;
774 break;
775 //assert
776 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000777
Duraid Madinab2322562005-04-26 07:23:02 +0000778 ops.push_back(blob);
779 return ops.size();
780}
781
782SDOperand ISel::BuildConstmulSequence(SDOperand N) {
783 //FIXME: we should shortcut this stuff for multiplies by 2^n+1
784 // in particular, *3 is nicer as *2+1, not *4-1
785 int64_t constant=cast<ConstantSDNode>(N.getOperand(1))->getValue();
786
787 bool flippedSign;
788 unsigned preliminaryShift=0;
789
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000790 assert(constant != 0 && "erk, you're trying to multiply by constant zero\n");
Duraid Madinab2322562005-04-26 07:23:02 +0000791
792 // first, we make the constant to multiply by positive
793 if(constant<0) {
794 constant=-constant;
795 flippedSign=true;
796 } else {
797 flippedSign=false;
798 }
799
800 // next, we make it odd.
801 for(; (constant%2==0); preliminaryShift++)
802 constant>>=1;
803
804 //OK, we have a positive, odd number of 64 bits or less. Convert it
805 //to a binary string, constantString[0] is the LSB
806 char constantString[65];
807 for(int i=0; i<64; i++)
808 constantString[i]='0'+((constant>>i)&0x1);
809 constantString[64]=0;
810
811 // now, Booth encode it
812 std::string boothEncodedString;
813 boothEncode(constantString, boothEncodedString);
814
815 std::vector<struct shiftaddblob> ops;
816 // do the transformation, filling out 'ops'
817 lefevre(boothEncodedString, ops);
818
Duraid Madinae75a24a2005-05-15 14:44:13 +0000819 assert(ops.size() < 80 && "constmul code has gone haywire\n");
820 SDOperand results[80]; // temporary results (of adds/subs of shifts)
Jeff Cohen00b168892005-07-27 06:12:32 +0000821
Duraid Madinab2322562005-04-26 07:23:02 +0000822 // now turn 'ops' into DAG bits
Duraid Madina85d5f602005-04-27 11:57:39 +0000823 for(unsigned i=0; i<ops.size(); i++) {
Duraid Madinab2322562005-04-26 07:23:02 +0000824 SDOperand amt = ISelDAG->getConstant(ops[i].firstShift, MVT::i64);
825 SDOperand val = (ops[i].firstVal == 0) ? N.getOperand(0) :
826 results[ops[i].firstVal-1];
827 SDOperand left = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
828 amt = ISelDAG->getConstant(ops[i].secondShift, MVT::i64);
829 val = (ops[i].secondVal == 0) ? N.getOperand(0) :
830 results[ops[i].secondVal-1];
831 SDOperand right = ISelDAG->getNode(ISD::SHL, MVT::i64, val, amt);
832 if(ops[i].isSub)
833 results[i] = ISelDAG->getNode(ISD::SUB, MVT::i64, left, right);
834 else
835 results[i] = ISelDAG->getNode(ISD::ADD, MVT::i64, left, right);
836 }
837
838 // don't forget flippedSign and preliminaryShift!
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000839 SDOperand shiftedresult;
Duraid Madinab2322562005-04-26 07:23:02 +0000840 if(preliminaryShift) {
841 SDOperand finalshift = ISelDAG->getConstant(preliminaryShift, MVT::i64);
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000842 shiftedresult = ISelDAG->getNode(ISD::SHL, MVT::i64,
Jeff Cohen00b168892005-07-27 06:12:32 +0000843 results[ops.size()-1], finalshift);
Duraid Madinab2322562005-04-26 07:23:02 +0000844 } else { // there was no preliminary divide-by-power-of-2 required
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000845 shiftedresult = results[ops.size()-1];
Duraid Madinab2322562005-04-26 07:23:02 +0000846 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000847
Duraid Madina40c9e6b2005-05-02 07:27:14 +0000848 SDOperand finalresult;
849 if(flippedSign) { // if we were multiplying by a negative constant:
850 SDOperand zero = ISelDAG->getConstant(0, MVT::i64);
851 // subtract the result from 0 to flip its sign
852 finalresult = ISelDAG->getNode(ISD::SUB, MVT::i64, zero, shiftedresult);
853 } else { // there was no preliminary multiply by -1 required
854 finalresult = shiftedresult;
855 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000856
857 return finalresult;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000858}
859
Duraid Madina4826a072005-04-06 09:55:17 +0000860/// ponderIntegerDivisionBy - When handling integer divides, if the divide
861/// is by a constant such that we can efficiently codegen it, this
862/// function says what to do. Currently, it returns 0 if the division must
863/// become a genuine divide, and 1 if the division can be turned into a
864/// right shift.
865static unsigned ponderIntegerDivisionBy(SDOperand N, bool isSigned,
866 unsigned& Imm) {
867 if (N.getOpcode() != ISD::Constant) return 0; // if not a divide by
868 // a constant, give up.
869
870 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
871
Chris Lattner0561b3f2005-08-02 19:26:06 +0000872 if (isPowerOf2_64(v)) { // if a division by a power of two, say so
873 Imm = Log2_64(v);
Duraid Madina4826a072005-04-06 09:55:17 +0000874 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000875 }
876
Duraid Madina4826a072005-04-06 09:55:17 +0000877 return 0; // fallthrough
878}
879
Duraid Madinac02780e2005-04-13 04:50:54 +0000880static unsigned ponderIntegerAndWith(SDOperand N, unsigned& Imm) {
881 if (N.getOpcode() != ISD::Constant) return 0; // if not ANDing with
882 // a constant, give up.
883
884 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
885
Chris Lattner0561b3f2005-08-02 19:26:06 +0000886 if (isMask_64(v)) { // if ANDing with ((2^n)-1) for some n
Jim Laskeyffb973d2005-08-20 11:05:23 +0000887 Imm = Log2_64(v) + 1;
Duraid Madinac02780e2005-04-13 04:50:54 +0000888 return 1; // say so
Misha Brukman4633f1c2005-04-21 23:13:11 +0000889 }
890
Duraid Madinac02780e2005-04-13 04:50:54 +0000891 return 0; // fallthrough
892}
893
Duraid Madinaf55e4032005-04-07 12:33:38 +0000894static unsigned ponderIntegerAdditionWith(SDOperand N, unsigned& Imm) {
895 if (N.getOpcode() != ISD::Constant) return 0; // if not adding a
896 // constant, give up.
897 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
898
899 if (v <= 8191 && v >= -8192) { // if this constants fits in 14 bits, say so
900 Imm = v & 0x3FFF; // 14 bits
901 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000902 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000903 return 0; // fallthrough
904}
905
906static unsigned ponderIntegerSubtractionFrom(SDOperand N, unsigned& Imm) {
907 if (N.getOpcode() != ISD::Constant) return 0; // if not subtracting a
908 // constant, give up.
909 int64_t v = (int64_t)cast<ConstantSDNode>(N)->getSignExtended();
910
911 if (v <= 127 && v >= -128) { // if this constants fits in 8 bits, say so
912 Imm = v & 0xFF; // 8 bits
913 return 1;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000914 }
Duraid Madinaf55e4032005-04-07 12:33:38 +0000915 return 0; // fallthrough
916}
917
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000918unsigned ISel::SelectExpr(SDOperand N) {
919 unsigned Result;
920 unsigned Tmp1, Tmp2, Tmp3;
921 unsigned Opc = 0;
922 MVT::ValueType DestType = N.getValueType();
923
924 unsigned opcode = N.getOpcode();
925
926 SDNode *Node = N.Val;
927 SDOperand Op0, Op1;
928
929 if (Node->getOpcode() == ISD::CopyFromReg)
930 // Just use the specified register as our input.
Chris Lattner707ebc52005-08-16 21:56:37 +0000931 return cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000932
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000933 unsigned &Reg = ExprMap[N];
934 if (Reg) return Reg;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000935
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000936 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000937 Reg = Result = (N.getValueType() != MVT::Other) ?
938 MakeReg(N.getValueType()) : 1;
939 else {
940 // If this is a call instruction, make sure to prepare ALL of the result
941 // values as well as the chain.
942 if (Node->getNumValues() == 1)
943 Reg = Result = 1; // Void call, just a chain.
944 else {
945 Result = MakeReg(Node->getValueType(0));
946 ExprMap[N.getValue(0)] = Result;
947 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
948 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
949 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
950 }
951 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000952
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000953 switch (N.getOpcode()) {
954 default:
955 Node->dump();
956 assert(0 && "Node not handled!\n");
957
958 case ISD::FrameIndex: {
959 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
960 BuildMI(BB, IA64::MOV, 1, Result).addFrameIndex(Tmp1);
961 return Result;
962 }
963
964 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000965 Tmp1 = BB->getParent()->getConstantPool()->
966 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000967 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 &&
Duraid Madina4bd708d2005-05-02 06:41:13 +00001050 "ISD::SELECT: 'select'ing something other than i1, i64 or f64!\n");
1051 // for i1, we load the condition into an integer register, then
1052 // conditionally copy Tmp2 and Tmp3 to Tmp1 in parallel (only one
1053 // of them will go through, since the integer register will hold
1054 // either 0 or 1)
1055 case MVT::i1: {
1056 bogoResult=MakeReg(MVT::i1);
1057
1058 // load the condition into an integer register
1059 unsigned condReg=MakeReg(MVT::i64);
1060 unsigned dummy=MakeReg(MVT::i64);
1061 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1062 BuildMI(BB, IA64::TPCADDIMM22, 2, condReg).addReg(dummy)
1063 .addImm(1).addReg(Tmp1);
1064
1065 // initialize Result (bool) to false (hence UNC) and if
1066 // the select condition (condReg) is false (0), copy Tmp3
1067 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoResult)
1068 .addReg(condReg).addReg(IA64::r0).addReg(Tmp3);
1069
1070 // now, if the selection condition is true, write 1 to the
1071 // result if Tmp2 is 1
1072 BuildMI(BB, IA64::TPCMPNE, 3, Result).addReg(bogoResult)
1073 .addReg(condReg).addReg(IA64::r0).addReg(Tmp2);
1074 break;
1075 }
1076 // for i64/f64, we just copy Tmp3 and then conditionally overwrite it
1077 // with Tmp2 if Tmp1 is true
Misha Brukman7847fca2005-04-22 17:54:37 +00001078 case MVT::i64:
1079 bogoResult=MakeReg(MVT::i64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001080 BuildMI(BB, IA64::MOV, 1, bogoResult).addReg(Tmp3);
1081 BuildMI(BB, IA64::CMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1082 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001083 break;
1084 case MVT::f64:
1085 bogoResult=MakeReg(MVT::f64);
Duraid Madina4bd708d2005-05-02 06:41:13 +00001086 BuildMI(BB, IA64::FMOV, 1, bogoResult).addReg(Tmp3);
1087 BuildMI(BB, IA64::CFMOV, 2, Result).addReg(bogoResult).addReg(Tmp2)
1088 .addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001089 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001090 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001091
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001092 return Result;
1093 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001094
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001095 case ISD::Constant: {
1096 unsigned depositPos=0;
1097 unsigned depositLen=0;
1098 switch (N.getValueType()) {
1099 default: assert(0 && "Cannot use constants of this type!");
1100 case MVT::i1: { // if a bool, we don't 'load' so much as generate
Misha Brukman7847fca2005-04-22 17:54:37 +00001101 // the constant:
1102 if(cast<ConstantSDNode>(N)->getValue()) // true:
1103 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1104 else // false:
1105 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(IA64::r0).addReg(IA64::r0);
1106 return Result; // early exit
1107 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001108 case MVT::i64: break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001109 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001110
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001111 int64_t immediate = cast<ConstantSDNode>(N)->getValue();
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001112
1113 if(immediate==0) { // if the constant is just zero,
1114 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r0); // just copy r0
1115 return Result; // early exit
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001116 }
1117
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001118 if (immediate <= 8191 && immediate >= -8192) {
1119 // if this constants fits in 14 bits, we use a mov the assembler will
1120 // turn into: "adds rDest=imm,r0" (and _not_ "andl"...)
1121 BuildMI(BB, IA64::MOVSIMM14, 1, Result).addSImm(immediate);
1122 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001123 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001124
1125 if (immediate <= 2097151 && immediate >= -2097152) {
1126 // if this constants fits in 22 bits, we use a mov the assembler will
1127 // turn into: "addl rDest=imm,r0"
1128 BuildMI(BB, IA64::MOVSIMM22, 1, Result).addSImm(immediate);
1129 return Result; // early exit
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001131
1132 /* otherwise, our immediate is big, so we use movl */
1133 uint64_t Imm = immediate;
Duraid Madina21478e52005-04-11 07:16:39 +00001134 BuildMI(BB, IA64::MOVLIMM64, 1, Result).addImm64(Imm);
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001135 return Result;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001136 }
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001137
1138 case ISD::UNDEF: {
1139 BuildMI(BB, IA64::IDEF, 0, Result);
1140 return Result;
1141 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001142
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001143 case ISD::GlobalAddress: {
1144 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1145 unsigned Tmp1 = MakeReg(MVT::i64);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001146
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001147 BuildMI(BB, IA64::ADD, 2, Tmp1).addGlobalAddress(GV).addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001148 BuildMI(BB, IA64::LD8, 1, Result).addReg(Tmp1);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001149
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001150 return Result;
1151 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001152
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001153 case ISD::ExternalSymbol: {
1154 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
Duraid Madinabeeaab22005-03-31 12:31:11 +00001155// assert(0 && "sorry, but what did you want an ExternalSymbol for again?");
1156 BuildMI(BB, IA64::MOV, 1, Result).addExternalSymbol(Sym); // XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001157 return Result;
1158 }
1159
1160 case ISD::FP_EXTEND: {
1161 Tmp1 = SelectExpr(N.getOperand(0));
1162 BuildMI(BB, IA64::FMOV, 1, Result).addReg(Tmp1);
1163 return Result;
1164 }
1165
Chris Lattner26e04bb2005-09-02 00:15:30 +00001166 case ISD::ANY_EXTEND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001167 case ISD::ZERO_EXTEND: {
1168 Tmp1 = SelectExpr(N.getOperand(0)); // value
Misha Brukman4633f1c2005-04-21 23:13:11 +00001169
Chris Lattner26e04bb2005-09-02 00:15:30 +00001170 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1171 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001172
Chris Lattner26e04bb2005-09-02 00:15:30 +00001173 // if the predicate reg has 1, we want a '1' in our GR.
1174 unsigned dummy = MakeReg(MVT::i64);
1175 // first load zero:
1176 BuildMI(BB, IA64::MOV, 1, dummy).addReg(IA64::r0);
1177 // ...then conditionally (PR:Tmp1) add 1:
1178 BuildMI(BB, IA64::TPCADDIMM22, 2, Result).addReg(dummy)
1179 .addImm(1).addReg(Tmp1);
1180 return Result; // XXX early exit!
1181 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001182
Chris Lattner26e04bb2005-09-02 00:15:30 +00001183 case ISD::SIGN_EXTEND:
1184 assert(N.getOperand(0).getValueType() == MVT::i1 &&
1185 "Cannot zero-extend this type!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001186
1187 Tmp1 = SelectExpr(N.getOperand(0)); // value
Chris Lattner26e04bb2005-09-02 00:15:30 +00001188 assert(0 && "don't know how to sign_extend from bool yet!");
1189 abort();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001190
Chris Lattner26e04bb2005-09-02 00:15:30 +00001191 case ISD::TRUNCATE:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001192 // we use the funky dep.z (deposit (zero)) instruction to deposit bits
1193 // of R0 appropriately.
Chris Lattner26e04bb2005-09-02 00:15:30 +00001194 assert(N.getOperand(0).getValueType() == MVT::i64 &&
1195 N.getValueType() == MVT::i1 && "Unknown truncate!");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001196 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001197
Chris Lattner26e04bb2005-09-02 00:15:30 +00001198 // if input (normal reg) is 0, 0!=0 -> false (0), if 1, 1!=0 ->true (1):
1199 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(IA64::r0);
1200 return Result; // XXX early exit!
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001201
Misha Brukman7847fca2005-04-22 17:54:37 +00001202/*
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001203 case ISD::FP_ROUND: {
1204 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001205 "error: trying to FP_ROUND something other than f64 -> f32!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001206 Tmp1 = SelectExpr(N.getOperand(0));
1207 BuildMI(BB, IA64::FADDS, 2, Result).addReg(Tmp1).addReg(IA64::F0);
1208 // we add 0.0 using a single precision add to do rounding
1209 return Result;
1210 }
1211*/
1212
1213// FIXME: the following 4 cases need cleaning
1214 case ISD::SINT_TO_FP: {
1215 Tmp1 = SelectExpr(N.getOperand(0));
1216 Tmp2 = MakeReg(MVT::f64);
1217 unsigned dummy = MakeReg(MVT::f64);
1218 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1219 BuildMI(BB, IA64::FCVTXF, 1, dummy).addReg(Tmp2);
1220 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1221 return Result;
1222 }
1223
1224 case ISD::UINT_TO_FP: {
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = MakeReg(MVT::f64);
1227 unsigned dummy = MakeReg(MVT::f64);
1228 BuildMI(BB, IA64::SETFSIG, 1, Tmp2).addReg(Tmp1);
1229 BuildMI(BB, IA64::FCVTXUF, 1, dummy).addReg(Tmp2);
1230 BuildMI(BB, IA64::FNORMD, 1, Result).addReg(dummy);
1231 return Result;
1232 }
1233
1234 case ISD::FP_TO_SINT: {
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = MakeReg(MVT::f64);
1237 BuildMI(BB, IA64::FCVTFXTRUNC, 1, Tmp2).addReg(Tmp1);
1238 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1239 return Result;
1240 }
1241
1242 case ISD::FP_TO_UINT: {
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 Tmp2 = MakeReg(MVT::f64);
1245 BuildMI(BB, IA64::FCVTFXUTRUNC, 1, Tmp2).addReg(Tmp1);
1246 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(Tmp2);
1247 return Result;
1248 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001249
1250 case ISD::FADD: {
1251 if (N.getOperand(0).getOpcode() == ISD::FMUL &&
1252 N.getOperand(0).Val->hasOneUse()) { // if we can fold this add
1253 // into an fma, do so:
1254 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001255 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1256 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1257 Tmp3 = SelectExpr(N.getOperand(1));
1258 BuildMI(BB, IA64::FMA, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1259 return Result; // early exit
1260 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001261
1262 //else, fallthrough:
1263 Tmp1 = SelectExpr(N.getOperand(0));
1264 Tmp2 = SelectExpr(N.getOperand(1));
1265 BuildMI(BB, IA64::FADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1266 return Result;
1267 }
Duraid Madinaed095022005-04-13 06:12:04 +00001268
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001269 case ISD::ADD: {
1270 if (N.getOperand(0).getOpcode() == ISD::SHL &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001271 N.getOperand(0).Val->hasOneUse()) { // if we might be able to fold
Duraid Madinaed095022005-04-13 06:12:04 +00001272 // this add into a shladd, try:
1273 ConstantSDNode *CSD = NULL;
1274 if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001275 (CSD->getValue() >= 1) && (CSD->getValue() <= 4) ) { // we can:
Duraid Madinaed095022005-04-13 06:12:04 +00001276
Misha Brukman7847fca2005-04-22 17:54:37 +00001277 // ++FusedSHLADD; // Statistic
1278 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1279 int shl_amt = CSD->getValue();
1280 Tmp3 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001281
Misha Brukman7847fca2005-04-22 17:54:37 +00001282 BuildMI(BB, IA64::SHLADD, 3, Result)
1283 .addReg(Tmp1).addImm(shl_amt).addReg(Tmp3);
1284 return Result; // early exit
Duraid Madinaed095022005-04-13 06:12:04 +00001285 }
1286 }
1287
1288 //else, fallthrough:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001289 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001290 switch (ponderIntegerAdditionWith(N.getOperand(1), Tmp3)) {
1291 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 Madina9b9d45f2005-03-17 18:17:03 +00001297 return Result;
1298 }
1299
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001300 case ISD::FMUL:
1301 Tmp1 = SelectExpr(N.getOperand(0));
1302 Tmp2 = SelectExpr(N.getOperand(1));
1303 BuildMI(BB, IA64::FMPY, 2, Result).addReg(Tmp1).addReg(Tmp2);
1304 return Result;
1305
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001306 case ISD::MUL: {
Duraid Madina4826a072005-04-06 09:55:17 +00001307
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001308 // TODO: speed!
Duraid Madinad2ff5ef2005-08-10 12:38:57 +00001309/* FIXME if(N.getOperand(1).getOpcode() != ISD::Constant) { // if not a const mul
1310 */
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001311 // boring old integer multiply with xma
Duraid Madinab2322562005-04-26 07:23:02 +00001312 Tmp1 = SelectExpr(N.getOperand(0));
1313 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001314
1315 unsigned TempFR1=MakeReg(MVT::f64);
1316 unsigned TempFR2=MakeReg(MVT::f64);
1317 unsigned TempFR3=MakeReg(MVT::f64);
1318 BuildMI(BB, IA64::SETFSIG, 1, TempFR1).addReg(Tmp1);
1319 BuildMI(BB, IA64::SETFSIG, 1, TempFR2).addReg(Tmp2);
1320 BuildMI(BB, IA64::XMAL, 1, TempFR3).addReg(TempFR1).addReg(TempFR2)
1321 .addReg(IA64::F0);
1322 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TempFR3);
1323 return Result; // early exit
1324 /* FIXME } else { // we are multiplying by an integer constant! yay
1325 return Reg = SelectExpr(BuildConstmulSequence(N)); // avert your eyes!
1326 } */
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001327 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001328
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001329 case ISD::FSUB:
1330 if(N.getOperand(0).getOpcode() == ISD::FMUL &&
Duraid Madina4826a072005-04-06 09:55:17 +00001331 N.getOperand(0).Val->hasOneUse()) { // if we can fold this sub
1332 // into an fms, do so:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001333 // ++FusedFP; // Statistic
Duraid Madina4826a072005-04-06 09:55:17 +00001334 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1335 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1336 Tmp3 = SelectExpr(N.getOperand(1));
1337 BuildMI(BB, IA64::FMS, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1338 return Result; // early exit
1339 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001340
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001341 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001342 Tmp1 = SelectExpr(N.getOperand(0));
1343 BuildMI(BB, IA64::FSUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
1344 return Result;
1345
1346 case ISD::SUB: {
1347 Tmp2 = SelectExpr(N.getOperand(1));
1348 switch (ponderIntegerSubtractionFrom(N.getOperand(0), Tmp3)) {
1349 case 1: // subtracting *from* an 8 bit constant:
1350 BuildMI(BB, IA64::SUBIMM8, 2, Result).addSImm(Tmp3).addReg(Tmp2);
1351 return Result; // early exit
1352 } // fallthrough and emit a reg+reg SUB:
1353 Tmp1 = SelectExpr(N.getOperand(0));
1354 BuildMI(BB, IA64::SUB, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001355 return Result;
1356 }
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001357
1358 case ISD::FABS: {
1359 Tmp1 = SelectExpr(N.getOperand(0));
1360 assert(DestType == MVT::f64 && "trying to fabs something other than f64?");
1361 BuildMI(BB, IA64::FABS, 1, Result).addReg(Tmp1);
1362 return Result;
1363 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001364
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001365 case ISD::FNEG: {
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001366 assert(DestType == MVT::f64 && "trying to fneg something other than f64?");
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001367
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368 if (ISD::FABS == N.getOperand(0).getOpcode()) { // && hasOneUse()?
Duraid Madina75c9fcb2005-04-02 10:33:53 +00001369 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1370 BuildMI(BB, IA64::FNEGABS, 1, Result).addReg(Tmp1); // fold in abs
1371 } else {
1372 Tmp1 = SelectExpr(N.getOperand(0));
1373 BuildMI(BB, IA64::FNEG, 1, Result).addReg(Tmp1); // plain old fneg
1374 }
1375
Duraid Madinaa7ee8b82005-04-02 05:18:38 +00001376 return Result;
1377 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001378
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001379 case ISD::AND: {
1380 switch (N.getValueType()) {
1381 default: assert(0 && "Cannot AND this type!");
1382 case MVT::i1: { // if a bool, we emit a pseudocode AND
1383 unsigned pA = SelectExpr(N.getOperand(0));
1384 unsigned pB = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001385
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001386/* our pseudocode for AND is:
1387 *
1388(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1389 cmp.eq pTemp,p0 = r0,r0 // pTemp = NOT pB
1390 ;;
1391(pB) cmp.ne pTemp,p0 = r0,r0
1392 ;;
1393(pTemp)cmp.ne pC,p0 = r0,r0 // if (NOT pB) pC = 0
1394
1395*/
1396 unsigned pTemp = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001397
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001398 unsigned bogusTemp1 = MakeReg(MVT::i1);
1399 unsigned bogusTemp2 = MakeReg(MVT::i1);
1400 unsigned bogusTemp3 = MakeReg(MVT::i1);
1401 unsigned bogusTemp4 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001402
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001403 BuildMI(BB, IA64::PCMPEQUNC, 3, bogusTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001404 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001405 BuildMI(BB, IA64::CMPEQ, 2, bogusTemp2)
Misha Brukman7847fca2005-04-22 17:54:37 +00001406 .addReg(IA64::r0).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001407 BuildMI(BB, IA64::TPCMPNE, 3, pTemp)
Misha Brukman7847fca2005-04-22 17:54:37 +00001408 .addReg(bogusTemp2).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001409 BuildMI(BB, IA64::TPCMPNE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001410 .addReg(bogusTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pTemp);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001411 break;
1412 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001413
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001414 // if not a bool, we just AND away:
1415 case MVT::i8:
1416 case MVT::i16:
1417 case MVT::i32:
1418 case MVT::i64: {
1419 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madina7340dd52005-08-21 15:43:53 +00001420 switch (ponderIntegerAndWith(N.getOperand(1), Tmp3)) {
Duraid Madinac02780e2005-04-13 04:50:54 +00001421 case 1: // ANDing a constant that is 2^n-1 for some n
Misha Brukman7847fca2005-04-22 17:54:37 +00001422 switch (Tmp3) {
1423 case 8: // if AND 0x00000000000000FF, be quaint and use zxt1
1424 BuildMI(BB, IA64::ZXT1, 1, Result).addReg(Tmp1);
1425 break;
1426 case 16: // if AND 0x000000000000FFFF, be quaint and use zxt2
1427 BuildMI(BB, IA64::ZXT2, 1, Result).addReg(Tmp1);
1428 break;
1429 case 32: // if AND 0x00000000FFFFFFFF, be quaint and use zxt4
1430 BuildMI(BB, IA64::ZXT4, 1, Result).addReg(Tmp1);
1431 break;
1432 default: // otherwise, use dep.z to paste zeros
Duraid Madina7340dd52005-08-21 15:43:53 +00001433 // FIXME: assert the dep.z is in bounds
1434 BuildMI(BB, IA64::DEPZ, 3, Result).addReg(Tmp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001435 .addImm(0).addImm(Tmp3);
1436 break;
Duraid Madina7340dd52005-08-21 15:43:53 +00001437 }
1438 return Result; // early exit
1439 } // fallthrough and emit a simple AND:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001440 Tmp2 = SelectExpr(N.getOperand(1));
1441 BuildMI(BB, IA64::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001442 }
1443 }
1444 return Result;
1445 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001446
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001447 case ISD::OR: {
1448 switch (N.getValueType()) {
1449 default: assert(0 && "Cannot OR this type!");
1450 case MVT::i1: { // if a bool, we emit a pseudocode OR
1451 unsigned pA = SelectExpr(N.getOperand(0));
1452 unsigned pB = SelectExpr(N.getOperand(1));
1453
1454 unsigned pTemp1 = MakeReg(MVT::i1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001455
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001456/* our pseudocode for OR is:
1457 *
1458
1459pC = pA OR pB
1460-------------
1461
Misha Brukman7847fca2005-04-22 17:54:37 +00001462(pA) cmp.eq.unc pC,p0 = r0,r0 // pC = pA
1463 ;;
1464(pB) cmp.eq pC,p0 = r0,r0 // if (pB) pC = 1
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001465
1466*/
1467 BuildMI(BB, IA64::PCMPEQUNC, 3, pTemp1)
Misha Brukman7847fca2005-04-22 17:54:37 +00001468 .addReg(IA64::r0).addReg(IA64::r0).addReg(pA);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001469 BuildMI(BB, IA64::TPCMPEQ, 4, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001470 .addReg(pTemp1).addReg(IA64::r0).addReg(IA64::r0).addReg(pB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001471 break;
1472 }
1473 // if not a bool, we just OR away:
1474 case MVT::i8:
1475 case MVT::i16:
1476 case MVT::i32:
1477 case MVT::i64: {
1478 Tmp1 = SelectExpr(N.getOperand(0));
1479 Tmp2 = SelectExpr(N.getOperand(1));
1480 BuildMI(BB, IA64::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1481 break;
1482 }
1483 }
1484 return Result;
1485 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001486
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001487 case ISD::XOR: {
1488 switch (N.getValueType()) {
1489 default: assert(0 && "Cannot XOR this type!");
1490 case MVT::i1: { // if a bool, we emit a pseudocode XOR
1491 unsigned pY = SelectExpr(N.getOperand(0));
1492 unsigned pZ = SelectExpr(N.getOperand(1));
1493
1494/* one possible routine for XOR is:
1495
1496 // Compute px = py ^ pz
1497 // using sum of products: px = (py & !pz) | (pz & !py)
1498 // Uses 5 instructions in 3 cycles.
1499 // cycle 1
1500(pz) cmp.eq.unc px = r0, r0 // px = pz
1501(py) cmp.eq.unc pt = r0, r0 // pt = py
1502 ;;
1503 // cycle 2
1504(pt) cmp.ne.and px = r0, r0 // px = px & !pt (px = pz & !pt)
1505(pz) cmp.ne.and pt = r0, r0 // pt = pt & !pz
1506 ;;
1507 } { .mmi
1508 // cycle 3
1509(pt) cmp.eq.or px = r0, r0 // px = px | pt
1510
1511*** Another, which we use here, requires one scratch GR. it is:
1512
1513 mov rt = 0 // initialize rt off critical path
1514 ;;
1515
1516 // cycle 1
1517(pz) cmp.eq.unc px = r0, r0 // px = pz
1518(pz) mov rt = 1 // rt = pz
1519 ;;
1520 // cycle 2
1521(py) cmp.ne px = 1, rt // if (py) px = !pz
1522
1523.. these routines kindly provided by Jim Hull
1524*/
1525 unsigned rt = MakeReg(MVT::i64);
1526
1527 // these two temporaries will never actually appear,
1528 // due to the two-address form of some of the instructions below
1529 unsigned bogoPR = MakeReg(MVT::i1); // becomes Result
1530 unsigned bogoGR = MakeReg(MVT::i64); // becomes rt
1531
1532 BuildMI(BB, IA64::MOV, 1, bogoGR).addReg(IA64::r0);
1533 BuildMI(BB, IA64::PCMPEQUNC, 3, bogoPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001534 .addReg(IA64::r0).addReg(IA64::r0).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001535 BuildMI(BB, IA64::TPCADDIMM22, 2, rt)
Misha Brukman7847fca2005-04-22 17:54:37 +00001536 .addReg(bogoGR).addImm(1).addReg(pZ);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001537 BuildMI(BB, IA64::TPCMPIMM8NE, 3, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001538 .addReg(bogoPR).addImm(1).addReg(rt).addReg(pY);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001539 break;
1540 }
1541 // if not a bool, we just XOR away:
1542 case MVT::i8:
1543 case MVT::i16:
1544 case MVT::i32:
1545 case MVT::i64: {
1546 Tmp1 = SelectExpr(N.getOperand(0));
1547 Tmp2 = SelectExpr(N.getOperand(1));
1548 BuildMI(BB, IA64::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1549 break;
1550 }
1551 }
1552 return Result;
1553 }
1554
Duraid Madina63bbed52005-05-11 05:16:09 +00001555 case ISD::CTPOP: {
1556 Tmp1 = SelectExpr(N.getOperand(0));
1557 BuildMI(BB, IA64::POPCNT, 1, Result).addReg(Tmp1);
1558 return Result;
1559 }
1560
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001561 case ISD::SHL: {
1562 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001563 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1564 Tmp2 = CN->getValue();
1565 BuildMI(BB, IA64::SHLI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1566 } else {
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 BuildMI(BB, IA64::SHL, 2, Result).addReg(Tmp1).addReg(Tmp2);
1569 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001570 return Result;
1571 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001572
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001573 case ISD::SRL: {
1574 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001575 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1576 Tmp2 = CN->getValue();
1577 BuildMI(BB, IA64::SHRUI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1578 } else {
1579 Tmp2 = SelectExpr(N.getOperand(1));
1580 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1581 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001582 return Result;
1583 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001584
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001585 case ISD::SRA: {
1586 Tmp1 = SelectExpr(N.getOperand(0));
Duraid Madinaf55e4032005-04-07 12:33:38 +00001587 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1588 Tmp2 = CN->getValue();
1589 BuildMI(BB, IA64::SHRSI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1590 } else {
1591 Tmp2 = SelectExpr(N.getOperand(1));
1592 BuildMI(BB, IA64::SHRS, 2, Result).addReg(Tmp1).addReg(Tmp2);
1593 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001594 return Result;
1595 }
1596
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001597 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001598 case ISD::SDIV:
1599 case ISD::UDIV:
1600 case ISD::SREM:
1601 case ISD::UREM: {
1602
1603 Tmp1 = SelectExpr(N.getOperand(0));
1604 Tmp2 = SelectExpr(N.getOperand(1));
1605
1606 bool isFP=false;
1607
1608 if(DestType == MVT::f64) // XXX: we're not gonna be fed MVT::f32, are we?
1609 isFP=true;
1610
1611 bool isModulus=false; // is it a division or a modulus?
1612 bool isSigned=false;
1613
1614 switch(N.getOpcode()) {
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001615 case ISD::FDIV:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001616 case ISD::SDIV: isModulus=false; isSigned=true; break;
1617 case ISD::UDIV: isModulus=false; isSigned=false; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001618 case ISD::FREM:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001619 case ISD::SREM: isModulus=true; isSigned=true; break;
1620 case ISD::UREM: isModulus=true; isSigned=false; break;
1621 }
1622
Duraid Madina4826a072005-04-06 09:55:17 +00001623 if(!isModulus && !isFP) { // if this is an integer divide,
1624 switch (ponderIntegerDivisionBy(N.getOperand(1), isSigned, Tmp3)) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001625 case 1: // division by a constant that's a power of 2
1626 Tmp1 = SelectExpr(N.getOperand(0));
1627 if(isSigned) { // argument could be negative, so emit some code:
1628 unsigned divAmt=Tmp3;
1629 unsigned tempGR1=MakeReg(MVT::i64);
1630 unsigned tempGR2=MakeReg(MVT::i64);
1631 unsigned tempGR3=MakeReg(MVT::i64);
1632 BuildMI(BB, IA64::SHRS, 2, tempGR1)
1633 .addReg(Tmp1).addImm(divAmt-1);
1634 BuildMI(BB, IA64::EXTRU, 3, tempGR2)
1635 .addReg(tempGR1).addImm(64-divAmt).addImm(divAmt);
1636 BuildMI(BB, IA64::ADD, 2, tempGR3)
1637 .addReg(Tmp1).addReg(tempGR2);
1638 BuildMI(BB, IA64::SHRS, 2, Result)
1639 .addReg(tempGR3).addImm(divAmt);
1640 }
1641 else // unsigned div-by-power-of-2 becomes a simple shift right:
1642 BuildMI(BB, IA64::SHRU, 2, Result).addReg(Tmp1).addImm(Tmp3);
1643 return Result; // early exit
Duraid Madina4826a072005-04-06 09:55:17 +00001644 }
1645 }
1646
Misha Brukman4633f1c2005-04-21 23:13:11 +00001647 unsigned TmpPR=MakeReg(MVT::i1); // we need two scratch
Duraid Madinabeeaab22005-03-31 12:31:11 +00001648 unsigned TmpPR2=MakeReg(MVT::i1); // predicate registers,
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001649 unsigned TmpF1=MakeReg(MVT::f64); // and one metric truckload of FP regs.
1650 unsigned TmpF2=MakeReg(MVT::f64); // lucky we have IA64?
1651 unsigned TmpF3=MakeReg(MVT::f64); // well, the real FIXME is to have
1652 unsigned TmpF4=MakeReg(MVT::f64); // isTwoAddress forms of these
1653 unsigned TmpF5=MakeReg(MVT::f64); // FP instructions so we can end up with
1654 unsigned TmpF6=MakeReg(MVT::f64); // stuff like setf.sig f10=f10 etc.
1655 unsigned TmpF7=MakeReg(MVT::f64);
1656 unsigned TmpF8=MakeReg(MVT::f64);
1657 unsigned TmpF9=MakeReg(MVT::f64);
1658 unsigned TmpF10=MakeReg(MVT::f64);
1659 unsigned TmpF11=MakeReg(MVT::f64);
1660 unsigned TmpF12=MakeReg(MVT::f64);
1661 unsigned TmpF13=MakeReg(MVT::f64);
1662 unsigned TmpF14=MakeReg(MVT::f64);
1663 unsigned TmpF15=MakeReg(MVT::f64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001664
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001665 // OK, emit some code:
1666
1667 if(!isFP) {
1668 // first, load the inputs into FP regs.
1669 BuildMI(BB, IA64::SETFSIG, 1, TmpF1).addReg(Tmp1);
1670 BuildMI(BB, IA64::SETFSIG, 1, TmpF2).addReg(Tmp2);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001671
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001672 // next, convert the inputs to FP
1673 if(isSigned) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001674 BuildMI(BB, IA64::FCVTXF, 1, TmpF3).addReg(TmpF1);
1675 BuildMI(BB, IA64::FCVTXF, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001676 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001677 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF3).addReg(TmpF1);
1678 BuildMI(BB, IA64::FCVTXUFS1, 1, TmpF4).addReg(TmpF2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001679 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001680
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001681 } else { // this is an FP divide/remainder, so we 'leak' some temp
1682 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
1683 TmpF3=Tmp1;
1684 TmpF4=Tmp2;
1685 }
1686
1687 // we start by computing an approximate reciprocal (good to 9 bits?)
Duraid Madina6dcceb52005-04-08 10:01:48 +00001688 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
1689 BuildMI(BB, IA64::FRCPAS1, 4)
1690 .addReg(TmpF5, MachineOperand::Def)
1691 .addReg(TmpPR, MachineOperand::Def)
1692 .addReg(TmpF3).addReg(TmpF4);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001693
Duraid Madinabeeaab22005-03-31 12:31:11 +00001694 if(!isModulus) { // if this is a divide, we worry about div-by-zero
1695 unsigned bogusPR=MakeReg(MVT::i1); // won't appear, due to twoAddress
1696 // TPCMPNE below
1697 BuildMI(BB, IA64::CMPEQ, 2, bogusPR).addReg(IA64::r0).addReg(IA64::r0);
1698 BuildMI(BB, IA64::TPCMPNE, 3, TmpPR2).addReg(bogusPR)
Misha Brukman7847fca2005-04-22 17:54:37 +00001699 .addReg(IA64::r0).addReg(IA64::r0).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001700 }
1701
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001702 // now we apply newton's method, thrice! (FIXME: this is ~72 bits of
1703 // precision, don't need this much for f32/i32)
1704 BuildMI(BB, IA64::CFNMAS1, 4, TmpF6)
1705 .addReg(TmpF4).addReg(TmpF5).addReg(IA64::F1).addReg(TmpPR);
1706 BuildMI(BB, IA64::CFMAS1, 4, TmpF7)
1707 .addReg(TmpF3).addReg(TmpF5).addReg(IA64::F0).addReg(TmpPR);
1708 BuildMI(BB, IA64::CFMAS1, 4, TmpF8)
1709 .addReg(TmpF6).addReg(TmpF6).addReg(IA64::F0).addReg(TmpPR);
1710 BuildMI(BB, IA64::CFMAS1, 4, TmpF9)
1711 .addReg(TmpF6).addReg(TmpF7).addReg(TmpF7).addReg(TmpPR);
1712 BuildMI(BB, IA64::CFMAS1, 4,TmpF10)
1713 .addReg(TmpF6).addReg(TmpF5).addReg(TmpF5).addReg(TmpPR);
1714 BuildMI(BB, IA64::CFMAS1, 4,TmpF11)
1715 .addReg(TmpF8).addReg(TmpF9).addReg(TmpF9).addReg(TmpPR);
1716 BuildMI(BB, IA64::CFMAS1, 4,TmpF12)
1717 .addReg(TmpF8).addReg(TmpF10).addReg(TmpF10).addReg(TmpPR);
1718 BuildMI(BB, IA64::CFNMAS1, 4,TmpF13)
1719 .addReg(TmpF4).addReg(TmpF11).addReg(TmpF3).addReg(TmpPR);
Duraid Madina6e02e682005-04-04 05:05:52 +00001720
1721 // FIXME: this is unfortunate :(
1722 // the story is that the dest reg of the fnma above and the fma below
1723 // (and therefore possibly the src of the fcvt.fx[u] as well) cannot
1724 // be the same register, or this code breaks if the first argument is
1725 // zero. (e.g. without this hack, 0%8 yields -64, not 0.)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001726 BuildMI(BB, IA64::CFMAS1, 4,TmpF14)
1727 .addReg(TmpF13).addReg(TmpF12).addReg(TmpF11).addReg(TmpPR);
1728
Duraid Madina6e02e682005-04-04 05:05:52 +00001729 if(isModulus) { // XXX: fragile! fixes _only_ mod, *breaks* div! !
1730 BuildMI(BB, IA64::IUSE, 1).addReg(TmpF13); // hack :(
1731 }
1732
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001733 if(!isFP) {
1734 // round to an integer
1735 if(isSigned)
Misha Brukman7847fca2005-04-22 17:54:37 +00001736 BuildMI(BB, IA64::FCVTFXTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001737 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001738 BuildMI(BB, IA64::FCVTFXUTRUNCS1, 1, TmpF15).addReg(TmpF14);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001739 } else {
1740 BuildMI(BB, IA64::FMOV, 1, TmpF15).addReg(TmpF14);
1741 // EXERCISE: can you see why TmpF15=TmpF14 does not work here, and
1742 // we really do need the above FMOV? ;)
1743 }
1744
1745 if(!isModulus) {
Duraid Madinabeeaab22005-03-31 12:31:11 +00001746 if(isFP) { // extra worrying about div-by-zero
1747 unsigned bogoResult=MakeReg(MVT::f64);
1748
1749 // we do a 'conditional fmov' (of the correct result, depending
1750 // on how the frcpa predicate turned out)
1751 BuildMI(BB, IA64::PFMOV, 2, bogoResult)
Misha Brukman7847fca2005-04-22 17:54:37 +00001752 .addReg(TmpF12).addReg(TmpPR2);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001753 BuildMI(BB, IA64::CFMOV, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00001754 .addReg(bogoResult).addReg(TmpF15).addReg(TmpPR);
Duraid Madinabeeaab22005-03-31 12:31:11 +00001755 }
Duraid Madina6e02e682005-04-04 05:05:52 +00001756 else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001757 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(TmpF15);
Duraid Madina6e02e682005-04-04 05:05:52 +00001758 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001759 } else { // this is a modulus
1760 if(!isFP) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001761 // answer = q * (-b) + a
1762 unsigned ModulusResult = MakeReg(MVT::f64);
1763 unsigned TmpF = MakeReg(MVT::f64);
1764 unsigned TmpI = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00001765
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 BuildMI(BB, IA64::SUB, 2, TmpI).addReg(IA64::r0).addReg(Tmp2);
1767 BuildMI(BB, IA64::SETFSIG, 1, TmpF).addReg(TmpI);
1768 BuildMI(BB, IA64::XMAL, 3, ModulusResult)
1769 .addReg(TmpF15).addReg(TmpF).addReg(TmpF1);
1770 BuildMI(BB, IA64::GETFSIG, 1, Result).addReg(ModulusResult);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001771 } else { // FP modulus! The horror... the horror....
Misha Brukman7847fca2005-04-22 17:54:37 +00001772 assert(0 && "sorry, no FP modulus just yet!\n!\n");
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001773 }
1774 }
1775
1776 return Result;
1777 }
1778
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001779 case ISD::SIGN_EXTEND_INREG: {
1780 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001781 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001782 default:
1783 Node->dump();
1784 assert(0 && "don't know how to sign extend this type");
1785 break;
1786 case MVT::i8: Opc = IA64::SXT1; break;
1787 case MVT::i16: Opc = IA64::SXT2; break;
1788 case MVT::i32: Opc = IA64::SXT4; break;
1789 }
1790 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1791 return Result;
1792 }
1793
1794 case ISD::SETCC: {
1795 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001796 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1797 if (MVT::isInteger(N.getOperand(0).getValueType())) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001798
Chris Lattner88ac32c2005-08-09 20:21:10 +00001799 if(ConstantSDNode *CSDN =
1800 dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1801 // if we are comparing against a constant zero
1802 if(CSDN->getValue()==0)
1803 Tmp2 = IA64::r0; // then we can just compare against r0
1804 else
1805 Tmp2 = SelectExpr(N.getOperand(1));
1806 } else // not comparing against a constant
1807 Tmp2 = SelectExpr(N.getOperand(1));
Duraid Madina5ef2ec92005-04-11 05:55:56 +00001808
Chris Lattner88ac32c2005-08-09 20:21:10 +00001809 switch (CC) {
1810 default: assert(0 && "Unknown integer comparison!");
1811 case ISD::SETEQ:
1812 BuildMI(BB, IA64::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1813 break;
1814 case ISD::SETGT:
1815 BuildMI(BB, IA64::CMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1816 break;
1817 case ISD::SETGE:
1818 BuildMI(BB, IA64::CMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 break;
1820 case ISD::SETLT:
1821 BuildMI(BB, IA64::CMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1822 break;
1823 case ISD::SETLE:
1824 BuildMI(BB, IA64::CMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1825 break;
1826 case ISD::SETNE:
1827 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 break;
1829 case ISD::SETULT:
1830 BuildMI(BB, IA64::CMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1831 break;
1832 case ISD::SETUGT:
1833 BuildMI(BB, IA64::CMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1834 break;
1835 case ISD::SETULE:
1836 BuildMI(BB, IA64::CMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1837 break;
1838 case ISD::SETUGE:
1839 BuildMI(BB, IA64::CMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1840 break;
1841 }
1842 } else { // if not integer, should be FP.
1843 assert(N.getOperand(0).getValueType() != MVT::f32 &&
1844 "error: SETCC should have had incoming f32 promoted to f64!\n");
1845
1846 if(ConstantFPSDNode *CFPSDN =
1847 dyn_cast<ConstantFPSDNode>(N.getOperand(1))) {
1848
1849 // if we are comparing against a constant +0.0 or +1.0
1850 if(CFPSDN->isExactlyValue(+0.0))
1851 Tmp2 = IA64::F0; // then we can just compare against f0
1852 else if(CFPSDN->isExactlyValue(+1.0))
1853 Tmp2 = IA64::F1; // or f1
Misha Brukman7847fca2005-04-22 17:54:37 +00001854 else
1855 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88ac32c2005-08-09 20:21:10 +00001856 } else // not comparing against a constant
1857 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001858
Chris Lattner88ac32c2005-08-09 20:21:10 +00001859 switch (CC) {
1860 default: assert(0 && "Unknown FP comparison!");
1861 case ISD::SETEQ:
1862 BuildMI(BB, IA64::FCMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 break;
1864 case ISD::SETGT:
1865 BuildMI(BB, IA64::FCMPGT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::SETGE:
1868 BuildMI(BB, IA64::FCMPGE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1869 break;
1870 case ISD::SETLT:
1871 BuildMI(BB, IA64::FCMPLT, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 break;
1873 case ISD::SETLE:
1874 BuildMI(BB, IA64::FCMPLE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 break;
1876 case ISD::SETNE:
1877 BuildMI(BB, IA64::FCMPNE, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 break;
1879 case ISD::SETULT:
1880 BuildMI(BB, IA64::FCMPLTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1881 break;
1882 case ISD::SETUGT:
1883 BuildMI(BB, IA64::FCMPGTU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 break;
1885 case ISD::SETULE:
1886 BuildMI(BB, IA64::FCMPLEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 break;
1888 case ISD::SETUGE:
1889 BuildMI(BB, IA64::FCMPGEU, 2, Result).addReg(Tmp1).addReg(Tmp2);
1890 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001891 }
1892 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001893 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;
Jeff Cohen00b168892005-07-27 06:12:32 +00001916
Misha Brukman7847fca2005-04-22 17:54:37 +00001917 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
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001921 MVT::ValueType TypeBeingLoaded =
1922 cast<VTSDNode>(Node->getOperand(3))->getVT();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001923 switch (TypeBeingLoaded) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001924 default: assert(0 && "Cannot extload/zextload this type!");
1925 // FIXME: bools?
1926 case MVT::i8: Opc = IA64::LD1; break;
1927 case MVT::i16: Opc = IA64::LD2; break;
1928 case MVT::i32: Opc = IA64::LD4; break;
1929 case MVT::f32: Opc = IA64::LDF4; break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001930 }
1931 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001932
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001933 SDOperand Chain = N.getOperand(0);
1934 SDOperand Address = N.getOperand(1);
1935
1936 if(Address.getOpcode() == ISD::GlobalAddress) {
1937 Select(Chain);
1938 unsigned dummy = MakeReg(MVT::i64);
1939 unsigned dummy2 = MakeReg(MVT::i64);
1940 BuildMI(BB, IA64::ADD, 2, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001941 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal())
1942 .addReg(IA64::r1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001943 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
1944 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001945 BuildMI(BB, Opc, 1, Result).addReg(dummy2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001946 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001947 // into a predicate register
1948 assert(Opc==IA64::LD1 && "problem loading a bool");
1949 unsigned dummy3 = MakeReg(MVT::i64);
1950 BuildMI(BB, Opc, 1, dummy3).addReg(dummy2);
1951 // we compare to 0. true? 0. false? 1.
1952 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001953 }
1954 } else if(ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001955 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattner143b6752005-08-26 17:18:44 +00001956 getConstantPoolIndex(CP->get());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001957 Select(Chain);
1958 IA64Lowering.restoreGP(BB);
1959 unsigned dummy = MakeReg(MVT::i64);
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001960 unsigned dummy2 = MakeReg(MVT::i64);
1961 BuildMI(BB, IA64::MOVLIMM64, 1, dummy2).addConstantPoolIndex(CPIdx);
1962 BuildMI(BB, IA64::ADD, 2, dummy).addReg(dummy2).addReg(IA64::r1); //CPI+GP
1963
1964
1965 // OLD BuildMI(BB, IA64::ADD, 2, dummy).addConstantPoolIndex(CPIdx)
1966 // (FIXME!) .addReg(IA64::r1); // CPI+GP
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001967 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001968 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001969 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001970 // into a predicate register
1971 assert(Opc==IA64::LD1 && "problem loading a bool");
1972 unsigned dummy3 = MakeReg(MVT::i64);
1973 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1974 // we compare to 0. true? 0. false? 1.
1975 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001976 }
1977 } else if(Address.getOpcode() == ISD::FrameIndex) {
1978 Select(Chain); // FIXME ? what about bools?
1979 unsigned dummy = MakeReg(MVT::i64);
1980 BuildMI(BB, IA64::MOV, 1, dummy)
Misha Brukman7847fca2005-04-22 17:54:37 +00001981 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001982 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001983 BuildMI(BB, Opc, 1, Result).addReg(dummy);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001984 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001985 // into a predicate register
1986 assert(Opc==IA64::LD1 && "problem loading a bool");
1987 unsigned dummy3 = MakeReg(MVT::i64);
1988 BuildMI(BB, Opc, 1, dummy3).addReg(dummy);
1989 // we compare to 0. true? 0. false? 1.
1990 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy3).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001991 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001992 } else { // none of the above...
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001993 Select(Chain);
1994 Tmp2 = SelectExpr(Address);
1995 if(!isBool)
Misha Brukman7847fca2005-04-22 17:54:37 +00001996 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001997 else { // emit a little pseudocode to load a bool (stored in one byte)
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 // into a predicate register
1999 assert(Opc==IA64::LD1 && "problem loading a bool");
2000 unsigned dummy = MakeReg(MVT::i64);
2001 BuildMI(BB, Opc, 1, dummy).addReg(Tmp2);
2002 // we compare to 0. true? 0. false? 1.
2003 BuildMI(BB, IA64::CMPNE, 2, Result).addReg(dummy).addReg(IA64::r0);
Jeff Cohen00b168892005-07-27 06:12:32 +00002004 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002005 }
2006
2007 return Result;
2008 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002009
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002010 case ISD::CopyFromReg: {
2011 if (Result == 1)
Misha Brukman4633f1c2005-04-21 23:13:11 +00002012 Result = ExprMap[N.getValue(0)] =
Misha Brukman7847fca2005-04-22 17:54:37 +00002013 MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00002014
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002015 SDOperand Chain = N.getOperand(0);
2016
2017 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00002018 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002019
2020 if(N.getValueType() == MVT::i1) // if a bool, we use pseudocode
Misha Brukman7847fca2005-04-22 17:54:37 +00002021 BuildMI(BB, IA64::PCMPEQUNC, 3, Result)
2022 .addReg(IA64::r0).addReg(IA64::r0).addReg(r);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002023 // (r) Result =cmp.eq.unc(r0,r0)
2024 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002025 BuildMI(BB, IA64::MOV, 1, Result).addReg(r); // otherwise MOV
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002026 return Result;
2027 }
2028
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002029 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002030 case ISD::CALL: {
2031 Select(N.getOperand(0));
2032
2033 // The chain for this call is now lowered.
2034 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002035
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002036 //grab the arguments
2037 std::vector<unsigned> argvregs;
2038
2039 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Misha Brukman7847fca2005-04-22 17:54:37 +00002040 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002041
2042 // see section 8.5.8 of "Itanium Software Conventions and
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002043 // Runtime Architecture Guide to see some examples of what's going
2044 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
2045 // while FP args get mapped to F8->F15 as needed)
2046
2047 unsigned used_FPArgs=0; // how many FP Args have been used so far?
Misha Brukman4633f1c2005-04-21 23:13:11 +00002048
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002049 // in reg args
2050 for(int i = 0, e = std::min(8, (int)argvregs.size()); i < e; ++i)
2051 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002052 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
2053 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
2054 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
2055 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002056
Misha Brukman7847fca2005-04-22 17:54:37 +00002057 switch(N.getOperand(i+2).getValueType())
2058 {
2059 default: // XXX do we need to support MVT::i1 here?
2060 Node->dump();
2061 N.getOperand(i).Val->dump();
2062 std::cerr << "Type for " << i << " is: " <<
2063 N.getOperand(i+2).getValueType() << std::endl;
2064 assert(0 && "Unknown value type for call");
2065 case MVT::i64:
2066 BuildMI(BB, IA64::MOV, 1, intArgs[i]).addReg(argvregs[i]);
2067 break;
2068 case MVT::f64:
2069 BuildMI(BB, IA64::FMOV, 1, FPArgs[used_FPArgs++])
2070 .addReg(argvregs[i]);
2071 // FIXME: we don't need to do this _all_ the time:
2072 BuildMI(BB, IA64::GETFD, 1, intArgs[i]).addReg(argvregs[i]);
2073 break;
2074 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002075 }
2076
2077 //in mem args
2078 for (int i = 8, e = argvregs.size(); i < e; ++i)
2079 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002080 unsigned tempAddr = MakeReg(MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +00002081
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002082 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002083 default:
2084 Node->dump();
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002085 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002086 std::cerr << "Type for " << i << " is: " <<
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002087 N.getOperand(i+2).getValueType() << "\n";
2088 assert(0 && "Unknown value type for call");
2089 case MVT::i1: // FIXME?
2090 case MVT::i8:
2091 case MVT::i16:
2092 case MVT::i32:
2093 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002094 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
2095 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2096 BuildMI(BB, IA64::ST8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002097 break;
2098 case MVT::f32:
2099 case MVT::f64:
2100 BuildMI(BB, IA64::ADDIMM22, 2, tempAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002101 .addReg(IA64::r12).addImm(16 + (i - 8) * 8); // r12 is SP
2102 BuildMI(BB, IA64::STF8, 2).addReg(tempAddr).addReg(argvregs[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002103 break;
2104 }
2105 }
Duraid Madinabeeaab22005-03-31 12:31:11 +00002106
Duraid Madina04aa46d2005-05-20 11:39:17 +00002107 // build the right kind of call. if we can branch directly, do so:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002108 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00002109 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002110 {
Misha Brukman7847fca2005-04-22 17:54:37 +00002111 BuildMI(BB, IA64::BRCALL, 1).addGlobalAddress(GASD->getGlobal(),true);
2112 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina04aa46d2005-05-20 11:39:17 +00002113 } else
Duraid Madinabeeaab22005-03-31 12:31:11 +00002114 if (ExternalSymbolSDNode *ESSDN =
Misha Brukman7847fca2005-04-22 17:54:37 +00002115 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Duraid Madinabeeaab22005-03-31 12:31:11 +00002116 { // FIXME : currently need this case for correctness, to avoid
Misha Brukman7847fca2005-04-22 17:54:37 +00002117 // "non-pic code with imm relocation against dynamic symbol" errors
2118 BuildMI(BB, IA64::BRCALL, 1)
2119 .addExternalSymbol(ESSDN->getSymbol(), true);
2120 IA64Lowering.restoreGP_SP_RP(BB);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002121 }
Duraid Madina04aa46d2005-05-20 11:39:17 +00002122 else { // otherwise we need to get the function descriptor
2123 // load the branch target (function)'s entry point and
Jeff Cohen00b168892005-07-27 06:12:32 +00002124 // GP, then branch
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002125 Tmp1 = SelectExpr(N.getOperand(1));
Duraid Madinabeeaab22005-03-31 12:31:11 +00002126
2127 unsigned targetEntryPoint=MakeReg(MVT::i64);
2128 unsigned targetGPAddr=MakeReg(MVT::i64);
2129 unsigned currentGP=MakeReg(MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002130
Duraid Madinabeeaab22005-03-31 12:31:11 +00002131 // b6 is a scratch branch register, we load the target entry point
2132 // from the base of the function descriptor
2133 BuildMI(BB, IA64::LD8, 1, targetEntryPoint).addReg(Tmp1);
2134 BuildMI(BB, IA64::MOV, 1, IA64::B6).addReg(targetEntryPoint);
2135
2136 // save the current GP:
2137 BuildMI(BB, IA64::MOV, 1, currentGP).addReg(IA64::r1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002138
Duraid Madinabeeaab22005-03-31 12:31:11 +00002139 /* TODO: we need to make sure doing this never, ever loads a
2140 * bogus value into r1 (GP). */
2141 // load the target GP (which is at mem[functiondescriptor+8])
2142 BuildMI(BB, IA64::ADDIMM22, 2, targetGPAddr)
Misha Brukman7847fca2005-04-22 17:54:37 +00002143 .addReg(Tmp1).addImm(8); // FIXME: addimm22? why not postincrement ld
Duraid Madinabeeaab22005-03-31 12:31:11 +00002144 BuildMI(BB, IA64::LD8, 1, IA64::r1).addReg(targetGPAddr);
2145
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002146 // and then jump: (well, call)
2147 BuildMI(BB, IA64::BRCALL, 1).addReg(IA64::B6);
Duraid Madinabeeaab22005-03-31 12:31:11 +00002148 // and finally restore the old GP
2149 BuildMI(BB, IA64::MOV, 1, IA64::r1).addReg(currentGP);
2150 IA64Lowering.restoreSP_RP(BB);
2151 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002152
2153 switch (Node->getValueType(0)) {
2154 default: assert(0 && "Unknown value type for call result!");
2155 case MVT::Other: return 1;
2156 case MVT::i1:
2157 BuildMI(BB, IA64::CMPNE, 2, Result)
Misha Brukman7847fca2005-04-22 17:54:37 +00002158 .addReg(IA64::r8).addReg(IA64::r0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002159 break;
2160 case MVT::i8:
2161 case MVT::i16:
2162 case MVT::i32:
2163 case MVT::i64:
2164 BuildMI(BB, IA64::MOV, 1, Result).addReg(IA64::r8);
2165 break;
2166 case MVT::f64:
2167 BuildMI(BB, IA64::FMOV, 1, Result).addReg(IA64::F8);
2168 break;
2169 }
2170 return Result+N.ResNo;
2171 }
2172
Misha Brukman4633f1c2005-04-21 23:13:11 +00002173 } // <- uhhh XXX
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002174 return 0;
2175}
2176
2177void ISel::Select(SDOperand N) {
2178 unsigned Tmp1, Tmp2, Opc;
2179 unsigned opcode = N.getOpcode();
2180
Nate Begeman85fdeb22005-03-24 04:39:54 +00002181 if (!LoweredTokens.insert(N).second)
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002182 return; // Already selected.
2183
2184 SDNode *Node = N.Val;
2185
2186 switch (Node->getOpcode()) {
2187 default:
2188 Node->dump(); std::cerr << "\n";
2189 assert(0 && "Node not handled yet!");
2190
2191 case ISD::EntryToken: return; // Noop
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002193 case ISD::TokenFactor: {
2194 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2195 Select(Node->getOperand(i));
2196 return;
2197 }
2198
2199 case ISD::CopyToReg: {
2200 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002201 Tmp1 = SelectExpr(N.getOperand(2));
2202 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002203
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002204 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002205 // if a bool, we use pseudocode
2206 if (N.getOperand(2).getValueType() == MVT::i1)
Misha Brukman7847fca2005-04-22 17:54:37 +00002207 BuildMI(BB, IA64::PCMPEQUNC, 3, Tmp2)
2208 .addReg(IA64::r0).addReg(IA64::r0).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002209 // (Tmp1) Tmp2 = cmp.eq.unc(r0,r0)
2210 else
Misha Brukman7847fca2005-04-22 17:54:37 +00002211 BuildMI(BB, IA64::MOV, 1, Tmp2).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002212 // XXX is this the right way 'round? ;)
Chris Lattner707ebc52005-08-16 21:56:37 +00002213 // FIXME: WHAT ABOUT FLOATING POINT?
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002214 }
2215 return;
2216 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002217
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002218 case ISD::RET: {
2219
2220 /* what the heck is going on here:
2221
2222<_sabre_> ret with two operands is obvious: chain and value
2223<camel_> yep
2224<_sabre_> ret with 3 values happens when 'expansion' occurs
2225<_sabre_> e.g. i64 gets split into 2x i32
2226<camel_> oh right
2227<_sabre_> you don't have this case on ia64
2228<camel_> yep
2229<_sabre_> so the two returned values go into EAX/EDX on ia32
2230<camel_> ahhh *memories*
2231<_sabre_> :)
2232<camel_> ok, thanks :)
2233<_sabre_> so yeah, everything that has a side effect takes a 'token chain'
2234<_sabre_> this is the first operand always
2235<_sabre_> these operand often define chains, they are the last operand
2236<_sabre_> they are printed as 'ch' if you do DAG.dump()
2237 */
Misha Brukman4633f1c2005-04-21 23:13:11 +00002238
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002239 switch (N.getNumOperands()) {
2240 default:
2241 assert(0 && "Unknown return instruction!");
2242 case 2:
2243 Select(N.getOperand(0));
2244 Tmp1 = SelectExpr(N.getOperand(1));
2245 switch (N.getOperand(1).getValueType()) {
2246 default: assert(0 && "All other types should have been promoted!!");
Misha Brukman7847fca2005-04-22 17:54:37 +00002247 // FIXME: do I need to add support for bools here?
2248 // (return '0' or '1' r8, basically...)
2249 //
2250 // FIXME: need to round floats - 80 bits is bad, the tester
2251 // told me so
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002252 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002253 // we mark r8 as live on exit up above in LowerArguments()
2254 BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
2255 break;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002256 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00002257 // we mark F8 as live on exit up above in LowerArguments()
2258 BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002259 }
2260 break;
2261 case 1:
2262 Select(N.getOperand(0));
2263 break;
2264 }
2265 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
2266 BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
2267 BuildMI(BB, IA64::RET, 0); // and then just emit a 'ret' instruction
2268 return;
2269 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002270
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002271 case ISD::BR: {
2272 Select(N.getOperand(0));
2273 MachineBasicBlock *Dest =
2274 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2275 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(IA64::p0).addMBB(Dest);
2276 // XXX HACK! we do _not_ need long branches all the time
2277 return;
2278 }
2279
2280 case ISD::ImplicitDef: {
2281 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002282 BuildMI(BB, IA64::IDEF, 0,
2283 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002284 return;
2285 }
2286
2287 case ISD::BRCOND: {
2288 MachineBasicBlock *Dest =
2289 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
2290
2291 Select(N.getOperand(0));
2292 Tmp1 = SelectExpr(N.getOperand(1));
2293 BuildMI(BB, IA64::BRLCOND_NOTCALL, 1).addReg(Tmp1).addMBB(Dest);
2294 // XXX HACK! we do _not_ need long branches all the time
2295 return;
2296 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002297
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002298 case ISD::EXTLOAD:
2299 case ISD::ZEXTLOAD:
2300 case ISD::SEXTLOAD:
2301 case ISD::LOAD:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002302 case ISD::TAILCALL:
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002303 case ISD::CALL:
2304 case ISD::CopyFromReg:
2305 case ISD::DYNAMIC_STACKALLOC:
2306 SelectExpr(N);
2307 return;
2308
2309 case ISD::TRUNCSTORE:
2310 case ISD::STORE: {
2311 Select(N.getOperand(0));
2312 Tmp1 = SelectExpr(N.getOperand(1)); // value
2313
2314 bool isBool=false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002315
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002316 if(opcode == ISD::STORE) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002317 switch (N.getOperand(1).getValueType()) {
2318 default: assert(0 && "Cannot store this type!");
2319 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2320 // FIXME?: for now, we treat bool loads the same as i8 stores */
2321 case MVT::i8: Opc = IA64::ST1; break;
2322 case MVT::i16: Opc = IA64::ST2; break;
2323 case MVT::i32: Opc = IA64::ST4; break;
2324 case MVT::i64: Opc = IA64::ST8; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002325
Misha Brukman7847fca2005-04-22 17:54:37 +00002326 case MVT::f32: Opc = IA64::STF4; break;
2327 case MVT::f64: Opc = IA64::STF8; break;
2328 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002329 } else { // truncstore
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002330 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002331 default: assert(0 && "unknown type in truncstore");
2332 case MVT::i1: Opc = IA64::ST1; isBool=true; break;
2333 //FIXME: DAG does not promote this load?
2334 case MVT::i8: Opc = IA64::ST1; break;
2335 case MVT::i16: Opc = IA64::ST2; break;
2336 case MVT::i32: Opc = IA64::ST4; break;
2337 case MVT::f32: Opc = IA64::STF4; break;
2338 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002339 }
2340
2341 if(N.getOperand(2).getOpcode() == ISD::GlobalAddress) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002342 unsigned dummy = MakeReg(MVT::i64);
2343 unsigned dummy2 = MakeReg(MVT::i64);
2344 BuildMI(BB, IA64::ADD, 2, dummy)
2345 .addGlobalAddress(cast<GlobalAddressSDNode>
2346 (N.getOperand(2))->getGlobal()).addReg(IA64::r1);
2347 BuildMI(BB, IA64::LD8, 1, dummy2).addReg(dummy);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002348
Misha Brukman7847fca2005-04-22 17:54:37 +00002349 if(!isBool)
2350 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(Tmp1);
2351 else { // we are storing a bool, so emit a little pseudocode
2352 // to store a predicate register as one byte
2353 assert(Opc==IA64::ST1);
2354 unsigned dummy3 = MakeReg(MVT::i64);
2355 unsigned dummy4 = MakeReg(MVT::i64);
2356 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2357 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2358 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2359 BuildMI(BB, Opc, 2).addReg(dummy2).addReg(dummy4);
2360 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002361 } else if(N.getOperand(2).getOpcode() == ISD::FrameIndex) {
2362
Misha Brukman7847fca2005-04-22 17:54:37 +00002363 // FIXME? (what about bools?)
Jeff Cohen00b168892005-07-27 06:12:32 +00002364
Misha Brukman7847fca2005-04-22 17:54:37 +00002365 unsigned dummy = MakeReg(MVT::i64);
2366 BuildMI(BB, IA64::MOV, 1, dummy)
2367 .addFrameIndex(cast<FrameIndexSDNode>(N.getOperand(2))->getIndex());
2368 BuildMI(BB, Opc, 2).addReg(dummy).addReg(Tmp1);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002369 } else { // otherwise
Misha Brukman7847fca2005-04-22 17:54:37 +00002370 Tmp2 = SelectExpr(N.getOperand(2)); //address
2371 if(!isBool)
2372 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(Tmp1);
2373 else { // we are storing a bool, so emit a little pseudocode
2374 // to store a predicate register as one byte
2375 assert(Opc==IA64::ST1);
2376 unsigned dummy3 = MakeReg(MVT::i64);
2377 unsigned dummy4 = MakeReg(MVT::i64);
2378 BuildMI(BB, IA64::MOV, 1, dummy3).addReg(IA64::r0);
2379 BuildMI(BB, IA64::TPCADDIMM22, 2, dummy4)
2380 .addReg(dummy3).addImm(1).addReg(Tmp1); // if(Tmp1) dummy=0+1;
2381 BuildMI(BB, Opc, 2).addReg(Tmp2).addReg(dummy4);
2382 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002383 }
2384 return;
2385 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00002386
Chris Lattner16cd04d2005-05-12 23:24:06 +00002387 case ISD::CALLSEQ_START:
2388 case ISD::CALLSEQ_END: {
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002389 Select(N.getOperand(0));
2390 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002391
Chris Lattner16cd04d2005-05-12 23:24:06 +00002392 Opc = N.getOpcode() == ISD::CALLSEQ_START ? IA64::ADJUSTCALLSTACKDOWN :
2393 IA64::ADJUSTCALLSTACKUP;
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002394 BuildMI(BB, Opc, 1).addImm(Tmp1);
2395 return;
2396 }
2397
2398 return;
2399 }
2400 assert(0 && "GAME OVER. INSERT COIN?");
2401}
2402
2403
2404/// createIA64PatternInstructionSelector - This pass converts an LLVM function
2405/// into a machine code representation using pattern matching and a machine
2406/// description file.
2407///
2408FunctionPass *llvm::createIA64PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002409 return new ISel(TM);
Duraid Madina9b9d45f2005-03-17 18:17:03 +00002410}
2411
2412