blob: bfb0c3e73658365e11e16745fbc7901c38822397 [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"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000016#include "llvm/Support/Debug.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000017#include "llvm/Instructions.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000018#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000019#include "llvm/Constants.h"
Chris Lattner30483732004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaeke9df92822004-06-15 19:16:07 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Brian Gaekec93a7522004-06-18 05:19:16 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000024#include "llvm/CodeGen/MachineFunction.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000025#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/InstVisitor.h"
29#include "llvm/Support/CFG.h"
30using namespace llvm;
31
32namespace {
33 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
34 TargetMachine &TM;
35 MachineFunction *F; // The function we are compiling into
36 MachineBasicBlock *BB; // The current MBB we are compiling
37
38 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
39
40 // MBBMap - Mapping between LLVM BB -> Machine BB
41 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
42
43 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
44
45 /// runOnFunction - Top level implementation of instruction selection for
46 /// the entire function.
47 ///
48 bool runOnFunction(Function &Fn);
49
50 virtual const char *getPassName() const {
51 return "SparcV8 Simple Instruction Selection";
52 }
53
Brian Gaeke532e60c2004-05-08 04:21:17 +000054 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
55 /// constant expression GEP support.
56 ///
57 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
58 Value *Src, User::op_iterator IdxBegin,
59 User::op_iterator IdxEnd, unsigned TargetReg);
60
Brian Gaeke00e514e2004-06-24 06:33:00 +000061 /// emitCastOperation - Common code shared between visitCastInst and
62 /// constant expression cast support.
63 ///
64 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
65 Value *Src, const Type *DestTy, unsigned TargetReg);
66
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +000067 /// emitIntegerCast, emitFPToIntegerCast - Helper methods for
68 /// emitCastOperation.
69 ///
Brian Gaekea54df252004-11-19 18:48:10 +000070 unsigned emitIntegerCast (MachineBasicBlock *BB,
71 MachineBasicBlock::iterator IP,
72 const Type *oldTy, unsigned SrcReg,
73 const Type *newTy, unsigned DestReg);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +000074 void emitFPToIntegerCast (MachineBasicBlock *BB,
75 MachineBasicBlock::iterator IP, const Type *oldTy,
76 unsigned SrcReg, const Type *newTy,
77 unsigned DestReg);
78
Chris Lattner1c809c52004-02-29 00:27:00 +000079 /// visitBasicBlock - This method is called when we are visiting a new basic
80 /// block. This simply creates a new MachineBasicBlock to emit code into
81 /// and adds it to the current MachineFunction. Subsequent visit* for
82 /// instructions will be invoked for all instructions in the basic block.
83 ///
84 void visitBasicBlock(BasicBlock &LLVM_BB) {
85 BB = MBBMap[&LLVM_BB];
86 }
87
Chris Lattner4be7ca52004-04-07 04:27:16 +000088 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000089 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000090 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000091 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000092 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000093 void visitBranchInst(BranchInst &I);
Chris Lattnerd14d5b42004-10-17 02:42:42 +000094 void visitUnreachableInst(UnreachableInst &I) {}
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000095 void visitCastInst(CastInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000096 void visitLoadInst(LoadInst &I);
97 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000098 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
99 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +0000100 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000101
Chris Lattner1c809c52004-02-29 00:27:00 +0000102 void visitInstruction(Instruction &I) {
103 std::cerr << "Unhandled instruction: " << I;
104 abort();
105 }
106
107 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
108 /// function, lowering any calls to unknown intrinsic functions into the
109 /// equivalent LLVM code.
110 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +0000111 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
112
Brian Gaeke562cb162004-04-07 17:04:09 +0000113 void LoadArgumentsToVirtualRegs(Function *F);
114
Brian Gaeke6c868a42004-06-17 22:34:08 +0000115 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
116 /// because we have to generate our sources into the source basic blocks,
117 /// not the current one.
118 ///
119 void SelectPHINodes();
120
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000121 /// copyConstantToRegister - Output the instructions required to put the
122 /// specified constant into the specified register.
123 ///
124 void copyConstantToRegister(MachineBasicBlock *MBB,
125 MachineBasicBlock::iterator IP,
126 Constant *C, unsigned R);
127
128 /// makeAnotherReg - This method returns the next register number we haven't
129 /// yet used.
130 ///
131 /// Long values are handled somewhat specially. They are always allocated
132 /// as pairs of 32 bit integer values. The register number returned is the
133 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
134 /// of the long value.
135 ///
136 unsigned makeAnotherReg(const Type *Ty) {
137 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
138 "Current target doesn't have SparcV8 reg info??");
139 const SparcV8RegisterInfo *MRI =
140 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
141 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
142 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
143 // Create the lower part
144 F->getSSARegMap()->createVirtualRegister(RC);
145 // Create the upper part.
146 return F->getSSARegMap()->createVirtualRegister(RC)-1;
147 }
148
149 // Add the mapping of regnumber => reg class to MachineFunction
150 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
151 return F->getSSARegMap()->createVirtualRegister(RC);
152 }
153
154 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
155 unsigned getReg(Value *V) {
156 // Just append to the end of the current bb.
157 MachineBasicBlock::iterator It = BB->end();
158 return getReg(V, BB, It);
159 }
160 unsigned getReg(Value *V, MachineBasicBlock *MBB,
161 MachineBasicBlock::iterator IPt) {
162 unsigned &Reg = RegMap[V];
163 if (Reg == 0) {
164 Reg = makeAnotherReg(V->getType());
165 RegMap[V] = Reg;
166 }
167 // If this operand is a constant, emit the code to copy the constant into
168 // the register here...
169 //
170 if (Constant *C = dyn_cast<Constant>(V)) {
171 copyConstantToRegister(MBB, IPt, C, Reg);
172 RegMap.erase(V); // Assign a new name to this constant if ref'd again
173 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
174 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000175 unsigned TmpReg = makeAnotherReg(V->getType());
176 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
177 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
178 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000179 RegMap.erase(V); // Assign a new name to this address if ref'd again
180 }
181
182 return Reg;
183 }
184
Chris Lattner1c809c52004-02-29 00:27:00 +0000185 };
186}
187
188FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
189 return new V8ISel(TM);
190}
191
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000192enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000193 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000194};
195
196static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000197 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000198 case Type::UByteTyID: case Type::SByteTyID: return cByte;
199 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000200 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000201 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000202 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000203 case Type::FloatTyID: return cFloat;
204 case Type::DoubleTyID: return cDouble;
205 default:
206 assert (0 && "Type of unknown class passed to getClass?");
207 return cByte;
208 }
209}
Brian Gaeke50094ed2004-10-10 19:57:18 +0000210
Chris Lattner0d538bb2004-04-07 04:36:53 +0000211static TypeClass getClassB(const Type *T) {
212 if (T == Type::BoolTy) return cByte;
213 return getClass(T);
214}
215
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000216/// copyConstantToRegister - Output the instructions required to put the
217/// specified constant into the specified register.
218///
219void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
220 MachineBasicBlock::iterator IP,
221 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000222 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
223 switch (CE->getOpcode()) {
224 case Instruction::GetElementPtr:
225 emitGEPOperation(MBB, IP, CE->getOperand(0),
226 CE->op_begin()+1, CE->op_end(), R);
227 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000228 case Instruction::Cast:
229 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
230 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000231 default:
232 std::cerr << "Copying this constant expr not yet handled: " << *CE;
233 abort();
234 }
Chris Lattnerd14d5b42004-10-17 02:42:42 +0000235 } else if (isa<UndefValue>(C)) {
236 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R);
237 if (getClassB (C->getType ()) == cLong)
238 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1);
239 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000240 }
241
Brian Gaekee302a7e2004-05-07 21:39:30 +0000242 if (C->getType()->isIntegral ()) {
243 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000244 unsigned Class = getClassB (C->getType ());
245 if (Class == cLong) {
246 unsigned TmpReg = makeAnotherReg (Type::IntTy);
247 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
248 // Copy the value into the register pair.
249 // R = top(more-significant) half, R+1 = bottom(less-significant) half
250 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000251 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
252 Val >> 32), R);
253 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
254 Val & 0xffffffffU), R+1);
Brian Gaeke9df92822004-06-15 19:16:07 +0000255 return;
256 }
257
258 assert(Class <= cInt && "Type not handled yet!");
259
Brian Gaekee302a7e2004-05-07 21:39:30 +0000260 if (C->getType() == Type::BoolTy) {
261 Val = (C == ConstantBool::True);
262 } else {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000263 ConstantInt *CI = cast<ConstantInt> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000264 Val = CI->getRawValue ();
265 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000266 switch (Class) {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000267 case cByte: Val = (int8_t) Val; break;
268 case cShort: Val = (int16_t) Val; break;
269 case cInt: Val = (int32_t) Val; break;
Brian Gaekee8061732004-03-04 00:56:25 +0000270 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000271 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000272 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000273 return;
274 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000275 if (Val == 0) {
276 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
277 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
278 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
279 } else {
280 unsigned TmpReg = makeAnotherReg (C->getType ());
281 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
282 .addSImm (((uint32_t) Val) >> 10);
283 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
284 .addSImm (((uint32_t) Val) & 0x03ff);
285 return;
286 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000287 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
288 // We need to spill the constant to memory...
289 MachineConstantPool *CP = F->getConstantPool();
290 unsigned CPI = CP->getConstantPoolIndex(CFP);
291 const Type *Ty = CFP->getType();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000292 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
293 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +0000294
295 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000296 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000297 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000298 BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg)
299 .addConstantPoolIndex (CPI);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000300 BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000301 } else if (isa<ConstantPointerNull>(C)) {
302 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000303 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Chris Lattner73302482004-07-18 07:26:17 +0000304 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000305 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
306 // that SETHI %reg,global == SETHI %reg,%hi(global) and
307 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
308 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner73302482004-07-18 07:26:17 +0000309 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
310 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
Brian Gaeke9df92822004-06-15 19:16:07 +0000311 } else {
312 std::cerr << "Offending constant: " << *C << "\n";
313 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000314 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000315}
Chris Lattner1c809c52004-02-29 00:27:00 +0000316
Brian Gaeke812c4882004-07-16 10:31:25 +0000317void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
Brian Gaeke562cb162004-04-07 17:04:09 +0000318 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
319 V8::I3, V8::I4, V8::I5 };
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000320
Brian Gaeke812c4882004-07-16 10:31:25 +0000321 // Add IMPLICIT_DEFs of input regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000322 unsigned ArgNo = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000323 for (Function::aiterator I = LF->abegin(), E = LF->aend();
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000324 I != E && ArgNo < 6; ++I, ++ArgNo) {
Brian Gaeke812c4882004-07-16 10:31:25 +0000325 switch (getClassB(I->getType())) {
326 case cByte:
327 case cShort:
328 case cInt:
329 case cFloat:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000330 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke812c4882004-07-16 10:31:25 +0000331 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000332 case cDouble:
333 case cLong:
334 // Double and Long use register pairs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000335 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
336 ++ArgNo;
337 if (ArgNo < 6)
338 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000339 break;
Brian Gaeke812c4882004-07-16 10:31:25 +0000340 default:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000341 assert (0 && "type not handled");
Brian Gaeke812c4882004-07-16 10:31:25 +0000342 return;
343 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000344 }
345
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000346 const unsigned *IAREnd = &IncomingArgRegs[6];
347 const unsigned *IAR = &IncomingArgRegs[0];
348 unsigned ArgOffset = 68;
Brian Gaeke4e459c42004-11-19 20:31:08 +0000349
350 // Store registers onto stack if this is a varargs function.
351 // FIXME: This doesn't really pertain to "loading arguments into
352 // virtual registers", so it's not clear that it really belongs here.
353 // FIXME: We could avoid storing any args onto the stack that don't
354 // need to be in memory, because they come before the ellipsis in the
355 // parameter list (and thus could never be accessed through va_arg).
356 if (LF->getFunctionType ()->isVarArg ()) {
357 for (unsigned i = 0; i < 6; ++i) {
358 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
359 assert (IAR != IAREnd
360 && "About to dereference past end of IncomingArgRegs");
361 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
362 ArgOffset += 4;
363 }
364 // Reset the pointers now that we're done.
365 ArgOffset = 68;
366 IAR = &IncomingArgRegs[0];
367 }
368
369 // Copy args out of their incoming hard regs or stack slots into virtual regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000370 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; ++I) {
371 Argument &A = *I;
372 unsigned ArgReg = getReg (A);
373 if (getClassB (A.getType ()) < cLong) {
374 // Get it out of the incoming arg register
375 if (ArgOffset < 92) {
376 assert (IAR != IAREnd
377 && "About to dereference past end of IncomingArgRegs");
378 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
379 } else {
380 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
381 BuildMI (BB, V8::LD, 3, ArgReg).addFrameIndex (FI).addSImm (0);
382 }
383 ArgOffset += 4;
384 } else if (getClassB (A.getType ()) == cFloat) {
385 if (ArgOffset < 92) {
Brian Gaeke1df468e2004-09-29 03:34:41 +0000386 // Single-fp args are passed in integer registers; go through
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000387 // memory to get them out of integer registers and back into fp. (Bleh!)
Brian Gaeke1df468e2004-09-29 03:34:41 +0000388 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
389 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000390 assert (IAR != IAREnd
391 && "About to dereference past end of IncomingArgRegs");
392 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
393 BuildMI (BB, V8::LDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
394 } else {
395 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
396 BuildMI (BB, V8::LDFri, 3, ArgReg).addFrameIndex (FI).addSImm (0);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000397 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000398 ArgOffset += 4;
399 } else if (getClassB (A.getType ()) == cDouble) {
400 // Double-fp args are passed in pairs of integer registers; go through
401 // memory to get them out of integer registers and back into fp. (Bleh!)
402 // We'd like to 'ldd' these right out of the incoming-args area,
403 // but it might not be 8-byte aligned (e.g., call x(int x, double d)).
404 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
405 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
406 if (ArgOffset < 92 && IAR != IAREnd) {
407 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
408 } else {
409 unsigned TempReg = makeAnotherReg (Type::IntTy);
410 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
411 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
Brian Gaeke6672f862004-09-30 19:44:32 +0000412 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000413 ArgOffset += 4;
414 if (ArgOffset < 92 && IAR != IAREnd) {
415 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (*IAR++);
416 } else {
417 unsigned TempReg = makeAnotherReg (Type::IntTy);
418 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
419 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000420 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000421 ArgOffset += 4;
422 BuildMI (BB, V8::LDDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
423 } else if (getClassB (A.getType ()) == cLong) {
424 // do the first half...
425 if (ArgOffset < 92) {
426 assert (IAR != IAREnd
427 && "About to dereference past end of IncomingArgRegs");
428 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
429 } else {
430 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
431 BuildMI (BB, V8::LD, 2, ArgReg).addFrameIndex (FI).addSImm (0);
432 }
433 ArgOffset += 4;
434 // ...then do the second half
435 if (ArgOffset < 92) {
436 assert (IAR != IAREnd
437 && "About to dereference past end of IncomingArgRegs");
438 BuildMI (BB, V8::ORrr, 2, ArgReg+1).addReg (V8::G0).addReg (*IAR++);
439 } else {
440 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
441 BuildMI (BB, V8::LD, 2, ArgReg+1).addFrameIndex (FI).addSImm (0);
442 }
443 ArgOffset += 4;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000444 } else {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000445 assert (0 && "Unknown class?!");
Brian Gaeke812c4882004-07-16 10:31:25 +0000446 }
Brian Gaeke562cb162004-04-07 17:04:09 +0000447 }
448}
449
Brian Gaeke6c868a42004-06-17 22:34:08 +0000450void V8ISel::SelectPHINodes() {
451 const TargetInstrInfo &TII = *TM.getInstrInfo();
452 const Function &LF = *F->getFunction(); // The LLVM function...
453 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
454 const BasicBlock *BB = I;
455 MachineBasicBlock &MBB = *MBBMap[I];
456
457 // Loop over all of the PHI nodes in the LLVM basic block...
458 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
459 for (BasicBlock::const_iterator I = BB->begin();
460 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
461
462 // Create a new machine instr PHI node, and insert it.
463 unsigned PHIReg = getReg(*PN);
464 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
465 V8::PHI, PN->getNumOperands(), PHIReg);
466
467 MachineInstr *LongPhiMI = 0;
468 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
469 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
470 V8::PHI, PN->getNumOperands(), PHIReg+1);
471
472 // PHIValues - Map of blocks to incoming virtual registers. We use this
473 // so that we only initialize one incoming value for a particular block,
474 // even if the block has multiple entries in the PHI node.
475 //
476 std::map<MachineBasicBlock*, unsigned> PHIValues;
477
478 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
479 MachineBasicBlock *PredMBB = 0;
480 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
481 PE = MBB.pred_end (); PI != PE; ++PI)
482 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
483 PredMBB = *PI;
484 break;
485 }
486 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
487
488 unsigned ValReg;
489 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
490 PHIValues.lower_bound(PredMBB);
491
492 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
493 // We already inserted an initialization of the register for this
494 // predecessor. Recycle it.
495 ValReg = EntryIt->second;
496
497 } else {
498 // Get the incoming value into a virtual register.
499 //
500 Value *Val = PN->getIncomingValue(i);
501
502 // If this is a constant or GlobalValue, we may have to insert code
503 // into the basic block to compute it into a virtual register.
504 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
505 isa<GlobalValue>(Val)) {
506 // Simple constants get emitted at the end of the basic block,
507 // before any terminator instructions. We "know" that the code to
508 // move a constant into a register will never clobber any flags.
509 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
510 } else {
511 // Because we don't want to clobber any values which might be in
512 // physical registers with the computation of this constant (which
513 // might be arbitrarily complex if it is a constant expression),
514 // just insert the computation at the top of the basic block.
515 MachineBasicBlock::iterator PI = PredMBB->begin();
516
517 // Skip over any PHI nodes though!
518 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
519 ++PI;
520
521 ValReg = getReg(Val, PredMBB, PI);
522 }
523
524 // Remember that we inserted a value for this PHI for this predecessor
525 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
526 }
527
528 PhiMI->addRegOperand(ValReg);
529 PhiMI->addMachineBasicBlockOperand(PredMBB);
530 if (LongPhiMI) {
531 LongPhiMI->addRegOperand(ValReg+1);
532 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
533 }
534 }
535
536 // Now that we emitted all of the incoming values for the PHI node, make
537 // sure to reposition the InsertPoint after the PHI that we just added.
538 // This is needed because we might have inserted a constant into this
539 // block, right after the PHI's which is before the old insert point!
540 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
541 ++PHIInsertPoint;
542 }
543 }
544}
545
Chris Lattner1c809c52004-02-29 00:27:00 +0000546bool V8ISel::runOnFunction(Function &Fn) {
547 // First pass over the function, lower any unknown intrinsic functions
548 // with the IntrinsicLowering class.
549 LowerUnknownIntrinsicFunctionCalls(Fn);
550
551 F = &MachineFunction::construct(&Fn, TM);
552
553 // Create all of the machine basic blocks for the function...
554 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
555 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
556
557 BB = &F->front();
558
559 // Set up a frame object for the return address. This is used by the
560 // llvm.returnaddress & llvm.frameaddress intrinisics.
561 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
562
563 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000564 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000565
566 // Instruction select everything except PHI nodes
567 visit(Fn);
568
569 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000570 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000571
572 RegMap.clear();
573 MBBMap.clear();
574 F = 0;
575 // We always build a machine code representation for the function
576 return true;
577}
578
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000579void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000580 Value *Op = I.getOperand(0);
581 unsigned DestReg = getReg(I);
582 MachineBasicBlock::iterator MI = BB->end();
583 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
584}
585
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000586
Brian Gaekea54df252004-11-19 18:48:10 +0000587unsigned V8ISel::emitIntegerCast (MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000588 MachineBasicBlock::iterator IP, const Type *oldTy,
589 unsigned SrcReg, const Type *newTy,
590 unsigned DestReg) {
591 if (oldTy == newTy) {
592 // No-op cast - just emit a copy; assume the reg. allocator will zap it.
593 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg);
Brian Gaekea54df252004-11-19 18:48:10 +0000594 return SrcReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000595 }
596 // Emit left-shift, then right-shift to sign- or zero-extend.
597 unsigned TmpReg = makeAnotherReg (newTy);
598 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
599 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg);
600 if (newTy->isSigned ()) { // sign-extend with SRA
601 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
602 } else { // zero-extend with SRL
603 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
604 }
Brian Gaekea54df252004-11-19 18:48:10 +0000605 // Return the temp reg. in case this is one half of a cast to long.
606 return TmpReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000607}
608
609void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB,
610 MachineBasicBlock::iterator IP,
611 const Type *oldTy, unsigned SrcReg,
612 const Type *newTy, unsigned DestReg) {
613 unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign;
614 unsigned oldTyClass = getClassB(oldTy);
615 if (oldTyClass == cFloat) {
616 FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4;
617 FPAlign = TM.getTargetData().getFloatAlignment();
618 } else { // it's a double
619 FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8;
620 FPAlign = TM.getTargetData().getDoubleAlignment();
621 }
622 unsigned TempReg = makeAnotherReg (oldTy);
623 BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg);
624 int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign);
625 BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0)
626 .addReg (TempReg);
627 unsigned TempReg2 = makeAnotherReg (newTy);
628 BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0);
629 emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg);
630}
631
Brian Gaeke00e514e2004-06-24 06:33:00 +0000632/// emitCastOperation - Common code shared between visitCastInst and constant
633/// expression cast support.
634///
635void V8ISel::emitCastOperation(MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000636 MachineBasicBlock::iterator IP, Value *Src,
637 const Type *DestTy, unsigned DestReg) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000638 const Type *SrcTy = Src->getType();
639 unsigned SrcClass = getClassB(SrcTy);
640 unsigned DestClass = getClassB(DestTy);
641 unsigned SrcReg = getReg(Src, BB, IP);
642
643 const Type *oldTy = SrcTy;
644 const Type *newTy = DestTy;
645 unsigned oldTyClass = SrcClass;
646 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000647
Brian Gaeke429022b2004-05-08 06:36:14 +0000648 if (oldTyClass < cLong && newTyClass < cLong) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000649 emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
650 } else switch (newTyClass) {
651 case cByte:
652 case cShort:
653 case cInt:
Brian Gaeke495a0972004-06-24 21:22:08 +0000654 switch (oldTyClass) {
Brian Gaekea54df252004-11-19 18:48:10 +0000655 case cLong:
656 // Treat it like a cast from the lower half of the value.
657 emitIntegerCast (BB, IP, Type::IntTy, SrcReg+1, newTy, DestReg);
658 break;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000659 case cFloat:
660 case cDouble:
661 emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
662 break;
663 default: goto not_yet;
664 }
665 return;
666
667 case cFloat:
668 switch (oldTyClass) {
669 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000670 case cFloat:
671 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
672 break;
673 case cDouble:
674 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
675 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000676 default: {
677 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000678 // cast integer type to float. Store it to a stack slot and then load
Brian Gaeke495a0972004-06-24 21:22:08 +0000679 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000680 unsigned TmpReg = makeAnotherReg (newTy);
681 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
682 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
683 .addReg (SrcReg);
684 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
685 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000686 break;
687 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000688 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000689 return;
690
691 case cDouble:
Brian Gaeke495a0972004-06-24 21:22:08 +0000692 switch (oldTyClass) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000693 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000694 case cFloat:
695 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
696 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000697 case cDouble: // use double move pseudo-instr
698 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000699 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000700 default: {
701 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
702 unsigned TmpReg = makeAnotherReg (newTy);
703 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
704 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
705 .addReg (SrcReg);
706 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
707 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
708 break;
709 }
710 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000711 return;
712
713 case cLong:
714 switch (oldTyClass) {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000715 case cByte:
716 case cShort:
Brian Gaekea54df252004-11-19 18:48:10 +0000717 case cInt: {
718 // Cast to (u)int in the bottom half, and sign(zero) extend in the top
719 // half.
720 const Type *OldHalfTy = oldTy->isSigned() ? Type::IntTy : Type::UIntTy;
721 const Type *NewHalfTy = newTy->isSigned() ? Type::IntTy : Type::UIntTy;
722 unsigned TempReg = emitIntegerCast (BB, IP, OldHalfTy, SrcReg,
723 NewHalfTy, DestReg+1);
724 if (newTy->isSigned ()) {
725 BuildMI (*BB, IP, V8::SRAri, 2, DestReg).addReg (TempReg)
726 .addZImm (31);
727 } else {
728 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0)
729 .addReg (V8::G0);
730 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000731 break;
Brian Gaekea54df252004-11-19 18:48:10 +0000732 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000733 case cLong:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000734 // Just copy both halves.
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000735 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
736 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
737 .addReg (SrcReg+1);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000738 break;
739 default: goto not_yet;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000740 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000741 return;
742
743 default: goto not_yet;
Brian Gaekee302a7e2004-05-07 21:39:30 +0000744 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000745 return;
746not_yet:
747 std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy
748 << ", DestTy = " << *DestTy << "\n";
749 abort ();
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000750}
751
Brian Gaekef3334eb2004-04-07 17:29:37 +0000752void V8ISel::visitLoadInst(LoadInst &I) {
753 unsigned DestReg = getReg (I);
754 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000755 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000756 case cByte:
757 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000758 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000759 else
Brian Gaeke44733032004-06-24 07:36:48 +0000760 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000761 return;
762 case cShort:
763 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000764 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000765 else
Brian Gaeke44733032004-06-24 07:36:48 +0000766 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000767 return;
768 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000769 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000770 return;
771 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000772 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
773 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
774 return;
775 case cFloat:
776 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
777 return;
778 case cDouble:
779 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000780 return;
781 default:
782 std::cerr << "Load instruction not handled: " << I;
783 abort ();
784 return;
785 }
786}
787
788void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000789 Value *SrcVal = I.getOperand (0);
790 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000791 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000792 switch (getClassB (SrcVal->getType ())) {
793 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000794 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000795 return;
796 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000797 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000798 return;
799 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000800 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000801 return;
802 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000803 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
804 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
805 return;
806 case cFloat:
807 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
808 return;
809 case cDouble:
810 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000811 return;
812 default:
813 std::cerr << "Store instruction not handled: " << I;
814 abort ();
815 return;
816 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000817}
818
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000819void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000820 MachineInstr *TheCall;
821 // Is it an intrinsic function call?
822 if (Function *F = I.getCalledFunction()) {
823 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
824 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
825 return;
826 }
827 }
828
Brian Gaeke50094ed2004-10-10 19:57:18 +0000829 unsigned extraStack = 0;
830 // How much extra call stack will we need?
831 for (unsigned i = 7; i < I.getNumOperands (); ++i) {
832 switch (getClassB (I.getOperand (i)->getType ())) {
833 case cLong: extraStack += 8; break;
834 case cFloat: extraStack += 4; break;
835 case cDouble: extraStack += 8; break;
836 default: extraStack += 4; break;
837 }
838 }
Brian Gaeke04fe7472004-11-14 05:19:00 +0000839 // Round up extra stack size to the nearest doubleword.
840 if (extraStack) { extraStack = (extraStack + 7) & ~7; }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000841
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000842 // Deal with args
Brian Gaeke562cb162004-04-07 17:04:09 +0000843 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000844 V8::O4, V8::O5 };
Brian Gaeke24b90c32004-11-14 03:22:07 +0000845 const unsigned *OAREnd = &OutgoingArgRegs[6];
Brian Gaeke6931fd62004-11-04 00:27:04 +0000846 const unsigned *OAR = &OutgoingArgRegs[0];
Brian Gaeke24b90c32004-11-14 03:22:07 +0000847 unsigned ArgOffset = 68;
Brian Gaekeda9b3662004-11-14 06:32:08 +0000848 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000849 for (unsigned i = 1; i < I.getNumOperands (); ++i) {
850 unsigned ArgReg = getReg (I.getOperand (i));
Brian Gaeke24b90c32004-11-14 03:22:07 +0000851 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
852 // Schlep it over into the incoming arg register
853 if (ArgOffset < 92) {
854 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
855 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000856 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000857 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000858 }
Brian Gaeke24b90c32004-11-14 03:22:07 +0000859 ArgOffset += 4;
860 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
861 if (ArgOffset < 92) {
862 // Single-fp args are passed in integer registers; go through
863 // memory to get them out of FP registers. (Bleh!)
864 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
865 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
866 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
867 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
868 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
869 } else {
870 BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
871 }
872 ArgOffset += 4;
873 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
874 // Double-fp args are passed in pairs of integer registers; go through
875 // memory to get them out of FP registers. (Bleh!)
876 // We'd like to 'std' these right onto the outgoing-args area, but it might
877 // not be 8-byte aligned (e.g., call x(int x, double d)). sigh.
878 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
879 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
880 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
881 if (ArgOffset < 92 && OAR != OAREnd) {
882 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
883 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
884 } else {
885 unsigned TempReg = makeAnotherReg (Type::IntTy);
886 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
887 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
888 }
889 ArgOffset += 4;
890 if (ArgOffset < 92 && OAR != OAREnd) {
891 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
892 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (4);
893 } else {
894 unsigned TempReg = makeAnotherReg (Type::IntTy);
895 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
896 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
897 }
898 ArgOffset += 4;
899 } else if (getClassB (I.getOperand (i)->getType ()) == cLong) {
900 // do the first half...
901 if (ArgOffset < 92) {
902 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
903 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
904 } else {
905 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
906 }
907 ArgOffset += 4;
908 // ...then do the second half
909 if (ArgOffset < 92) {
910 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
911 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1);
912 } else {
913 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1);
914 }
915 ArgOffset += 4;
Brian Gaeke50094ed2004-10-10 19:57:18 +0000916 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000917 assert (0 && "Unknown class?!");
Brian Gaeked54c38b2004-04-07 16:41:22 +0000918 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000919 }
Brian Gaeked54c38b2004-04-07 16:41:22 +0000920
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000921 // Emit call instruction
922 if (Function *F = I.getCalledFunction ()) {
923 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
924 } else { // Emit an indirect call...
925 unsigned Reg = getReg (I.getCalledValue ());
926 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
927 }
928
Brian Gaeke50094ed2004-10-10 19:57:18 +0000929 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack);
930
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000931 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000932 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000933 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000934 unsigned DestReg = getReg (I);
Brian Gaeke299b39d2004-10-10 20:34:17 +0000935 switch (getClassB (I.getType ())) {
Brian Gaekeea8494b2004-04-06 22:09:23 +0000936 case cByte:
937 case cShort:
938 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000939 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
940 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000941 case cFloat:
942 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
943 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000944 case cDouble:
945 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
946 break;
947 case cLong:
948 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
949 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
950 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000951 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000952 std::cerr << "Return type of call instruction not handled: " << I;
953 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000954 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000955}
Chris Lattner1c809c52004-02-29 00:27:00 +0000956
957void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000958 if (I.getNumOperands () == 1) {
959 unsigned RetValReg = getReg (I.getOperand (0));
Brian Gaeke299b39d2004-10-10 20:34:17 +0000960 switch (getClassB (I.getOperand (0)->getType ())) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000961 case cByte:
962 case cShort:
963 case cInt:
964 // Schlep it over into i0 (where it will become o0 after restore).
965 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
966 break;
Brian Gaekef9a75462004-07-08 07:22:27 +0000967 case cFloat:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000968 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
Brian Gaekef9a75462004-07-08 07:22:27 +0000969 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000970 case cDouble:
971 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000972 break;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000973 case cLong:
974 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
975 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
976 break;
Brian Gaeke08f64c32004-03-06 05:32:28 +0000977 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000978 std::cerr << "Return instruction of this type not handled: " << I;
979 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000980 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000981 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000982
Brian Gaeke08f64c32004-03-06 05:32:28 +0000983 // Just emit a 'retl' instruction to return.
984 BuildMI(BB, V8::RETL, 0);
985 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000986}
987
Brian Gaeke532e60c2004-05-08 04:21:17 +0000988static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
989 Function::iterator I = BB; ++I; // Get iterator to next block
990 return I != BB->getParent()->end() ? &*I : 0;
991}
992
993/// visitBranchInst - Handles conditional and unconditional branches.
994///
995void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000996 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +0000997 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
998 BB->addSuccessor (takenSuccMBB);
999 if (I.isConditional()) { // conditional branch
1000 BasicBlock *notTakenSucc = I.getSuccessor (1);
1001 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
1002 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001003
Brian Gaeke6c868a42004-06-17 22:34:08 +00001004 // CondReg=(<condition>);
1005 // If (CondReg==0) goto notTakenSuccMBB;
1006 unsigned CondReg = getReg (I.getCondition ());
1007 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
1008 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001009 }
Brian Gaeke6c868a42004-06-17 22:34:08 +00001010 // goto takenSuccMBB;
1011 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001012}
1013
1014/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
1015/// constant expression GEP support.
1016///
Brian Gaeke9f564822004-05-08 05:27:20 +00001017void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +00001018 MachineBasicBlock::iterator IP,
1019 Value *Src, User::op_iterator IdxBegin,
1020 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +00001021 const TargetData &TD = TM.getTargetData ();
1022 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001023 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +00001024
1025 // GEPs have zero or more indices; we must perform a struct access
1026 // or array access for each one.
1027 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
1028 ++oi) {
1029 Value *idx = *oi;
1030 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
1031 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
1032 // It's a struct access. idx is the index into the structure,
1033 // which names the field. Use the TargetData structure to
1034 // pick out what the layout of the structure is in memory.
1035 // Use the (constant) structure index's value to find the
1036 // right byte offset from the StructLayout class's list of
1037 // structure member offsets.
1038 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
1039 unsigned memberOffset =
1040 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
1041 // Emit an ADD to add memberOffset to the basePtr.
1042 BuildMI (*MBB, IP, V8::ADDri, 2,
1043 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
1044 // The next type is the member of the structure selected by the
1045 // index.
1046 Ty = StTy->getElementType (fieldIndex);
1047 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
1048 // It's an array or pointer access: [ArraySize x ElementType].
1049 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1050 // must find the size of the pointed-to type (Not coincidentally, the next
1051 // type is the type of the elements in the array).
1052 Ty = SqTy->getElementType ();
1053 unsigned elementSize = TD.getTypeSize (Ty);
1054 unsigned idxReg = getReg (idx, MBB, IP);
1055 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
1056 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001057 copyConstantToRegister (MBB, IP,
1058 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
Brian Gaeke9f564822004-05-08 05:27:20 +00001059 // Emit a SMUL to multiply the register holding the index by
1060 // elementSize, putting the result in OffsetReg.
1061 BuildMI (*MBB, IP, V8::SMULrr, 2,
1062 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
1063 // Emit an ADD to add OffsetReg to the basePtr.
1064 BuildMI (*MBB, IP, V8::ADDrr, 2,
1065 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1066 }
1067 basePtrReg = nextBasePtrReg;
1068 }
1069 // After we have processed all the indices, the result is left in
1070 // basePtrReg. Move it to the register where we were expected to
1071 // put the answer.
1072 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001073}
1074
1075void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
1076 unsigned outputReg = getReg (I);
1077 emitGEPOperation (BB, BB->end (), I.getOperand (0),
1078 I.op_begin ()+1, I.op_end (), outputReg);
1079}
1080
Brian Gaeked6a10532004-06-15 21:09:46 +00001081
Chris Lattner4be7ca52004-04-07 04:27:16 +00001082void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001083 unsigned DestReg = getReg (I);
1084 unsigned Op0Reg = getReg (I.getOperand (0));
1085 unsigned Op1Reg = getReg (I.getOperand (1));
1086
Brian Gaekeec3227f2004-06-27 22:47:33 +00001087 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +00001088 unsigned OpCase = ~0;
1089
Brian Gaekeec3227f2004-06-27 22:47:33 +00001090 if (Class > cLong) {
1091 switch (I.getOpcode ()) {
1092 case Instruction::Add: OpCase = 0; break;
1093 case Instruction::Sub: OpCase = 1; break;
1094 case Instruction::Mul: OpCase = 2; break;
1095 case Instruction::Div: OpCase = 3; break;
1096 default: visitInstruction (I); return;
1097 }
1098 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
1099 V8::FSUBS, V8::FSUBD,
1100 V8::FMULS, V8::FMULD,
1101 V8::FDIVS, V8::FDIVD };
1102 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
1103 .addReg (Op0Reg).addReg (Op1Reg);
1104 return;
1105 }
1106
1107 unsigned ResultReg = DestReg;
Brian Gaeke1df468e2004-09-29 03:34:41 +00001108 if (Class != cInt && Class != cLong)
Brian Gaekeec3227f2004-06-27 22:47:33 +00001109 ResultReg = makeAnotherReg (I.getType ());
1110
Brian Gaeke1df468e2004-09-29 03:34:41 +00001111 if (Class == cLong) {
1112 DEBUG (std::cerr << "Class = cLong\n");
1113 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
1114 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
1115 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
1116 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
1117 }
1118
1119 // FIXME: support long, ulong.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001120 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +00001121 case Instruction::Add: OpCase = 0; break;
1122 case Instruction::Sub: OpCase = 1; break;
1123 case Instruction::Mul: OpCase = 2; break;
1124 case Instruction::And: OpCase = 3; break;
1125 case Instruction::Or: OpCase = 4; break;
1126 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +00001127 case Instruction::Shl: OpCase = 6; break;
1128 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +00001129
1130 case Instruction::Div:
1131 case Instruction::Rem: {
1132 unsigned Dest = ResultReg;
1133 if (I.getOpcode() == Instruction::Rem)
1134 Dest = makeAnotherReg(I.getType());
1135
1136 // FIXME: this is probably only right for 32 bit operands.
1137 if (I.getType ()->isSigned()) {
1138 unsigned Tmp = makeAnotherReg (I.getType ());
1139 // Sign extend into the Y register
1140 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
1141 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
1142 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
1143 } else {
1144 // Zero extend into the Y register, ie, just set it to zero
1145 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
1146 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +00001147 }
Chris Lattner22ede702004-04-07 04:06:46 +00001148
1149 if (I.getOpcode() == Instruction::Rem) {
1150 unsigned Tmp = makeAnotherReg (I.getType ());
1151 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
1152 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +00001153 }
Chris Lattner22ede702004-04-07 04:06:46 +00001154 break;
1155 }
1156 default:
1157 visitInstruction (I);
1158 return;
1159 }
1160
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001161 static const unsigned Opcodes[] = {
1162 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
1163 V8::SLLrr, V8::SRLrr, V8::SRArr
1164 };
Chris Lattner22ede702004-04-07 04:06:46 +00001165 if (OpCase != ~0U) {
Chris Lattner22ede702004-04-07 04:06:46 +00001166 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001167 }
1168
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001169 switch (getClassB (I.getType ())) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001170 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001171 if (I.getType ()->isSigned ()) { // add byte
1172 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1173 } else { // add ubyte
1174 unsigned TmpReg = makeAnotherReg (I.getType ());
1175 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1176 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1177 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001178 break;
1179 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001180 if (I.getType ()->isSigned ()) { // add short
1181 unsigned TmpReg = makeAnotherReg (I.getType ());
1182 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1183 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1184 } else { // add ushort
1185 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +00001186 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1187 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +00001188 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001189 break;
1190 case cInt:
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001191 // Nothing to do here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001192 break;
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001193 case cLong:
1194 // Only support and, or, xor.
1195 if (OpCase < 3 || OpCase > 5) {
1196 visitInstruction (I);
1197 return;
1198 }
1199 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +00001200 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1201 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001202 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001203 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001204 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001205 }
1206}
1207
Misha Brukmanea091262004-06-30 21:47:40 +00001208void V8ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner4d0cda42004-04-07 05:04:51 +00001209 unsigned Op0Reg = getReg (I.getOperand (0));
1210 unsigned Op1Reg = getReg (I.getOperand (1));
1211 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +00001212 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +00001213
1214 // Compare the two values.
Brian Gaeke3a085892004-07-08 09:08:35 +00001215 assert (getClass (Ty) != cLong && "can't setcc on longs yet");
1216 if (getClass (Ty) < cLong) {
1217 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
1218 } else if (getClass (Ty) == cFloat) {
1219 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1220 } else if (getClass (Ty) == cDouble) {
1221 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1222 }
Chris Lattner4d0cda42004-04-07 05:04:51 +00001223
Brian Gaeke429022b2004-05-08 06:36:14 +00001224 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001225 switch (I.getOpcode()) {
1226 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +00001227 case Instruction::SetEQ: BranchIdx = 0; break;
1228 case Instruction::SetNE: BranchIdx = 1; break;
1229 case Instruction::SetLT: BranchIdx = 2; break;
1230 case Instruction::SetGT: BranchIdx = 3; break;
1231 case Instruction::SetLE: BranchIdx = 4; break;
1232 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001233 }
Brian Gaeke3a085892004-07-08 09:08:35 +00001234 unsigned Column = 0;
Brian Gaekeb3e00172004-11-17 22:06:56 +00001235 if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1;
1236 if (Ty->isFloatingPoint()) Column = 2;
Brian Gaeke3a085892004-07-08 09:08:35 +00001237 static unsigned OpcodeTab[3*6] = {
1238 // LLVM SparcV8
1239 // unsigned signed fp
1240 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1241 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1242 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1243 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1244 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1245 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
Brian Gaeke429022b2004-05-08 06:36:14 +00001246 };
Brian Gaeke3a085892004-07-08 09:08:35 +00001247 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
Brian Gaeke6c868a42004-06-17 22:34:08 +00001248
1249 MachineBasicBlock *thisMBB = BB;
1250 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1251 // thisMBB:
1252 // ...
1253 // subcc %reg0, %reg1, %g0
1254 // bCC copy1MBB
1255 // ba copy0MBB
1256
1257 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1258 // if we could insert other, non-terminator instructions after the
1259 // bCC. But MBB->getFirstTerminator() can't understand this.
1260 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1261 F->getBasicBlockList ().push_back (copy1MBB);
1262 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1263 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1264 F->getBasicBlockList ().push_back (copy0MBB);
1265 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1266 // Update machine-CFG edges
1267 BB->addSuccessor (copy1MBB);
1268 BB->addSuccessor (copy0MBB);
1269
1270 // copy0MBB:
1271 // %FalseValue = or %G0, 0
1272 // ba sinkMBB
1273 BB = copy0MBB;
1274 unsigned FalseValue = makeAnotherReg (I.getType ());
1275 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1276 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1277 F->getBasicBlockList ().push_back (sinkMBB);
1278 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1279 // Update machine-CFG edges
1280 BB->addSuccessor (sinkMBB);
1281
1282 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1283 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1284 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1285 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1286
1287 // copy1MBB:
1288 // %TrueValue = or %G0, 1
1289 // ba sinkMBB
1290 BB = copy1MBB;
1291 unsigned TrueValue = makeAnotherReg (I.getType ());
1292 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1293 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1294 // Update machine-CFG edges
1295 BB->addSuccessor (sinkMBB);
1296
1297 // sinkMBB:
1298 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1299 // ...
1300 BB = sinkMBB;
1301 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1302 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001303}
1304
Brian Gaekec93a7522004-06-18 05:19:16 +00001305void V8ISel::visitAllocaInst(AllocaInst &I) {
1306 // Find the data size of the alloca inst's getAllocatedType.
1307 const Type *Ty = I.getAllocatedType();
1308 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001309
Brian Gaekec93a7522004-06-18 05:19:16 +00001310 unsigned ArraySizeReg = getReg (I.getArraySize ());
1311 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1312 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1313 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1314 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001315
1316 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1317 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001318
Brian Gaekec93a7522004-06-18 05:19:16 +00001319 // Round up TmpReg1 to nearest doubleword boundary:
1320 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1321 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001322
1323 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001324 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001325
1326 // Put a pointer to the space into the result register, by copying
1327 // the stack pointer.
1328 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1329
1330 // Inform the Frame Information that we have just allocated a variable-sized
1331 // object.
1332 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001333}
Chris Lattner1c809c52004-02-29 00:27:00 +00001334
1335/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1336/// function, lowering any calls to unknown intrinsic functions into the
1337/// equivalent LLVM code.
1338void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1339 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1340 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1341 if (CallInst *CI = dyn_cast<CallInst>(I++))
1342 if (Function *F = CI->getCalledFunction())
1343 switch (F->getIntrinsicID()) {
1344 case Intrinsic::not_intrinsic: break;
1345 default:
1346 // All other intrinsic calls we must lower.
1347 Instruction *Before = CI->getPrev();
1348 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1349 if (Before) { // Move iterator to instruction after call
1350 I = Before; ++I;
1351 } else {
1352 I = BB->begin();
1353 }
1354 }
1355}
1356
1357
1358void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattner1c809c52004-02-29 00:27:00 +00001359 switch (ID) {
Brian Gaeke9e672a22004-11-19 18:53:59 +00001360 default:
1361 std::cerr << "Sorry, unknown intrinsic function call:\n" << CI; abort ();
1362
1363 case Intrinsic::vastart:
1364 std::cerr << "Sorry, va_start intrinsic still unsupported:\n" << CI; abort ();
1365
1366 case Intrinsic::vaend:
Brian Gaeke2f95ed62004-11-19 19:21:34 +00001367 // va_end is a no-op on SparcV8.
1368 return;
Brian Gaeke9e672a22004-11-19 18:53:59 +00001369
1370 case Intrinsic::vacopy:
1371 std::cerr << "Sorry, va_copy intrinsic still unsupported:\n" << CI; abort ();
Chris Lattner1c809c52004-02-29 00:27:00 +00001372 }
1373}