blob: f8b8a887af0c037d2fcbde53772683ec413b6689 [file] [log] [blame]
Chris Lattner1c809c52004-02-29 00:27:00 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a simple peephole instruction selector for the V8 target
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcV8.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000015#include "SparcV8InstrInfo.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000016#include "llvm/Instructions.h"
17#include "llvm/IntrinsicLowering.h"
18#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000019#include "llvm/Constants.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineFunction.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/GetElementPtrTypeIterator.h"
25#include "llvm/Support/InstVisitor.h"
26#include "llvm/Support/CFG.h"
27using namespace llvm;
28
29namespace {
30 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
31 TargetMachine &TM;
32 MachineFunction *F; // The function we are compiling into
33 MachineBasicBlock *BB; // The current MBB we are compiling
34
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
36
37 // MBBMap - Mapping between LLVM BB -> Machine BB
38 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
39
40 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
41
42 /// runOnFunction - Top level implementation of instruction selection for
43 /// the entire function.
44 ///
45 bool runOnFunction(Function &Fn);
46
47 virtual const char *getPassName() const {
48 return "SparcV8 Simple Instruction Selection";
49 }
50
51 /// visitBasicBlock - This method is called when we are visiting a new basic
52 /// block. This simply creates a new MachineBasicBlock to emit code into
53 /// and adds it to the current MachineFunction. Subsequent visit* for
54 /// instructions will be invoked for all instructions in the basic block.
55 ///
56 void visitBasicBlock(BasicBlock &LLVM_BB) {
57 BB = MBBMap[&LLVM_BB];
58 }
59
Chris Lattner4be7ca52004-04-07 04:27:16 +000060 void visitBinaryOperator(Instruction &I);
61 void visitShiftInstruction(Instruction &I) { visitBinaryOperator(I); }
Chris Lattner4d0cda42004-04-07 05:04:51 +000062 void visitSetCondInst(Instruction &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000063 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000064 void visitReturnInst(ReturnInst &I);
65 void visitLoadInst(LoadInst &I);
66 void visitStoreInst(StoreInst &I);
Chris Lattner1c809c52004-02-29 00:27:00 +000067
68 void visitInstruction(Instruction &I) {
69 std::cerr << "Unhandled instruction: " << I;
70 abort();
71 }
72
73 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
74 /// function, lowering any calls to unknown intrinsic functions into the
75 /// equivalent LLVM code.
76 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000077 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
78
Brian Gaeke562cb162004-04-07 17:04:09 +000079 void LoadArgumentsToVirtualRegs(Function *F);
80
Brian Gaekebc1d27a2004-03-03 23:03:14 +000081 /// copyConstantToRegister - Output the instructions required to put the
82 /// specified constant into the specified register.
83 ///
84 void copyConstantToRegister(MachineBasicBlock *MBB,
85 MachineBasicBlock::iterator IP,
86 Constant *C, unsigned R);
87
88 /// makeAnotherReg - This method returns the next register number we haven't
89 /// yet used.
90 ///
91 /// Long values are handled somewhat specially. They are always allocated
92 /// as pairs of 32 bit integer values. The register number returned is the
93 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
94 /// of the long value.
95 ///
96 unsigned makeAnotherReg(const Type *Ty) {
97 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
98 "Current target doesn't have SparcV8 reg info??");
99 const SparcV8RegisterInfo *MRI =
100 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
101 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
102 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
103 // Create the lower part
104 F->getSSARegMap()->createVirtualRegister(RC);
105 // Create the upper part.
106 return F->getSSARegMap()->createVirtualRegister(RC)-1;
107 }
108
109 // Add the mapping of regnumber => reg class to MachineFunction
110 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
111 return F->getSSARegMap()->createVirtualRegister(RC);
112 }
113
114 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
115 unsigned getReg(Value *V) {
116 // Just append to the end of the current bb.
117 MachineBasicBlock::iterator It = BB->end();
118 return getReg(V, BB, It);
119 }
120 unsigned getReg(Value *V, MachineBasicBlock *MBB,
121 MachineBasicBlock::iterator IPt) {
122 unsigned &Reg = RegMap[V];
123 if (Reg == 0) {
124 Reg = makeAnotherReg(V->getType());
125 RegMap[V] = Reg;
126 }
127 // If this operand is a constant, emit the code to copy the constant into
128 // the register here...
129 //
130 if (Constant *C = dyn_cast<Constant>(V)) {
131 copyConstantToRegister(MBB, IPt, C, Reg);
132 RegMap.erase(V); // Assign a new name to this constant if ref'd again
133 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
134 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000135 unsigned TmpReg = makeAnotherReg(V->getType());
136 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
137 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
138 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000139 RegMap.erase(V); // Assign a new name to this address if ref'd again
140 }
141
142 return Reg;
143 }
144
Chris Lattner1c809c52004-02-29 00:27:00 +0000145 };
146}
147
148FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
149 return new V8ISel(TM);
150}
151
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000152enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000153 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000154};
155
156static TypeClass getClass (const Type *T) {
157 switch (T->getPrimitiveID ()) {
158 case Type::UByteTyID: case Type::SByteTyID: return cByte;
159 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000160 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000161 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000162 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000163 case Type::FloatTyID: return cFloat;
164 case Type::DoubleTyID: return cDouble;
165 default:
166 assert (0 && "Type of unknown class passed to getClass?");
167 return cByte;
168 }
169}
Chris Lattner0d538bb2004-04-07 04:36:53 +0000170static TypeClass getClassB(const Type *T) {
171 if (T == Type::BoolTy) return cByte;
172 return getClass(T);
173}
174
175
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000176
177/// copyConstantToRegister - Output the instructions required to put the
178/// specified constant into the specified register.
179///
180void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
181 MachineBasicBlock::iterator IP,
182 Constant *C, unsigned R) {
Brian Gaeke775158d2004-03-04 04:37:45 +0000183 if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000184 unsigned Class = getClass(C->getType());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000185 uint64_t Val = CI->getRawValue ();
Brian Gaekee8061732004-03-04 00:56:25 +0000186 switch (Class) {
187 case cByte:
Chris Lattner4be7ca52004-04-07 04:27:16 +0000188 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm((uint8_t)Val);
Brian Gaekee8061732004-03-04 00:56:25 +0000189 return;
190 case cShort: {
191 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000192 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
193 .addImm (((uint16_t) Val) >> 10);
194 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
195 .addImm (((uint16_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000196 return;
197 }
198 case cInt: {
199 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000200 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10);
201 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
202 .addImm (((uint32_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000203 return;
204 }
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000205 case cLong: {
206 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000207 uint32_t topHalf = (uint32_t) (Val >> 32);
208 uint32_t bottomHalf = (uint32_t)Val;
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000209 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000210 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
211 .addImm (topHalf & 0x03ff);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000212 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000213 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
214 .addImm (bottomHalf & 0x03ff);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000215 return;
216 }
Brian Gaekee8061732004-03-04 00:56:25 +0000217 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000218 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000219 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000220 return;
221 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000222 }
223
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000224 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000225 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000226}
Chris Lattner1c809c52004-02-29 00:27:00 +0000227
Brian Gaeke562cb162004-04-07 17:04:09 +0000228void V8ISel::LoadArgumentsToVirtualRegs (Function *F) {
229 unsigned ArgOffset = 0;
230 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
231 V8::I3, V8::I4, V8::I5 };
232 assert (F->asize () < 7
233 && "Can't handle loading excess call args off the stack yet");
234
235 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
236 unsigned Reg = getReg(*I);
237 switch (getClassB(I->getType())) {
238 case cByte:
239 case cShort:
240 case cInt:
241 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
242 .addReg (IncomingArgRegs[ArgOffset]);
243 break;
244 default:
245 assert (0 && "Only <=32-bit, integral arguments currently handled");
246 return;
247 }
248 ++ArgOffset;
249 }
250}
251
Chris Lattner1c809c52004-02-29 00:27:00 +0000252bool V8ISel::runOnFunction(Function &Fn) {
253 // First pass over the function, lower any unknown intrinsic functions
254 // with the IntrinsicLowering class.
255 LowerUnknownIntrinsicFunctionCalls(Fn);
256
257 F = &MachineFunction::construct(&Fn, TM);
258
259 // Create all of the machine basic blocks for the function...
260 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
261 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
262
263 BB = &F->front();
264
265 // Set up a frame object for the return address. This is used by the
266 // llvm.returnaddress & llvm.frameaddress intrinisics.
267 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
268
269 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000270 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000271
272 // Instruction select everything except PHI nodes
273 visit(Fn);
274
275 // Select the PHI nodes
276 //SelectPHINodes();
277
278 RegMap.clear();
279 MBBMap.clear();
280 F = 0;
281 // We always build a machine code representation for the function
282 return true;
283}
284
Brian Gaekef3334eb2004-04-07 17:29:37 +0000285void V8ISel::visitLoadInst(LoadInst &I) {
286 unsigned DestReg = getReg (I);
287 unsigned PtrReg = getReg (I.getOperand (0));
288 switch (getClass (I.getType ())) {
289 case cByte:
290 if (I.getType ()->isSigned ())
291 BuildMI (BB, V8::LDSBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
292 else
293 BuildMI (BB, V8::LDUBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
294 return;
295 case cShort:
296 if (I.getType ()->isSigned ())
297 BuildMI (BB, V8::LDSHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
298 else
299 BuildMI (BB, V8::LDUHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
300 return;
301 case cInt:
302 BuildMI (BB, V8::LDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
303 return;
304 case cLong:
305 BuildMI (BB, V8::LDDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
306 return;
307 default:
308 std::cerr << "Load instruction not handled: " << I;
309 abort ();
310 return;
311 }
312}
313
314void V8ISel::visitStoreInst(StoreInst &I) {
315 unsigned SrcReg = getReg (I.getOperand (0));
316 unsigned PtrReg = getReg (I.getOperand (1));
317 std::cerr << "Store instruction not handled: " << I;
318 abort ();
319}
320
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000321void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeked54c38b2004-04-07 16:41:22 +0000322 assert (I.getNumOperands () < 8
323 && "Can't handle pushing excess call args on the stack yet");
Brian Gaeke562cb162004-04-07 17:04:09 +0000324 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000325 V8::O4, V8::O5 };
326 for (unsigned i = 1; i < 7; ++i)
327 if (i < I.getNumOperands ()) {
328 unsigned ArgReg = getReg (I.getOperand (i));
329 // Schlep it over into the incoming arg register
Brian Gaeke562cb162004-04-07 17:04:09 +0000330 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
Brian Gaeked54c38b2004-04-07 16:41:22 +0000331 .addReg (ArgReg);
332 }
333
Brian Gaekeea8494b2004-04-06 22:09:23 +0000334 unsigned DestReg = getReg (I);
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000335 BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0));
Brian Gaekeea8494b2004-04-06 22:09:23 +0000336 if (I.getType ()->getPrimitiveID () == Type::VoidTyID)
337 return;
338 // Deal w/ return value
339 switch (getClass (I.getType ())) {
340 case cByte:
341 case cShort:
342 case cInt:
343 // Schlep it over into the destination register
344 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
345 break;
346 default:
347 visitInstruction (I);
348 return;
349 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000350}
Chris Lattner1c809c52004-02-29 00:27:00 +0000351
352void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000353 if (I.getNumOperands () == 1) {
354 unsigned RetValReg = getReg (I.getOperand (0));
355 switch (getClass (I.getOperand (0)->getType ())) {
356 case cByte:
357 case cShort:
358 case cInt:
359 // Schlep it over into i0 (where it will become o0 after restore).
360 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
361 break;
362 default:
363 visitInstruction (I);
364 return;
365 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000366 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000367
Brian Gaeke08f64c32004-03-06 05:32:28 +0000368 // Just emit a 'retl' instruction to return.
369 BuildMI(BB, V8::RETL, 0);
370 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000371}
372
Chris Lattner4be7ca52004-04-07 04:27:16 +0000373void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000374 unsigned DestReg = getReg (I);
375 unsigned Op0Reg = getReg (I.getOperand (0));
376 unsigned Op1Reg = getReg (I.getOperand (1));
377
Chris Lattner0d538bb2004-04-07 04:36:53 +0000378 unsigned ResultReg = DestReg;
379 if (getClassB(I.getType()) != cInt)
380 ResultReg = makeAnotherReg (I.getType ());
Chris Lattner22ede702004-04-07 04:06:46 +0000381 unsigned OpCase = ~0;
382
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000383 // FIXME: support long, ulong, fp.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000384 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000385 case Instruction::Add: OpCase = 0; break;
386 case Instruction::Sub: OpCase = 1; break;
387 case Instruction::Mul: OpCase = 2; break;
388 case Instruction::And: OpCase = 3; break;
389 case Instruction::Or: OpCase = 4; break;
390 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +0000391 case Instruction::Shl: OpCase = 6; break;
392 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +0000393
394 case Instruction::Div:
395 case Instruction::Rem: {
396 unsigned Dest = ResultReg;
397 if (I.getOpcode() == Instruction::Rem)
398 Dest = makeAnotherReg(I.getType());
399
400 // FIXME: this is probably only right for 32 bit operands.
401 if (I.getType ()->isSigned()) {
402 unsigned Tmp = makeAnotherReg (I.getType ());
403 // Sign extend into the Y register
404 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
405 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
406 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
407 } else {
408 // Zero extend into the Y register, ie, just set it to zero
409 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
410 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000411 }
Chris Lattner22ede702004-04-07 04:06:46 +0000412
413 if (I.getOpcode() == Instruction::Rem) {
414 unsigned Tmp = makeAnotherReg (I.getType ());
415 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
416 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000417 }
Chris Lattner22ede702004-04-07 04:06:46 +0000418 break;
419 }
420 default:
421 visitInstruction (I);
422 return;
423 }
424
425 if (OpCase != ~0U) {
426 static const unsigned Opcodes[] = {
Chris Lattner4be7ca52004-04-07 04:27:16 +0000427 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
428 V8::SLLrr, V8::SRLrr, V8::SRArr
Chris Lattner22ede702004-04-07 04:06:46 +0000429 };
430 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000431 }
432
433 switch (getClass (I.getType ())) {
434 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000435 if (I.getType ()->isSigned ()) { // add byte
436 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
437 } else { // add ubyte
438 unsigned TmpReg = makeAnotherReg (I.getType ());
439 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
440 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
441 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000442 break;
443 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000444 if (I.getType ()->isSigned ()) { // add short
445 unsigned TmpReg = makeAnotherReg (I.getType ());
446 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
447 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
448 } else { // add ushort
449 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +0000450 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
451 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +0000452 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000453 break;
454 case cInt:
Chris Lattner0d538bb2004-04-07 04:36:53 +0000455 // Nothing todo here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000456 break;
457 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000458 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000459 return;
460 }
461}
462
Chris Lattner4d0cda42004-04-07 05:04:51 +0000463void V8ISel::visitSetCondInst(Instruction &I) {
464 unsigned Op0Reg = getReg (I.getOperand (0));
465 unsigned Op1Reg = getReg (I.getOperand (1));
466 unsigned DestReg = getReg (I);
467
468 // Compare the two values.
469 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
470
471 // Put 0 into a register.
472 //unsigned ZeroReg = makeAnotheRReg(Type::IntTy);
473 //BuildMI(BB, V8::ORri, 2, ZeroReg).addReg(V8::G0).addReg(V8::G0);
474
475 unsigned Opcode;
476 switch (I.getOpcode()) {
477 default: assert(0 && "Unknown setcc instruction!");
478 case Instruction::SetEQ:
479 case Instruction::SetNE:
480 case Instruction::SetLT:
481 case Instruction::SetGT:
482 case Instruction::SetLE:
483 case Instruction::SetGE:
Brian Gaeked54c38b2004-04-07 16:41:22 +0000484 ;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000485 }
486
487 // FIXME: We need either conditional moves like the V9 has (e.g. movge), or we
488 // need to be able to turn a single LLVM basic block into multiple machine
489 // code basic blocks. For now, it probably makes sense to emit Sparc V9
490 // instructions until the code generator is upgraded. Note that this should
491 // only happen when the setcc cannot be folded into the branch, but this needs
492 // to be handled correctly!
493
494 visitInstruction(I);
495}
496
497
Chris Lattner1c809c52004-02-29 00:27:00 +0000498
499/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
500/// function, lowering any calls to unknown intrinsic functions into the
501/// equivalent LLVM code.
502void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
503 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
504 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
505 if (CallInst *CI = dyn_cast<CallInst>(I++))
506 if (Function *F = CI->getCalledFunction())
507 switch (F->getIntrinsicID()) {
508 case Intrinsic::not_intrinsic: break;
509 default:
510 // All other intrinsic calls we must lower.
511 Instruction *Before = CI->getPrev();
512 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
513 if (Before) { // Move iterator to instruction after call
514 I = Before; ++I;
515 } else {
516 I = BB->begin();
517 }
518 }
519}
520
521
522void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
523 unsigned TmpReg1, TmpReg2;
524 switch (ID) {
525 default: assert(0 && "Intrinsic not supported!");
526 }
527}