blob: fe11d84d50612fce783e066e85108ea7b400eeaf [file] [log] [blame]
Chris Lattner1c809c52004-02-29 00:27:00 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a simple peephole instruction selector for the V8 target
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcV8.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000015#include "SparcV8InstrInfo.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000016#include "llvm/Support/Debug.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000017#include "llvm/Instructions.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000018#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000019#include "llvm/Constants.h"
Chris Lattner30483732004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaeke9df92822004-06-15 19:16:07 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Brian Gaekec93a7522004-06-18 05:19:16 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000024#include "llvm/CodeGen/MachineFunction.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000025#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/InstVisitor.h"
29#include "llvm/Support/CFG.h"
30using namespace llvm;
31
32namespace {
33 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
34 TargetMachine &TM;
35 MachineFunction *F; // The function we are compiling into
36 MachineBasicBlock *BB; // The current MBB we are compiling
37
38 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
39
40 // MBBMap - Mapping between LLVM BB -> Machine BB
41 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
42
43 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
44
45 /// runOnFunction - Top level implementation of instruction selection for
46 /// the entire function.
47 ///
48 bool runOnFunction(Function &Fn);
49
50 virtual const char *getPassName() const {
51 return "SparcV8 Simple Instruction Selection";
52 }
53
Brian Gaeke532e60c2004-05-08 04:21:17 +000054 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
55 /// constant expression GEP support.
56 ///
57 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
58 Value *Src, User::op_iterator IdxBegin,
59 User::op_iterator IdxEnd, unsigned TargetReg);
60
Brian Gaeke00e514e2004-06-24 06:33:00 +000061 /// emitCastOperation - Common code shared between visitCastInst and
62 /// constant expression cast support.
63 ///
64 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
65 Value *Src, const Type *DestTy, unsigned TargetReg);
66
Chris Lattner1c809c52004-02-29 00:27:00 +000067 /// visitBasicBlock - This method is called when we are visiting a new basic
68 /// block. This simply creates a new MachineBasicBlock to emit code into
69 /// and adds it to the current MachineFunction. Subsequent visit* for
70 /// instructions will be invoked for all instructions in the basic block.
71 ///
72 void visitBasicBlock(BasicBlock &LLVM_BB) {
73 BB = MBBMap[&LLVM_BB];
74 }
75
Chris Lattner4be7ca52004-04-07 04:27:16 +000076 void visitBinaryOperator(Instruction &I);
Brian Gaeked6a10532004-06-15 21:09:46 +000077 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
Misha Brukmanea091262004-06-30 21:47:40 +000078 void visitSetCondInst(SetCondInst &I);
Chris Lattner4be7ca52004-04-07 04:27:16 +000079 void visitCallInst(CallInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000080 void visitReturnInst(ReturnInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000081 void visitBranchInst(BranchInst &I);
Brian Gaeke3d11e8a2004-04-13 18:27:46 +000082 void visitCastInst(CastInst &I);
Brian Gaekef3334eb2004-04-07 17:29:37 +000083 void visitLoadInst(LoadInst &I);
84 void visitStoreInst(StoreInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000085 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
86 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaekec93a7522004-06-18 05:19:16 +000087 void visitAllocaInst(AllocaInst &I);
Brian Gaeke532e60c2004-05-08 04:21:17 +000088
Chris Lattner1c809c52004-02-29 00:27:00 +000089 void visitInstruction(Instruction &I) {
90 std::cerr << "Unhandled instruction: " << I;
91 abort();
92 }
93
94 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
95 /// function, lowering any calls to unknown intrinsic functions into the
96 /// equivalent LLVM code.
97 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000098 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
99
Brian Gaeke562cb162004-04-07 17:04:09 +0000100 void LoadArgumentsToVirtualRegs(Function *F);
101
Brian Gaeke6c868a42004-06-17 22:34:08 +0000102 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
103 /// because we have to generate our sources into the source basic blocks,
104 /// not the current one.
105 ///
106 void SelectPHINodes();
107
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000108 /// copyConstantToRegister - Output the instructions required to put the
109 /// specified constant into the specified register.
110 ///
111 void copyConstantToRegister(MachineBasicBlock *MBB,
112 MachineBasicBlock::iterator IP,
113 Constant *C, unsigned R);
114
115 /// makeAnotherReg - This method returns the next register number we haven't
116 /// yet used.
117 ///
118 /// Long values are handled somewhat specially. They are always allocated
119 /// as pairs of 32 bit integer values. The register number returned is the
120 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
121 /// of the long value.
122 ///
123 unsigned makeAnotherReg(const Type *Ty) {
124 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
125 "Current target doesn't have SparcV8 reg info??");
126 const SparcV8RegisterInfo *MRI =
127 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
128 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
129 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
130 // Create the lower part
131 F->getSSARegMap()->createVirtualRegister(RC);
132 // Create the upper part.
133 return F->getSSARegMap()->createVirtualRegister(RC)-1;
134 }
135
136 // Add the mapping of regnumber => reg class to MachineFunction
137 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
138 return F->getSSARegMap()->createVirtualRegister(RC);
139 }
140
141 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
142 unsigned getReg(Value *V) {
143 // Just append to the end of the current bb.
144 MachineBasicBlock::iterator It = BB->end();
145 return getReg(V, BB, It);
146 }
147 unsigned getReg(Value *V, MachineBasicBlock *MBB,
148 MachineBasicBlock::iterator IPt) {
149 unsigned &Reg = RegMap[V];
150 if (Reg == 0) {
151 Reg = makeAnotherReg(V->getType());
152 RegMap[V] = Reg;
153 }
154 // If this operand is a constant, emit the code to copy the constant into
155 // the register here...
156 //
157 if (Constant *C = dyn_cast<Constant>(V)) {
158 copyConstantToRegister(MBB, IPt, C, Reg);
159 RegMap.erase(V); // Assign a new name to this constant if ref'd again
160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000162 unsigned TmpReg = makeAnotherReg(V->getType());
163 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
164 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
165 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000166 RegMap.erase(V); // Assign a new name to this address if ref'd again
167 }
168
169 return Reg;
170 }
171
Chris Lattner1c809c52004-02-29 00:27:00 +0000172 };
173}
174
175FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
176 return new V8ISel(TM);
177}
178
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000179enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000180 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000181};
182
183static TypeClass getClass (const Type *T) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000184 switch (T->getTypeID()) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000185 case Type::UByteTyID: case Type::SByteTyID: return cByte;
186 case Type::UShortTyID: case Type::ShortTyID: return cShort;
Brian Gaeke562cb162004-04-07 17:04:09 +0000187 case Type::PointerTyID:
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000188 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000189 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000190 case Type::FloatTyID: return cFloat;
191 case Type::DoubleTyID: return cDouble;
192 default:
193 assert (0 && "Type of unknown class passed to getClass?");
194 return cByte;
195 }
196}
Chris Lattner0d538bb2004-04-07 04:36:53 +0000197static TypeClass getClassB(const Type *T) {
198 if (T == Type::BoolTy) return cByte;
199 return getClass(T);
200}
201
202
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000203
204/// copyConstantToRegister - Output the instructions required to put the
205/// specified constant into the specified register.
206///
207void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
208 MachineBasicBlock::iterator IP,
209 Constant *C, unsigned R) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000210 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
211 switch (CE->getOpcode()) {
212 case Instruction::GetElementPtr:
213 emitGEPOperation(MBB, IP, CE->getOperand(0),
214 CE->op_begin()+1, CE->op_end(), R);
215 return;
Brian Gaeke00e514e2004-06-24 06:33:00 +0000216 case Instruction::Cast:
217 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
218 return;
Brian Gaeke9df92822004-06-15 19:16:07 +0000219 default:
220 std::cerr << "Copying this constant expr not yet handled: " << *CE;
221 abort();
222 }
223 }
224
Brian Gaekee302a7e2004-05-07 21:39:30 +0000225 if (C->getType()->isIntegral ()) {
226 uint64_t Val;
Brian Gaeke9df92822004-06-15 19:16:07 +0000227 unsigned Class = getClassB (C->getType ());
228 if (Class == cLong) {
229 unsigned TmpReg = makeAnotherReg (Type::IntTy);
230 unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
231 // Copy the value into the register pair.
232 // R = top(more-significant) half, R+1 = bottom(less-significant) half
233 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000234 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
235 Val >> 32), R);
236 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
237 Val & 0xffffffffU), R+1);
Brian Gaeke9df92822004-06-15 19:16:07 +0000238 return;
239 }
240
241 assert(Class <= cInt && "Type not handled yet!");
242
Brian Gaekee302a7e2004-05-07 21:39:30 +0000243 if (C->getType() == Type::BoolTy) {
244 Val = (C == ConstantBool::True);
245 } else {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000246 ConstantInt *CI = cast<ConstantInt> (C);
Brian Gaekee302a7e2004-05-07 21:39:30 +0000247 Val = CI->getRawValue ();
248 }
Brian Gaeke9df92822004-06-15 19:16:07 +0000249 switch (Class) {
Brian Gaeke13dc4332004-06-24 09:17:47 +0000250 case cByte: Val = (int8_t) Val; break;
251 case cShort: Val = (int16_t) Val; break;
252 case cInt: Val = (int32_t) Val; break;
Brian Gaekee8061732004-03-04 00:56:25 +0000253 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000254 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000255 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000256 return;
257 }
Brian Gaeke13dc4332004-06-24 09:17:47 +0000258 if (Val == 0) {
259 BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
260 } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
261 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
262 } else {
263 unsigned TmpReg = makeAnotherReg (C->getType ());
264 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
265 .addSImm (((uint32_t) Val) >> 10);
266 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
267 .addSImm (((uint32_t) Val) & 0x03ff);
268 return;
269 }
Brian Gaekec93a7522004-06-18 05:19:16 +0000270 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
271 // We need to spill the constant to memory...
272 MachineConstantPool *CP = F->getConstantPool();
273 unsigned CPI = CP->getConstantPoolIndex(CFP);
274 const Type *Ty = CFP->getType();
Brian Gaeke1df468e2004-09-29 03:34:41 +0000275 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
276 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +0000277
278 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Brian Gaeke44733032004-06-24 07:36:48 +0000279 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000280 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addConstantPoolIndex (CPI);
281 BuildMI (*MBB, IP, V8::ORri, 2, AddrReg).addReg (TmpReg).addConstantPoolIndex (CPI);
282 BuildMI (*MBB, IP, LoadOpcode, 2, R).addReg (AddrReg).addSImm (0);
Brian Gaeke9df92822004-06-15 19:16:07 +0000283 } else if (isa<ConstantPointerNull>(C)) {
284 // Copy zero (null pointer) to the register.
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000285 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
Chris Lattner73302482004-07-18 07:26:17 +0000286 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
Brian Gaeke9df92822004-06-15 19:16:07 +0000287 // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
288 // that SETHI %reg,global == SETHI %reg,%hi(global) and
289 // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
290 unsigned TmpReg = makeAnotherReg (C->getType ());
Chris Lattner73302482004-07-18 07:26:17 +0000291 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
292 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
Brian Gaeke9df92822004-06-15 19:16:07 +0000293 } else {
294 std::cerr << "Offending constant: " << *C << "\n";
295 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000296 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000297}
Chris Lattner1c809c52004-02-29 00:27:00 +0000298
Brian Gaeke812c4882004-07-16 10:31:25 +0000299void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
300 unsigned ArgOffset;
Brian Gaeke562cb162004-04-07 17:04:09 +0000301 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
302 V8::I3, V8::I4, V8::I5 };
Brian Gaeke812c4882004-07-16 10:31:25 +0000303 // Add IMPLICIT_DEFs of input regs.
304 ArgOffset = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000305 for (Function::aiterator I = LF->abegin(), E = LF->aend();
306 I != E && ArgOffset < 6; ++I, ++ArgOffset) {
Brian Gaeke812c4882004-07-16 10:31:25 +0000307 unsigned Reg = getReg(*I);
308 switch (getClassB(I->getType())) {
309 case cByte:
310 case cShort:
311 case cInt:
312 case cFloat:
313 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
314 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000315 case cDouble:
316 case cLong:
317 // Double and Long use register pairs.
318 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
319 ++ArgOffset;
320 if (ArgOffset < 6)
321 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
322 break;
Brian Gaeke812c4882004-07-16 10:31:25 +0000323 default:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000324 assert (0 && "type not handled");
Brian Gaeke812c4882004-07-16 10:31:25 +0000325 return;
326 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000327 }
328
329 ArgOffset = 0;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000330 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E;
331 ++I, ++ArgOffset) {
Brian Gaeke562cb162004-04-07 17:04:09 +0000332 unsigned Reg = getReg(*I);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000333 if (ArgOffset < 6) {
334
335 switch (getClassB(I->getType())) {
336 case cByte:
337 case cShort:
338 case cInt:
339 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
340 .addReg (IncomingArgRegs[ArgOffset]);
341 break;
342 case cFloat: {
343 // Single-fp args are passed in integer registers; go through
344 // memory to get them into FP registers. (Bleh!)
345 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
346 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
347 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0)
348 .addReg (IncomingArgRegs[ArgOffset]);
349 BuildMI (BB, V8::LDFri, 2, Reg).addFrameIndex (FI).addSImm (0);
350 break;
351 }
352 default:
353 // FIXME: handle cDouble, cLong
354 assert (0 && "64-bit (double, long, etc.) function args not handled");
355 return;
356 }
357
358 } else {
359
360 switch (getClassB(I->getType())) {
361 case cByte:
362 case cShort:
363 case cInt: {
364 int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset));
365 BuildMI (BB, V8::LD, 2, Reg).addFrameIndex (FI).addSImm(0);
366 break;
367 }
368 case cFloat: {
369 int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset));
370 BuildMI (BB, V8::LDFri, 2, Reg).addFrameIndex (FI).addSImm(0);
371 break;
372 }
373 case cDouble: {
374 int FI = F->getFrameInfo()->CreateFixedObject(8, 68 + (4 * ArgOffset));
375 BuildMI (BB, V8::LDDFri, 2, Reg).addFrameIndex (FI).addSImm(0);
376 break;
377 }
378 default:
379 // FIXME: handle cLong
380 assert (0 && "64-bit integer (long/ulong) function args not handled");
381 return;
382 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000383 }
Brian Gaeke562cb162004-04-07 17:04:09 +0000384 }
Brian Gaeke812c4882004-07-16 10:31:25 +0000385
Brian Gaeke562cb162004-04-07 17:04:09 +0000386}
387
Brian Gaeke6c868a42004-06-17 22:34:08 +0000388void V8ISel::SelectPHINodes() {
389 const TargetInstrInfo &TII = *TM.getInstrInfo();
390 const Function &LF = *F->getFunction(); // The LLVM function...
391 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
392 const BasicBlock *BB = I;
393 MachineBasicBlock &MBB = *MBBMap[I];
394
395 // Loop over all of the PHI nodes in the LLVM basic block...
396 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
397 for (BasicBlock::const_iterator I = BB->begin();
398 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
399
400 // Create a new machine instr PHI node, and insert it.
401 unsigned PHIReg = getReg(*PN);
402 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
403 V8::PHI, PN->getNumOperands(), PHIReg);
404
405 MachineInstr *LongPhiMI = 0;
406 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
407 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
408 V8::PHI, PN->getNumOperands(), PHIReg+1);
409
410 // PHIValues - Map of blocks to incoming virtual registers. We use this
411 // so that we only initialize one incoming value for a particular block,
412 // even if the block has multiple entries in the PHI node.
413 //
414 std::map<MachineBasicBlock*, unsigned> PHIValues;
415
416 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
417 MachineBasicBlock *PredMBB = 0;
418 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
419 PE = MBB.pred_end (); PI != PE; ++PI)
420 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
421 PredMBB = *PI;
422 break;
423 }
424 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
425
426 unsigned ValReg;
427 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
428 PHIValues.lower_bound(PredMBB);
429
430 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
431 // We already inserted an initialization of the register for this
432 // predecessor. Recycle it.
433 ValReg = EntryIt->second;
434
435 } else {
436 // Get the incoming value into a virtual register.
437 //
438 Value *Val = PN->getIncomingValue(i);
439
440 // If this is a constant or GlobalValue, we may have to insert code
441 // into the basic block to compute it into a virtual register.
442 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
443 isa<GlobalValue>(Val)) {
444 // Simple constants get emitted at the end of the basic block,
445 // before any terminator instructions. We "know" that the code to
446 // move a constant into a register will never clobber any flags.
447 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
448 } else {
449 // Because we don't want to clobber any values which might be in
450 // physical registers with the computation of this constant (which
451 // might be arbitrarily complex if it is a constant expression),
452 // just insert the computation at the top of the basic block.
453 MachineBasicBlock::iterator PI = PredMBB->begin();
454
455 // Skip over any PHI nodes though!
456 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
457 ++PI;
458
459 ValReg = getReg(Val, PredMBB, PI);
460 }
461
462 // Remember that we inserted a value for this PHI for this predecessor
463 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
464 }
465
466 PhiMI->addRegOperand(ValReg);
467 PhiMI->addMachineBasicBlockOperand(PredMBB);
468 if (LongPhiMI) {
469 LongPhiMI->addRegOperand(ValReg+1);
470 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
471 }
472 }
473
474 // Now that we emitted all of the incoming values for the PHI node, make
475 // sure to reposition the InsertPoint after the PHI that we just added.
476 // This is needed because we might have inserted a constant into this
477 // block, right after the PHI's which is before the old insert point!
478 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
479 ++PHIInsertPoint;
480 }
481 }
482}
483
Chris Lattner1c809c52004-02-29 00:27:00 +0000484bool V8ISel::runOnFunction(Function &Fn) {
485 // First pass over the function, lower any unknown intrinsic functions
486 // with the IntrinsicLowering class.
487 LowerUnknownIntrinsicFunctionCalls(Fn);
488
489 F = &MachineFunction::construct(&Fn, TM);
490
491 // Create all of the machine basic blocks for the function...
492 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
493 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
494
495 BB = &F->front();
496
497 // Set up a frame object for the return address. This is used by the
498 // llvm.returnaddress & llvm.frameaddress intrinisics.
499 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
500
501 // Copy incoming arguments off of the stack and out of fixed registers.
Brian Gaeke562cb162004-04-07 17:04:09 +0000502 LoadArgumentsToVirtualRegs(&Fn);
Chris Lattner1c809c52004-02-29 00:27:00 +0000503
504 // Instruction select everything except PHI nodes
505 visit(Fn);
506
507 // Select the PHI nodes
Brian Gaeke6c868a42004-06-17 22:34:08 +0000508 SelectPHINodes();
Chris Lattner1c809c52004-02-29 00:27:00 +0000509
510 RegMap.clear();
511 MBBMap.clear();
512 F = 0;
513 // We always build a machine code representation for the function
514 return true;
515}
516
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000517void V8ISel::visitCastInst(CastInst &I) {
Brian Gaeke00e514e2004-06-24 06:33:00 +0000518 Value *Op = I.getOperand(0);
519 unsigned DestReg = getReg(I);
520 MachineBasicBlock::iterator MI = BB->end();
521 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
522}
523
524/// emitCastOperation - Common code shared between visitCastInst and constant
525/// expression cast support.
526///
527void V8ISel::emitCastOperation(MachineBasicBlock *BB,
528 MachineBasicBlock::iterator IP,
529 Value *Src, const Type *DestTy,
530 unsigned DestReg) {
531 const Type *SrcTy = Src->getType();
532 unsigned SrcClass = getClassB(SrcTy);
533 unsigned DestClass = getClassB(DestTy);
534 unsigned SrcReg = getReg(Src, BB, IP);
535
536 const Type *oldTy = SrcTy;
537 const Type *newTy = DestTy;
538 unsigned oldTyClass = SrcClass;
539 unsigned newTyClass = DestClass;
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000540
Brian Gaeke429022b2004-05-08 06:36:14 +0000541 if (oldTyClass < cLong && newTyClass < cLong) {
542 if (oldTyClass >= newTyClass) {
543 // Emit a reg->reg copy to do a equal-size or narrowing cast,
544 // and do sign/zero extension (necessary if we change signedness).
545 unsigned TmpReg1 = makeAnotherReg (newTy);
546 unsigned TmpReg2 = makeAnotherReg (newTy);
Brian Gaeke00e514e2004-06-24 06:33:00 +0000547 BuildMI (*BB, IP, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg);
Brian Gaeke429022b2004-05-08 06:36:14 +0000548 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000549 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000550 if (newTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000551 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000552 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000553 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000554 }
555 } else {
556 unsigned TmpReg1 = makeAnotherReg (oldTy);
557 unsigned TmpReg2 = makeAnotherReg (newTy);
558 unsigned TmpReg3 = makeAnotherReg (newTy);
559 // Widening integer cast. Make sure it's fully sign/zero-extended
560 // wrt the input type, then make sure it's fully sign/zero-extended wrt
561 // the output type. Kind of stupid, but simple...
562 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000563 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg);
Brian Gaeke429022b2004-05-08 06:36:14 +0000564 if (oldTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000565 BuildMI(*BB, IP, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000566 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000567 BuildMI(*BB, IP, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
Brian Gaeke429022b2004-05-08 06:36:14 +0000568 }
569 shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
Brian Gaeke00e514e2004-06-24 06:33:00 +0000570 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2);
Brian Gaeke429022b2004-05-08 06:36:14 +0000571 if (newTy->isSigned ()) { // sign-extend with SRA
Brian Gaeke00e514e2004-06-24 06:33:00 +0000572 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
Brian Gaeke429022b2004-05-08 06:36:14 +0000573 } else { // zero-extend with SRL
Brian Gaeke00e514e2004-06-24 06:33:00 +0000574 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
Brian Gaeke429022b2004-05-08 06:36:14 +0000575 }
Brian Gaekee302a7e2004-05-07 21:39:30 +0000576 }
577 } else {
Brian Gaeke495a0972004-06-24 21:22:08 +0000578 if (newTyClass == cFloat) {
Brian Gaekeec3227f2004-06-27 22:47:33 +0000579 assert (oldTyClass != cLong && "cast long to float not implemented yet");
Brian Gaeke495a0972004-06-24 21:22:08 +0000580 switch (oldTyClass) {
581 case cFloat:
582 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
583 break;
584 case cDouble:
585 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
586 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000587 default: {
588 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Brian Gaeke495a0972004-06-24 21:22:08 +0000589 // cast int to float. Store it to a stack slot and then load
590 // it using ldf into a floating point register. then do fitos.
Brian Gaekeec3227f2004-06-27 22:47:33 +0000591 unsigned TmpReg = makeAnotherReg (newTy);
592 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
593 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
594 .addReg (SrcReg);
595 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
596 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000597 break;
598 }
Brian Gaekeec3227f2004-06-27 22:47:33 +0000599 }
Brian Gaeke495a0972004-06-24 21:22:08 +0000600 } else if (newTyClass == cDouble) {
Brian Gaekeec3227f2004-06-27 22:47:33 +0000601 assert (oldTyClass != cLong && "cast long to double not implemented yet");
Brian Gaeke495a0972004-06-24 21:22:08 +0000602 switch (oldTyClass) {
603 case cFloat:
604 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
605 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000606 case cDouble: // use double move pseudo-instr
607 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
Brian Gaeke495a0972004-06-24 21:22:08 +0000608 break;
Brian Gaekeec3227f2004-06-27 22:47:33 +0000609 default: {
610 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
611 unsigned TmpReg = makeAnotherReg (newTy);
612 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
613 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
614 .addReg (SrcReg);
615 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
616 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
617 break;
618 }
619 }
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000620 } else if (newTyClass == cLong) {
621 if (oldTyClass == cLong) {
622 // Just copy it
623 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
624 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
625 .addReg (SrcReg+1);
626 } else {
627 std::cerr << "Cast still unsupported: SrcTy = "
628 << *SrcTy << ", DestTy = " << *DestTy << "\n";
629 abort ();
630 }
Brian Gaeke44733032004-06-24 07:36:48 +0000631 } else {
632 std::cerr << "Cast still unsupported: SrcTy = "
633 << *SrcTy << ", DestTy = " << *DestTy << "\n";
634 abort ();
635 }
Brian Gaekee302a7e2004-05-07 21:39:30 +0000636 }
Brian Gaeke3d11e8a2004-04-13 18:27:46 +0000637}
638
Brian Gaekef3334eb2004-04-07 17:29:37 +0000639void V8ISel::visitLoadInst(LoadInst &I) {
640 unsigned DestReg = getReg (I);
641 unsigned PtrReg = getReg (I.getOperand (0));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000642 switch (getClassB (I.getType ())) {
Brian Gaekef3334eb2004-04-07 17:29:37 +0000643 case cByte:
644 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000645 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000646 else
Brian Gaeke44733032004-06-24 07:36:48 +0000647 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000648 return;
649 case cShort:
650 if (I.getType ()->isSigned ())
Brian Gaeke44733032004-06-24 07:36:48 +0000651 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000652 else
Brian Gaeke44733032004-06-24 07:36:48 +0000653 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000654 return;
655 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000656 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000657 return;
658 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000659 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
660 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
661 return;
662 case cFloat:
663 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
664 return;
665 case cDouble:
666 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000667 return;
668 default:
669 std::cerr << "Load instruction not handled: " << I;
670 abort ();
671 return;
672 }
673}
674
675void V8ISel::visitStoreInst(StoreInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000676 Value *SrcVal = I.getOperand (0);
677 unsigned SrcReg = getReg (SrcVal);
Brian Gaekef3334eb2004-04-07 17:29:37 +0000678 unsigned PtrReg = getReg (I.getOperand (1));
Brian Gaeke532e60c2004-05-08 04:21:17 +0000679 switch (getClassB (SrcVal->getType ())) {
680 case cByte:
Brian Gaeke44733032004-06-24 07:36:48 +0000681 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000682 return;
683 case cShort:
Brian Gaeke44733032004-06-24 07:36:48 +0000684 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000685 return;
686 case cInt:
Brian Gaeke44733032004-06-24 07:36:48 +0000687 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000688 return;
689 case cLong:
Brian Gaeke44733032004-06-24 07:36:48 +0000690 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
691 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
692 return;
693 case cFloat:
694 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
695 return;
696 case cDouble:
697 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000698 return;
699 default:
700 std::cerr << "Store instruction not handled: " << I;
701 abort ();
702 return;
703 }
Brian Gaekef3334eb2004-04-07 17:29:37 +0000704}
705
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000706void V8ISel::visitCallInst(CallInst &I) {
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000707 MachineInstr *TheCall;
708 // Is it an intrinsic function call?
709 if (Function *F = I.getCalledFunction()) {
710 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
711 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
712 return;
713 }
714 }
715
716 // Deal with args
Brian Gaeked54c38b2004-04-07 16:41:22 +0000717 assert (I.getNumOperands () < 8
718 && "Can't handle pushing excess call args on the stack yet");
Brian Gaeke562cb162004-04-07 17:04:09 +0000719 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
Brian Gaeked54c38b2004-04-07 16:41:22 +0000720 V8::O4, V8::O5 };
721 for (unsigned i = 1; i < 7; ++i)
722 if (i < I.getNumOperands ()) {
723 unsigned ArgReg = getReg (I.getOperand (i));
Brian Gaeke812c4882004-07-16 10:31:25 +0000724 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
725 // Schlep it over into the incoming arg register
726 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
727 .addReg (ArgReg);
728 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
729 // Single-fp args are passed in integer registers; go through
730 // memory to get them out of FP registers. (Bleh!)
731 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
732 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
733 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0)
734 .addReg (ArgReg);
735 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i - 1]).addFrameIndex (FI)
736 .addSImm (0);
Brian Gaeke1df468e2004-09-29 03:34:41 +0000737 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
738 // Double-fp args are passed in pairs of integer registers; go through
739 // memory to get them out of FP registers. (Bleh!)
740 assert (i <= 5 && "Can't deal with double-fp args past #5 yet");
741 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
742 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
743 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0)
744 .addReg (ArgReg);
745 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i - 1]).addFrameIndex (FI)
746 .addSImm (0);
747 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i]).addFrameIndex (FI)
748 .addSImm (4);
Brian Gaeke812c4882004-07-16 10:31:25 +0000749 } else {
750 assert (0 && "64-bit (double, long, etc.) 'call' opnds not handled");
751 }
Brian Gaeked54c38b2004-04-07 16:41:22 +0000752 }
753
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000754 // Emit call instruction
755 if (Function *F = I.getCalledFunction ()) {
756 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
757 } else { // Emit an indirect call...
758 unsigned Reg = getReg (I.getCalledValue ());
759 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
760 }
761
762 // Deal w/ return value: schlep it over into the destination register
Brian Gaekee14e3382004-06-15 20:06:32 +0000763 if (I.getType () == Type::VoidTy)
Brian Gaekeea8494b2004-04-06 22:09:23 +0000764 return;
Brian Gaekee14e3382004-06-15 20:06:32 +0000765 unsigned DestReg = getReg (I);
Brian Gaekeea8494b2004-04-06 22:09:23 +0000766 switch (getClass (I.getType ())) {
767 case cByte:
768 case cShort:
769 case cInt:
Brian Gaekeea8494b2004-04-06 22:09:23 +0000770 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
771 break;
Brian Gaeke9d67ea02004-06-18 06:27:48 +0000772 case cFloat:
773 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
774 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000775 case cDouble:
776 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
777 break;
778 case cLong:
779 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
780 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
781 break;
Brian Gaekeea8494b2004-04-06 22:09:23 +0000782 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000783 std::cerr << "Return type of call instruction not handled: " << I;
784 abort ();
Brian Gaekeea8494b2004-04-06 22:09:23 +0000785 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000786}
Chris Lattner1c809c52004-02-29 00:27:00 +0000787
788void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000789 if (I.getNumOperands () == 1) {
790 unsigned RetValReg = getReg (I.getOperand (0));
791 switch (getClass (I.getOperand (0)->getType ())) {
792 case cByte:
793 case cShort:
794 case cInt:
795 // Schlep it over into i0 (where it will become o0 after restore).
796 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
797 break;
Brian Gaekef9a75462004-07-08 07:22:27 +0000798 case cFloat:
Brian Gaeke1df468e2004-09-29 03:34:41 +0000799 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
Brian Gaekef9a75462004-07-08 07:22:27 +0000800 break;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000801 case cDouble:
802 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
Brian Gaeke812c4882004-07-16 10:31:25 +0000803 break;
Brian Gaeke2a9f5392004-07-08 07:52:13 +0000804 case cLong:
805 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
806 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
807 break;
Brian Gaeke08f64c32004-03-06 05:32:28 +0000808 default:
Brian Gaeke532e60c2004-05-08 04:21:17 +0000809 std::cerr << "Return instruction of this type not handled: " << I;
810 abort ();
Brian Gaeke08f64c32004-03-06 05:32:28 +0000811 }
Chris Lattner1c809c52004-02-29 00:27:00 +0000812 }
Chris Lattner0d538bb2004-04-07 04:36:53 +0000813
Brian Gaeke08f64c32004-03-06 05:32:28 +0000814 // Just emit a 'retl' instruction to return.
815 BuildMI(BB, V8::RETL, 0);
816 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000817}
818
Brian Gaeke532e60c2004-05-08 04:21:17 +0000819static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
820 Function::iterator I = BB; ++I; // Get iterator to next block
821 return I != BB->getParent()->end() ? &*I : 0;
822}
823
824/// visitBranchInst - Handles conditional and unconditional branches.
825///
826void V8ISel::visitBranchInst(BranchInst &I) {
Brian Gaeke532e60c2004-05-08 04:21:17 +0000827 BasicBlock *takenSucc = I.getSuccessor (0);
Brian Gaeke6c868a42004-06-17 22:34:08 +0000828 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
829 BB->addSuccessor (takenSuccMBB);
830 if (I.isConditional()) { // conditional branch
831 BasicBlock *notTakenSucc = I.getSuccessor (1);
832 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
833 BB->addSuccessor (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000834
Brian Gaeke6c868a42004-06-17 22:34:08 +0000835 // CondReg=(<condition>);
836 // If (CondReg==0) goto notTakenSuccMBB;
837 unsigned CondReg = getReg (I.getCondition ());
838 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
839 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000840 }
Brian Gaeke6c868a42004-06-17 22:34:08 +0000841 // goto takenSuccMBB;
842 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000843}
844
845/// emitGEPOperation - Common code shared between visitGetElementPtrInst and
846/// constant expression GEP support.
847///
Brian Gaeke9f564822004-05-08 05:27:20 +0000848void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
Brian Gaeke532e60c2004-05-08 04:21:17 +0000849 MachineBasicBlock::iterator IP,
850 Value *Src, User::op_iterator IdxBegin,
851 User::op_iterator IdxEnd, unsigned TargetReg) {
Brian Gaeke9f564822004-05-08 05:27:20 +0000852 const TargetData &TD = TM.getTargetData ();
853 const Type *Ty = Src->getType ();
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000854 unsigned basePtrReg = getReg (Src, MBB, IP);
Brian Gaeke9f564822004-05-08 05:27:20 +0000855
856 // GEPs have zero or more indices; we must perform a struct access
857 // or array access for each one.
858 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
859 ++oi) {
860 Value *idx = *oi;
861 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
862 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
863 // It's a struct access. idx is the index into the structure,
864 // which names the field. Use the TargetData structure to
865 // pick out what the layout of the structure is in memory.
866 // Use the (constant) structure index's value to find the
867 // right byte offset from the StructLayout class's list of
868 // structure member offsets.
869 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
870 unsigned memberOffset =
871 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
872 // Emit an ADD to add memberOffset to the basePtr.
873 BuildMI (*MBB, IP, V8::ADDri, 2,
874 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
875 // The next type is the member of the structure selected by the
876 // index.
877 Ty = StTy->getElementType (fieldIndex);
878 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
879 // It's an array or pointer access: [ArraySize x ElementType].
880 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
881 // must find the size of the pointed-to type (Not coincidentally, the next
882 // type is the type of the elements in the array).
883 Ty = SqTy->getElementType ();
884 unsigned elementSize = TD.getTypeSize (Ty);
885 unsigned idxReg = getReg (idx, MBB, IP);
886 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
887 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000888 copyConstantToRegister (MBB, IP,
889 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
Brian Gaeke9f564822004-05-08 05:27:20 +0000890 // Emit a SMUL to multiply the register holding the index by
891 // elementSize, putting the result in OffsetReg.
892 BuildMI (*MBB, IP, V8::SMULrr, 2,
893 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
894 // Emit an ADD to add OffsetReg to the basePtr.
895 BuildMI (*MBB, IP, V8::ADDrr, 2,
896 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
897 }
898 basePtrReg = nextBasePtrReg;
899 }
900 // After we have processed all the indices, the result is left in
901 // basePtrReg. Move it to the register where we were expected to
902 // put the answer.
903 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
Brian Gaeke532e60c2004-05-08 04:21:17 +0000904}
905
906void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
907 unsigned outputReg = getReg (I);
908 emitGEPOperation (BB, BB->end (), I.getOperand (0),
909 I.op_begin ()+1, I.op_end (), outputReg);
910}
911
Brian Gaeked6a10532004-06-15 21:09:46 +0000912
Chris Lattner4be7ca52004-04-07 04:27:16 +0000913void V8ISel::visitBinaryOperator (Instruction &I) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000914 unsigned DestReg = getReg (I);
915 unsigned Op0Reg = getReg (I.getOperand (0));
916 unsigned Op1Reg = getReg (I.getOperand (1));
917
Brian Gaekeec3227f2004-06-27 22:47:33 +0000918 unsigned Class = getClassB (I.getType());
Chris Lattner22ede702004-04-07 04:06:46 +0000919 unsigned OpCase = ~0;
920
Brian Gaekeec3227f2004-06-27 22:47:33 +0000921 if (Class > cLong) {
922 switch (I.getOpcode ()) {
923 case Instruction::Add: OpCase = 0; break;
924 case Instruction::Sub: OpCase = 1; break;
925 case Instruction::Mul: OpCase = 2; break;
926 case Instruction::Div: OpCase = 3; break;
927 default: visitInstruction (I); return;
928 }
929 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
930 V8::FSUBS, V8::FSUBD,
931 V8::FMULS, V8::FMULD,
932 V8::FDIVS, V8::FDIVD };
933 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
934 .addReg (Op0Reg).addReg (Op1Reg);
935 return;
936 }
937
938 unsigned ResultReg = DestReg;
Brian Gaeke1df468e2004-09-29 03:34:41 +0000939 if (Class != cInt && Class != cLong)
Brian Gaekeec3227f2004-06-27 22:47:33 +0000940 ResultReg = makeAnotherReg (I.getType ());
941
Brian Gaeke1df468e2004-09-29 03:34:41 +0000942 if (Class == cLong) {
943 DEBUG (std::cerr << "Class = cLong\n");
944 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
945 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
946 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
947 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
948 }
949
950 // FIXME: support long, ulong.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000951 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000952 case Instruction::Add: OpCase = 0; break;
953 case Instruction::Sub: OpCase = 1; break;
954 case Instruction::Mul: OpCase = 2; break;
955 case Instruction::And: OpCase = 3; break;
956 case Instruction::Or: OpCase = 4; break;
957 case Instruction::Xor: OpCase = 5; break;
Chris Lattner4be7ca52004-04-07 04:27:16 +0000958 case Instruction::Shl: OpCase = 6; break;
959 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
Chris Lattner22ede702004-04-07 04:06:46 +0000960
961 case Instruction::Div:
962 case Instruction::Rem: {
963 unsigned Dest = ResultReg;
964 if (I.getOpcode() == Instruction::Rem)
965 Dest = makeAnotherReg(I.getType());
966
967 // FIXME: this is probably only right for 32 bit operands.
968 if (I.getType ()->isSigned()) {
969 unsigned Tmp = makeAnotherReg (I.getType ());
970 // Sign extend into the Y register
971 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
972 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
973 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
974 } else {
975 // Zero extend into the Y register, ie, just set it to zero
976 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
977 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000978 }
Chris Lattner22ede702004-04-07 04:06:46 +0000979
980 if (I.getOpcode() == Instruction::Rem) {
981 unsigned Tmp = makeAnotherReg (I.getType ());
982 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
983 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000984 }
Chris Lattner22ede702004-04-07 04:06:46 +0000985 break;
986 }
987 default:
988 visitInstruction (I);
989 return;
990 }
991
Brian Gaekec7fd0f42004-06-24 08:55:09 +0000992 static const unsigned Opcodes[] = {
993 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
994 V8::SLLrr, V8::SRLrr, V8::SRArr
995 };
Chris Lattner22ede702004-04-07 04:06:46 +0000996 if (OpCase != ~0U) {
Chris Lattner22ede702004-04-07 04:06:46 +0000997 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000998 }
999
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001000 switch (getClassB (I.getType ())) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001001 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001002 if (I.getType ()->isSigned ()) { // add byte
1003 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1004 } else { // add ubyte
1005 unsigned TmpReg = makeAnotherReg (I.getType ());
1006 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1007 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1008 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001009 break;
1010 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001011 if (I.getType ()->isSigned ()) { // add short
1012 unsigned TmpReg = makeAnotherReg (I.getType ());
1013 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1014 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1015 } else { // add ushort
1016 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +00001017 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1018 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +00001019 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001020 break;
1021 case cInt:
Brian Gaekeccdd70a2004-07-08 08:08:10 +00001022 // Nothing to do here.
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001023 break;
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001024 case cLong:
1025 // Only support and, or, xor.
1026 if (OpCase < 3 || OpCase > 5) {
1027 visitInstruction (I);
1028 return;
1029 }
1030 // Do the other half of the value:
Brian Gaekeec3227f2004-06-27 22:47:33 +00001031 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1032 .addReg (Op1Reg+1);
Brian Gaekec7fd0f42004-06-24 08:55:09 +00001033 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001034 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +00001035 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +00001036 }
1037}
1038
Misha Brukmanea091262004-06-30 21:47:40 +00001039void V8ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner4d0cda42004-04-07 05:04:51 +00001040 unsigned Op0Reg = getReg (I.getOperand (0));
1041 unsigned Op1Reg = getReg (I.getOperand (1));
1042 unsigned DestReg = getReg (I);
Brian Gaeke429022b2004-05-08 06:36:14 +00001043 const Type *Ty = I.getOperand (0)->getType ();
Chris Lattner4d0cda42004-04-07 05:04:51 +00001044
1045 // Compare the two values.
Brian Gaeke3a085892004-07-08 09:08:35 +00001046 assert (getClass (Ty) != cLong && "can't setcc on longs yet");
1047 if (getClass (Ty) < cLong) {
1048 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
1049 } else if (getClass (Ty) == cFloat) {
1050 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1051 } else if (getClass (Ty) == cDouble) {
1052 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1053 }
Chris Lattner4d0cda42004-04-07 05:04:51 +00001054
Brian Gaeke429022b2004-05-08 06:36:14 +00001055 unsigned BranchIdx;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001056 switch (I.getOpcode()) {
1057 default: assert(0 && "Unknown setcc instruction!");
Brian Gaeke429022b2004-05-08 06:36:14 +00001058 case Instruction::SetEQ: BranchIdx = 0; break;
1059 case Instruction::SetNE: BranchIdx = 1; break;
1060 case Instruction::SetLT: BranchIdx = 2; break;
1061 case Instruction::SetGT: BranchIdx = 3; break;
1062 case Instruction::SetLE: BranchIdx = 4; break;
1063 case Instruction::SetGE: BranchIdx = 5; break;
Chris Lattner4d0cda42004-04-07 05:04:51 +00001064 }
Brian Gaeke3a085892004-07-08 09:08:35 +00001065 unsigned Column = 0;
1066 if (Ty->isSigned()) ++Column;
1067 if (Ty->isFloatingPoint()) ++Column;
1068 static unsigned OpcodeTab[3*6] = {
1069 // LLVM SparcV8
1070 // unsigned signed fp
1071 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1072 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1073 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1074 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1075 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1076 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
Brian Gaeke429022b2004-05-08 06:36:14 +00001077 };
Brian Gaeke3a085892004-07-08 09:08:35 +00001078 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
Brian Gaeke6c868a42004-06-17 22:34:08 +00001079
1080 MachineBasicBlock *thisMBB = BB;
1081 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1082 // thisMBB:
1083 // ...
1084 // subcc %reg0, %reg1, %g0
1085 // bCC copy1MBB
1086 // ba copy0MBB
1087
1088 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1089 // if we could insert other, non-terminator instructions after the
1090 // bCC. But MBB->getFirstTerminator() can't understand this.
1091 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1092 F->getBasicBlockList ().push_back (copy1MBB);
1093 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1094 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1095 F->getBasicBlockList ().push_back (copy0MBB);
1096 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1097 // Update machine-CFG edges
1098 BB->addSuccessor (copy1MBB);
1099 BB->addSuccessor (copy0MBB);
1100
1101 // copy0MBB:
1102 // %FalseValue = or %G0, 0
1103 // ba sinkMBB
1104 BB = copy0MBB;
1105 unsigned FalseValue = makeAnotherReg (I.getType ());
1106 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1107 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1108 F->getBasicBlockList ().push_back (sinkMBB);
1109 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1110 // Update machine-CFG edges
1111 BB->addSuccessor (sinkMBB);
1112
1113 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1114 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1115 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1116 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1117
1118 // copy1MBB:
1119 // %TrueValue = or %G0, 1
1120 // ba sinkMBB
1121 BB = copy1MBB;
1122 unsigned TrueValue = makeAnotherReg (I.getType ());
1123 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1124 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1125 // Update machine-CFG edges
1126 BB->addSuccessor (sinkMBB);
1127
1128 // sinkMBB:
1129 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1130 // ...
1131 BB = sinkMBB;
1132 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1133 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001134}
1135
Brian Gaekec93a7522004-06-18 05:19:16 +00001136void V8ISel::visitAllocaInst(AllocaInst &I) {
1137 // Find the data size of the alloca inst's getAllocatedType.
1138 const Type *Ty = I.getAllocatedType();
1139 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
Chris Lattner4d0cda42004-04-07 05:04:51 +00001140
Brian Gaekec93a7522004-06-18 05:19:16 +00001141 unsigned ArraySizeReg = getReg (I.getArraySize ());
1142 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1143 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1144 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1145 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
Brian Gaekec93a7522004-06-18 05:19:16 +00001146
1147 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1148 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001149
Brian Gaekec93a7522004-06-18 05:19:16 +00001150 // Round up TmpReg1 to nearest doubleword boundary:
1151 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1152 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001153
1154 // Subtract size from stack pointer, thereby allocating some space.
Brian Gaekec93a7522004-06-18 05:19:16 +00001155 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
Brian Gaekecfaf2242004-06-18 08:45:52 +00001156
1157 // Put a pointer to the space into the result register, by copying
1158 // the stack pointer.
1159 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1160
1161 // Inform the Frame Information that we have just allocated a variable-sized
1162 // object.
1163 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaekec93a7522004-06-18 05:19:16 +00001164}
Chris Lattner1c809c52004-02-29 00:27:00 +00001165
1166/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1167/// function, lowering any calls to unknown intrinsic functions into the
1168/// equivalent LLVM code.
1169void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1170 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1171 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1172 if (CallInst *CI = dyn_cast<CallInst>(I++))
1173 if (Function *F = CI->getCalledFunction())
1174 switch (F->getIntrinsicID()) {
1175 case Intrinsic::not_intrinsic: break;
1176 default:
1177 // All other intrinsic calls we must lower.
1178 Instruction *Before = CI->getPrev();
1179 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1180 if (Before) { // Move iterator to instruction after call
1181 I = Before; ++I;
1182 } else {
1183 I = BB->begin();
1184 }
1185 }
1186}
1187
1188
1189void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1190 unsigned TmpReg1, TmpReg2;
1191 switch (ID) {
1192 default: assert(0 && "Intrinsic not supported!");
1193 }
1194}