blob: f30db1bab6e1304e1d2b2100a1d1912e0d702b63 [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
Brian Gaeked90282d2004-11-19 20:57:24 +000037 int VarArgsOffset; // Offset from fp for start of varargs area
Chris Lattner1c809c52004-02-29 00:27:00 +000038
39 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
40
41 // MBBMap - Mapping between LLVM BB -> Machine BB
42 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
43
44 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
45
46 /// runOnFunction - Top level implementation of instruction selection for
47 /// the entire function.
48 ///
49 bool runOnFunction(Function &Fn);
50
51 virtual const char *getPassName() const {
52 return "SparcV8 Simple Instruction Selection";
53 }
54
Brian Gaeke532e60c2004-05-08 04:21:17 +000055 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
56 /// constant expression GEP support.
57 ///
58 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
59 Value *Src, User::op_iterator IdxBegin,
60 User::op_iterator IdxEnd, unsigned TargetReg);
61
Brian Gaeke00e514e2004-06-24 06:33:00 +000062 /// emitCastOperation - Common code shared between visitCastInst and
63 /// constant expression cast support.
64 ///
65 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
66 Value *Src, const Type *DestTy, unsigned TargetReg);
67
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +000068 /// emitIntegerCast, emitFPToIntegerCast - Helper methods for
69 /// emitCastOperation.
70 ///
Brian Gaekea54df252004-11-19 18:48:10 +000071 unsigned emitIntegerCast (MachineBasicBlock *BB,
72 MachineBasicBlock::iterator IP,
73 const Type *oldTy, unsigned SrcReg,
74 const Type *newTy, unsigned DestReg);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +000075 void emitFPToIntegerCast (MachineBasicBlock *BB,
76 MachineBasicBlock::iterator IP, const Type *oldTy,
77 unsigned SrcReg, const Type *newTy,
78 unsigned DestReg);
79
Chris Lattner1c809c52004-02-29 00:27:00 +000080 /// visitBasicBlock - This method is called when we are visiting a new basic
81 /// block. This simply creates a new MachineBasicBlock to emit code into
82 /// and adds it to the current MachineFunction. Subsequent visit* for
83 /// instructions will be invoked for all instructions in the basic block.
84 ///
85 void visitBasicBlock(BasicBlock &LLVM_BB) {
86 BB = MBBMap[&LLVM_BB];
87 }
88
Brian Gaeke5f91de22004-11-21 07:13:16 +000089 void emitOp64LibraryCall (MachineBasicBlock *MBB,
90 MachineBasicBlock::iterator IP,
91 unsigned DestReg, const char *FuncName,
92 unsigned Op0Reg, unsigned Op1Reg);
Chris Lattner4be7ca52004-04-07 04:27:16 +000093 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000094 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000095 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000096 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000097 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000098 void visitBranchInst(BranchInst &I);
Chris Lattnerd14d5b42004-10-17 02:42:42 +000099 void visitUnreachableInst(UnreachableInst &I) {}
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000100 void visitCastInst(CastInst &I);
Brian Gaekeb6c409a2004-11-19 21:08:18 +0000101 void visitVANextInst(VANextInst &I);
102 void visitVAArgInst(VAArgInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000103 void visitLoadInst(LoadInst &I);
104 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000105 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
106 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +0000107 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000108
Chris Lattner1c809c52004-02-29 00:27:00 +0000109 void visitInstruction(Instruction &I) {
110 std::cerr << "Unhandled instruction: " << I;
111 abort();
112 }
113
114 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
115 /// function, lowering any calls to unknown intrinsic functions into the
116 /// equivalent LLVM code.
117 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +0000118 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
119
Brian Gaeke562cb162004-04-07 17:04:09 +0000120 void LoadArgumentsToVirtualRegs(Function *F);
121
Brian Gaeke6c868a42004-06-17 22:34:08 +0000122 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
123 /// because we have to generate our sources into the source basic blocks,
124 /// not the current one.
125 ///
126 void SelectPHINodes();
127
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000128 /// copyConstantToRegister - Output the instructions required to put the
129 /// specified constant into the specified register.
130 ///
131 void copyConstantToRegister(MachineBasicBlock *MBB,
132 MachineBasicBlock::iterator IP,
133 Constant *C, unsigned R);
134
135 /// makeAnotherReg - This method returns the next register number we haven't
136 /// yet used.
137 ///
138 /// Long values are handled somewhat specially. They are always allocated
139 /// as pairs of 32 bit integer values. The register number returned is the
140 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
141 /// of the long value.
142 ///
143 unsigned makeAnotherReg(const Type *Ty) {
144 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
145 "Current target doesn't have SparcV8 reg info??");
146 const SparcV8RegisterInfo *MRI =
147 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
148 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
149 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
150 // Create the lower part
151 F->getSSARegMap()->createVirtualRegister(RC);
152 // Create the upper part.
153 return F->getSSARegMap()->createVirtualRegister(RC)-1;
154 }
155
156 // Add the mapping of regnumber => reg class to MachineFunction
157 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
158 return F->getSSARegMap()->createVirtualRegister(RC);
159 }
160
161 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
162 unsigned getReg(Value *V) {
163 // Just append to the end of the current bb.
164 MachineBasicBlock::iterator It = BB->end();
165 return getReg(V, BB, It);
166 }
167 unsigned getReg(Value *V, MachineBasicBlock *MBB,
168 MachineBasicBlock::iterator IPt) {
169 unsigned &Reg = RegMap[V];
170 if (Reg == 0) {
171 Reg = makeAnotherReg(V->getType());
172 RegMap[V] = Reg;
173 }
174 // If this operand is a constant, emit the code to copy the constant into
175 // the register here...
176 //
177 if (Constant *C = dyn_cast<Constant>(V)) {
178 copyConstantToRegister(MBB, IPt, C, Reg);
179 RegMap.erase(V); // Assign a new name to this constant if ref'd again
180 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
181 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000182 unsigned TmpReg = makeAnotherReg(V->getType());
183 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
184 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
185 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000186 RegMap.erase(V); // Assign a new name to this address if ref'd again
187 }
188
189 return Reg;
190 }
191
Chris Lattner1c809c52004-02-29 00:27:00 +0000192 };
193}
194
195FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
196 return new V8ISel(TM);
197}
198
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000199enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000200 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000201};
202
203static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000204 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000205 case Type::UByteTyID: case Type::SByteTyID: return cByte;
206 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000207 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000208 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000209 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000210 case Type::FloatTyID: return cFloat;
211 case Type::DoubleTyID: return cDouble;
212 default:
213 assert (0 && "Type of unknown class passed to getClass?");
214 return cByte;
215 }
216}
Brian Gaeke50094ed2004-10-10 19:57:18 +0000217
Chris Lattner0d538bb2004-04-07 04:36:53 +0000218static TypeClass getClassB(const Type *T) {
219 if (T == Type::BoolTy) return cByte;
220 return getClass(T);
221}
222
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000223/// copyConstantToRegister - Output the instructions required to put the
224/// specified constant into the specified register.
225///
226void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
227 MachineBasicBlock::iterator IP,
228 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000229 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
230 switch (CE->getOpcode()) {
231 case Instruction::GetElementPtr:
232 emitGEPOperation(MBB, IP, CE->getOperand(0),
233 CE->op_begin()+1, CE->op_end(), R);
234 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000235 case Instruction::Cast:
236 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
237 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000238 default:
239 std::cerr << "Copying this constant expr not yet handled: " << *CE;
240 abort();
241 }
Chris Lattnerd14d5b42004-10-17 02:42:42 +0000242 } else if (isa<UndefValue>(C)) {
243 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R);
244 if (getClassB (C->getType ()) == cLong)
245 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1);
246 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000247 }
248
Brian Gaekee302a7e2004-05-07 21:39:30 +0000249 if (C->getType()->isIntegral ()) {
250 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000251 unsigned Class = getClassB (C->getType ());
252 if (Class == cLong) {
253 unsigned TmpReg = makeAnotherReg (Type::IntTy);
254 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
255 // Copy the value into the register pair.
256 // R = top(more-significant) half, R+1 = bottom(less-significant) half
257 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000258 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
259 Val >> 32), R);
260 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
261 Val & 0xffffffffU), R+1);
Brian Gaeke9df92822004-06-15 19:16:07 +0000262 return;
263 }
264
265 assert(Class <= cInt && "Type not handled yet!");
266
Brian Gaekee302a7e2004-05-07 21:39:30 +0000267 if (C->getType() == Type::BoolTy) {
268 Val = (C == ConstantBool::True);
269 } else {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000270 ConstantInt *CI = cast<ConstantInt> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000271 Val = CI->getRawValue ();
272 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000273 switch (Class) {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000274 case cByte: Val = (int8_t) Val; break;
275 case cShort: Val = (int16_t) Val; break;
276 case cInt: Val = (int32_t) Val; break;
Brian Gaekee8061732004-03-04 00:56:25 +0000277 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000278 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000279 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000280 return;
281 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000282 if (Val == 0) {
283 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
284 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
285 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
286 } else {
287 unsigned TmpReg = makeAnotherReg (C->getType ());
288 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
289 .addSImm (((uint32_t) Val) >> 10);
290 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
291 .addSImm (((uint32_t) Val) & 0x03ff);
292 return;
293 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000294 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
295 // We need to spill the constant to memory...
296 MachineConstantPool *CP = F->getConstantPool();
297 unsigned CPI = CP->getConstantPoolIndex(CFP);
298 const Type *Ty = CFP->getType();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000299 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
300 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +0000301
302 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000303 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000304 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000305 BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg)
306 .addConstantPoolIndex (CPI);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000307 BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000308 } else if (isa<ConstantPointerNull>(C)) {
309 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000310 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Chris Lattner73302482004-07-18 07:26:17 +0000311 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000312 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
313 // that SETHI %reg,global == SETHI %reg,%hi(global) and
314 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
315 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner73302482004-07-18 07:26:17 +0000316 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
317 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
Brian Gaeke9df92822004-06-15 19:16:07 +0000318 } else {
319 std::cerr << "Offending constant: " << *C << "\n";
320 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000321 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000322}
Chris Lattner1c809c52004-02-29 00:27:00 +0000323
Brian Gaeke812c4882004-07-16 10:31:25 +0000324void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
Brian Gaeke562cb162004-04-07 17:04:09 +0000325 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
326 V8::I3, V8::I4, V8::I5 };
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000327
Brian Gaeke812c4882004-07-16 10:31:25 +0000328 // Add IMPLICIT_DEFs of input regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000329 unsigned ArgNo = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000330 for (Function::aiterator I = LF->abegin(), E = LF->aend();
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000331 I != E && ArgNo < 6; ++I, ++ArgNo) {
Brian Gaeke812c4882004-07-16 10:31:25 +0000332 switch (getClassB(I->getType())) {
333 case cByte:
334 case cShort:
335 case cInt:
336 case cFloat:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000337 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke812c4882004-07-16 10:31:25 +0000338 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000339 case cDouble:
340 case cLong:
341 // Double and Long use register pairs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000342 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
343 ++ArgNo;
344 if (ArgNo < 6)
345 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000346 break;
Brian Gaeke812c4882004-07-16 10:31:25 +0000347 default:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000348 assert (0 && "type not handled");
Brian Gaeke812c4882004-07-16 10:31:25 +0000349 return;
350 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000351 }
352
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000353 const unsigned *IAREnd = &IncomingArgRegs[6];
354 const unsigned *IAR = &IncomingArgRegs[0];
355 unsigned ArgOffset = 68;
Brian Gaeke4e459c42004-11-19 20:31:08 +0000356
357 // Store registers onto stack if this is a varargs function.
358 // FIXME: This doesn't really pertain to "loading arguments into
359 // virtual registers", so it's not clear that it really belongs here.
360 // FIXME: We could avoid storing any args onto the stack that don't
361 // need to be in memory, because they come before the ellipsis in the
362 // parameter list (and thus could never be accessed through va_arg).
363 if (LF->getFunctionType ()->isVarArg ()) {
364 for (unsigned i = 0; i < 6; ++i) {
365 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
366 assert (IAR != IAREnd
367 && "About to dereference past end of IncomingArgRegs");
368 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
369 ArgOffset += 4;
370 }
371 // Reset the pointers now that we're done.
372 ArgOffset = 68;
373 IAR = &IncomingArgRegs[0];
374 }
375
376 // Copy args out of their incoming hard regs or stack slots into virtual regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000377 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; ++I) {
378 Argument &A = *I;
379 unsigned ArgReg = getReg (A);
380 if (getClassB (A.getType ()) < cLong) {
381 // Get it out of the incoming arg register
382 if (ArgOffset < 92) {
383 assert (IAR != IAREnd
384 && "About to dereference past end of IncomingArgRegs");
385 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
386 } else {
387 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
388 BuildMI (BB, V8::LD, 3, ArgReg).addFrameIndex (FI).addSImm (0);
389 }
390 ArgOffset += 4;
391 } else if (getClassB (A.getType ()) == cFloat) {
392 if (ArgOffset < 92) {
Brian Gaeke1df468e2004-09-29 03:34:41 +0000393 // Single-fp args are passed in integer registers; go through
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000394 // memory to get them out of integer registers and back into fp. (Bleh!)
Brian Gaeke1df468e2004-09-29 03:34:41 +0000395 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
396 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000397 assert (IAR != IAREnd
398 && "About to dereference past end of IncomingArgRegs");
399 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
400 BuildMI (BB, V8::LDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
401 } else {
402 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
403 BuildMI (BB, V8::LDFri, 3, ArgReg).addFrameIndex (FI).addSImm (0);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000404 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000405 ArgOffset += 4;
406 } else if (getClassB (A.getType ()) == cDouble) {
407 // Double-fp args are passed in pairs of integer registers; go through
408 // memory to get them out of integer registers and back into fp. (Bleh!)
409 // We'd like to 'ldd' these right out of the incoming-args area,
410 // but it might not be 8-byte aligned (e.g., call x(int x, double d)).
411 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
412 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
413 if (ArgOffset < 92 && IAR != IAREnd) {
414 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
415 } else {
416 unsigned TempReg = makeAnotherReg (Type::IntTy);
417 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
418 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
Brian Gaeke6672f862004-09-30 19:44:32 +0000419 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000420 ArgOffset += 4;
421 if (ArgOffset < 92 && IAR != IAREnd) {
422 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (*IAR++);
423 } else {
424 unsigned TempReg = makeAnotherReg (Type::IntTy);
425 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
426 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000427 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000428 ArgOffset += 4;
429 BuildMI (BB, V8::LDDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
430 } else if (getClassB (A.getType ()) == cLong) {
431 // do the first half...
432 if (ArgOffset < 92) {
433 assert (IAR != IAREnd
434 && "About to dereference past end of IncomingArgRegs");
435 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
436 } else {
437 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
438 BuildMI (BB, V8::LD, 2, ArgReg).addFrameIndex (FI).addSImm (0);
439 }
440 ArgOffset += 4;
441 // ...then do the second half
442 if (ArgOffset < 92) {
443 assert (IAR != IAREnd
444 && "About to dereference past end of IncomingArgRegs");
445 BuildMI (BB, V8::ORrr, 2, ArgReg+1).addReg (V8::G0).addReg (*IAR++);
446 } else {
447 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
448 BuildMI (BB, V8::LD, 2, ArgReg+1).addFrameIndex (FI).addSImm (0);
449 }
450 ArgOffset += 4;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000451 } else {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000452 assert (0 && "Unknown class?!");
Brian Gaeke812c4882004-07-16 10:31:25 +0000453 }
Brian Gaeke562cb162004-04-07 17:04:09 +0000454 }
Brian Gaeked90282d2004-11-19 20:57:24 +0000455
456 // If the function takes variable number of arguments, remember the fp
457 // offset for the start of the first vararg value... this is used to expand
458 // llvm.va_start.
459 if (LF->getFunctionType ()->isVarArg ())
460 VarArgsOffset = ArgOffset;
Brian Gaeke562cb162004-04-07 17:04:09 +0000461}
462
Brian Gaeke6c868a42004-06-17 22:34:08 +0000463void V8ISel::SelectPHINodes() {
464 const TargetInstrInfo &TII = *TM.getInstrInfo();
465 const Function &LF = *F->getFunction(); // The LLVM function...
466 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
467 const BasicBlock *BB = I;
468 MachineBasicBlock &MBB = *MBBMap[I];
469
470 // Loop over all of the PHI nodes in the LLVM basic block...
471 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
472 for (BasicBlock::const_iterator I = BB->begin();
473 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
474
475 // Create a new machine instr PHI node, and insert it.
476 unsigned PHIReg = getReg(*PN);
477 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
478 V8::PHI, PN->getNumOperands(), PHIReg);
479
480 MachineInstr *LongPhiMI = 0;
481 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
482 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
483 V8::PHI, PN->getNumOperands(), PHIReg+1);
484
485 // PHIValues - Map of blocks to incoming virtual registers. We use this
486 // so that we only initialize one incoming value for a particular block,
487 // even if the block has multiple entries in the PHI node.
488 //
489 std::map<MachineBasicBlock*, unsigned> PHIValues;
490
491 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
492 MachineBasicBlock *PredMBB = 0;
493 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
494 PE = MBB.pred_end (); PI != PE; ++PI)
495 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
496 PredMBB = *PI;
497 break;
498 }
499 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
500
501 unsigned ValReg;
502 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
503 PHIValues.lower_bound(PredMBB);
504
505 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
506 // We already inserted an initialization of the register for this
507 // predecessor. Recycle it.
508 ValReg = EntryIt->second;
509
510 } else {
511 // Get the incoming value into a virtual register.
512 //
513 Value *Val = PN->getIncomingValue(i);
514
515 // If this is a constant or GlobalValue, we may have to insert code
516 // into the basic block to compute it into a virtual register.
517 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
518 isa<GlobalValue>(Val)) {
519 // Simple constants get emitted at the end of the basic block,
520 // before any terminator instructions. We "know" that the code to
521 // move a constant into a register will never clobber any flags.
522 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
523 } else {
524 // Because we don't want to clobber any values which might be in
525 // physical registers with the computation of this constant (which
526 // might be arbitrarily complex if it is a constant expression),
527 // just insert the computation at the top of the basic block.
528 MachineBasicBlock::iterator PI = PredMBB->begin();
529
530 // Skip over any PHI nodes though!
531 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
532 ++PI;
533
534 ValReg = getReg(Val, PredMBB, PI);
535 }
536
537 // Remember that we inserted a value for this PHI for this predecessor
538 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
539 }
540
541 PhiMI->addRegOperand(ValReg);
542 PhiMI->addMachineBasicBlockOperand(PredMBB);
543 if (LongPhiMI) {
544 LongPhiMI->addRegOperand(ValReg+1);
545 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
546 }
547 }
548
549 // Now that we emitted all of the incoming values for the PHI node, make
550 // sure to reposition the InsertPoint after the PHI that we just added.
551 // This is needed because we might have inserted a constant into this
552 // block, right after the PHI's which is before the old insert point!
553 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
554 ++PHIInsertPoint;
555 }
556 }
557}
558
Chris Lattner1c809c52004-02-29 00:27:00 +0000559bool V8ISel::runOnFunction(Function &Fn) {
560 // First pass over the function, lower any unknown intrinsic functions
561 // with the IntrinsicLowering class.
562 LowerUnknownIntrinsicFunctionCalls(Fn);
563
564 F = &MachineFunction::construct(&Fn, TM);
565
566 // Create all of the machine basic blocks for the function...
567 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
568 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
569
570 BB = &F->front();
571
572 // Set up a frame object for the return address. This is used by the
573 // llvm.returnaddress & llvm.frameaddress intrinisics.
574 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
575
576 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000577 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000578
579 // Instruction select everything except PHI nodes
580 visit(Fn);
581
582 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000583 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000584
585 RegMap.clear();
586 MBBMap.clear();
587 F = 0;
588 // We always build a machine code representation for the function
589 return true;
590}
591
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000592void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000593 Value *Op = I.getOperand(0);
594 unsigned DestReg = getReg(I);
595 MachineBasicBlock::iterator MI = BB->end();
596 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
597}
598
Brian Gaekea54df252004-11-19 18:48:10 +0000599unsigned V8ISel::emitIntegerCast (MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000600 MachineBasicBlock::iterator IP, const Type *oldTy,
601 unsigned SrcReg, const Type *newTy,
602 unsigned DestReg) {
603 if (oldTy == newTy) {
604 // No-op cast - just emit a copy; assume the reg. allocator will zap it.
605 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg);
Brian Gaekea54df252004-11-19 18:48:10 +0000606 return SrcReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000607 }
608 // Emit left-shift, then right-shift to sign- or zero-extend.
609 unsigned TmpReg = makeAnotherReg (newTy);
610 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
611 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg);
612 if (newTy->isSigned ()) { // sign-extend with SRA
613 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
614 } else { // zero-extend with SRL
615 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
616 }
Brian Gaekea54df252004-11-19 18:48:10 +0000617 // Return the temp reg. in case this is one half of a cast to long.
618 return TmpReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000619}
620
621void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB,
622 MachineBasicBlock::iterator IP,
623 const Type *oldTy, unsigned SrcReg,
624 const Type *newTy, unsigned DestReg) {
625 unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign;
626 unsigned oldTyClass = getClassB(oldTy);
627 if (oldTyClass == cFloat) {
628 FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4;
629 FPAlign = TM.getTargetData().getFloatAlignment();
630 } else { // it's a double
631 FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8;
632 FPAlign = TM.getTargetData().getDoubleAlignment();
633 }
634 unsigned TempReg = makeAnotherReg (oldTy);
635 BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg);
636 int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign);
637 BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0)
638 .addReg (TempReg);
639 unsigned TempReg2 = makeAnotherReg (newTy);
640 BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0);
641 emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg);
642}
643
Brian Gaeke00e514e2004-06-24 06:33:00 +0000644/// emitCastOperation - Common code shared between visitCastInst and constant
645/// expression cast support.
646///
647void V8ISel::emitCastOperation(MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000648 MachineBasicBlock::iterator IP, Value *Src,
649 const Type *DestTy, unsigned DestReg) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000650 const Type *SrcTy = Src->getType();
651 unsigned SrcClass = getClassB(SrcTy);
652 unsigned DestClass = getClassB(DestTy);
653 unsigned SrcReg = getReg(Src, BB, IP);
654
655 const Type *oldTy = SrcTy;
656 const Type *newTy = DestTy;
657 unsigned oldTyClass = SrcClass;
658 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000659
Brian Gaeke429022b2004-05-08 06:36:14 +0000660 if (oldTyClass < cLong && newTyClass < cLong) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000661 emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
662 } else switch (newTyClass) {
663 case cByte:
664 case cShort:
665 case cInt:
Brian Gaeke495a0972004-06-24 21:22:08 +0000666 switch (oldTyClass) {
Brian Gaekea54df252004-11-19 18:48:10 +0000667 case cLong:
668 // Treat it like a cast from the lower half of the value.
669 emitIntegerCast (BB, IP, Type::IntTy, SrcReg+1, newTy, DestReg);
670 break;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000671 case cFloat:
672 case cDouble:
673 emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
674 break;
675 default: goto not_yet;
676 }
677 return;
678
679 case cFloat:
680 switch (oldTyClass) {
681 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000682 case cFloat:
683 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
684 break;
685 case cDouble:
686 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
687 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000688 default: {
689 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000690 // cast integer type to float. Store it to a stack slot and then load
Brian Gaeke495a0972004-06-24 21:22:08 +0000691 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000692 unsigned TmpReg = makeAnotherReg (newTy);
693 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
694 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
695 .addReg (SrcReg);
696 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
697 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000698 break;
699 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000700 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000701 return;
702
703 case cDouble:
Brian Gaeke495a0972004-06-24 21:22:08 +0000704 switch (oldTyClass) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000705 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000706 case cFloat:
707 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
708 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000709 case cDouble: // use double move pseudo-instr
710 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000711 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000712 default: {
713 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
714 unsigned TmpReg = makeAnotherReg (newTy);
715 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
716 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
717 .addReg (SrcReg);
718 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
719 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
720 break;
721 }
722 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000723 return;
724
725 case cLong:
726 switch (oldTyClass) {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000727 case cByte:
728 case cShort:
Brian Gaekea54df252004-11-19 18:48:10 +0000729 case cInt: {
730 // Cast to (u)int in the bottom half, and sign(zero) extend in the top
731 // half.
732 const Type *OldHalfTy = oldTy->isSigned() ? Type::IntTy : Type::UIntTy;
733 const Type *NewHalfTy = newTy->isSigned() ? Type::IntTy : Type::UIntTy;
734 unsigned TempReg = emitIntegerCast (BB, IP, OldHalfTy, SrcReg,
735 NewHalfTy, DestReg+1);
736 if (newTy->isSigned ()) {
737 BuildMI (*BB, IP, V8::SRAri, 2, DestReg).addReg (TempReg)
738 .addZImm (31);
739 } else {
740 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0)
741 .addReg (V8::G0);
742 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000743 break;
Brian Gaekea54df252004-11-19 18:48:10 +0000744 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000745 case cLong:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000746 // Just copy both halves.
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000747 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
748 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
749 .addReg (SrcReg+1);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000750 break;
751 default: goto not_yet;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000752 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000753 return;
754
755 default: goto not_yet;
Brian Gaekee302a7e2004-05-07 21:39:30 +0000756 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000757 return;
758not_yet:
759 std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy
760 << ", DestTy = " << *DestTy << "\n";
761 abort ();
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000762}
763
Brian Gaekef3334eb2004-04-07 17:29:37 +0000764void V8ISel::visitLoadInst(LoadInst &I) {
765 unsigned DestReg = getReg (I);
766 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000767 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000768 case cByte:
769 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000770 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000771 else
Brian Gaeke44733032004-06-24 07:36:48 +0000772 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000773 return;
774 case cShort:
775 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000776 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000777 else
Brian Gaeke44733032004-06-24 07:36:48 +0000778 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000779 return;
780 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000781 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000782 return;
783 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000784 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
785 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
786 return;
787 case cFloat:
788 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
789 return;
790 case cDouble:
791 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000792 return;
793 default:
794 std::cerr << "Load instruction not handled: " << I;
795 abort ();
796 return;
797 }
798}
799
800void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000801 Value *SrcVal = I.getOperand (0);
802 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000803 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000804 switch (getClassB (SrcVal->getType ())) {
805 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000806 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000807 return;
808 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000809 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000810 return;
811 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000812 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000813 return;
814 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000815 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
816 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
817 return;
818 case cFloat:
819 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
820 return;
821 case cDouble:
822 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000823 return;
824 default:
825 std::cerr << "Store instruction not handled: " << I;
826 abort ();
827 return;
828 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000829}
830
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000831void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000832 MachineInstr *TheCall;
833 // Is it an intrinsic function call?
834 if (Function *F = I.getCalledFunction()) {
835 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
836 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
837 return;
838 }
839 }
840
Brian Gaeke50094ed2004-10-10 19:57:18 +0000841 // How much extra call stack will we need?
Brian Gaeke79fe8332004-11-21 03:35:22 +0000842 int extraStack = 0;
843 for (unsigned i = 0; i < I.getNumOperands (); ++i) {
Brian Gaeke50094ed2004-10-10 19:57:18 +0000844 switch (getClassB (I.getOperand (i)->getType ())) {
845 case cLong: extraStack += 8; break;
846 case cFloat: extraStack += 4; break;
847 case cDouble: extraStack += 8; break;
848 default: extraStack += 4; break;
849 }
850 }
Brian Gaeke79fe8332004-11-21 03:35:22 +0000851 extraStack -= 24;
852 if (extraStack < 0) {
853 extraStack = 0;
854 } else {
855 // Round up extra stack size to the nearest doubleword.
856 extraStack = (extraStack + 7) & ~7;
857 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000858
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000859 // Deal with args
Brian Gaeke562cb162004-04-07 17:04:09 +0000860 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000861 V8::O4, V8::O5 };
Brian Gaeke24b90c32004-11-14 03:22:07 +0000862 const unsigned *OAREnd = &OutgoingArgRegs[6];
Brian Gaeke6931fd62004-11-04 00:27:04 +0000863 const unsigned *OAR = &OutgoingArgRegs[0];
Brian Gaeke24b90c32004-11-14 03:22:07 +0000864 unsigned ArgOffset = 68;
Brian Gaekeda9b3662004-11-14 06:32:08 +0000865 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000866 for (unsigned i = 1; i < I.getNumOperands (); ++i) {
867 unsigned ArgReg = getReg (I.getOperand (i));
Brian Gaeke24b90c32004-11-14 03:22:07 +0000868 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
869 // Schlep it over into the incoming arg register
870 if (ArgOffset < 92) {
871 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
872 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000873 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000874 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000875 }
Brian Gaeke24b90c32004-11-14 03:22:07 +0000876 ArgOffset += 4;
877 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
878 if (ArgOffset < 92) {
879 // Single-fp args are passed in integer registers; go through
880 // memory to get them out of FP registers. (Bleh!)
881 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
882 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
883 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
884 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
885 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
886 } else {
887 BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
888 }
889 ArgOffset += 4;
890 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
891 // Double-fp args are passed in pairs of integer registers; go through
892 // memory to get them out of FP registers. (Bleh!)
893 // We'd like to 'std' these right onto the outgoing-args area, but it might
894 // not be 8-byte aligned (e.g., call x(int x, double d)). sigh.
895 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
896 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
897 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
898 if (ArgOffset < 92 && OAR != OAREnd) {
899 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
900 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
901 } else {
902 unsigned TempReg = makeAnotherReg (Type::IntTy);
903 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
904 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
905 }
906 ArgOffset += 4;
907 if (ArgOffset < 92 && OAR != OAREnd) {
908 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
909 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (4);
910 } else {
911 unsigned TempReg = makeAnotherReg (Type::IntTy);
912 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
913 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
914 }
915 ArgOffset += 4;
916 } else if (getClassB (I.getOperand (i)->getType ()) == cLong) {
917 // do the first half...
918 if (ArgOffset < 92) {
919 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
920 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
921 } else {
922 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
923 }
924 ArgOffset += 4;
925 // ...then do the second half
926 if (ArgOffset < 92) {
927 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
928 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1);
929 } else {
930 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1);
931 }
932 ArgOffset += 4;
Brian Gaeke50094ed2004-10-10 19:57:18 +0000933 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000934 assert (0 && "Unknown class?!");
Brian Gaeked54c38b2004-04-07 16:41:22 +0000935 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000936 }
Brian Gaeked54c38b2004-04-07 16:41:22 +0000937
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000938 // Emit call instruction
939 if (Function *F = I.getCalledFunction ()) {
940 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
941 } else { // Emit an indirect call...
942 unsigned Reg = getReg (I.getCalledValue ());
943 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
944 }
945
Brian Gaeke50094ed2004-10-10 19:57:18 +0000946 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack);
947
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000948 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000949 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000950 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000951 unsigned DestReg = getReg (I);
Brian Gaeke299b39d2004-10-10 20:34:17 +0000952 switch (getClassB (I.getType ())) {
Brian Gaekeea8494b2004-04-06 22:09:23 +0000953 case cByte:
954 case cShort:
955 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000956 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
957 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000958 case cFloat:
959 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
960 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000961 case cDouble:
962 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
963 break;
964 case cLong:
965 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
966 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
967 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000968 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000969 std::cerr << "Return type of call instruction not handled: " << I;
970 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000971 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000972}
Chris Lattner1c809c52004-02-29 00:27:00 +0000973
974void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000975 if (I.getNumOperands () == 1) {
976 unsigned RetValReg = getReg (I.getOperand (0));
Brian Gaeke299b39d2004-10-10 20:34:17 +0000977 switch (getClassB (I.getOperand (0)->getType ())) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000978 case cByte:
979 case cShort:
980 case cInt:
981 // Schlep it over into i0 (where it will become o0 after restore).
982 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
983 break;
Brian Gaekef9a75462004-07-08 07:22:27 +0000984 case cFloat:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000985 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
Brian Gaekef9a75462004-07-08 07:22:27 +0000986 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000987 case cDouble:
988 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000989 break;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000990 case cLong:
991 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
992 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
993 break;
Brian Gaeke08f64c32004-03-06 05:32:28 +0000994 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000995 std::cerr << "Return instruction of this type not handled: " << I;
996 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000997 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000998 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000999
Brian Gaeke08f64c32004-03-06 05:32:28 +00001000 // Just emit a 'retl' instruction to return.
1001 BuildMI(BB, V8::RETL, 0);
1002 return;
Chris Lattner1c809c52004-02-29 00:27:00 +00001003}
1004
Brian Gaeke532e60c2004-05-08 04:21:17 +00001005static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1006 Function::iterator I = BB; ++I; // Get iterator to next block
1007 return I != BB->getParent()->end() ? &*I : 0;
1008}
1009
1010/// visitBranchInst - Handles conditional and unconditional branches.
1011///
1012void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +00001013 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +00001014 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
1015 BB->addSuccessor (takenSuccMBB);
1016 if (I.isConditional()) { // conditional branch
1017 BasicBlock *notTakenSucc = I.getSuccessor (1);
1018 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
1019 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001020
Brian Gaeke6c868a42004-06-17 22:34:08 +00001021 // CondReg=(<condition>);
1022 // If (CondReg==0) goto notTakenSuccMBB;
1023 unsigned CondReg = getReg (I.getCondition ());
1024 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
1025 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001026 }
Brian Gaeke6c868a42004-06-17 22:34:08 +00001027 // goto takenSuccMBB;
1028 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001029}
1030
1031/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
1032/// constant expression GEP support.
1033///
Brian Gaeke9f564822004-05-08 05:27:20 +00001034void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +00001035 MachineBasicBlock::iterator IP,
1036 Value *Src, User::op_iterator IdxBegin,
1037 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +00001038 const TargetData &TD = TM.getTargetData ();
1039 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001040 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +00001041
1042 // GEPs have zero or more indices; we must perform a struct access
1043 // or array access for each one.
1044 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
1045 ++oi) {
1046 Value *idx = *oi;
1047 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
1048 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
1049 // It's a struct access. idx is the index into the structure,
1050 // which names the field. Use the TargetData structure to
1051 // pick out what the layout of the structure is in memory.
1052 // Use the (constant) structure index's value to find the
1053 // right byte offset from the StructLayout class's list of
1054 // structure member offsets.
1055 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
1056 unsigned memberOffset =
1057 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
1058 // Emit an ADD to add memberOffset to the basePtr.
1059 BuildMI (*MBB, IP, V8::ADDri, 2,
1060 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
1061 // The next type is the member of the structure selected by the
1062 // index.
1063 Ty = StTy->getElementType (fieldIndex);
1064 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
1065 // It's an array or pointer access: [ArraySize x ElementType].
1066 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1067 // must find the size of the pointed-to type (Not coincidentally, the next
1068 // type is the type of the elements in the array).
1069 Ty = SqTy->getElementType ();
1070 unsigned elementSize = TD.getTypeSize (Ty);
1071 unsigned idxReg = getReg (idx, MBB, IP);
1072 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
1073 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001074 copyConstantToRegister (MBB, IP,
1075 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
Brian Gaeke9f564822004-05-08 05:27:20 +00001076 // Emit a SMUL to multiply the register holding the index by
1077 // elementSize, putting the result in OffsetReg.
1078 BuildMI (*MBB, IP, V8::SMULrr, 2,
1079 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
1080 // Emit an ADD to add OffsetReg to the basePtr.
1081 BuildMI (*MBB, IP, V8::ADDrr, 2,
1082 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1083 }
1084 basePtrReg = nextBasePtrReg;
1085 }
1086 // After we have processed all the indices, the result is left in
1087 // basePtrReg. Move it to the register where we were expected to
1088 // put the answer.
1089 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001090}
1091
1092void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
1093 unsigned outputReg = getReg (I);
1094 emitGEPOperation (BB, BB->end (), I.getOperand (0),
1095 I.op_begin ()+1, I.op_end (), outputReg);
1096}
1097
Brian Gaeke5f91de22004-11-21 07:13:16 +00001098void V8ISel::emitOp64LibraryCall (MachineBasicBlock *MBB,
1099 MachineBasicBlock::iterator IP,
1100 unsigned DestReg,
1101 const char *FuncName,
1102 unsigned Op0Reg, unsigned Op1Reg) {
1103 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O0).addReg (V8::G0).addReg (Op0Reg);
1104 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O1).addReg (V8::G0).addReg (Op0Reg+1);
1105 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O2).addReg (V8::G0).addReg (Op1Reg);
1106 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O3).addReg (V8::G0).addReg (Op1Reg+1);
1107 BuildMI (*MBB, IP, V8::CALL, 1).addExternalSymbol (FuncName, true);
1108 BuildMI (*MBB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (V8::O0);
1109 BuildMI (*MBB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0).addReg (V8::O1);
1110}
Brian Gaeked6a10532004-06-15 21:09:46 +00001111
Chris Lattner4be7ca52004-04-07 04:27:16 +00001112void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001113 unsigned DestReg = getReg (I);
1114 unsigned Op0Reg = getReg (I.getOperand (0));
1115 unsigned Op1Reg = getReg (I.getOperand (1));
1116
Brian Gaekeec3227f2004-06-27 22:47:33 +00001117 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +00001118 unsigned OpCase = ~0;
1119
Brian Gaekeec3227f2004-06-27 22:47:33 +00001120 if (Class > cLong) {
1121 switch (I.getOpcode ()) {
1122 case Instruction::Add: OpCase = 0; break;
1123 case Instruction::Sub: OpCase = 1; break;
1124 case Instruction::Mul: OpCase = 2; break;
1125 case Instruction::Div: OpCase = 3; break;
1126 default: visitInstruction (I); return;
1127 }
1128 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
1129 V8::FSUBS, V8::FSUBD,
1130 V8::FMULS, V8::FMULD,
1131 V8::FDIVS, V8::FDIVD };
1132 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
1133 .addReg (Op0Reg).addReg (Op1Reg);
1134 return;
1135 }
1136
1137 unsigned ResultReg = DestReg;
Brian Gaeke1df468e2004-09-29 03:34:41 +00001138 if (Class != cInt && Class != cLong)
Brian Gaekeec3227f2004-06-27 22:47:33 +00001139 ResultReg = makeAnotherReg (I.getType ());
1140
Brian Gaeke1df468e2004-09-29 03:34:41 +00001141 if (Class == cLong) {
Brian Gaeke5f91de22004-11-21 07:13:16 +00001142 const char *FuncName;
Brian Gaeke1df468e2004-09-29 03:34:41 +00001143 DEBUG (std::cerr << "Class = cLong\n");
1144 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
1145 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
1146 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
1147 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
Brian Gaeke5f91de22004-11-21 07:13:16 +00001148 switch (I.getOpcode ()) {
1149 case Instruction::Add:
1150 BuildMI (BB, V8::ADDCCrr, 2, ResultReg+1).addReg (Op0Reg+1)
1151 .addReg (Op1Reg+1);
1152 BuildMI (BB, V8::ADDXrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1153 return;
1154 case Instruction::Sub:
1155 BuildMI (BB, V8::SUBCCrr, 2, ResultReg+1).addReg (Op0Reg+1)
1156 .addReg (Op1Reg+1);
1157 BuildMI (BB, V8::SUBXrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1158 return;
1159 case Instruction::Mul:
1160 FuncName = I.getType ()->isSigned () ? "__mul64" : "__umul64";
1161 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1162 return;
1163 case Instruction::Div:
1164 FuncName = I.getType ()->isSigned () ? "__div64" : "__udiv64";
1165 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1166 return;
1167 case Instruction::Rem:
1168 FuncName = I.getType ()->isSigned () ? "__rem64" : "__urem64";
1169 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1170 return;
1171 }
Brian Gaeke1df468e2004-09-29 03:34:41 +00001172 }
1173
1174 // FIXME: support long, ulong.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001175 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +00001176 case Instruction::Add: OpCase = 0; break;
1177 case Instruction::Sub: OpCase = 1; break;
1178 case Instruction::Mul: OpCase = 2; break;
1179 case Instruction::And: OpCase = 3; break;
1180 case Instruction::Or: OpCase = 4; break;
1181 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +00001182 case Instruction::Shl: OpCase = 6; break;
1183 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +00001184
1185 case Instruction::Div:
1186 case Instruction::Rem: {
1187 unsigned Dest = ResultReg;
1188 if (I.getOpcode() == Instruction::Rem)
1189 Dest = makeAnotherReg(I.getType());
1190
1191 // FIXME: this is probably only right for 32 bit operands.
1192 if (I.getType ()->isSigned()) {
1193 unsigned Tmp = makeAnotherReg (I.getType ());
1194 // Sign extend into the Y register
1195 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
1196 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
1197 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
1198 } else {
1199 // Zero extend into the Y register, ie, just set it to zero
1200 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
1201 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +00001202 }
Chris Lattner22ede702004-04-07 04:06:46 +00001203
1204 if (I.getOpcode() == Instruction::Rem) {
1205 unsigned Tmp = makeAnotherReg (I.getType ());
1206 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
1207 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +00001208 }
Chris Lattner22ede702004-04-07 04:06:46 +00001209 break;
1210 }
1211 default:
1212 visitInstruction (I);
1213 return;
1214 }
1215
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001216 static const unsigned Opcodes[] = {
1217 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
1218 V8::SLLrr, V8::SRLrr, V8::SRArr
1219 };
Chris Lattner22ede702004-04-07 04:06:46 +00001220 if (OpCase != ~0U) {
Chris Lattner22ede702004-04-07 04:06:46 +00001221 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001222 }
1223
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001224 switch (getClassB (I.getType ())) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001225 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001226 if (I.getType ()->isSigned ()) { // add byte
1227 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1228 } else { // add ubyte
1229 unsigned TmpReg = makeAnotherReg (I.getType ());
1230 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1231 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1232 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001233 break;
1234 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001235 if (I.getType ()->isSigned ()) { // add short
1236 unsigned TmpReg = makeAnotherReg (I.getType ());
1237 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1238 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1239 } else { // add ushort
1240 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +00001241 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1242 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +00001243 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001244 break;
1245 case cInt:
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001246 // Nothing to do here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001247 break;
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001248 case cLong:
Brian Gaeke5f91de22004-11-21 07:13:16 +00001249 // Only support and, or, xor here - others taken care of above.
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001250 if (OpCase < 3 || OpCase > 5) {
1251 visitInstruction (I);
1252 return;
1253 }
1254 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +00001255 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1256 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001257 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001258 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001259 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001260 }
1261}
1262
Misha Brukmanea091262004-06-30 21:47:40 +00001263void V8ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner4d0cda42004-04-07 05:04:51 +00001264 unsigned Op0Reg = getReg (I.getOperand (0));
1265 unsigned Op1Reg = getReg (I.getOperand (1));
1266 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +00001267 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +00001268
1269 // Compare the two values.
Brian Gaeke3a085892004-07-08 09:08:35 +00001270 if (getClass (Ty) < cLong) {
1271 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
Brian Gaeke5f91de22004-11-21 07:13:16 +00001272 } else if (getClass (Ty) == cLong) {
Brian Gaekec7b4f102004-11-21 08:11:28 +00001273 switch (I.getOpcode()) {
1274 default: assert(0 && "Unknown setcc instruction!");
1275 case Instruction::SetEQ:
1276 case Instruction::SetNE: {
1277 unsigned TempReg0 = makeAnotherReg (Type::IntTy),
1278 TempReg1 = makeAnotherReg (Type::IntTy),
1279 TempReg2 = makeAnotherReg (Type::IntTy),
1280 TempReg3 = makeAnotherReg (Type::IntTy);
1281 MachineOpCode Opcode;
1282 int Immed;
1283 // These guys are special - no branches needed!
1284 BuildMI (BB, V8::XORrr, 2, TempReg0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1285 BuildMI (BB, V8::XORrr, 2, TempReg1).addReg (Op0Reg).addReg (Op1Reg);
1286 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (V8::G0).addReg (TempReg1);
1287 Opcode = I.getOpcode() == Instruction::SetEQ ? V8::SUBXri : V8::ADDXri;
1288 Immed = I.getOpcode() == Instruction::SetEQ ? -1 : 0;
1289 BuildMI (BB, Opcode, 2, TempReg2).addReg (V8::G0).addSImm (Immed);
1290 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (V8::G0).addReg (TempReg0);
1291 BuildMI (BB, Opcode, 2, TempReg3).addReg (V8::G0).addSImm (Immed);
1292 Opcode = I.getOpcode() == Instruction::SetEQ ? V8::ANDrr : V8::ORrr;
1293 BuildMI (BB, Opcode, 2, DestReg).addReg (TempReg2).addReg (TempReg3);
1294 return;
1295 }
1296 case Instruction::SetLT:
1297 case Instruction::SetGE:
1298 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1299 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg).addReg (Op1Reg);
1300 break;
1301 case Instruction::SetGT:
1302 case Instruction::SetLE:
1303 BuildMI (BB, V8::SUBCCri, 2, V8::G0).addReg (V8::G0).addSImm (1);
1304 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1305 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg).addReg (Op1Reg);
1306 break;
1307 }
Brian Gaeke3a085892004-07-08 09:08:35 +00001308 } else if (getClass (Ty) == cFloat) {
1309 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1310 } else if (getClass (Ty) == cDouble) {
1311 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1312 }
Chris Lattner4d0cda42004-04-07 05:04:51 +00001313
Brian Gaeke429022b2004-05-08 06:36:14 +00001314 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001315 switch (I.getOpcode()) {
1316 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +00001317 case Instruction::SetEQ: BranchIdx = 0; break;
1318 case Instruction::SetNE: BranchIdx = 1; break;
1319 case Instruction::SetLT: BranchIdx = 2; break;
1320 case Instruction::SetGT: BranchIdx = 3; break;
1321 case Instruction::SetLE: BranchIdx = 4; break;
1322 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001323 }
Brian Gaekec7b4f102004-11-21 08:11:28 +00001324
Brian Gaeke3a085892004-07-08 09:08:35 +00001325 unsigned Column = 0;
Brian Gaekeb3e00172004-11-17 22:06:56 +00001326 if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1;
1327 if (Ty->isFloatingPoint()) Column = 2;
Brian Gaeke3a085892004-07-08 09:08:35 +00001328 static unsigned OpcodeTab[3*6] = {
1329 // LLVM SparcV8
1330 // unsigned signed fp
1331 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1332 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1333 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1334 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1335 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1336 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
Brian Gaeke429022b2004-05-08 06:36:14 +00001337 };
Brian Gaeke3a085892004-07-08 09:08:35 +00001338 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
Brian Gaeke6c868a42004-06-17 22:34:08 +00001339
1340 MachineBasicBlock *thisMBB = BB;
1341 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1342 // thisMBB:
1343 // ...
1344 // subcc %reg0, %reg1, %g0
1345 // bCC copy1MBB
1346 // ba copy0MBB
1347
1348 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1349 // if we could insert other, non-terminator instructions after the
1350 // bCC. But MBB->getFirstTerminator() can't understand this.
1351 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1352 F->getBasicBlockList ().push_back (copy1MBB);
1353 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1354 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1355 F->getBasicBlockList ().push_back (copy0MBB);
1356 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1357 // Update machine-CFG edges
1358 BB->addSuccessor (copy1MBB);
1359 BB->addSuccessor (copy0MBB);
1360
1361 // copy0MBB:
1362 // %FalseValue = or %G0, 0
1363 // ba sinkMBB
1364 BB = copy0MBB;
1365 unsigned FalseValue = makeAnotherReg (I.getType ());
1366 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1367 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1368 F->getBasicBlockList ().push_back (sinkMBB);
1369 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1370 // Update machine-CFG edges
1371 BB->addSuccessor (sinkMBB);
1372
1373 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1374 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1375 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1376 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1377
1378 // copy1MBB:
1379 // %TrueValue = or %G0, 1
1380 // ba sinkMBB
1381 BB = copy1MBB;
1382 unsigned TrueValue = makeAnotherReg (I.getType ());
1383 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1384 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1385 // Update machine-CFG edges
1386 BB->addSuccessor (sinkMBB);
1387
1388 // sinkMBB:
1389 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1390 // ...
1391 BB = sinkMBB;
1392 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1393 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001394}
1395
Brian Gaekec93a7522004-06-18 05:19:16 +00001396void V8ISel::visitAllocaInst(AllocaInst &I) {
1397 // Find the data size of the alloca inst's getAllocatedType.
1398 const Type *Ty = I.getAllocatedType();
1399 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001400
Brian Gaekec93a7522004-06-18 05:19:16 +00001401 unsigned ArraySizeReg = getReg (I.getArraySize ());
1402 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1403 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1404 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1405 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001406
Brian Gaeke79fe8332004-11-21 03:35:22 +00001407 // StackAdjReg = (ArraySize * TySize) rounded up to nearest
1408 // doubleword boundary.
Brian Gaekec93a7522004-06-18 05:19:16 +00001409 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001410
Brian Gaekec93a7522004-06-18 05:19:16 +00001411 // Round up TmpReg1 to nearest doubleword boundary:
1412 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1413 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001414
1415 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001416 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001417
1418 // Put a pointer to the space into the result register, by copying
1419 // the stack pointer.
1420 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1421
1422 // Inform the Frame Information that we have just allocated a variable-sized
1423 // object.
1424 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001425}
Chris Lattner1c809c52004-02-29 00:27:00 +00001426
1427/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1428/// function, lowering any calls to unknown intrinsic functions into the
1429/// equivalent LLVM code.
1430void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1431 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1432 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1433 if (CallInst *CI = dyn_cast<CallInst>(I++))
1434 if (Function *F = CI->getCalledFunction())
1435 switch (F->getIntrinsicID()) {
Brian Gaeked90282d2004-11-19 20:57:24 +00001436 case Intrinsic::vastart:
1437 case Intrinsic::vacopy:
1438 case Intrinsic::vaend:
1439 // We directly implement these intrinsics
Chris Lattner1c809c52004-02-29 00:27:00 +00001440 case Intrinsic::not_intrinsic: break;
1441 default:
1442 // All other intrinsic calls we must lower.
1443 Instruction *Before = CI->getPrev();
1444 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1445 if (Before) { // Move iterator to instruction after call
1446 I = Before; ++I;
1447 } else {
1448 I = BB->begin();
1449 }
1450 }
1451}
1452
1453
1454void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattner1c809c52004-02-29 00:27:00 +00001455 switch (ID) {
Brian Gaeke9e672a22004-11-19 18:53:59 +00001456 default:
1457 std::cerr << "Sorry, unknown intrinsic function call:\n" << CI; abort ();
1458
Brian Gaeked90282d2004-11-19 20:57:24 +00001459 case Intrinsic::vastart: {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001460 // Add the VarArgsOffset to the frame pointer, and copy it to the result.
Brian Gaeked90282d2004-11-19 20:57:24 +00001461 unsigned DestReg = getReg (CI);
1462 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (V8::FP).addSImm (VarArgsOffset);
1463 return;
1464 }
Brian Gaeke9e672a22004-11-19 18:53:59 +00001465
1466 case Intrinsic::vaend:
Brian Gaeke2f95ed62004-11-19 19:21:34 +00001467 // va_end is a no-op on SparcV8.
1468 return;
Brian Gaeke9e672a22004-11-19 18:53:59 +00001469
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001470 case Intrinsic::vacopy: {
1471 // Copy the va_list ptr (arg1) to the result.
1472 unsigned DestReg = getReg (CI), SrcReg = getReg (CI.getOperand (1));
1473 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
1474 return;
1475 }
Chris Lattner1c809c52004-02-29 00:27:00 +00001476 }
1477}
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001478
1479void V8ISel::visitVANextInst (VANextInst &I) {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001480 // Add the type size to the vararg pointer (arg0).
1481 unsigned DestReg = getReg (I);
1482 unsigned SrcReg = getReg (I.getOperand (0));
1483 unsigned TySize = TM.getTargetData ().getTypeSize (I.getArgType ());
1484 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (SrcReg).addSImm (TySize);
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001485}
1486
1487void V8ISel::visitVAArgInst (VAArgInst &I) {
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001488 unsigned VAList = getReg (I.getOperand (0));
1489 unsigned DestReg = getReg (I);
1490
1491 switch (I.getType ()->getTypeID ()) {
1492 case Type::PointerTyID:
1493 case Type::UIntTyID:
1494 case Type::IntTyID:
1495 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1496 return;
1497
1498 case Type::ULongTyID:
1499 case Type::LongTyID:
1500 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1501 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (VAList).addSImm (4);
1502 return;
1503
Brian Gaeke79fe8332004-11-21 03:35:22 +00001504 case Type::DoubleTyID: {
1505 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
1506 unsigned TempReg = makeAnotherReg (Type::IntTy);
1507 unsigned TempReg2 = makeAnotherReg (Type::IntTy);
1508 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
1509 BuildMI (BB, V8::LD, 2, TempReg).addReg (VAList).addSImm (0);
1510 BuildMI (BB, V8::LD, 2, TempReg2).addReg (VAList).addSImm (4);
1511 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
1512 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg2);
1513 BuildMI (BB, V8::LDDFri, 2, DestReg).addFrameIndex (FI).addSImm (0);
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001514 return;
Brian Gaeke79fe8332004-11-21 03:35:22 +00001515 }
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001516
1517 default:
1518 std::cerr << "Sorry, vaarg instruction of this type still unsupported:\n"
1519 << I;
1520 abort ();
1521 return;
1522 }
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001523}