blob: 771657a34b775d55e6f838993bbf31aa202061cd [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
Chris Lattner4be7ca52004-04-07 04:27:16 +000089 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000090 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000091 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000092 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000093 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000094 void visitBranchInst(BranchInst &I);
Chris Lattnerd14d5b42004-10-17 02:42:42 +000095 void visitUnreachableInst(UnreachableInst &I) {}
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000096 void visitCastInst(CastInst &I);
Brian Gaekeb6c409a2004-11-19 21:08:18 +000097 void visitVANextInst(VANextInst &I);
98 void visitVAArgInst(VAArgInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000099 void visitLoadInst(LoadInst &I);
100 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000101 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
102 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +0000103 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000104
Chris Lattner1c809c52004-02-29 00:27:00 +0000105 void visitInstruction(Instruction &I) {
106 std::cerr << "Unhandled instruction: " << I;
107 abort();
108 }
109
110 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
111 /// function, lowering any calls to unknown intrinsic functions into the
112 /// equivalent LLVM code.
113 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +0000114 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
115
Brian Gaeke562cb162004-04-07 17:04:09 +0000116 void LoadArgumentsToVirtualRegs(Function *F);
117
Brian Gaeke6c868a42004-06-17 22:34:08 +0000118 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
119 /// because we have to generate our sources into the source basic blocks,
120 /// not the current one.
121 ///
122 void SelectPHINodes();
123
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000124 /// copyConstantToRegister - Output the instructions required to put the
125 /// specified constant into the specified register.
126 ///
127 void copyConstantToRegister(MachineBasicBlock *MBB,
128 MachineBasicBlock::iterator IP,
129 Constant *C, unsigned R);
130
131 /// makeAnotherReg - This method returns the next register number we haven't
132 /// yet used.
133 ///
134 /// Long values are handled somewhat specially. They are always allocated
135 /// as pairs of 32 bit integer values. The register number returned is the
136 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
137 /// of the long value.
138 ///
139 unsigned makeAnotherReg(const Type *Ty) {
140 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
141 "Current target doesn't have SparcV8 reg info??");
142 const SparcV8RegisterInfo *MRI =
143 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
144 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
145 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
146 // Create the lower part
147 F->getSSARegMap()->createVirtualRegister(RC);
148 // Create the upper part.
149 return F->getSSARegMap()->createVirtualRegister(RC)-1;
150 }
151
152 // Add the mapping of regnumber => reg class to MachineFunction
153 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
154 return F->getSSARegMap()->createVirtualRegister(RC);
155 }
156
157 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
158 unsigned getReg(Value *V) {
159 // Just append to the end of the current bb.
160 MachineBasicBlock::iterator It = BB->end();
161 return getReg(V, BB, It);
162 }
163 unsigned getReg(Value *V, MachineBasicBlock *MBB,
164 MachineBasicBlock::iterator IPt) {
165 unsigned &Reg = RegMap[V];
166 if (Reg == 0) {
167 Reg = makeAnotherReg(V->getType());
168 RegMap[V] = Reg;
169 }
170 // If this operand is a constant, emit the code to copy the constant into
171 // the register here...
172 //
173 if (Constant *C = dyn_cast<Constant>(V)) {
174 copyConstantToRegister(MBB, IPt, C, Reg);
175 RegMap.erase(V); // Assign a new name to this constant if ref'd again
176 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
177 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000178 unsigned TmpReg = makeAnotherReg(V->getType());
179 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
180 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
181 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000182 RegMap.erase(V); // Assign a new name to this address if ref'd again
183 }
184
185 return Reg;
186 }
187
Chris Lattner1c809c52004-02-29 00:27:00 +0000188 };
189}
190
191FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
192 return new V8ISel(TM);
193}
194
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000195enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000196 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000197};
198
199static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000200 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000201 case Type::UByteTyID: case Type::SByteTyID: return cByte;
202 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000203 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000204 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000205 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000206 case Type::FloatTyID: return cFloat;
207 case Type::DoubleTyID: return cDouble;
208 default:
209 assert (0 && "Type of unknown class passed to getClass?");
210 return cByte;
211 }
212}
Brian Gaeke50094ed2004-10-10 19:57:18 +0000213
Chris Lattner0d538bb2004-04-07 04:36:53 +0000214static TypeClass getClassB(const Type *T) {
215 if (T == Type::BoolTy) return cByte;
216 return getClass(T);
217}
218
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000219/// copyConstantToRegister - Output the instructions required to put the
220/// specified constant into the specified register.
221///
222void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
223 MachineBasicBlock::iterator IP,
224 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000225 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
226 switch (CE->getOpcode()) {
227 case Instruction::GetElementPtr:
228 emitGEPOperation(MBB, IP, CE->getOperand(0),
229 CE->op_begin()+1, CE->op_end(), R);
230 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000231 case Instruction::Cast:
232 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
233 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000234 default:
235 std::cerr << "Copying this constant expr not yet handled: " << *CE;
236 abort();
237 }
Chris Lattnerd14d5b42004-10-17 02:42:42 +0000238 } else if (isa<UndefValue>(C)) {
239 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R);
240 if (getClassB (C->getType ()) == cLong)
241 BuildMI(*MBB, IP, V8::IMPLICIT_DEF, 0, R+1);
242 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000243 }
244
Brian Gaekee302a7e2004-05-07 21:39:30 +0000245 if (C->getType()->isIntegral ()) {
246 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000247 unsigned Class = getClassB (C->getType ());
248 if (Class == cLong) {
249 unsigned TmpReg = makeAnotherReg (Type::IntTy);
250 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
251 // Copy the value into the register pair.
252 // R = top(more-significant) half, R+1 = bottom(less-significant) half
253 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000254 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
255 Val >> 32), R);
256 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
257 Val & 0xffffffffU), R+1);
Brian Gaeke9df92822004-06-15 19:16:07 +0000258 return;
259 }
260
261 assert(Class <= cInt && "Type not handled yet!");
262
Brian Gaekee302a7e2004-05-07 21:39:30 +0000263 if (C->getType() == Type::BoolTy) {
264 Val = (C == ConstantBool::True);
265 } else {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000266 ConstantInt *CI = cast<ConstantInt> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000267 Val = CI->getRawValue ();
268 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000269 switch (Class) {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000270 case cByte: Val = (int8_t) Val; break;
271 case cShort: Val = (int16_t) Val; break;
272 case cInt: Val = (int32_t) Val; break;
Brian Gaekee8061732004-03-04 00:56:25 +0000273 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000274 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000275 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000276 return;
277 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000278 if (Val == 0) {
279 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
280 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
281 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
282 } else {
283 unsigned TmpReg = makeAnotherReg (C->getType ());
284 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
285 .addSImm (((uint32_t) Val) >> 10);
286 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
287 .addSImm (((uint32_t) Val) & 0x03ff);
288 return;
289 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000290 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
291 // We need to spill the constant to memory...
292 MachineConstantPool *CP = F->getConstantPool();
293 unsigned CPI = CP->getConstantPoolIndex(CFP);
294 const Type *Ty = CFP->getType();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000295 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
296 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +0000297
298 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000299 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000300 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000301 BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg)
302 .addConstantPoolIndex (CPI);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000303 BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000304 } else if (isa<ConstantPointerNull>(C)) {
305 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000306 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Chris Lattner73302482004-07-18 07:26:17 +0000307 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000308 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
309 // that SETHI %reg,global == SETHI %reg,%hi(global) and
310 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
311 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner73302482004-07-18 07:26:17 +0000312 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
313 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
Brian Gaeke9df92822004-06-15 19:16:07 +0000314 } else {
315 std::cerr << "Offending constant: " << *C << "\n";
316 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000317 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000318}
Chris Lattner1c809c52004-02-29 00:27:00 +0000319
Brian Gaeke812c4882004-07-16 10:31:25 +0000320void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
Brian Gaeke562cb162004-04-07 17:04:09 +0000321 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
322 V8::I3, V8::I4, V8::I5 };
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000323
Brian Gaeke812c4882004-07-16 10:31:25 +0000324 // Add IMPLICIT_DEFs of input regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000325 unsigned ArgNo = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000326 for (Function::aiterator I = LF->abegin(), E = LF->aend();
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000327 I != E && ArgNo < 6; ++I, ++ArgNo) {
Brian Gaeke812c4882004-07-16 10:31:25 +0000328 switch (getClassB(I->getType())) {
329 case cByte:
330 case cShort:
331 case cInt:
332 case cFloat:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000333 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke812c4882004-07-16 10:31:25 +0000334 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000335 case cDouble:
336 case cLong:
337 // Double and Long use register pairs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000338 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
339 ++ArgNo;
340 if (ArgNo < 6)
341 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgNo]);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000342 break;
Brian Gaeke812c4882004-07-16 10:31:25 +0000343 default:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000344 assert (0 && "type not handled");
Brian Gaeke812c4882004-07-16 10:31:25 +0000345 return;
346 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000347 }
348
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000349 const unsigned *IAREnd = &IncomingArgRegs[6];
350 const unsigned *IAR = &IncomingArgRegs[0];
351 unsigned ArgOffset = 68;
Brian Gaeke4e459c42004-11-19 20:31:08 +0000352
353 // Store registers onto stack if this is a varargs function.
354 // FIXME: This doesn't really pertain to "loading arguments into
355 // virtual registers", so it's not clear that it really belongs here.
356 // FIXME: We could avoid storing any args onto the stack that don't
357 // need to be in memory, because they come before the ellipsis in the
358 // parameter list (and thus could never be accessed through va_arg).
359 if (LF->getFunctionType ()->isVarArg ()) {
360 for (unsigned i = 0; i < 6; ++i) {
361 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
362 assert (IAR != IAREnd
363 && "About to dereference past end of IncomingArgRegs");
364 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
365 ArgOffset += 4;
366 }
367 // Reset the pointers now that we're done.
368 ArgOffset = 68;
369 IAR = &IncomingArgRegs[0];
370 }
371
372 // Copy args out of their incoming hard regs or stack slots into virtual regs.
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000373 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E; ++I) {
374 Argument &A = *I;
375 unsigned ArgReg = getReg (A);
376 if (getClassB (A.getType ()) < cLong) {
377 // Get it out of the incoming arg register
378 if (ArgOffset < 92) {
379 assert (IAR != IAREnd
380 && "About to dereference past end of IncomingArgRegs");
381 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
382 } else {
383 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
384 BuildMI (BB, V8::LD, 3, ArgReg).addFrameIndex (FI).addSImm (0);
385 }
386 ArgOffset += 4;
387 } else if (getClassB (A.getType ()) == cFloat) {
388 if (ArgOffset < 92) {
Brian Gaeke1df468e2004-09-29 03:34:41 +0000389 // Single-fp args are passed in integer registers; go through
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000390 // memory to get them out of integer registers and back into fp. (Bleh!)
Brian Gaeke1df468e2004-09-29 03:34:41 +0000391 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
392 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000393 assert (IAR != IAREnd
394 && "About to dereference past end of IncomingArgRegs");
395 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
396 BuildMI (BB, V8::LDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
397 } else {
398 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
399 BuildMI (BB, V8::LDFri, 3, ArgReg).addFrameIndex (FI).addSImm (0);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000400 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000401 ArgOffset += 4;
402 } else if (getClassB (A.getType ()) == cDouble) {
403 // Double-fp args are passed in pairs of integer registers; go through
404 // memory to get them out of integer registers and back into fp. (Bleh!)
405 // We'd like to 'ldd' these right out of the incoming-args area,
406 // but it might not be 8-byte aligned (e.g., call x(int x, double d)).
407 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
408 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
409 if (ArgOffset < 92 && IAR != IAREnd) {
410 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (*IAR++);
411 } else {
412 unsigned TempReg = makeAnotherReg (Type::IntTy);
413 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
414 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0).addReg (TempReg);
Brian Gaeke6672f862004-09-30 19:44:32 +0000415 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000416 ArgOffset += 4;
417 if (ArgOffset < 92 && IAR != IAREnd) {
418 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (*IAR++);
419 } else {
420 unsigned TempReg = makeAnotherReg (Type::IntTy);
421 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
422 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4).addReg (TempReg);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000423 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000424 ArgOffset += 4;
425 BuildMI (BB, V8::LDDFri, 2, ArgReg).addFrameIndex (FI).addSImm (0);
426 } else if (getClassB (A.getType ()) == cLong) {
427 // do the first half...
428 if (ArgOffset < 92) {
429 assert (IAR != IAREnd
430 && "About to dereference past end of IncomingArgRegs");
431 BuildMI (BB, V8::ORrr, 2, ArgReg).addReg (V8::G0).addReg (*IAR++);
432 } else {
433 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
434 BuildMI (BB, V8::LD, 2, ArgReg).addFrameIndex (FI).addSImm (0);
435 }
436 ArgOffset += 4;
437 // ...then do the second half
438 if (ArgOffset < 92) {
439 assert (IAR != IAREnd
440 && "About to dereference past end of IncomingArgRegs");
441 BuildMI (BB, V8::ORrr, 2, ArgReg+1).addReg (V8::G0).addReg (*IAR++);
442 } else {
443 int FI = F->getFrameInfo()->CreateFixedObject(4, ArgOffset);
444 BuildMI (BB, V8::LD, 2, ArgReg+1).addFrameIndex (FI).addSImm (0);
445 }
446 ArgOffset += 4;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000447 } else {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000448 assert (0 && "Unknown class?!");
Brian Gaeke812c4882004-07-16 10:31:25 +0000449 }
Brian Gaeke562cb162004-04-07 17:04:09 +0000450 }
Brian Gaeked90282d2004-11-19 20:57:24 +0000451
452 // If the function takes variable number of arguments, remember the fp
453 // offset for the start of the first vararg value... this is used to expand
454 // llvm.va_start.
455 if (LF->getFunctionType ()->isVarArg ())
456 VarArgsOffset = ArgOffset;
Brian Gaeke562cb162004-04-07 17:04:09 +0000457}
458
Brian Gaeke6c868a42004-06-17 22:34:08 +0000459void V8ISel::SelectPHINodes() {
460 const TargetInstrInfo &TII = *TM.getInstrInfo();
461 const Function &LF = *F->getFunction(); // The LLVM function...
462 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
463 const BasicBlock *BB = I;
464 MachineBasicBlock &MBB = *MBBMap[I];
465
466 // Loop over all of the PHI nodes in the LLVM basic block...
467 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
468 for (BasicBlock::const_iterator I = BB->begin();
469 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
470
471 // Create a new machine instr PHI node, and insert it.
472 unsigned PHIReg = getReg(*PN);
473 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
474 V8::PHI, PN->getNumOperands(), PHIReg);
475
476 MachineInstr *LongPhiMI = 0;
477 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
478 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
479 V8::PHI, PN->getNumOperands(), PHIReg+1);
480
481 // PHIValues - Map of blocks to incoming virtual registers. We use this
482 // so that we only initialize one incoming value for a particular block,
483 // even if the block has multiple entries in the PHI node.
484 //
485 std::map<MachineBasicBlock*, unsigned> PHIValues;
486
487 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
488 MachineBasicBlock *PredMBB = 0;
489 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
490 PE = MBB.pred_end (); PI != PE; ++PI)
491 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
492 PredMBB = *PI;
493 break;
494 }
495 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
496
497 unsigned ValReg;
498 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
499 PHIValues.lower_bound(PredMBB);
500
501 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
502 // We already inserted an initialization of the register for this
503 // predecessor. Recycle it.
504 ValReg = EntryIt->second;
505
506 } else {
507 // Get the incoming value into a virtual register.
508 //
509 Value *Val = PN->getIncomingValue(i);
510
511 // If this is a constant or GlobalValue, we may have to insert code
512 // into the basic block to compute it into a virtual register.
513 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
514 isa<GlobalValue>(Val)) {
515 // Simple constants get emitted at the end of the basic block,
516 // before any terminator instructions. We "know" that the code to
517 // move a constant into a register will never clobber any flags.
518 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
519 } else {
520 // Because we don't want to clobber any values which might be in
521 // physical registers with the computation of this constant (which
522 // might be arbitrarily complex if it is a constant expression),
523 // just insert the computation at the top of the basic block.
524 MachineBasicBlock::iterator PI = PredMBB->begin();
525
526 // Skip over any PHI nodes though!
527 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
528 ++PI;
529
530 ValReg = getReg(Val, PredMBB, PI);
531 }
532
533 // Remember that we inserted a value for this PHI for this predecessor
534 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
535 }
536
537 PhiMI->addRegOperand(ValReg);
538 PhiMI->addMachineBasicBlockOperand(PredMBB);
539 if (LongPhiMI) {
540 LongPhiMI->addRegOperand(ValReg+1);
541 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
542 }
543 }
544
545 // Now that we emitted all of the incoming values for the PHI node, make
546 // sure to reposition the InsertPoint after the PHI that we just added.
547 // This is needed because we might have inserted a constant into this
548 // block, right after the PHI's which is before the old insert point!
549 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
550 ++PHIInsertPoint;
551 }
552 }
553}
554
Chris Lattner1c809c52004-02-29 00:27:00 +0000555bool V8ISel::runOnFunction(Function &Fn) {
556 // First pass over the function, lower any unknown intrinsic functions
557 // with the IntrinsicLowering class.
558 LowerUnknownIntrinsicFunctionCalls(Fn);
559
560 F = &MachineFunction::construct(&Fn, TM);
561
562 // Create all of the machine basic blocks for the function...
563 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
564 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
565
566 BB = &F->front();
567
568 // Set up a frame object for the return address. This is used by the
569 // llvm.returnaddress & llvm.frameaddress intrinisics.
570 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
571
572 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000573 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000574
575 // Instruction select everything except PHI nodes
576 visit(Fn);
577
578 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000579 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000580
581 RegMap.clear();
582 MBBMap.clear();
583 F = 0;
584 // We always build a machine code representation for the function
585 return true;
586}
587
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000588void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000589 Value *Op = I.getOperand(0);
590 unsigned DestReg = getReg(I);
591 MachineBasicBlock::iterator MI = BB->end();
592 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
593}
594
Brian Gaekea54df252004-11-19 18:48:10 +0000595unsigned V8ISel::emitIntegerCast (MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000596 MachineBasicBlock::iterator IP, const Type *oldTy,
597 unsigned SrcReg, const Type *newTy,
598 unsigned DestReg) {
599 if (oldTy == newTy) {
600 // No-op cast - just emit a copy; assume the reg. allocator will zap it.
601 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg(SrcReg);
Brian Gaekea54df252004-11-19 18:48:10 +0000602 return SrcReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000603 }
604 // Emit left-shift, then right-shift to sign- or zero-extend.
605 unsigned TmpReg = makeAnotherReg (newTy);
606 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
607 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg).addZImm (shiftWidth).addReg(SrcReg);
608 if (newTy->isSigned ()) { // sign-extend with SRA
609 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
610 } else { // zero-extend with SRL
611 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg);
612 }
Brian Gaekea54df252004-11-19 18:48:10 +0000613 // Return the temp reg. in case this is one half of a cast to long.
614 return TmpReg;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000615}
616
617void V8ISel::emitFPToIntegerCast (MachineBasicBlock *BB,
618 MachineBasicBlock::iterator IP,
619 const Type *oldTy, unsigned SrcReg,
620 const Type *newTy, unsigned DestReg) {
621 unsigned FPCastOpcode, FPStoreOpcode, FPSize, FPAlign;
622 unsigned oldTyClass = getClassB(oldTy);
623 if (oldTyClass == cFloat) {
624 FPCastOpcode = V8::FSTOI; FPStoreOpcode = V8::STFri; FPSize = 4;
625 FPAlign = TM.getTargetData().getFloatAlignment();
626 } else { // it's a double
627 FPCastOpcode = V8::FDTOI; FPStoreOpcode = V8::STDFri; FPSize = 8;
628 FPAlign = TM.getTargetData().getDoubleAlignment();
629 }
630 unsigned TempReg = makeAnotherReg (oldTy);
631 BuildMI (*BB, IP, FPCastOpcode, 1, TempReg).addReg (SrcReg);
632 int FI = F->getFrameInfo()->CreateStackObject(FPSize, FPAlign);
633 BuildMI (*BB, IP, FPStoreOpcode, 3).addFrameIndex (FI).addSImm (0)
634 .addReg (TempReg);
635 unsigned TempReg2 = makeAnotherReg (newTy);
636 BuildMI (*BB, IP, V8::LD, 3, TempReg2).addFrameIndex (FI).addSImm (0);
637 emitIntegerCast (BB, IP, Type::IntTy, TempReg2, newTy, DestReg);
638}
639
Brian Gaeke00e514e2004-06-24 06:33:00 +0000640/// emitCastOperation - Common code shared between visitCastInst and constant
641/// expression cast support.
642///
643void V8ISel::emitCastOperation(MachineBasicBlock *BB,
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000644 MachineBasicBlock::iterator IP, Value *Src,
645 const Type *DestTy, unsigned DestReg) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000646 const Type *SrcTy = Src->getType();
647 unsigned SrcClass = getClassB(SrcTy);
648 unsigned DestClass = getClassB(DestTy);
649 unsigned SrcReg = getReg(Src, BB, IP);
650
651 const Type *oldTy = SrcTy;
652 const Type *newTy = DestTy;
653 unsigned oldTyClass = SrcClass;
654 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000655
Brian Gaeke429022b2004-05-08 06:36:14 +0000656 if (oldTyClass < cLong && newTyClass < cLong) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000657 emitIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
658 } else switch (newTyClass) {
659 case cByte:
660 case cShort:
661 case cInt:
Brian Gaeke495a0972004-06-24 21:22:08 +0000662 switch (oldTyClass) {
Brian Gaekea54df252004-11-19 18:48:10 +0000663 case cLong:
664 // Treat it like a cast from the lower half of the value.
665 emitIntegerCast (BB, IP, Type::IntTy, SrcReg+1, newTy, DestReg);
666 break;
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000667 case cFloat:
668 case cDouble:
669 emitFPToIntegerCast (BB, IP, oldTy, SrcReg, newTy, DestReg);
670 break;
671 default: goto not_yet;
672 }
673 return;
674
675 case cFloat:
676 switch (oldTyClass) {
677 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000678 case cFloat:
679 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
680 break;
681 case cDouble:
682 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
683 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000684 default: {
685 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000686 // cast integer type to float. Store it to a stack slot and then load
Brian Gaeke495a0972004-06-24 21:22:08 +0000687 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000688 unsigned TmpReg = makeAnotherReg (newTy);
689 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
690 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
691 .addReg (SrcReg);
692 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
693 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000694 break;
695 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000696 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000697 return;
698
699 case cDouble:
Brian Gaeke495a0972004-06-24 21:22:08 +0000700 switch (oldTyClass) {
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000701 case cLong: goto not_yet;
Brian Gaeke495a0972004-06-24 21:22:08 +0000702 case cFloat:
703 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
704 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000705 case cDouble: // use double move pseudo-instr
706 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000707 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000708 default: {
709 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
710 unsigned TmpReg = makeAnotherReg (newTy);
711 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
712 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
713 .addReg (SrcReg);
714 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
715 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
716 break;
717 }
718 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000719 return;
720
721 case cLong:
722 switch (oldTyClass) {
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000723 case cByte:
724 case cShort:
Brian Gaekea54df252004-11-19 18:48:10 +0000725 case cInt: {
726 // Cast to (u)int in the bottom half, and sign(zero) extend in the top
727 // half.
728 const Type *OldHalfTy = oldTy->isSigned() ? Type::IntTy : Type::UIntTy;
729 const Type *NewHalfTy = newTy->isSigned() ? Type::IntTy : Type::UIntTy;
730 unsigned TempReg = emitIntegerCast (BB, IP, OldHalfTy, SrcReg,
731 NewHalfTy, DestReg+1);
732 if (newTy->isSigned ()) {
733 BuildMI (*BB, IP, V8::SRAri, 2, DestReg).addReg (TempReg)
734 .addZImm (31);
735 } else {
736 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0)
737 .addReg (V8::G0);
738 }
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000739 break;
Brian Gaekea54df252004-11-19 18:48:10 +0000740 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000741 case cLong:
Brian Gaeke7c0afe02004-11-18 07:43:33 +0000742 // Just copy both halves.
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000743 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
744 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
745 .addReg (SrcReg+1);
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000746 break;
747 default: goto not_yet;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000748 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000749 return;
750
751 default: goto not_yet;
Brian Gaekee302a7e2004-05-07 21:39:30 +0000752 }
Brian Gaeke8b6c1ff2004-10-14 19:39:34 +0000753 return;
754not_yet:
755 std::cerr << "Sorry, cast still unsupported: SrcTy = " << *SrcTy
756 << ", DestTy = " << *DestTy << "\n";
757 abort ();
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000758}
759
Brian Gaekef3334eb2004-04-07 17:29:37 +0000760void V8ISel::visitLoadInst(LoadInst &I) {
761 unsigned DestReg = getReg (I);
762 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000763 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000764 case cByte:
765 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000766 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000767 else
Brian Gaeke44733032004-06-24 07:36:48 +0000768 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000769 return;
770 case cShort:
771 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000772 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000773 else
Brian Gaeke44733032004-06-24 07:36:48 +0000774 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000775 return;
776 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000777 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000778 return;
779 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000780 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
781 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
782 return;
783 case cFloat:
784 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
785 return;
786 case cDouble:
787 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000788 return;
789 default:
790 std::cerr << "Load instruction not handled: " << I;
791 abort ();
792 return;
793 }
794}
795
796void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000797 Value *SrcVal = I.getOperand (0);
798 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000799 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000800 switch (getClassB (SrcVal->getType ())) {
801 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000802 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000803 return;
804 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000805 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000806 return;
807 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000808 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000809 return;
810 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000811 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
812 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
813 return;
814 case cFloat:
815 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
816 return;
817 case cDouble:
818 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000819 return;
820 default:
821 std::cerr << "Store instruction not handled: " << I;
822 abort ();
823 return;
824 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000825}
826
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000827void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000828 MachineInstr *TheCall;
829 // Is it an intrinsic function call?
830 if (Function *F = I.getCalledFunction()) {
831 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
832 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
833 return;
834 }
835 }
836
Brian Gaeke50094ed2004-10-10 19:57:18 +0000837 unsigned extraStack = 0;
838 // How much extra call stack will we need?
839 for (unsigned i = 7; i < I.getNumOperands (); ++i) {
840 switch (getClassB (I.getOperand (i)->getType ())) {
841 case cLong: extraStack += 8; break;
842 case cFloat: extraStack += 4; break;
843 case cDouble: extraStack += 8; break;
844 default: extraStack += 4; break;
845 }
846 }
Brian Gaeke04fe7472004-11-14 05:19:00 +0000847 // Round up extra stack size to the nearest doubleword.
848 if (extraStack) { extraStack = (extraStack + 7) & ~7; }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000849
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000850 // Deal with args
Brian Gaeke562cb162004-04-07 17:04:09 +0000851 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000852 V8::O4, V8::O5 };
Brian Gaeke24b90c32004-11-14 03:22:07 +0000853 const unsigned *OAREnd = &OutgoingArgRegs[6];
Brian Gaeke6931fd62004-11-04 00:27:04 +0000854 const unsigned *OAR = &OutgoingArgRegs[0];
Brian Gaeke24b90c32004-11-14 03:22:07 +0000855 unsigned ArgOffset = 68;
Brian Gaekeda9b3662004-11-14 06:32:08 +0000856 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKDOWN, 1).addImm (extraStack);
Brian Gaeke50094ed2004-10-10 19:57:18 +0000857 for (unsigned i = 1; i < I.getNumOperands (); ++i) {
858 unsigned ArgReg = getReg (I.getOperand (i));
Brian Gaeke24b90c32004-11-14 03:22:07 +0000859 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
860 // Schlep it over into the incoming arg register
861 if (ArgOffset < 92) {
862 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
863 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000864 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000865 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000866 }
Brian Gaeke24b90c32004-11-14 03:22:07 +0000867 ArgOffset += 4;
868 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
869 if (ArgOffset < 92) {
870 // Single-fp args are passed in integer registers; go through
871 // memory to get them out of FP registers. (Bleh!)
872 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
873 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
874 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
875 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
876 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
877 } else {
878 BuildMI (BB, V8::STFri, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
879 }
880 ArgOffset += 4;
881 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
882 // Double-fp args are passed in pairs of integer registers; go through
883 // memory to get them out of FP registers. (Bleh!)
884 // We'd like to 'std' these right onto the outgoing-args area, but it might
885 // not be 8-byte aligned (e.g., call x(int x, double d)). sigh.
886 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
887 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
888 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0).addReg (ArgReg);
889 if (ArgOffset < 92 && OAR != OAREnd) {
890 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
891 BuildMI (BB, V8::LD, 2, *OAR++).addFrameIndex (FI).addSImm (0);
892 } else {
893 unsigned TempReg = makeAnotherReg (Type::IntTy);
894 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (0);
895 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
896 }
897 ArgOffset += 4;
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 (4);
901 } else {
902 unsigned TempReg = makeAnotherReg (Type::IntTy);
903 BuildMI (BB, V8::LD, 2, TempReg).addFrameIndex (FI).addSImm (4);
904 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (TempReg);
905 }
906 ArgOffset += 4;
907 } else if (getClassB (I.getOperand (i)->getType ()) == cLong) {
908 // do the first half...
909 if (ArgOffset < 92) {
910 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
911 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg);
912 } else {
913 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg);
914 }
915 ArgOffset += 4;
916 // ...then do the second half
917 if (ArgOffset < 92) {
918 assert (OAR != OAREnd && "About to dereference past end of OutgoingArgRegs");
919 BuildMI (BB, V8::ORrr, 2, *OAR++).addReg (V8::G0).addReg (ArgReg+1);
920 } else {
921 BuildMI (BB, V8::ST, 3).addReg (V8::SP).addSImm (ArgOffset).addReg (ArgReg+1);
922 }
923 ArgOffset += 4;
Brian Gaeke50094ed2004-10-10 19:57:18 +0000924 } else {
Brian Gaeke24b90c32004-11-14 03:22:07 +0000925 assert (0 && "Unknown class?!");
Brian Gaeked54c38b2004-04-07 16:41:22 +0000926 }
Brian Gaeke50094ed2004-10-10 19:57:18 +0000927 }
Brian Gaeked54c38b2004-04-07 16:41:22 +0000928
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000929 // Emit call instruction
930 if (Function *F = I.getCalledFunction ()) {
931 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
932 } else { // Emit an indirect call...
933 unsigned Reg = getReg (I.getCalledValue ());
934 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
935 }
936
Brian Gaeke50094ed2004-10-10 19:57:18 +0000937 if (extraStack) BuildMI (BB, V8::ADJCALLSTACKUP, 1).addImm (extraStack);
938
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000939 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000940 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000941 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000942 unsigned DestReg = getReg (I);
Brian Gaeke299b39d2004-10-10 20:34:17 +0000943 switch (getClassB (I.getType ())) {
Brian Gaekeea8494b2004-04-06 22:09:23 +0000944 case cByte:
945 case cShort:
946 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000947 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
948 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000949 case cFloat:
950 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
951 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000952 case cDouble:
953 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
954 break;
955 case cLong:
956 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
957 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
958 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000959 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000960 std::cerr << "Return type of call instruction not handled: " << I;
961 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000962 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000963}
Chris Lattner1c809c52004-02-29 00:27:00 +0000964
965void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000966 if (I.getNumOperands () == 1) {
967 unsigned RetValReg = getReg (I.getOperand (0));
Brian Gaeke299b39d2004-10-10 20:34:17 +0000968 switch (getClassB (I.getOperand (0)->getType ())) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000969 case cByte:
970 case cShort:
971 case cInt:
972 // Schlep it over into i0 (where it will become o0 after restore).
973 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
974 break;
Brian Gaekef9a75462004-07-08 07:22:27 +0000975 case cFloat:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000976 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
Brian Gaekef9a75462004-07-08 07:22:27 +0000977 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000978 case cDouble:
979 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000980 break;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000981 case cLong:
982 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
983 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
984 break;
Brian Gaeke08f64c32004-03-06 05:32:28 +0000985 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000986 std::cerr << "Return instruction of this type not handled: " << I;
987 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000988 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000989 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000990
Brian Gaeke08f64c32004-03-06 05:32:28 +0000991 // Just emit a 'retl' instruction to return.
992 BuildMI(BB, V8::RETL, 0);
993 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000994}
995
Brian Gaeke532e60c2004-05-08 04:21:17 +0000996static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
997 Function::iterator I = BB; ++I; // Get iterator to next block
998 return I != BB->getParent()->end() ? &*I : 0;
999}
1000
1001/// visitBranchInst - Handles conditional and unconditional branches.
1002///
1003void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +00001004 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +00001005 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
1006 BB->addSuccessor (takenSuccMBB);
1007 if (I.isConditional()) { // conditional branch
1008 BasicBlock *notTakenSucc = I.getSuccessor (1);
1009 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
1010 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001011
Brian Gaeke6c868a42004-06-17 22:34:08 +00001012 // CondReg=(<condition>);
1013 // If (CondReg==0) goto notTakenSuccMBB;
1014 unsigned CondReg = getReg (I.getCondition ());
1015 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
1016 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001017 }
Brian Gaeke6c868a42004-06-17 22:34:08 +00001018 // goto takenSuccMBB;
1019 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001020}
1021
1022/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
1023/// constant expression GEP support.
1024///
Brian Gaeke9f564822004-05-08 05:27:20 +00001025void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +00001026 MachineBasicBlock::iterator IP,
1027 Value *Src, User::op_iterator IdxBegin,
1028 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +00001029 const TargetData &TD = TM.getTargetData ();
1030 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001031 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +00001032
1033 // GEPs have zero or more indices; we must perform a struct access
1034 // or array access for each one.
1035 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
1036 ++oi) {
1037 Value *idx = *oi;
1038 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
1039 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
1040 // It's a struct access. idx is the index into the structure,
1041 // which names the field. Use the TargetData structure to
1042 // pick out what the layout of the structure is in memory.
1043 // Use the (constant) structure index's value to find the
1044 // right byte offset from the StructLayout class's list of
1045 // structure member offsets.
1046 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
1047 unsigned memberOffset =
1048 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
1049 // Emit an ADD to add memberOffset to the basePtr.
1050 BuildMI (*MBB, IP, V8::ADDri, 2,
1051 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
1052 // The next type is the member of the structure selected by the
1053 // index.
1054 Ty = StTy->getElementType (fieldIndex);
1055 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
1056 // It's an array or pointer access: [ArraySize x ElementType].
1057 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1058 // must find the size of the pointed-to type (Not coincidentally, the next
1059 // type is the type of the elements in the array).
1060 Ty = SqTy->getElementType ();
1061 unsigned elementSize = TD.getTypeSize (Ty);
1062 unsigned idxReg = getReg (idx, MBB, IP);
1063 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
1064 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001065 copyConstantToRegister (MBB, IP,
1066 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
Brian Gaeke9f564822004-05-08 05:27:20 +00001067 // Emit a SMUL to multiply the register holding the index by
1068 // elementSize, putting the result in OffsetReg.
1069 BuildMI (*MBB, IP, V8::SMULrr, 2,
1070 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
1071 // Emit an ADD to add OffsetReg to the basePtr.
1072 BuildMI (*MBB, IP, V8::ADDrr, 2,
1073 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1074 }
1075 basePtrReg = nextBasePtrReg;
1076 }
1077 // After we have processed all the indices, the result is left in
1078 // basePtrReg. Move it to the register where we were expected to
1079 // put the answer.
1080 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +00001081}
1082
1083void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
1084 unsigned outputReg = getReg (I);
1085 emitGEPOperation (BB, BB->end (), I.getOperand (0),
1086 I.op_begin ()+1, I.op_end (), outputReg);
1087}
1088
Brian Gaeked6a10532004-06-15 21:09:46 +00001089
Chris Lattner4be7ca52004-04-07 04:27:16 +00001090void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001091 unsigned DestReg = getReg (I);
1092 unsigned Op0Reg = getReg (I.getOperand (0));
1093 unsigned Op1Reg = getReg (I.getOperand (1));
1094
Brian Gaekeec3227f2004-06-27 22:47:33 +00001095 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +00001096 unsigned OpCase = ~0;
1097
Brian Gaekeec3227f2004-06-27 22:47:33 +00001098 if (Class > cLong) {
1099 switch (I.getOpcode ()) {
1100 case Instruction::Add: OpCase = 0; break;
1101 case Instruction::Sub: OpCase = 1; break;
1102 case Instruction::Mul: OpCase = 2; break;
1103 case Instruction::Div: OpCase = 3; break;
1104 default: visitInstruction (I); return;
1105 }
1106 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
1107 V8::FSUBS, V8::FSUBD,
1108 V8::FMULS, V8::FMULD,
1109 V8::FDIVS, V8::FDIVD };
1110 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
1111 .addReg (Op0Reg).addReg (Op1Reg);
1112 return;
1113 }
1114
1115 unsigned ResultReg = DestReg;
Brian Gaeke1df468e2004-09-29 03:34:41 +00001116 if (Class != cInt && Class != cLong)
Brian Gaekeec3227f2004-06-27 22:47:33 +00001117 ResultReg = makeAnotherReg (I.getType ());
1118
Brian Gaeke1df468e2004-09-29 03:34:41 +00001119 if (Class == cLong) {
1120 DEBUG (std::cerr << "Class = cLong\n");
1121 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
1122 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
1123 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
1124 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
1125 }
1126
1127 // FIXME: support long, ulong.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001128 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +00001129 case Instruction::Add: OpCase = 0; break;
1130 case Instruction::Sub: OpCase = 1; break;
1131 case Instruction::Mul: OpCase = 2; break;
1132 case Instruction::And: OpCase = 3; break;
1133 case Instruction::Or: OpCase = 4; break;
1134 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +00001135 case Instruction::Shl: OpCase = 6; break;
1136 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +00001137
1138 case Instruction::Div:
1139 case Instruction::Rem: {
1140 unsigned Dest = ResultReg;
1141 if (I.getOpcode() == Instruction::Rem)
1142 Dest = makeAnotherReg(I.getType());
1143
1144 // FIXME: this is probably only right for 32 bit operands.
1145 if (I.getType ()->isSigned()) {
1146 unsigned Tmp = makeAnotherReg (I.getType ());
1147 // Sign extend into the Y register
1148 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
1149 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
1150 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
1151 } else {
1152 // Zero extend into the Y register, ie, just set it to zero
1153 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
1154 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +00001155 }
Chris Lattner22ede702004-04-07 04:06:46 +00001156
1157 if (I.getOpcode() == Instruction::Rem) {
1158 unsigned Tmp = makeAnotherReg (I.getType ());
1159 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
1160 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +00001161 }
Chris Lattner22ede702004-04-07 04:06:46 +00001162 break;
1163 }
1164 default:
1165 visitInstruction (I);
1166 return;
1167 }
1168
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001169 static const unsigned Opcodes[] = {
1170 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
1171 V8::SLLrr, V8::SRLrr, V8::SRArr
1172 };
Chris Lattner22ede702004-04-07 04:06:46 +00001173 if (OpCase != ~0U) {
Chris Lattner22ede702004-04-07 04:06:46 +00001174 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001175 }
1176
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001177 switch (getClassB (I.getType ())) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001178 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001179 if (I.getType ()->isSigned ()) { // add byte
1180 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1181 } else { // add ubyte
1182 unsigned TmpReg = makeAnotherReg (I.getType ());
1183 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1184 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1185 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001186 break;
1187 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001188 if (I.getType ()->isSigned ()) { // add short
1189 unsigned TmpReg = makeAnotherReg (I.getType ());
1190 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1191 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1192 } else { // add ushort
1193 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +00001194 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1195 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +00001196 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001197 break;
1198 case cInt:
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001199 // Nothing to do here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001200 break;
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001201 case cLong:
1202 // Only support and, or, xor.
1203 if (OpCase < 3 || OpCase > 5) {
1204 visitInstruction (I);
1205 return;
1206 }
1207 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +00001208 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1209 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001210 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001211 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001212 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001213 }
1214}
1215
Misha Brukmanea091262004-06-30 21:47:40 +00001216void V8ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner4d0cda42004-04-07 05:04:51 +00001217 unsigned Op0Reg = getReg (I.getOperand (0));
1218 unsigned Op1Reg = getReg (I.getOperand (1));
1219 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +00001220 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +00001221
1222 // Compare the two values.
Brian Gaeke3a085892004-07-08 09:08:35 +00001223 assert (getClass (Ty) != cLong && "can't setcc on longs yet");
1224 if (getClass (Ty) < cLong) {
1225 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
1226 } else if (getClass (Ty) == cFloat) {
1227 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1228 } else if (getClass (Ty) == cDouble) {
1229 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1230 }
Chris Lattner4d0cda42004-04-07 05:04:51 +00001231
Brian Gaeke429022b2004-05-08 06:36:14 +00001232 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001233 switch (I.getOpcode()) {
1234 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +00001235 case Instruction::SetEQ: BranchIdx = 0; break;
1236 case Instruction::SetNE: BranchIdx = 1; break;
1237 case Instruction::SetLT: BranchIdx = 2; break;
1238 case Instruction::SetGT: BranchIdx = 3; break;
1239 case Instruction::SetLE: BranchIdx = 4; break;
1240 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001241 }
Brian Gaeke3a085892004-07-08 09:08:35 +00001242 unsigned Column = 0;
Brian Gaekeb3e00172004-11-17 22:06:56 +00001243 if (Ty->isSigned() && !Ty->isFloatingPoint()) Column = 1;
1244 if (Ty->isFloatingPoint()) Column = 2;
Brian Gaeke3a085892004-07-08 09:08:35 +00001245 static unsigned OpcodeTab[3*6] = {
1246 // LLVM SparcV8
1247 // unsigned signed fp
1248 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1249 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1250 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1251 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1252 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1253 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
Brian Gaeke429022b2004-05-08 06:36:14 +00001254 };
Brian Gaeke3a085892004-07-08 09:08:35 +00001255 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
Brian Gaeke6c868a42004-06-17 22:34:08 +00001256
1257 MachineBasicBlock *thisMBB = BB;
1258 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1259 // thisMBB:
1260 // ...
1261 // subcc %reg0, %reg1, %g0
1262 // bCC copy1MBB
1263 // ba copy0MBB
1264
1265 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1266 // if we could insert other, non-terminator instructions after the
1267 // bCC. But MBB->getFirstTerminator() can't understand this.
1268 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1269 F->getBasicBlockList ().push_back (copy1MBB);
1270 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1271 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1272 F->getBasicBlockList ().push_back (copy0MBB);
1273 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1274 // Update machine-CFG edges
1275 BB->addSuccessor (copy1MBB);
1276 BB->addSuccessor (copy0MBB);
1277
1278 // copy0MBB:
1279 // %FalseValue = or %G0, 0
1280 // ba sinkMBB
1281 BB = copy0MBB;
1282 unsigned FalseValue = makeAnotherReg (I.getType ());
1283 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1284 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1285 F->getBasicBlockList ().push_back (sinkMBB);
1286 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1287 // Update machine-CFG edges
1288 BB->addSuccessor (sinkMBB);
1289
1290 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1291 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1292 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1293 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1294
1295 // copy1MBB:
1296 // %TrueValue = or %G0, 1
1297 // ba sinkMBB
1298 BB = copy1MBB;
1299 unsigned TrueValue = makeAnotherReg (I.getType ());
1300 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1301 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1302 // Update machine-CFG edges
1303 BB->addSuccessor (sinkMBB);
1304
1305 // sinkMBB:
1306 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1307 // ...
1308 BB = sinkMBB;
1309 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1310 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001311}
1312
Brian Gaekec93a7522004-06-18 05:19:16 +00001313void V8ISel::visitAllocaInst(AllocaInst &I) {
1314 // Find the data size of the alloca inst's getAllocatedType.
1315 const Type *Ty = I.getAllocatedType();
1316 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001317
Brian Gaekec93a7522004-06-18 05:19:16 +00001318 unsigned ArraySizeReg = getReg (I.getArraySize ());
1319 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1320 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1321 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1322 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001323
1324 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1325 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001326
Brian Gaekec93a7522004-06-18 05:19:16 +00001327 // Round up TmpReg1 to nearest doubleword boundary:
1328 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1329 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001330
1331 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001332 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001333
1334 // Put a pointer to the space into the result register, by copying
1335 // the stack pointer.
1336 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1337
1338 // Inform the Frame Information that we have just allocated a variable-sized
1339 // object.
1340 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001341}
Chris Lattner1c809c52004-02-29 00:27:00 +00001342
1343/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1344/// function, lowering any calls to unknown intrinsic functions into the
1345/// equivalent LLVM code.
1346void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1347 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1348 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1349 if (CallInst *CI = dyn_cast<CallInst>(I++))
1350 if (Function *F = CI->getCalledFunction())
1351 switch (F->getIntrinsicID()) {
Brian Gaeked90282d2004-11-19 20:57:24 +00001352 case Intrinsic::vastart:
1353 case Intrinsic::vacopy:
1354 case Intrinsic::vaend:
1355 // We directly implement these intrinsics
Chris Lattner1c809c52004-02-29 00:27:00 +00001356 case Intrinsic::not_intrinsic: break;
1357 default:
1358 // All other intrinsic calls we must lower.
1359 Instruction *Before = CI->getPrev();
1360 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1361 if (Before) { // Move iterator to instruction after call
1362 I = Before; ++I;
1363 } else {
1364 I = BB->begin();
1365 }
1366 }
1367}
1368
1369
1370void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattner1c809c52004-02-29 00:27:00 +00001371 switch (ID) {
Brian Gaeke9e672a22004-11-19 18:53:59 +00001372 default:
1373 std::cerr << "Sorry, unknown intrinsic function call:\n" << CI; abort ();
1374
Brian Gaeked90282d2004-11-19 20:57:24 +00001375 case Intrinsic::vastart: {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001376 // Add the VarArgsOffset to the frame pointer, and copy it to the result.
Brian Gaeked90282d2004-11-19 20:57:24 +00001377 unsigned DestReg = getReg (CI);
1378 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (V8::FP).addSImm (VarArgsOffset);
1379 return;
1380 }
Brian Gaeke9e672a22004-11-19 18:53:59 +00001381
1382 case Intrinsic::vaend:
Brian Gaeke2f95ed62004-11-19 19:21:34 +00001383 // va_end is a no-op on SparcV8.
1384 return;
Brian Gaeke9e672a22004-11-19 18:53:59 +00001385
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001386 case Intrinsic::vacopy: {
1387 // Copy the va_list ptr (arg1) to the result.
1388 unsigned DestReg = getReg (CI), SrcReg = getReg (CI.getOperand (1));
1389 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
1390 return;
1391 }
Chris Lattner1c809c52004-02-29 00:27:00 +00001392 }
1393}
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001394
1395void V8ISel::visitVANextInst (VANextInst &I) {
Brian Gaekee6e7e3a2004-11-20 03:32:12 +00001396 // Add the type size to the vararg pointer (arg0).
1397 unsigned DestReg = getReg (I);
1398 unsigned SrcReg = getReg (I.getOperand (0));
1399 unsigned TySize = TM.getTargetData ().getTypeSize (I.getArgType ());
1400 BuildMI (BB, V8::ADDri, 2, DestReg).addReg (SrcReg).addSImm (TySize);
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001401}
1402
1403void V8ISel::visitVAArgInst (VAArgInst &I) {
Brian Gaekeb95cbee2004-11-20 22:50:42 +00001404 unsigned VAList = getReg (I.getOperand (0));
1405 unsigned DestReg = getReg (I);
1406
1407 switch (I.getType ()->getTypeID ()) {
1408 case Type::PointerTyID:
1409 case Type::UIntTyID:
1410 case Type::IntTyID:
1411 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1412 return;
1413
1414 case Type::ULongTyID:
1415 case Type::LongTyID:
1416 BuildMI (BB, V8::LD, 2, DestReg).addReg (VAList).addSImm (0);
1417 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (VAList).addSImm (4);
1418 return;
1419
1420 case Type::FloatTyID:
1421 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (VAList).addSImm (0);
1422 return;
1423
1424 case Type::DoubleTyID:
1425
1426 default:
1427 std::cerr << "Sorry, vaarg instruction of this type still unsupported:\n"
1428 << I;
1429 abort ();
1430 return;
1431 }
Brian Gaekeb6c409a2004-11-19 21:08:18 +00001432}