blob: d75d4d3d4e3c626e0deffd3d11b6f0214c6fd843 [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);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000065 void visitCastInst(CastInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000066 void visitLoadInst(LoadInst &I);
67 void visitStoreInst(StoreInst &I);
Chris Lattner1c809c52004-02-29 00:27:00 +000068
69 void visitInstruction(Instruction &I) {
70 std::cerr << "Unhandled instruction: " << I;
71 abort();
72 }
73
74 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
75 /// function, lowering any calls to unknown intrinsic functions into the
76 /// equivalent LLVM code.
77 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000078 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
79
Brian Gaeke562cb162004-04-07 17:04:09 +000080 void LoadArgumentsToVirtualRegs(Function *F);
81
Brian Gaekebc1d27a2004-03-03 23:03:14 +000082 /// copyConstantToRegister - Output the instructions required to put the
83 /// specified constant into the specified register.
84 ///
85 void copyConstantToRegister(MachineBasicBlock *MBB,
86 MachineBasicBlock::iterator IP,
87 Constant *C, unsigned R);
88
89 /// makeAnotherReg - This method returns the next register number we haven't
90 /// yet used.
91 ///
92 /// Long values are handled somewhat specially. They are always allocated
93 /// as pairs of 32 bit integer values. The register number returned is the
94 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
95 /// of the long value.
96 ///
97 unsigned makeAnotherReg(const Type *Ty) {
98 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
99 "Current target doesn't have SparcV8 reg info??");
100 const SparcV8RegisterInfo *MRI =
101 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
102 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
103 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
104 // Create the lower part
105 F->getSSARegMap()->createVirtualRegister(RC);
106 // Create the upper part.
107 return F->getSSARegMap()->createVirtualRegister(RC)-1;
108 }
109
110 // Add the mapping of regnumber => reg class to MachineFunction
111 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
112 return F->getSSARegMap()->createVirtualRegister(RC);
113 }
114
115 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
116 unsigned getReg(Value *V) {
117 // Just append to the end of the current bb.
118 MachineBasicBlock::iterator It = BB->end();
119 return getReg(V, BB, It);
120 }
121 unsigned getReg(Value *V, MachineBasicBlock *MBB,
122 MachineBasicBlock::iterator IPt) {
123 unsigned &Reg = RegMap[V];
124 if (Reg == 0) {
125 Reg = makeAnotherReg(V->getType());
126 RegMap[V] = Reg;
127 }
128 // If this operand is a constant, emit the code to copy the constant into
129 // the register here...
130 //
131 if (Constant *C = dyn_cast<Constant>(V)) {
132 copyConstantToRegister(MBB, IPt, C, Reg);
133 RegMap.erase(V); // Assign a new name to this constant if ref'd again
134 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
135 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000136 unsigned TmpReg = makeAnotherReg(V->getType());
137 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
138 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
139 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000140 RegMap.erase(V); // Assign a new name to this address if ref'd again
141 }
142
143 return Reg;
144 }
145
Chris Lattner1c809c52004-02-29 00:27:00 +0000146 };
147}
148
149FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
150 return new V8ISel(TM);
151}
152
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000153enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000154 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000155};
156
157static TypeClass getClass (const Type *T) {
158 switch (T->getPrimitiveID ()) {
159 case Type::UByteTyID: case Type::SByteTyID: return cByte;
160 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000161 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000162 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000163 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000164 case Type::FloatTyID: return cFloat;
165 case Type::DoubleTyID: return cDouble;
166 default:
167 assert (0 && "Type of unknown class passed to getClass?");
168 return cByte;
169 }
170}
Chris Lattner0d538bb2004-04-07 04:36:53 +0000171static TypeClass getClassB(const Type *T) {
172 if (T == Type::BoolTy) return cByte;
173 return getClass(T);
174}
175
176
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000177
178/// copyConstantToRegister - Output the instructions required to put the
179/// specified constant into the specified register.
180///
181void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
182 MachineBasicBlock::iterator IP,
183 Constant *C, unsigned R) {
Brian Gaeke775158d2004-03-04 04:37:45 +0000184 if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000185 unsigned Class = getClass(C->getType());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000186 uint64_t Val = CI->getRawValue ();
Brian Gaekee8061732004-03-04 00:56:25 +0000187 switch (Class) {
188 case cByte:
Chris Lattner4be7ca52004-04-07 04:27:16 +0000189 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm((uint8_t)Val);
Brian Gaekee8061732004-03-04 00:56:25 +0000190 return;
191 case cShort: {
192 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000193 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
194 .addImm (((uint16_t) Val) >> 10);
195 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
196 .addImm (((uint16_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000197 return;
198 }
199 case cInt: {
200 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner4be7ca52004-04-07 04:27:16 +0000201 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10);
202 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
203 .addImm (((uint32_t) Val) & 0x03ff);
Brian Gaekee8061732004-03-04 00:56:25 +0000204 return;
205 }
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000206 case cLong: {
207 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000208 uint32_t topHalf = (uint32_t) (Val >> 32);
209 uint32_t bottomHalf = (uint32_t)Val;
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000210 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000211 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
212 .addImm (topHalf & 0x03ff);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000213 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10);
Chris Lattner4be7ca52004-04-07 04:27:16 +0000214 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
215 .addImm (bottomHalf & 0x03ff);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000216 return;
217 }
Brian Gaekee8061732004-03-04 00:56:25 +0000218 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000219 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000220 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000221 return;
222 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000223 }
224
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000225 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000226 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000227}
Chris Lattner1c809c52004-02-29 00:27:00 +0000228
Brian Gaeke562cb162004-04-07 17:04:09 +0000229void V8ISel::LoadArgumentsToVirtualRegs (Function *F) {
230 unsigned ArgOffset = 0;
231 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
232 V8::I3, V8::I4, V8::I5 };
233 assert (F->asize () < 7
234 && "Can't handle loading excess call args off the stack yet");
235
236 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
237 unsigned Reg = getReg(*I);
238 switch (getClassB(I->getType())) {
239 case cByte:
240 case cShort:
241 case cInt:
242 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
243 .addReg (IncomingArgRegs[ArgOffset]);
244 break;
245 default:
246 assert (0 && "Only <=32-bit, integral arguments currently handled");
247 return;
248 }
249 ++ArgOffset;
250 }
251}
252
Chris Lattner1c809c52004-02-29 00:27:00 +0000253bool V8ISel::runOnFunction(Function &Fn) {
254 // First pass over the function, lower any unknown intrinsic functions
255 // with the IntrinsicLowering class.
256 LowerUnknownIntrinsicFunctionCalls(Fn);
257
258 F = &MachineFunction::construct(&Fn, TM);
259
260 // Create all of the machine basic blocks for the function...
261 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
262 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
263
264 BB = &F->front();
265
266 // Set up a frame object for the return address. This is used by the
267 // llvm.returnaddress & llvm.frameaddress intrinisics.
268 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
269
270 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000271 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000272
273 // Instruction select everything except PHI nodes
274 visit(Fn);
275
276 // Select the PHI nodes
277 //SelectPHINodes();
278
279 RegMap.clear();
280 MBBMap.clear();
281 F = 0;
282 // We always build a machine code representation for the function
283 return true;
284}
285
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000286void V8ISel::visitCastInst(CastInst &I) {
287 unsigned SrcReg = getReg (I.getOperand (0));
288 unsigned DestReg = getReg (I.getOperand (0));
289 const Type *oldTy = I.getOperand (0)->getType ();
290 const Type *newTy = I.getType ();
291
292 std::cerr << "Cast instruction not supported: " << I;
293 abort ();
294}
295
Brian Gaekef3334eb2004-04-07 17:29:37 +0000296void V8ISel::visitLoadInst(LoadInst &I) {
297 unsigned DestReg = getReg (I);
298 unsigned PtrReg = getReg (I.getOperand (0));
299 switch (getClass (I.getType ())) {
300 case cByte:
301 if (I.getType ()->isSigned ())
302 BuildMI (BB, V8::LDSBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
303 else
304 BuildMI (BB, V8::LDUBmr, 1, DestReg).addReg (PtrReg).addSImm(0);
305 return;
306 case cShort:
307 if (I.getType ()->isSigned ())
308 BuildMI (BB, V8::LDSHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
309 else
310 BuildMI (BB, V8::LDUHmr, 1, DestReg).addReg (PtrReg).addSImm(0);
311 return;
312 case cInt:
313 BuildMI (BB, V8::LDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
314 return;
315 case cLong:
316 BuildMI (BB, V8::LDDmr, 1, DestReg).addReg (PtrReg).addSImm(0);
317 return;
318 default:
319 std::cerr << "Load instruction not handled: " << I;
320 abort ();
321 return;
322 }
323}
324
325void V8ISel::visitStoreInst(StoreInst &I) {
326 unsigned SrcReg = getReg (I.getOperand (0));
327 unsigned PtrReg = getReg (I.getOperand (1));
328 std::cerr << "Store instruction not handled: " << I;
329 abort ();
330}
331
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000332void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeked54c38b2004-04-07 16:41:22 +0000333 assert (I.getNumOperands () < 8
334 && "Can't handle pushing excess call args on the stack yet");
Brian Gaeke562cb162004-04-07 17:04:09 +0000335 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000336 V8::O4, V8::O5 };
337 for (unsigned i = 1; i < 7; ++i)
338 if (i < I.getNumOperands ()) {
339 unsigned ArgReg = getReg (I.getOperand (i));
340 // Schlep it over into the incoming arg register
Brian Gaeke562cb162004-04-07 17:04:09 +0000341 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
Brian Gaeked54c38b2004-04-07 16:41:22 +0000342 .addReg (ArgReg);
343 }
344
Brian Gaekeea8494b2004-04-06 22:09:23 +0000345 unsigned DestReg = getReg (I);
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000346 BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0));
Brian Gaekeea8494b2004-04-06 22:09:23 +0000347 if (I.getType ()->getPrimitiveID () == Type::VoidTyID)
348 return;
349 // Deal w/ return value
350 switch (getClass (I.getType ())) {
351 case cByte:
352 case cShort:
353 case cInt:
354 // Schlep it over into the destination register
355 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
356 break;
357 default:
358 visitInstruction (I);
359 return;
360 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000361}
Chris Lattner1c809c52004-02-29 00:27:00 +0000362
363void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000364 if (I.getNumOperands () == 1) {
365 unsigned RetValReg = getReg (I.getOperand (0));
366 switch (getClass (I.getOperand (0)->getType ())) {
367 case cByte:
368 case cShort:
369 case cInt:
370 // Schlep it over into i0 (where it will become o0 after restore).
371 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
372 break;
373 default:
374 visitInstruction (I);
375 return;
376 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000377 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000378
Brian Gaeke08f64c32004-03-06 05:32:28 +0000379 // Just emit a 'retl' instruction to return.
380 BuildMI(BB, V8::RETL, 0);
381 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000382}
383
Chris Lattner4be7ca52004-04-07 04:27:16 +0000384void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000385 unsigned DestReg = getReg (I);
386 unsigned Op0Reg = getReg (I.getOperand (0));
387 unsigned Op1Reg = getReg (I.getOperand (1));
388
Chris Lattner0d538bb2004-04-07 04:36:53 +0000389 unsigned ResultReg = DestReg;
390 if (getClassB(I.getType()) != cInt)
391 ResultReg = makeAnotherReg (I.getType ());
Chris Lattner22ede702004-04-07 04:06:46 +0000392 unsigned OpCase = ~0;
393
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000394 // FIXME: support long, ulong, fp.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000395 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000396 case Instruction::Add: OpCase = 0; break;
397 case Instruction::Sub: OpCase = 1; break;
398 case Instruction::Mul: OpCase = 2; break;
399 case Instruction::And: OpCase = 3; break;
400 case Instruction::Or: OpCase = 4; break;
401 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +0000402 case Instruction::Shl: OpCase = 6; break;
403 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +0000404
405 case Instruction::Div:
406 case Instruction::Rem: {
407 unsigned Dest = ResultReg;
408 if (I.getOpcode() == Instruction::Rem)
409 Dest = makeAnotherReg(I.getType());
410
411 // FIXME: this is probably only right for 32 bit operands.
412 if (I.getType ()->isSigned()) {
413 unsigned Tmp = makeAnotherReg (I.getType ());
414 // Sign extend into the Y register
415 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
416 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
417 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
418 } else {
419 // Zero extend into the Y register, ie, just set it to zero
420 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
421 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000422 }
Chris Lattner22ede702004-04-07 04:06:46 +0000423
424 if (I.getOpcode() == Instruction::Rem) {
425 unsigned Tmp = makeAnotherReg (I.getType ());
426 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
427 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000428 }
Chris Lattner22ede702004-04-07 04:06:46 +0000429 break;
430 }
431 default:
432 visitInstruction (I);
433 return;
434 }
435
436 if (OpCase != ~0U) {
437 static const unsigned Opcodes[] = {
Chris Lattner4be7ca52004-04-07 04:27:16 +0000438 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
439 V8::SLLrr, V8::SRLrr, V8::SRArr
Chris Lattner22ede702004-04-07 04:06:46 +0000440 };
441 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000442 }
443
444 switch (getClass (I.getType ())) {
445 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000446 if (I.getType ()->isSigned ()) { // add byte
447 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
448 } else { // add ubyte
449 unsigned TmpReg = makeAnotherReg (I.getType ());
450 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
451 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
452 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000453 break;
454 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000455 if (I.getType ()->isSigned ()) { // add short
456 unsigned TmpReg = makeAnotherReg (I.getType ());
457 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
458 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
459 } else { // add ushort
460 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +0000461 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
462 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +0000463 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000464 break;
465 case cInt:
Chris Lattner0d538bb2004-04-07 04:36:53 +0000466 // Nothing todo here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000467 break;
468 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000469 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000470 return;
471 }
472}
473
Chris Lattner4d0cda42004-04-07 05:04:51 +0000474void V8ISel::visitSetCondInst(Instruction &I) {
475 unsigned Op0Reg = getReg (I.getOperand (0));
476 unsigned Op1Reg = getReg (I.getOperand (1));
477 unsigned DestReg = getReg (I);
478
479 // Compare the two values.
480 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
481
482 // Put 0 into a register.
483 //unsigned ZeroReg = makeAnotheRReg(Type::IntTy);
484 //BuildMI(BB, V8::ORri, 2, ZeroReg).addReg(V8::G0).addReg(V8::G0);
485
486 unsigned Opcode;
487 switch (I.getOpcode()) {
488 default: assert(0 && "Unknown setcc instruction!");
489 case Instruction::SetEQ:
490 case Instruction::SetNE:
491 case Instruction::SetLT:
492 case Instruction::SetGT:
493 case Instruction::SetLE:
494 case Instruction::SetGE:
Brian Gaeked54c38b2004-04-07 16:41:22 +0000495 ;
Chris Lattner4d0cda42004-04-07 05:04:51 +0000496 }
497
498 // FIXME: We need either conditional moves like the V9 has (e.g. movge), or we
499 // need to be able to turn a single LLVM basic block into multiple machine
500 // code basic blocks. For now, it probably makes sense to emit Sparc V9
501 // instructions until the code generator is upgraded. Note that this should
502 // only happen when the setcc cannot be folded into the branch, but this needs
503 // to be handled correctly!
504
505 visitInstruction(I);
506}
507
508
Chris Lattner1c809c52004-02-29 00:27:00 +0000509
510/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
511/// function, lowering any calls to unknown intrinsic functions into the
512/// equivalent LLVM code.
513void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
514 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
515 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
516 if (CallInst *CI = dyn_cast<CallInst>(I++))
517 if (Function *F = CI->getCalledFunction())
518 switch (F->getIntrinsicID()) {
519 case Intrinsic::not_intrinsic: break;
520 default:
521 // All other intrinsic calls we must lower.
522 Instruction *Before = CI->getPrev();
523 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
524 if (Before) { // Move iterator to instruction after call
525 I = Before; ++I;
526 } else {
527 I = BB->begin();
528 }
529 }
530}
531
532
533void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
534 unsigned TmpReg1, TmpReg2;
535 switch (ID) {
536 default: assert(0 && "Intrinsic not supported!");
537 }
538}