blob: 03415cc9fa837a6b4dd6457f84e43a27ca18899b [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);
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +000093 void emitShift64 (MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
94 Instruction &I, unsigned DestReg, unsigned Op0Reg,
95 unsigned Op1Reg);
Chris Lattner4be7ca52004-04-07 04:27:16 +000096 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000097 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000098 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000099 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000100 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000101 void visitBranchInst(BranchInst &I);
Chris Lattnerd14d5b42004-10-17 02:42:42 +0000102 void visitUnreachableInst(UnreachableInst &I) {}
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000103 void visitCastInst(CastInst &I);
Brian Gaekeb6c409a2004-11-19 21:08:18 +0000104 void visitVANextInst(VANextInst &I);
105 void visitVAArgInst(VAArgInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000106 void visitLoadInst(LoadInst &I);
107 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000108 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
109 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +0000110 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000111
Chris Lattner1c809c52004-02-29 00:27:00 +0000112 void visitInstruction(Instruction &I) {
113 std::cerr << "Unhandled instruction: " << I;
114 abort();
115 }
116
117 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
118 /// function, lowering any calls to unknown intrinsic functions into the
119 /// equivalent LLVM code.
120 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +0000121 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
122
Brian Gaeke562cb162004-04-07 17:04:09 +0000123 void LoadArgumentsToVirtualRegs(Function *F);
124
Brian Gaeke6c868a42004-06-17 22:34:08 +0000125 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
126 /// because we have to generate our sources into the source basic blocks,
127 /// not the current one.
128 ///
129 void SelectPHINodes();
130
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000131 /// copyConstantToRegister - Output the instructions required to put the
132 /// specified constant into the specified register.
133 ///
134 void copyConstantToRegister(MachineBasicBlock *MBB,
135 MachineBasicBlock::iterator IP,
136 Constant *C, unsigned R);
137
138 /// makeAnotherReg - This method returns the next register number we haven't
139 /// yet used.
140 ///
141 /// Long values are handled somewhat specially. They are always allocated
142 /// as pairs of 32 bit integer values. The register number returned is the
143 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
144 /// of the long value.
145 ///
146 unsigned makeAnotherReg(const Type *Ty) {
147 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
148 "Current target doesn't have SparcV8 reg info??");
149 const SparcV8RegisterInfo *MRI =
150 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
151 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
152 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
153 // Create the lower part
154 F->getSSARegMap()->createVirtualRegister(RC);
155 // Create the upper part.
156 return F->getSSARegMap()->createVirtualRegister(RC)-1;
157 }
158
159 // Add the mapping of regnumber => reg class to MachineFunction
160 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
161 return F->getSSARegMap()->createVirtualRegister(RC);
162 }
163
164 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
165 unsigned getReg(Value *V) {
166 // Just append to the end of the current bb.
167 MachineBasicBlock::iterator It = BB->end();
168 return getReg(V, BB, It);
169 }
170 unsigned getReg(Value *V, MachineBasicBlock *MBB,
171 MachineBasicBlock::iterator IPt) {
172 unsigned &Reg = RegMap[V];
173 if (Reg == 0) {
174 Reg = makeAnotherReg(V->getType());
175 RegMap[V] = Reg;
176 }
177 // If this operand is a constant, emit the code to copy the constant into
178 // the register here...
179 //
180 if (Constant *C = dyn_cast<Constant>(V)) {
181 copyConstantToRegister(MBB, IPt, C, Reg);
182 RegMap.erase(V); // Assign a new name to this constant if ref'd again
183 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
184 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000185 unsigned TmpReg = makeAnotherReg(V->getType());
186 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
187 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
188 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000189 RegMap.erase(V); // Assign a new name to this address if ref'd again
190 }
191
192 return Reg;
193 }
194
Chris Lattner1c809c52004-02-29 00:27:00 +0000195 };
196}
197
198FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
199 return new V8ISel(TM);
200}
201
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000202enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000203 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000204};
205
206static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000207 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000208 case Type::UByteTyID: case Type::SByteTyID: return cByte;
209 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000210 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000211 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000212 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000213 case Type::FloatTyID: return cFloat;
214 case Type::DoubleTyID: return cDouble;
215 default:
216 assert (0 && "Type of unknown class passed to getClass?");
217 return cByte;
218 }
219}
Brian Gaeke50094ed2004-10-10 19:57:18 +0000220
Chris Lattner0d538bb2004-04-07 04:36:53 +0000221static TypeClass getClassB(const Type *T) {
222 if (T == Type::BoolTy) return cByte;
223 return getClass(T);
224}
225
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000226/// copyConstantToRegister - Output the instructions required to put the
227/// specified constant into the specified register.
228///
229void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
230 MachineBasicBlock::iterator IP,
231 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000232 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
233 switch (CE->getOpcode()) {
234 case Instruction::GetElementPtr:
235 emitGEPOperation(MBB, IP, CE->getOperand(0),
236 CE->op_begin()+1, CE->op_end(), R);
237 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000238 case Instruction::Cast:
239 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
240 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000241 default:
242 std::cerr << "Copying this constant expr not yet handled: " << *CE;
243 abort();
244 }
Chris Lattnerd14d5b42004-10-17 02:42:42 +0000245 } else if (isa<UndefValue>(C)) {
246 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R);
247 if (getClassB (C->getType ()) == cLong)
248 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1);
249 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000250 }
251
Brian Gaekee302a7e2004-05-07 21:39:30 +0000252 if (C->getType()->isIntegral ()) {
253 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000254 unsigned Class = getClassB (C->getType ());
255 if (Class == cLong) {
256 unsigned TmpReg = makeAnotherReg (Type::IntTy);
257 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
258 // Copy the value into the register pair.
259 // R = top(more-significant) half, R+1 = bottom(less-significant) half
260 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000261 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
262 Val >> 32), R);
263 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
264 Val & 0xffffffffU), R+1);
Brian Gaeke9df92822004-06-15 19:16:07 +0000265 return;
266 }
267
268 assert(Class <= cInt && "Type not handled yet!");
269
Brian Gaekee302a7e2004-05-07 21:39:30 +0000270 if (C->getType() == Type::BoolTy) {
271 Val = (C == ConstantBool::True);
272 } else {
Brian Gaekef731be02004-12-12 07:42:58 +0000273 ConstantIntegral *CI = cast<ConstantIntegral> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000274 Val = CI->getRawValue ();
275 }
Brian Gaekef731be02004-12-12 07:42:58 +0000276 if (C->getType()->isSigned()) {
277 switch (Class) {
278 case cByte: Val = (int8_t) Val; break;
279 case cShort: Val = (int16_t) Val; break;
280 case cInt: Val = (int32_t) Val; break;
281 }
282 } else {
283 switch (Class) {
284 case cByte: Val = (uint8_t) Val; break;
285 case cShort: Val = (uint16_t) Val; break;
286 case cInt: Val = (uint32_t) Val; break;
287 }
Brian Gaekee8061732004-03-04 00:56:25 +0000288 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000289 if (Val == 0) {
290 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
291 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
292 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
293 } else {
294 unsigned TmpReg = makeAnotherReg (C->getType ());
295 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
296 .addSImm (((uint32_t) Val) >> 10);
297 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
298 .addSImm (((uint32_t) Val) & 0x03ff);
299 return;
300 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000301 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
302 // We need to spill the constant to memory...
303 MachineConstantPool *CP = F->getConstantPool();
304 unsigned CPI = CP->getConstantPoolIndex(CFP);
305 const Type *Ty = CFP->getType();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000306 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
307 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +0000308
309 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000310 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000311 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000312 BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg)
313 .addConstantPoolIndex (CPI);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000314 BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000315 } else if (isa<ConstantPointerNull>(C)) {
316 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000317 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Chris Lattner73302482004-07-18 07:26:17 +0000318 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000319 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
320 // that SETHI %reg,global == SETHI %reg,%hi(global) and
321 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
322 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner73302482004-07-18 07:26:17 +0000323 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
324 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
Brian Gaeke9df92822004-06-15 19:16:07 +0000325 } else {
326 std::cerr << "Offending constant: " << *C << "\n";
327 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000328 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000329}
Chris Lattner1c809c52004-02-29 00:27:00 +0000330
Brian Gaeke812c4882004-07-16 10:31:25 +0000331void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
Brian Gaeke562cb162004-04-07 17:04:09 +0000332 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
333 V8::I3, V8::I4, V8::I5 };
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000334
Brian Gaeke812c4882004-07-16 10:31:25 +0000335 // Add IMPLICIT_DEFs of input regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000336 unsigned ArgNo = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000337 for (Function::aiterator I = LF->abegin(), E = LF->aend();
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000338 I != E && ArgNo < 6; ++I, ++ArgNo) {
Brian Gaeke812c4882004-07-16 10:31:25 +0000339 switch (getClassB(I->getType())) {
340 case cByte:
341 case cShort:
342 case cInt:
343 case cFloat:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000344 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke812c4882004-07-16 10:31:25 +0000345 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000346 case cDouble:
347 case cLong:
348 // Double and Long use register pairs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000349 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
350 ++ArgNo;
351 if (ArgNo < 6)
352 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000353 break;
Brian Gaeke812c4882004-07-16 10:31:25 +0000354 default:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000355 assert (0 && "type not handled");
Brian Gaeke812c4882004-07-16 10:31:25 +0000356 return;
357 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000358 }
359
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000360 const unsigned *IAREnd = &IncomingArgRegs[6];
361 const unsigned *IAR = &IncomingArgRegs[0];
362 unsigned ArgOffset = 68;
Brian Gaeke4e459c42004-11-19 20:31:08 +0000363
364 // Store registers onto stack if this is a varargs function.
365 // FIXME: This doesn't really pertain to "loading arguments into
366 // virtual registers", so it's not clear that it really belongs here.
367 // FIXME: We could avoid storing any args onto the stack that don't
368 // need to be in memory, because they come before the ellipsis in the
369 // parameter list (and thus could never be accessed through va_arg).
370 if (LF->getFunctionType ()->isVarArg ()) {
371 for (unsigned i = 0; i < 6; ++i) {
372 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
373 assert (IAR != IAREnd
374 && "About to dereference past end of IncomingArgRegs");
375 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
376 ArgOffset += 4;
377 }
378 // Reset the pointers now that we're done.
379 ArgOffset = 68;
380 IAR = &IncomingArgRegs[0];
381 }
382
383 // Copy args out of their incoming hard regs or stack slots into virtual regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000384 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; ++I) {
385 Argument &A = *I;
386 unsigned ArgReg = getReg (A);
387 if (getClassB (A.getType ()) < cLong) {
388 // Get it out of the incoming arg register
389 if (ArgOffset < 92) {
390 assert (IAR != IAREnd
391 && "About to dereference past end of IncomingArgRegs");
392 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
393 } else {
394 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
395 BuildMI (BB, V8::LD, 3, ArgReg).addFrameIndex (FI).addSImm (0);
396 }
397 ArgOffset += 4;
398 } else if (getClassB (A.getType ()) == cFloat) {
399 if (ArgOffset < 92) {
Brian Gaeke1df468e2004-09-29 03:34:41 +0000400 // Single-fp args are passed in integer registers; go through
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000401 // memory to get them out of integer registers and back into fp. (Bleh!)
Brian Gaeke1df468e2004-09-29 03:34:41 +0000402 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
403 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000404 assert (IAR != IAREnd
405 && "About to dereference past end of IncomingArgRegs");
406 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
407 BuildMI (BB, V8::LDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
408 } else {
409 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
410 BuildMI (BB, V8::LDFri, 3, ArgReg).addFrameIndex (FI).addSImm (0);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000411 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000412 ArgOffset += 4;
413 } else if (getClassB (A.getType ()) == cDouble) {
414 // Double-fp args are passed in pairs of integer registers; go through
415 // memory to get them out of integer registers and back into fp. (Bleh!)
416 // We'd like to 'ldd' these right out of the incoming-args area,
417 // but it might not be 8-byte aligned (e.g., call x(int x, double d)).
418 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
419 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
420 if (ArgOffset < 92 && IAR != IAREnd) {
421 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
422 } else {
423 unsigned TempReg = makeAnotherReg (Type::IntTy);
424 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
425 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
Brian Gaeke6672f862004-09-30 19:44:32 +0000426 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000427 ArgOffset += 4;
428 if (ArgOffset < 92 && IAR != IAREnd) {
429 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (*IAR++);
430 } else {
431 unsigned TempReg = makeAnotherReg (Type::IntTy);
432 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
433 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000434 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000435 ArgOffset += 4;
436 BuildMI (BB, V8::LDDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
437 } else if (getClassB (A.getType ()) == cLong) {
438 // do the first half...
439 if (ArgOffset < 92) {
440 assert (IAR != IAREnd
441 && "About to dereference past end of IncomingArgRegs");
442 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
443 } else {
444 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
445 BuildMI (BB, V8::LD, 2, ArgReg).addFrameIndex (FI).addSImm (0);
446 }
447 ArgOffset += 4;
448 // ...then do the second half
449 if (ArgOffset < 92) {
450 assert (IAR != IAREnd
451 && "About to dereference past end of IncomingArgRegs");
452 BuildMI (BB, V8::ORrr, 2, ArgReg+1).addReg (V8::G0).addReg (*IAR++);
453 } else {
454 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
455 BuildMI (BB, V8::LD, 2, ArgReg+1).addFrameIndex (FI).addSImm (0);
456 }
457 ArgOffset += 4;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000458 } else {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000459 assert (0 && "Unknown class?!");
Brian Gaeke812c4882004-07-16 10:31:25 +0000460 }
Brian Gaeke562cb162004-04-07 17:04:09 +0000461 }
Brian Gaeked90282d2004-11-19 20:57:24 +0000462
463 // If the function takes variable number of arguments, remember the fp
464 // offset for the start of the first vararg value... this is used to expand
465 // llvm.va_start.
466 if (LF->getFunctionType ()->isVarArg ())
467 VarArgsOffset = ArgOffset;
Brian Gaeke562cb162004-04-07 17:04:09 +0000468}
469
Brian Gaeke6c868a42004-06-17 22:34:08 +0000470void V8ISel::SelectPHINodes() {
471 const TargetInstrInfo &TII = *TM.getInstrInfo();
472 const Function &LF = *F->getFunction(); // The LLVM function...
473 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
474 const BasicBlock *BB = I;
475 MachineBasicBlock &MBB = *MBBMap[I];
476
477 // Loop over all of the PHI nodes in the LLVM basic block...
478 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
479 for (BasicBlock::const_iterator I = BB->begin();
480 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
481
482 // Create a new machine instr PHI node, and insert it.
483 unsigned PHIReg = getReg(*PN);
484 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
485 V8::PHI, PN->getNumOperands(), PHIReg);
486
487 MachineInstr *LongPhiMI = 0;
488 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
489 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
490 V8::PHI, PN->getNumOperands(), PHIReg+1);
491
492 // PHIValues - Map of blocks to incoming virtual registers. We use this
493 // so that we only initialize one incoming value for a particular block,
494 // even if the block has multiple entries in the PHI node.
495 //
496 std::map<MachineBasicBlock*, unsigned> PHIValues;
497
498 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
499 MachineBasicBlock *PredMBB = 0;
500 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
501 PE = MBB.pred_end (); PI != PE; ++PI)
502 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
503 PredMBB = *PI;
504 break;
505 }
506 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
507
508 unsigned ValReg;
509 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
510 PHIValues.lower_bound(PredMBB);
511
512 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
513 // We already inserted an initialization of the register for this
514 // predecessor. Recycle it.
515 ValReg = EntryIt->second;
516
517 } else {
518 // Get the incoming value into a virtual register.
519 //
520 Value *Val = PN->getIncomingValue(i);
521
522 // If this is a constant or GlobalValue, we may have to insert code
523 // into the basic block to compute it into a virtual register.
524 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
525 isa<GlobalValue>(Val)) {
526 // Simple constants get emitted at the end of the basic block,
527 // before any terminator instructions. We "know" that the code to
528 // move a constant into a register will never clobber any flags.
529 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
530 } else {
531 // Because we don't want to clobber any values which might be in
532 // physical registers with the computation of this constant (which
533 // might be arbitrarily complex if it is a constant expression),
534 // just insert the computation at the top of the basic block.
535 MachineBasicBlock::iterator PI = PredMBB->begin();
536
537 // Skip over any PHI nodes though!
538 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
539 ++PI;
540
541 ValReg = getReg(Val, PredMBB, PI);
542 }
543
544 // Remember that we inserted a value for this PHI for this predecessor
545 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
546 }
547
548 PhiMI->addRegOperand(ValReg);
549 PhiMI->addMachineBasicBlockOperand(PredMBB);
550 if (LongPhiMI) {
551 LongPhiMI->addRegOperand(ValReg+1);
552 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
553 }
554 }
555
556 // Now that we emitted all of the incoming values for the PHI node, make
557 // sure to reposition the InsertPoint after the PHI that we just added.
558 // This is needed because we might have inserted a constant into this
559 // block, right after the PHI's which is before the old insert point!
560 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
561 ++PHIInsertPoint;
562 }
563 }
564}
565
Chris Lattner1c809c52004-02-29 00:27:00 +0000566bool V8ISel::runOnFunction(Function &Fn) {
567 // First pass over the function, lower any unknown intrinsic functions
568 // with the IntrinsicLowering class.
569 LowerUnknownIntrinsicFunctionCalls(Fn);
570
571 F = &MachineFunction::construct(&Fn, TM);
572
573 // Create all of the machine basic blocks for the function...
574 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
575 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
576
577 BB = &F->front();
578
579 // Set up a frame object for the return address. This is used by the
580 // llvm.returnaddress & llvm.frameaddress intrinisics.
581 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
582
583 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000584 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000585
586 // Instruction select everything except PHI nodes
587 visit(Fn);
588
589 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000590 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000591
592 RegMap.clear();
593 MBBMap.clear();
594 F = 0;
595 // We always build a machine code representation for the function
596 return true;
597}
598
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000599void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000600 Value *Op = I.getOperand(0);
601 unsigned DestReg = getReg(I);
602 MachineBasicBlock::iterator MI = BB->end();
603 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
604}
605
Brian Gaekea54df252004-11-19 18:48:10 +0000606unsigned V8ISel::emitIntegerCast (MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000607 MachineBasicBlock::iterator IP, const Type *oldTy,
608 unsigned SrcReg, const Type *newTy,
609 unsigned DestReg) {
610 if (oldTy == newTy) {
611 // No-op cast - just emit a copy; assume the reg. allocator will zap it.
612 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg);
Brian Gaekea54df252004-11-19 18:48:10 +0000613 return SrcReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000614 }
615 // Emit left-shift, then right-shift to sign- or zero-extend.
616 unsigned TmpReg = makeAnotherReg (newTy);
617 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
618 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg);
619 if (newTy->isSigned ()) { // sign-extend with SRA
620 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
621 } else { // zero-extend with SRL
622 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
623 }
Brian Gaekea54df252004-11-19 18:48:10 +0000624 // Return the temp reg. in case this is one half of a cast to long.
625 return TmpReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000626}
627
628void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB,
629 MachineBasicBlock::iterator IP,
630 const Type *oldTy, unsigned SrcReg,
631 const Type *newTy, unsigned DestReg) {
632 unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign;
633 unsigned oldTyClass = getClassB(oldTy);
634 if (oldTyClass == cFloat) {
635 FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4;
636 FPAlign = TM.getTargetData().getFloatAlignment();
637 } else { // it's a double
638 FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8;
639 FPAlign = TM.getTargetData().getDoubleAlignment();
640 }
641 unsigned TempReg = makeAnotherReg (oldTy);
642 BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg);
643 int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign);
644 BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0)
645 .addReg (TempReg);
646 unsigned TempReg2 = makeAnotherReg (newTy);
647 BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0);
648 emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg);
649}
650
Brian Gaeke00e514e2004-06-24 06:33:00 +0000651/// emitCastOperation - Common code shared between visitCastInst and constant
652/// expression cast support.
653///
654void V8ISel::emitCastOperation(MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000655 MachineBasicBlock::iterator IP, Value *Src,
656 const Type *DestTy, unsigned DestReg) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000657 const Type *SrcTy = Src->getType();
658 unsigned SrcClass = getClassB(SrcTy);
659 unsigned DestClass = getClassB(DestTy);
660 unsigned SrcReg = getReg(Src, BB, IP);
661
662 const Type *oldTy = SrcTy;
663 const Type *newTy = DestTy;
664 unsigned oldTyClass = SrcClass;
665 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000666
Brian Gaeke429022b2004-05-08 06:36:14 +0000667 if (oldTyClass < cLong && newTyClass < cLong) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000668 emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
669 } else switch (newTyClass) {
670 case cByte:
671 case cShort:
672 case cInt:
Brian Gaeke495a0972004-06-24 21:22:08 +0000673 switch (oldTyClass) {
Brian Gaekea54df252004-11-19 18:48:10 +0000674 case cLong:
675 // Treat it like a cast from the lower half of the value.
676 emitIntegerCast (BB, IP, Type::IntTy, SrcReg+1, newTy, DestReg);
677 break;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000678 case cFloat:
679 case cDouble:
680 emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
681 break;
682 default: goto not_yet;
683 }
684 return;
685
686 case cFloat:
687 switch (oldTyClass) {
688 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000689 case cFloat:
690 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
691 break;
692 case cDouble:
693 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
694 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000695 default: {
696 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000697 // cast integer type to float. Store it to a stack slot and then load
Brian Gaeke495a0972004-06-24 21:22:08 +0000698 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000699 unsigned TmpReg = makeAnotherReg (newTy);
700 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
701 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
702 .addReg (SrcReg);
703 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
704 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000705 break;
706 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000707 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000708 return;
709
710 case cDouble:
Brian Gaeke495a0972004-06-24 21:22:08 +0000711 switch (oldTyClass) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000712 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000713 case cFloat:
714 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
715 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000716 case cDouble: // use double move pseudo-instr
717 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000718 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000719 default: {
720 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
721 unsigned TmpReg = makeAnotherReg (newTy);
722 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
723 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
724 .addReg (SrcReg);
725 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
726 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
727 break;
728 }
729 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000730 return;
731
732 case cLong:
733 switch (oldTyClass) {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000734 case cByte:
735 case cShort:
Brian Gaekea54df252004-11-19 18:48:10 +0000736 case cInt: {
737 // Cast to (u)int in the bottom half, and sign(zero) extend in the top
738 // half.
739 const Type *OldHalfTy = oldTy->isSigned() ? Type::IntTy : Type::UIntTy;
740 const Type *NewHalfTy = newTy->isSigned() ? Type::IntTy : Type::UIntTy;
741 unsigned TempReg = emitIntegerCast (BB, IP, OldHalfTy, SrcReg,
742 NewHalfTy, DestReg+1);
743 if (newTy->isSigned ()) {
744 BuildMI (*BB, IP, V8::SRAri, 2, DestReg).addReg (TempReg)
745 .addZImm (31);
746 } else {
747 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0)
748 .addReg (V8::G0);
749 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000750 break;
Brian Gaekea54df252004-11-19 18:48:10 +0000751 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000752 case cLong:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000753 // Just copy both halves.
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000754 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
755 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
756 .addReg (SrcReg+1);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000757 break;
758 default: goto not_yet;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000759 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000760 return;
761
762 default: goto not_yet;
Brian Gaekee302a7e2004-05-07 21:39:30 +0000763 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000764 return;
765not_yet:
766 std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy
767 << ", DestTy = " << *DestTy << "\n";
768 abort ();
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000769}
770
Brian Gaekef3334eb2004-04-07 17:29:37 +0000771void V8ISel::visitLoadInst(LoadInst &I) {
772 unsigned DestReg = getReg (I);
773 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000774 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000775 case cByte:
776 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000777 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000778 else
Brian Gaeke44733032004-06-24 07:36:48 +0000779 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000780 return;
781 case cShort:
782 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000783 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000784 else
Brian Gaeke44733032004-06-24 07:36:48 +0000785 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000786 return;
787 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000788 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000789 return;
790 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000791 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
792 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
793 return;
794 case cFloat:
795 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
796 return;
797 case cDouble:
798 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000799 return;
800 default:
801 std::cerr << "Load instruction not handled: " << I;
802 abort ();
803 return;
804 }
805}
806
807void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000808 Value *SrcVal = I.getOperand (0);
809 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000810 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000811 switch (getClassB (SrcVal->getType ())) {
812 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000813 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000814 return;
815 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000816 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000817 return;
818 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000819 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000820 return;
821 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000822 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
823 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
824 return;
825 case cFloat:
826 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
827 return;
828 case cDouble:
829 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000830 return;
831 default:
832 std::cerr << "Store instruction not handled: " << I;
833 abort ();
834 return;
835 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000836}
837
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000838void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000839 MachineInstr *TheCall;
840 // Is it an intrinsic function call?
841 if (Function *F = I.getCalledFunction()) {
842 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
843 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
844 return;
845 }
846 }
847
Brian Gaeke50094ed2004-10-10 19:57:18 +0000848 // How much extra call stack will we need?
Brian Gaeke79fe8332004-11-21 03:35:22 +0000849 int extraStack = 0;
850 for (unsigned i = 0; i < I.getNumOperands (); ++i) {
Brian Gaeke50094ed2004-10-10 19:57:18 +0000851 switch (getClassB (I.getOperand (i)->getType ())) {
852 case cLong: extraStack += 8; break;
853 case cFloat: extraStack += 4; break;
854 case cDouble: extraStack += 8; break;
855 default: extraStack += 4; break;
856 }
857 }
Brian Gaeke79fe8332004-11-21 03:35:22 +0000858 extraStack -= 24;
859 if (extraStack < 0) {
860 extraStack = 0;
861 } else {
862 // Round up extra stack size to the nearest doubleword.
863 extraStack = (extraStack + 7) & ~7;
864 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000865
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000866 // Deal with args
Brian Gaeke562cb162004-04-07 17:04:09 +0000867 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000868 V8::O4, V8::O5 };
Brian Gaeke24b90c32004-11-14 03:22:07 +0000869 const unsigned *OAREnd = &OutgoingArgRegs[6];
Brian Gaeke6931fd62004-11-04 00:27:04 +0000870 const unsigned *OAR = &OutgoingArgRegs[0];
Brian Gaeke24b90c32004-11-14 03:22:07 +0000871 unsigned ArgOffset = 68;
Brian Gaekeda9b3662004-11-14 06:32:08 +0000872 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000873 for (unsigned i = 1; i < I.getNumOperands (); ++i) {
874 unsigned ArgReg = getReg (I.getOperand (i));
Brian Gaeke24b90c32004-11-14 03:22:07 +0000875 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
876 // Schlep it over into the incoming arg register
877 if (ArgOffset < 92) {
878 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
879 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000880 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000881 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000882 }
Brian Gaeke24b90c32004-11-14 03:22:07 +0000883 ArgOffset += 4;
884 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
885 if (ArgOffset < 92) {
886 // Single-fp args are passed in integer registers; go through
887 // memory to get them out of FP registers. (Bleh!)
888 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
889 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
890 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
891 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
892 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
893 } else {
894 BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
895 }
896 ArgOffset += 4;
897 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
898 // Double-fp args are passed in pairs of integer registers; go through
899 // memory to get them out of FP registers. (Bleh!)
900 // We'd like to 'std' these right onto the outgoing-args area, but it might
901 // not be 8-byte aligned (e.g., call x(int x, double d)). sigh.
902 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
903 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
904 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
905 if (ArgOffset < 92 && OAR != OAREnd) {
906 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
907 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
908 } else {
909 unsigned TempReg = makeAnotherReg (Type::IntTy);
910 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
911 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
912 }
913 ArgOffset += 4;
914 if (ArgOffset < 92 && OAR != OAREnd) {
915 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
916 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (4);
917 } else {
918 unsigned TempReg = makeAnotherReg (Type::IntTy);
919 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
920 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
921 }
922 ArgOffset += 4;
923 } else if (getClassB (I.getOperand (i)->getType ()) == cLong) {
924 // do the first half...
925 if (ArgOffset < 92) {
926 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
927 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
928 } else {
929 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
930 }
931 ArgOffset += 4;
932 // ...then do the second half
933 if (ArgOffset < 92) {
934 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
935 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1);
936 } else {
937 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1);
938 }
939 ArgOffset += 4;
Brian Gaeke50094ed2004-10-10 19:57:18 +0000940 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000941 assert (0 && "Unknown class?!");
Brian Gaeked54c38b2004-04-07 16:41:22 +0000942 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000943 }
Brian Gaeked54c38b2004-04-07 16:41:22 +0000944
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000945 // Emit call instruction
946 if (Function *F = I.getCalledFunction ()) {
947 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
948 } else { // Emit an indirect call...
949 unsigned Reg = getReg (I.getCalledValue ());
950 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
951 }
952
Brian Gaeke50094ed2004-10-10 19:57:18 +0000953 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack);
954
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000955 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000956 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000957 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000958 unsigned DestReg = getReg (I);
Brian Gaeke299b39d2004-10-10 20:34:17 +0000959 switch (getClassB (I.getType ())) {
Brian Gaekeea8494b2004-04-06 22:09:23 +0000960 case cByte:
961 case cShort:
962 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000963 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
964 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000965 case cFloat:
966 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
967 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000968 case cDouble:
969 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
970 break;
971 case cLong:
972 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
973 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
974 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000975 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000976 std::cerr << "Return type of call instruction not handled: " << I;
977 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000978 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000979}
Chris Lattner1c809c52004-02-29 00:27:00 +0000980
981void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000982 if (I.getNumOperands () == 1) {
983 unsigned RetValReg = getReg (I.getOperand (0));
Brian Gaeke299b39d2004-10-10 20:34:17 +0000984 switch (getClassB (I.getOperand (0)->getType ())) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000985 case cByte:
986 case cShort:
987 case cInt:
988 // Schlep it over into i0 (where it will become o0 after restore).
989 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
990 break;
Brian Gaekef9a75462004-07-08 07:22:27 +0000991 case cFloat:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000992 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
Brian Gaekef9a75462004-07-08 07:22:27 +0000993 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000994 case cDouble:
995 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000996 break;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000997 case cLong:
998 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
999 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
1000 break;
Brian Gaeke08f64c32004-03-06 05:32:28 +00001001 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +00001002 std::cerr << "Return instruction of this type not handled: " << I;
1003 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +00001004 }
Chris Lattner1c809c52004-02-29 00:27:00 +00001005 }
Chris Lattner0d538bb2004-04-07 04:36:53 +00001006
Brian Gaeke08f64c32004-03-06 05:32:28 +00001007 // Just emit a 'retl' instruction to return.
1008 BuildMI(BB, V8::RETL, 0);
1009 return;
Chris Lattner1c809c52004-02-29 00:27:00 +00001010}
1011
Brian Gaeke532e60c2004-05-08 04:21:17 +00001012static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1013 Function::iterator I = BB; ++I; // Get iterator to next block
1014 return I != BB->getParent()->end() ? &*I : 0;
1015}
1016
Brian Gaeke6a8c46c2004-12-12 06:01:26 +00001017/// canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it
1018/// into the conditional branch which is the only user of the cc instruction.
1019/// This is the case if the conditional branch is the only user of the setcc.
1020///
1021static SetCondInst *canFoldSetCCIntoBranch(Value *V) {
Brian Gaekef731be02004-12-12 07:42:58 +00001022 //return 0; // disable.
Brian Gaeke81cf1502004-12-12 06:22:30 +00001023 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
1024 if (SCI->hasOneUse()) {
1025 BranchInst *User = dyn_cast<BranchInst>(SCI->use_back());
1026 if (User
1027 && (SCI->getNext() == User)
1028 && (getClassB(SCI->getOperand(0)->getType()) != cLong)
1029 && User->isConditional() && (User->getCondition() == V))
1030 return SCI;
1031 }
1032 return 0;
Brian Gaeke6a8c46c2004-12-12 06:01:26 +00001033}
1034
Brian Gaeke532e60c2004-05-08 04:21:17 +00001035/// visitBranchInst - Handles conditional and unconditional branches.
1036///
1037void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +00001038 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +00001039 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
1040 BB->addSuccessor (takenSuccMBB);
1041 if (I.isConditional()) { // conditional branch
1042 BasicBlock *notTakenSucc = I.getSuccessor (1);
1043 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
1044 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001045
Brian Gaekef731be02004-12-12 07:42:58 +00001046 // See if we can fold a previous setcc instr into this branch.
1047 SetCondInst *SCI = canFoldSetCCIntoBranch(I.getCondition());
1048 if (SCI == 0) {
1049 // The condition did not come from a setcc which we could fold.
1050 // CondReg=(<condition>);
1051 // If (CondReg==0) goto notTakenSuccMBB;
1052 unsigned CondReg = getReg (I.getCondition ());
1053 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
1054 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
1055 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
1056 return;
1057 }
1058
1059 // Fold the setCC instr into the branch.
1060 unsigned Op0Reg = getReg (SCI->getOperand (0));
1061 unsigned Op1Reg = getReg (SCI->getOperand (1));
1062 const Type *Ty = SCI->getOperand (0)->getType ();
1063
1064 // Compare the two values.
1065 if (getClass (Ty) < cLong) {
1066 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
1067 } else if (getClass (Ty) == cLong) {
1068 assert (0 && "Can't fold setcc long/ulong into branch");
1069 } else if (getClass (Ty) == cFloat) {
1070 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1071 } else if (getClass (Ty) == cDouble) {
1072 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1073 }
1074
1075 unsigned BranchIdx;
1076 switch (SCI->getOpcode()) {
1077 default: assert(0 && "Unknown setcc instruction!");
1078 case Instruction::SetEQ: BranchIdx = 0; break;
1079 case Instruction::SetNE: BranchIdx = 1; break;
1080 case Instruction::SetLT: BranchIdx = 2; break;
1081 case Instruction::SetGT: BranchIdx = 3; break;
1082 case Instruction::SetLE: BranchIdx = 4; break;
1083 case Instruction::SetGE: BranchIdx = 5; break;
1084 }
1085
1086 unsigned Column = 0;
1087 if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1;
1088 if (Ty->isFloatingPoint()) Column = 2;
1089 static unsigned OpcodeTab[3*6] = {
1090 // LLVM SparcV8
1091 // unsigned signed fp
1092 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1093 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1094 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1095 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1096 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1097 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
1098 };
1099 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
1100 BuildMI (BB, Opcode, 1).addMBB (takenSuccMBB);
1101 BuildMI (BB, V8::BA, 1).addMBB (notTakenSuccMBB);
1102 } else {
1103 // goto takenSuccMBB;
1104 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001105 }
1106}
1107
1108/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
1109/// constant expression GEP support.
1110///
Brian Gaeke9f564822004-05-08 05:27:20 +00001111void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +00001112 MachineBasicBlock::iterator IP,
1113 Value *Src, User::op_iterator IdxBegin,
1114 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +00001115 const TargetData &TD = TM.getTargetData ();
1116 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001117 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +00001118
1119 // GEPs have zero or more indices; we must perform a struct access
1120 // or array access for each one.
1121 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
1122 ++oi) {
1123 Value *idx = *oi;
1124 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
1125 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
1126 // It's a struct access. idx is the index into the structure,
1127 // which names the field. Use the TargetData structure to
1128 // pick out what the layout of the structure is in memory.
1129 // Use the (constant) structure index's value to find the
1130 // right byte offset from the StructLayout class's list of
1131 // structure member offsets.
1132 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
1133 unsigned memberOffset =
1134 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
1135 // Emit an ADD to add memberOffset to the basePtr.
Brian Gaeke4f70b632004-12-11 05:19:02 +00001136 // We might have to copy memberOffset into a register first, if
1137 // it's big.
Brian Gaeke31e57592004-11-24 04:07:33 +00001138 if (memberOffset + 4096 < 8191) {
1139 BuildMI (*MBB, IP, V8::ADDri, 2,
1140 nextBasePtrReg).addReg (basePtrReg).addSImm (memberOffset);
1141 } else {
1142 unsigned offsetReg = makeAnotherReg (Type::IntTy);
1143 copyConstantToRegister (MBB, IP,
Brian Gaeke4f70b632004-12-11 05:19:02 +00001144 ConstantSInt::get(Type::IntTy, memberOffset), offsetReg);
Brian Gaeke31e57592004-11-24 04:07:33 +00001145 BuildMI (*MBB, IP, V8::ADDrr, 2,
1146 nextBasePtrReg).addReg (basePtrReg).addReg (offsetReg);
1147 }
Brian Gaeke9f564822004-05-08 05:27:20 +00001148 // The next type is the member of the structure selected by the
1149 // index.
1150 Ty = StTy->getElementType (fieldIndex);
1151 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
1152 // It's an array or pointer access: [ArraySize x ElementType].
1153 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1154 // must find the size of the pointed-to type (Not coincidentally, the next
1155 // type is the type of the elements in the array).
1156 Ty = SqTy->getElementType ();
1157 unsigned elementSize = TD.getTypeSize (Ty);
Brian Gaeke4f70b632004-12-11 05:19:02 +00001158 unsigned OffsetReg = ~0U;
1159 int64_t Offset = -1;
1160 bool addImmed = false;
1161 if (isa<ConstantIntegral> (idx)) {
1162 // If idx is a constant, we don't have to emit the multiply.
1163 int64_t Val = cast<ConstantIntegral> (idx)->getRawValue ();
1164 if ((Val * elementSize) + 4096 < 8191) {
1165 // (Val * elementSize) is constant and fits in an immediate field.
1166 // emit: nextBasePtrReg = ADDri basePtrReg, (Val * elementSize)
1167 addImmed = true;
1168 Offset = Val * elementSize;
1169 } else {
1170 // (Val * elementSize) is constant, but doesn't fit in an immediate
1171 // field. emit: OffsetReg = (Val * elementSize)
1172 // nextBasePtrReg = ADDrr OffsetReg, basePtrReg
1173 OffsetReg = makeAnotherReg (Type::IntTy);
1174 copyConstantToRegister (MBB, IP,
1175 ConstantSInt::get(Type::IntTy, Val * elementSize), OffsetReg);
1176 }
1177 } else {
1178 // idx is not constant, we have to shift or multiply.
1179 OffsetReg = makeAnotherReg (Type::IntTy);
1180 unsigned idxReg = getReg (idx, MBB, IP);
1181 switch (elementSize) {
1182 case 1:
1183 BuildMI (*MBB, IP, V8::ORrr, 2, OffsetReg).addReg (V8::G0).addReg (idxReg);
1184 break;
1185 case 2:
1186 BuildMI (*MBB, IP, V8::SLLri, 2, OffsetReg).addReg (idxReg).addZImm (1);
1187 break;
1188 case 4:
1189 BuildMI (*MBB, IP, V8::SLLri, 2, OffsetReg).addReg (idxReg).addZImm (2);
1190 break;
1191 case 8:
1192 BuildMI (*MBB, IP, V8::SLLri, 2, OffsetReg).addReg (idxReg).addZImm (3);
1193 break;
1194 default: {
1195 if (elementSize + 4096 < 8191) {
1196 // Emit a SMUL to multiply the register holding the index by
1197 // elementSize, putting the result in OffsetReg.
1198 BuildMI (*MBB, IP, V8::SMULri, 2,
1199 OffsetReg).addReg (idxReg).addSImm (elementSize);
1200 } else {
1201 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
1202 copyConstantToRegister (MBB, IP,
1203 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
1204 // Emit a SMUL to multiply the register holding the index by
1205 // the register w/ elementSize, putting the result in OffsetReg.
1206 BuildMI (*MBB, IP, V8::SMULrr, 2,
1207 OffsetReg).addReg (idxReg).addReg (elementSizeReg);
1208 }
1209 break;
1210 }
1211 }
1212 }
1213 if (addImmed) {
1214 // Emit an ADD to add the constant immediate Offset to the basePtr.
1215 BuildMI (*MBB, IP, V8::ADDri, 2,
1216 nextBasePtrReg).addReg (basePtrReg).addSImm (Offset);
1217 } else {
1218 // Emit an ADD to add OffsetReg to the basePtr.
1219 BuildMI (*MBB, IP, V8::ADDrr, 2,
1220 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1221 }
Brian Gaeke9f564822004-05-08 05:27:20 +00001222 }
1223 basePtrReg = nextBasePtrReg;
1224 }
1225 // After we have processed all the indices, the result is left in
1226 // basePtrReg. Move it to the register where we were expected to
1227 // put the answer.
1228 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001229}
1230
1231void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
1232 unsigned outputReg = getReg (I);
1233 emitGEPOperation (BB, BB->end (), I.getOperand (0),
1234 I.op_begin ()+1, I.op_end (), outputReg);
1235}
1236
Brian Gaeke5f91de22004-11-21 07:13:16 +00001237void V8ISel::emitOp64LibraryCall (MachineBasicBlock *MBB,
1238 MachineBasicBlock::iterator IP,
1239 unsigned DestReg,
1240 const char *FuncName,
1241 unsigned Op0Reg, unsigned Op1Reg) {
1242 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O0).addReg (V8::G0).addReg (Op0Reg);
1243 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O1).addReg (V8::G0).addReg (Op0Reg+1);
1244 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O2).addReg (V8::G0).addReg (Op1Reg);
1245 BuildMI (*MBB, IP, V8::ORrr, 2, V8::O3).addReg (V8::G0).addReg (Op1Reg+1);
1246 BuildMI (*MBB, IP, V8::CALL, 1).addExternalSymbol (FuncName, true);
1247 BuildMI (*MBB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (V8::O0);
1248 BuildMI (*MBB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0).addReg (V8::O1);
1249}
Brian Gaeked6a10532004-06-15 21:09:46 +00001250
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +00001251void V8ISel::emitShift64 (MachineBasicBlock *MBB,
1252 MachineBasicBlock::iterator IP, Instruction &I,
Brian Gaekefbe558c2004-11-23 08:14:09 +00001253 unsigned DestReg, unsigned SrcReg,
1254 unsigned ShiftAmtReg) {
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +00001255 bool isSigned = I.getType()->isSigned();
1256
1257 switch (I.getOpcode ()) {
Brian Gaeke88108b82004-11-23 21:10:50 +00001258 case Instruction::Shr: {
1259 unsigned CarryReg = makeAnotherReg (Type::IntTy),
1260 ThirtyTwo = makeAnotherReg (Type::IntTy),
1261 HalfShiftReg = makeAnotherReg (Type::IntTy),
1262 NegHalfShiftReg = makeAnotherReg (Type::IntTy),
1263 TempReg = makeAnotherReg (Type::IntTy);
1264 unsigned OneShiftOutReg = makeAnotherReg (Type::ULongTy),
1265 TwoShiftsOutReg = makeAnotherReg (Type::ULongTy);
1266
1267 MachineBasicBlock *thisMBB = BB;
1268 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1269 MachineBasicBlock *shiftMBB = new MachineBasicBlock (LLVM_BB);
1270 F->getBasicBlockList ().push_back (shiftMBB);
1271 MachineBasicBlock *oneShiftMBB = new MachineBasicBlock (LLVM_BB);
1272 F->getBasicBlockList ().push_back (oneShiftMBB);
1273 MachineBasicBlock *twoShiftsMBB = new MachineBasicBlock (LLVM_BB);
1274 F->getBasicBlockList ().push_back (twoShiftsMBB);
1275 MachineBasicBlock *continueMBB = new MachineBasicBlock (LLVM_BB);
1276 F->getBasicBlockList ().push_back (continueMBB);
1277
1278 // .lshr_begin:
1279 // ...
1280 // subcc %g0, ShiftAmtReg, %g0 ! Is ShAmt == 0?
1281 // be .lshr_continue ! Then don't shift.
1282 // ba .lshr_shift ! else shift.
1283
1284 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (V8::G0)
1285 .addReg (ShiftAmtReg);
1286 BuildMI (BB, V8::BE, 1).addMBB (continueMBB);
1287 BuildMI (BB, V8::BA, 1).addMBB (shiftMBB);
1288
1289 // Update machine-CFG edges
1290 BB->addSuccessor (continueMBB);
1291 BB->addSuccessor (shiftMBB);
1292
1293 // .lshr_shift: ! [preds: begin]
1294 // or %g0, 32, ThirtyTwo
1295 // subcc ThirtyTwo, ShiftAmtReg, HalfShiftReg ! Calculate 32 - shamt
1296 // bg .lshr_two_shifts ! If >0, b two_shifts
1297 // ba .lshr_one_shift ! else one_shift.
1298
1299 BB = shiftMBB;
1300
1301 BuildMI (BB, V8::ORri, 2, ThirtyTwo).addReg (V8::G0).addSImm (32);
1302 BuildMI (BB, V8::SUBCCrr, 2, HalfShiftReg).addReg (ThirtyTwo)
1303 .addReg (ShiftAmtReg);
1304 BuildMI (BB, V8::BG, 1).addMBB (twoShiftsMBB);
1305 BuildMI (BB, V8::BA, 1).addMBB (oneShiftMBB);
1306
1307 // Update machine-CFG edges
1308 BB->addSuccessor (twoShiftsMBB);
1309 BB->addSuccessor (oneShiftMBB);
1310
1311 // .lshr_two_shifts: ! [preds: shift]
1312 // sll SrcReg, HalfShiftReg, CarryReg ! Save the borrows
1313 // ! <SHIFT> in following is sra if signed, srl if unsigned
1314 // <SHIFT> SrcReg, ShiftAmtReg, TwoShiftsOutReg ! Shift top half
1315 // srl SrcReg+1, ShiftAmtReg, TempReg ! Shift bottom half
1316 // or TempReg, CarryReg, TwoShiftsOutReg+1 ! Restore the borrows
1317 // ba .lshr_continue
1318 unsigned ShiftOpcode = (isSigned ? V8::SRArr : V8::SRLrr);
1319
1320 BB = twoShiftsMBB;
1321
1322 BuildMI (BB, V8::SLLrr, 2, CarryReg).addReg (SrcReg)
1323 .addReg (HalfShiftReg);
1324 BuildMI (BB, ShiftOpcode, 2, TwoShiftsOutReg).addReg (SrcReg)
1325 .addReg (ShiftAmtReg);
1326 BuildMI (BB, V8::SRLrr, 2, TempReg).addReg (SrcReg+1)
1327 .addReg (ShiftAmtReg);
1328 BuildMI (BB, V8::ORrr, 2, TwoShiftsOutReg+1).addReg (TempReg)
1329 .addReg (CarryReg);
1330 BuildMI (BB, V8::BA, 1).addMBB (continueMBB);
1331
1332 // Update machine-CFG edges
1333 BB->addSuccessor (continueMBB);
1334
1335 // .lshr_one_shift: ! [preds: shift]
1336 // ! if unsigned:
1337 // or %g0, %g0, OneShiftOutReg ! Zero top half
1338 // ! or, if signed:
1339 // sra SrcReg, 31, OneShiftOutReg ! Sign-ext top half
1340 // sub %g0, HalfShiftReg, NegHalfShiftReg ! Make ShiftAmt >0
1341 // <SHIFT> SrcReg, NegHalfShiftReg, OneShiftOutReg+1 ! Shift bottom half
1342 // ba .lshr_continue
1343
1344 BB = oneShiftMBB;
1345
1346 if (isSigned)
1347 BuildMI (BB, V8::SRAri, 2, OneShiftOutReg).addReg (SrcReg).addZImm (31);
1348 else
1349 BuildMI (BB, V8::ORrr, 2, OneShiftOutReg).addReg (V8::G0)
1350 .addReg (V8::G0);
1351 BuildMI (BB, V8::SUBrr, 2, NegHalfShiftReg).addReg (V8::G0)
1352 .addReg (HalfShiftReg);
1353 BuildMI (BB, ShiftOpcode, 2, OneShiftOutReg+1).addReg (SrcReg)
1354 .addReg (NegHalfShiftReg);
1355 BuildMI (BB, V8::BA, 1).addMBB (continueMBB);
1356
1357 // Update machine-CFG edges
1358 BB->addSuccessor (continueMBB);
1359
1360 // .lshr_continue: ! [preds: begin, do_one_shift, do_two_shifts]
1361 // phi (SrcReg, begin), (TwoShiftsOutReg, two_shifts),
1362 // (OneShiftOutReg, one_shift), DestReg ! Phi top half...
1363 // phi (SrcReg+1, begin), (TwoShiftsOutReg+1, two_shifts),
1364 // (OneShiftOutReg+1, one_shift), DestReg+1 ! And phi bottom half.
1365
1366 BB = continueMBB;
1367 BuildMI (BB, V8::PHI, 6, DestReg).addReg (SrcReg).addMBB (thisMBB)
1368 .addReg (TwoShiftsOutReg).addMBB (twoShiftsMBB)
1369 .addReg (OneShiftOutReg).addMBB (oneShiftMBB);
1370 BuildMI (BB, V8::PHI, 6, DestReg+1).addReg (SrcReg+1).addMBB (thisMBB)
1371 .addReg (TwoShiftsOutReg+1).addMBB (twoShiftsMBB)
1372 .addReg (OneShiftOutReg+1).addMBB (oneShiftMBB);
1373 return;
1374 }
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +00001375 case Instruction::Shl:
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +00001376 default:
1377 std::cerr << "Sorry, 64-bit shifts are not yet supported:\n" << I;
1378 abort ();
1379 }
1380}
1381
Chris Lattner4be7ca52004-04-07 04:27:16 +00001382void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001383 unsigned DestReg = getReg (I);
1384 unsigned Op0Reg = getReg (I.getOperand (0));
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001385
Brian Gaekeec3227f2004-06-27 22:47:33 +00001386 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +00001387 unsigned OpCase = ~0;
1388
Brian Gaekeec3227f2004-06-27 22:47:33 +00001389 if (Class > cLong) {
Brian Gaeke1f421812004-12-10 08:39:28 +00001390 unsigned Op1Reg = getReg (I.getOperand (1));
Brian Gaekeec3227f2004-06-27 22:47:33 +00001391 switch (I.getOpcode ()) {
1392 case Instruction::Add: OpCase = 0; break;
1393 case Instruction::Sub: OpCase = 1; break;
1394 case Instruction::Mul: OpCase = 2; break;
1395 case Instruction::Div: OpCase = 3; break;
1396 default: visitInstruction (I); return;
1397 }
1398 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
1399 V8::FSUBS, V8::FSUBD,
1400 V8::FMULS, V8::FMULD,
1401 V8::FDIVS, V8::FDIVD };
1402 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
1403 .addReg (Op0Reg).addReg (Op1Reg);
1404 return;
1405 }
1406
1407 unsigned ResultReg = DestReg;
Brian Gaeke1df468e2004-09-29 03:34:41 +00001408 if (Class != cInt && Class != cLong)
Brian Gaekeec3227f2004-06-27 22:47:33 +00001409 ResultReg = makeAnotherReg (I.getType ());
1410
Brian Gaeke1df468e2004-09-29 03:34:41 +00001411 if (Class == cLong) {
Brian Gaeke5f91de22004-11-21 07:13:16 +00001412 const char *FuncName;
Brian Gaeke1f421812004-12-10 08:39:28 +00001413 unsigned Op1Reg = getReg (I.getOperand (1));
Brian Gaeke1df468e2004-09-29 03:34:41 +00001414 DEBUG (std::cerr << "Class = cLong\n");
1415 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
1416 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
1417 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
1418 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
Brian Gaeke5f91de22004-11-21 07:13:16 +00001419 switch (I.getOpcode ()) {
1420 case Instruction::Add:
1421 BuildMI (BB, V8::ADDCCrr, 2, ResultReg+1).addReg (Op0Reg+1)
1422 .addReg (Op1Reg+1);
1423 BuildMI (BB, V8::ADDXrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1424 return;
1425 case Instruction::Sub:
1426 BuildMI (BB, V8::SUBCCrr, 2, ResultReg+1).addReg (Op0Reg+1)
1427 .addReg (Op1Reg+1);
1428 BuildMI (BB, V8::SUBXrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1429 return;
1430 case Instruction::Mul:
1431 FuncName = I.getType ()->isSigned () ? "__mul64" : "__umul64";
1432 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1433 return;
1434 case Instruction::Div:
1435 FuncName = I.getType ()->isSigned () ? "__div64" : "__udiv64";
1436 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1437 return;
1438 case Instruction::Rem:
1439 FuncName = I.getType ()->isSigned () ? "__rem64" : "__urem64";
1440 emitOp64LibraryCall (BB, BB->end (), DestReg, FuncName, Op0Reg, Op1Reg);
1441 return;
Brian Gaeke9ffcf9f2004-11-22 08:02:06 +00001442 case Instruction::Shl:
1443 case Instruction::Shr:
1444 emitShift64 (BB, BB->end (), I, DestReg, Op0Reg, Op1Reg);
1445 return;
Brian Gaeke5f91de22004-11-21 07:13:16 +00001446 }
Brian Gaeke1df468e2004-09-29 03:34:41 +00001447 }
1448
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001449 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +00001450 case Instruction::Add: OpCase = 0; break;
1451 case Instruction::Sub: OpCase = 1; break;
1452 case Instruction::Mul: OpCase = 2; break;
1453 case Instruction::And: OpCase = 3; break;
1454 case Instruction::Or: OpCase = 4; break;
1455 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +00001456 case Instruction::Shl: OpCase = 6; break;
1457 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +00001458
1459 case Instruction::Div:
1460 case Instruction::Rem: {
1461 unsigned Dest = ResultReg;
Brian Gaeke1f421812004-12-10 08:39:28 +00001462 unsigned Op1Reg = getReg (I.getOperand (1));
Chris Lattner22ede702004-04-07 04:06:46 +00001463 if (I.getOpcode() == Instruction::Rem)
1464 Dest = makeAnotherReg(I.getType());
1465
1466 // FIXME: this is probably only right for 32 bit operands.
1467 if (I.getType ()->isSigned()) {
1468 unsigned Tmp = makeAnotherReg (I.getType ());
1469 // Sign extend into the Y register
1470 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
1471 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
1472 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
1473 } else {
1474 // Zero extend into the Y register, ie, just set it to zero
1475 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
1476 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +00001477 }
Chris Lattner22ede702004-04-07 04:06:46 +00001478
1479 if (I.getOpcode() == Instruction::Rem) {
1480 unsigned Tmp = makeAnotherReg (I.getType ());
1481 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
1482 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +00001483 }
Chris Lattner22ede702004-04-07 04:06:46 +00001484 break;
1485 }
1486 default:
1487 visitInstruction (I);
1488 return;
1489 }
1490
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001491 static const unsigned Opcodes[] = {
1492 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
1493 V8::SLLrr, V8::SRLrr, V8::SRArr
1494 };
Brian Gaeke1f421812004-12-10 08:39:28 +00001495 static const unsigned OpcodesRI[] = {
1496 V8::ADDri, V8::SUBri, V8::SMULri, V8::ANDri, V8::ORri, V8::XORri,
1497 V8::SLLri, V8::SRLri, V8::SRAri
1498 };
1499 unsigned Op1Reg = ~0U;
Chris Lattner22ede702004-04-07 04:06:46 +00001500 if (OpCase != ~0U) {
Brian Gaeke1f421812004-12-10 08:39:28 +00001501 Value *Arg1 = I.getOperand (1);
1502 bool useImmed = false;
1503 int64_t Val = 0;
1504 if ((getClassB (I.getType ()) <= cInt) && (isa<ConstantIntegral> (Arg1))) {
1505 Val = cast<ConstantIntegral> (Arg1)->getRawValue ();
1506 useImmed = (Val > -4096 && Val < 4095);
1507 }
1508 if (useImmed) {
1509 BuildMI (BB, OpcodesRI[OpCase], 2, ResultReg).addReg (Op0Reg).addSImm (Val);
1510 } else {
1511 Op1Reg = getReg (I.getOperand (1));
1512 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1513 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001514 }
1515
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001516 switch (getClassB (I.getType ())) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001517 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001518 if (I.getType ()->isSigned ()) { // add byte
1519 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1520 } else { // add ubyte
1521 unsigned TmpReg = makeAnotherReg (I.getType ());
1522 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1523 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1524 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001525 break;
1526 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001527 if (I.getType ()->isSigned ()) { // add short
1528 unsigned TmpReg = makeAnotherReg (I.getType ());
1529 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1530 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1531 } else { // add ushort
1532 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +00001533 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1534 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +00001535 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001536 break;
1537 case cInt:
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001538 // Nothing to do here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001539 break;
Brian Gaeke1f421812004-12-10 08:39:28 +00001540 case cLong: {
Brian Gaeke5f91de22004-11-21 07:13:16 +00001541 // Only support and, or, xor here - others taken care of above.
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001542 if (OpCase < 3 || OpCase > 5) {
1543 visitInstruction (I);
1544 return;
1545 }
1546 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +00001547 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1548 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001549 break;
Brian Gaeke1f421812004-12-10 08:39:28 +00001550 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001551 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001552 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001553 }
1554}
1555
Misha Brukmanea091262004-06-30 21:47:40 +00001556void V8ISel::visitSetCondInst(SetCondInst &I) {
Brian Gaeke6a8c46c2004-12-12 06:01:26 +00001557 if (canFoldSetCCIntoBranch(&I))
1558 return; // Fold this into a branch.
1559
Chris Lattner4d0cda42004-04-07 05:04:51 +00001560 unsigned Op0Reg = getReg (I.getOperand (0));
1561 unsigned Op1Reg = getReg (I.getOperand (1));
1562 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +00001563 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +00001564
1565 // Compare the two values.
Brian Gaeke3a085892004-07-08 09:08:35 +00001566 if (getClass (Ty) < cLong) {
1567 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
Brian Gaeke5f91de22004-11-21 07:13:16 +00001568 } else if (getClass (Ty) == cLong) {
Brian Gaekec7b4f102004-11-21 08:11:28 +00001569 switch (I.getOpcode()) {
1570 default: assert(0 && "Unknown setcc instruction!");
1571 case Instruction::SetEQ:
1572 case Instruction::SetNE: {
1573 unsigned TempReg0 = makeAnotherReg (Type::IntTy),
1574 TempReg1 = makeAnotherReg (Type::IntTy),
1575 TempReg2 = makeAnotherReg (Type::IntTy),
1576 TempReg3 = makeAnotherReg (Type::IntTy);
1577 MachineOpCode Opcode;
1578 int Immed;
1579 // These guys are special - no branches needed!
1580 BuildMI (BB, V8::XORrr, 2, TempReg0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1581 BuildMI (BB, V8::XORrr, 2, TempReg1).addReg (Op0Reg).addReg (Op1Reg);
1582 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (V8::G0).addReg (TempReg1);
1583 Opcode = I.getOpcode() == Instruction::SetEQ ? V8::SUBXri : V8::ADDXri;
1584 Immed = I.getOpcode() == Instruction::SetEQ ? -1 : 0;
1585 BuildMI (BB, Opcode, 2, TempReg2).addReg (V8::G0).addSImm (Immed);
1586 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (V8::G0).addReg (TempReg0);
1587 BuildMI (BB, Opcode, 2, TempReg3).addReg (V8::G0).addSImm (Immed);
1588 Opcode = I.getOpcode() == Instruction::SetEQ ? V8::ANDrr : V8::ORrr;
1589 BuildMI (BB, Opcode, 2, DestReg).addReg (TempReg2).addReg (TempReg3);
1590 return;
1591 }
1592 case Instruction::SetLT:
1593 case Instruction::SetGE:
1594 BuildMI (BB, V8::SUBCCrr, 2, V8::G0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1595 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg).addReg (Op1Reg);
1596 break;
1597 case Instruction::SetGT:
1598 case Instruction::SetLE:
1599 BuildMI (BB, V8::SUBCCri, 2, V8::G0).addReg (V8::G0).addSImm (1);
1600 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg+1).addReg (Op1Reg+1);
1601 BuildMI (BB, V8::SUBXCCrr, 2, V8::G0).addReg (Op0Reg).addReg (Op1Reg);
1602 break;
1603 }
Brian Gaeke3a085892004-07-08 09:08:35 +00001604 } else if (getClass (Ty) == cFloat) {
1605 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1606 } else if (getClass (Ty) == cDouble) {
1607 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1608 }
Chris Lattner4d0cda42004-04-07 05:04:51 +00001609
Brian Gaeke429022b2004-05-08 06:36:14 +00001610 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001611 switch (I.getOpcode()) {
1612 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +00001613 case Instruction::SetEQ: BranchIdx = 0; break;
1614 case Instruction::SetNE: BranchIdx = 1; break;
1615 case Instruction::SetLT: BranchIdx = 2; break;
1616 case Instruction::SetGT: BranchIdx = 3; break;
1617 case Instruction::SetLE: BranchIdx = 4; break;
1618 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001619 }
Brian Gaekec7b4f102004-11-21 08:11:28 +00001620
Brian Gaeke3a085892004-07-08 09:08:35 +00001621 unsigned Column = 0;
Brian Gaekeb3e00172004-11-17 22:06:56 +00001622 if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1;
1623 if (Ty->isFloatingPoint()) Column = 2;
Brian Gaeke3a085892004-07-08 09:08:35 +00001624 static unsigned OpcodeTab[3*6] = {
1625 // LLVM SparcV8
1626 // unsigned signed fp
1627 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1628 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1629 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1630 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1631 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1632 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
Brian Gaeke429022b2004-05-08 06:36:14 +00001633 };
Brian Gaeke3a085892004-07-08 09:08:35 +00001634 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
Brian Gaeke6c868a42004-06-17 22:34:08 +00001635
1636 MachineBasicBlock *thisMBB = BB;
1637 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1638 // thisMBB:
1639 // ...
1640 // subcc %reg0, %reg1, %g0
1641 // bCC copy1MBB
1642 // ba copy0MBB
1643
1644 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1645 // if we could insert other, non-terminator instructions after the
1646 // bCC. But MBB->getFirstTerminator() can't understand this.
1647 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1648 F->getBasicBlockList ().push_back (copy1MBB);
1649 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1650 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1651 F->getBasicBlockList ().push_back (copy0MBB);
1652 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1653 // Update machine-CFG edges
1654 BB->addSuccessor (copy1MBB);
1655 BB->addSuccessor (copy0MBB);
1656
1657 // copy0MBB:
1658 // %FalseValue = or %G0, 0
1659 // ba sinkMBB
1660 BB = copy0MBB;
1661 unsigned FalseValue = makeAnotherReg (I.getType ());
1662 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1663 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1664 F->getBasicBlockList ().push_back (sinkMBB);
1665 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1666 // Update machine-CFG edges
1667 BB->addSuccessor (sinkMBB);
1668
1669 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1670 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1671 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1672 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1673
1674 // copy1MBB:
1675 // %TrueValue = or %G0, 1
1676 // ba sinkMBB
1677 BB = copy1MBB;
1678 unsigned TrueValue = makeAnotherReg (I.getType ());
1679 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1680 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1681 // Update machine-CFG edges
1682 BB->addSuccessor (sinkMBB);
1683
1684 // sinkMBB:
1685 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1686 // ...
1687 BB = sinkMBB;
1688 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1689 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001690}
1691
Brian Gaekec93a7522004-06-18 05:19:16 +00001692void V8ISel::visitAllocaInst(AllocaInst &I) {
1693 // Find the data size of the alloca inst's getAllocatedType.
1694 const Type *Ty = I.getAllocatedType();
1695 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001696
Brian Gaekec93a7522004-06-18 05:19:16 +00001697 unsigned ArraySizeReg = getReg (I.getArraySize ());
1698 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1699 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1700 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1701 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001702
Brian Gaeke79fe8332004-11-21 03:35:22 +00001703 // StackAdjReg = (ArraySize * TySize) rounded up to nearest
1704 // doubleword boundary.
Brian Gaekec93a7522004-06-18 05:19:16 +00001705 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001706
Brian Gaekec93a7522004-06-18 05:19:16 +00001707 // Round up TmpReg1 to nearest doubleword boundary:
1708 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1709 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001710
1711 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001712 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001713
1714 // Put a pointer to the space into the result register, by copying
1715 // the stack pointer.
1716 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1717
1718 // Inform the Frame Information that we have just allocated a variable-sized
1719 // object.
1720 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001721}
Chris Lattner1c809c52004-02-29 00:27:00 +00001722
1723/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1724/// function, lowering any calls to unknown intrinsic functions into the
1725/// equivalent LLVM code.
1726void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1727 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1728 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1729 if (CallInst *CI = dyn_cast<CallInst>(I++))
1730 if (Function *F = CI->getCalledFunction())
1731 switch (F->getIntrinsicID()) {
Brian Gaeked90282d2004-11-19 20:57:24 +00001732 case Intrinsic::vastart:
1733 case Intrinsic::vacopy:
1734 case Intrinsic::vaend:
1735 // We directly implement these intrinsics
Chris Lattner1c809c52004-02-29 00:27:00 +00001736 case Intrinsic::not_intrinsic: break;
1737 default:
1738 // All other intrinsic calls we must lower.
1739 Instruction *Before = CI->getPrev();
1740 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1741 if (Before) { // Move iterator to instruction after call
1742 I = Before; ++I;
1743 } else {
1744 I = BB->begin();
1745 }
1746 }
1747}
1748
1749
1750void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattner1c809c52004-02-29 00:27:00 +00001751 switch (ID) {
Brian Gaeke9e672a22004-11-19 18:53:59 +00001752 default:
1753 std::cerr << "Sorry, unknown intrinsic function call:\n" << CI; abort ();
1754
Brian Gaeked90282d2004-11-19 20:57:24 +00001755 case Intrinsic::vastart: {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001756 // Add the VarArgsOffset to the frame pointer, and copy it to the result.
Brian Gaeked90282d2004-11-19 20:57:24 +00001757 unsigned DestReg = getReg (CI);
1758 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (V8::FP).addSImm (VarArgsOffset);
1759 return;
1760 }
Brian Gaeke9e672a22004-11-19 18:53:59 +00001761
1762 case Intrinsic::vaend:
Brian Gaeke2f95ed62004-11-19 19:21:34 +00001763 // va_end is a no-op on SparcV8.
1764 return;
Brian Gaeke9e672a22004-11-19 18:53:59 +00001765
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001766 case Intrinsic::vacopy: {
1767 // Copy the va_list ptr (arg1) to the result.
1768 unsigned DestReg = getReg (CI), SrcReg = getReg (CI.getOperand (1));
1769 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
1770 return;
1771 }
Chris Lattner1c809c52004-02-29 00:27:00 +00001772 }
1773}
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001774
1775void V8ISel::visitVANextInst (VANextInst &I) {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001776 // Add the type size to the vararg pointer (arg0).
1777 unsigned DestReg = getReg (I);
1778 unsigned SrcReg = getReg (I.getOperand (0));
1779 unsigned TySize = TM.getTargetData ().getTypeSize (I.getArgType ());
1780 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (SrcReg).addSImm (TySize);
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001781}
1782
1783void V8ISel::visitVAArgInst (VAArgInst &I) {
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001784 unsigned VAList = getReg (I.getOperand (0));
1785 unsigned DestReg = getReg (I);
1786
1787 switch (I.getType ()->getTypeID ()) {
1788 case Type::PointerTyID:
1789 case Type::UIntTyID:
1790 case Type::IntTyID:
1791 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1792 return;
1793
1794 case Type::ULongTyID:
1795 case Type::LongTyID:
1796 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1797 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (VAList).addSImm (4);
1798 return;
1799
Brian Gaeke79fe8332004-11-21 03:35:22 +00001800 case Type::DoubleTyID: {
1801 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
1802 unsigned TempReg = makeAnotherReg (Type::IntTy);
1803 unsigned TempReg2 = makeAnotherReg (Type::IntTy);
1804 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
1805 BuildMI (BB, V8::LD, 2, TempReg).addReg (VAList).addSImm (0);
1806 BuildMI (BB, V8::LD, 2, TempReg2).addReg (VAList).addSImm (4);
1807 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
1808 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg2);
1809 BuildMI (BB, V8::LDDFri, 2, DestReg).addFrameIndex (FI).addSImm (0);
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001810 return;
Brian Gaeke79fe8332004-11-21 03:35:22 +00001811 }
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001812
1813 default:
1814 std::cerr << "Sorry, vaarg instruction of this type still unsupported:\n"
1815 << I;
1816 abort ();
1817 return;
1818 }
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001819}