1 //===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple peephole instruction selector for the V8 target
12 //===----------------------------------------------------------------------===//
15 #include "SparcV8InstrInfo.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Constants.h"
20 #include "llvm/CodeGen/IntrinsicLowering.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/SSARegMap.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "llvm/Support/CFG.h"
33 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
35 MachineFunction *F; // The function we are compiling into
36 MachineBasicBlock *BB; // The current MBB we are compiling
38 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
40 // MBBMap - Mapping between LLVM BB -> Machine BB
41 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
43 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
45 /// runOnFunction - Top level implementation of instruction selection for
46 /// the entire function.
48 bool runOnFunction(Function &Fn);
50 virtual const char *getPassName() const {
51 return "SparcV8 Simple Instruction Selection";
54 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
55 /// constant expression GEP support.
57 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
58 Value *Src, User::op_iterator IdxBegin,
59 User::op_iterator IdxEnd, unsigned TargetReg);
61 /// emitCastOperation - Common code shared between visitCastInst and
62 /// constant expression cast support.
64 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
65 Value *Src, const Type *DestTy, unsigned TargetReg);
67 /// 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.
72 void visitBasicBlock(BasicBlock &LLVM_BB) {
73 BB = MBBMap[&LLVM_BB];
76 void visitBinaryOperator(Instruction &I);
77 void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
78 void visitSetCondInst(SetCondInst &I);
79 void visitCallInst(CallInst &I);
80 void visitReturnInst(ReturnInst &I);
81 void visitBranchInst(BranchInst &I);
82 void visitCastInst(CastInst &I);
83 void visitLoadInst(LoadInst &I);
84 void visitStoreInst(StoreInst &I);
85 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
86 void visitGetElementPtrInst(GetElementPtrInst &I);
87 void visitAllocaInst(AllocaInst &I);
89 void visitInstruction(Instruction &I) {
90 std::cerr << "Unhandled instruction: " << I;
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);
98 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
100 void LoadArgumentsToVirtualRegs(Function *F);
102 /// 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.
106 void SelectPHINodes();
108 /// copyConstantToRegister - Output the instructions required to put the
109 /// specified constant into the specified register.
111 void copyConstantToRegister(MachineBasicBlock *MBB,
112 MachineBasicBlock::iterator IP,
113 Constant *C, unsigned R);
115 /// makeAnotherReg - This method returns the next register number we haven't
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.
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;
136 // Add the mapping of regnumber => reg class to MachineFunction
137 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
138 return F->getSSARegMap()->createVirtualRegister(RC);
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);
147 unsigned getReg(Value *V, MachineBasicBlock *MBB,
148 MachineBasicBlock::iterator IPt) {
149 unsigned &Reg = RegMap[V];
151 Reg = makeAnotherReg(V->getType());
154 // If this operand is a constant, emit the code to copy the constant into
155 // the register here...
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
162 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);
166 RegMap.erase(V); // Assign a new name to this address if ref'd again
175 FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
176 return new V8ISel(TM);
180 cByte, cShort, cInt, cLong, cFloat, cDouble
183 static TypeClass getClass (const Type *T) {
184 switch (T->getTypeID()) {
185 case Type::UByteTyID: case Type::SByteTyID: return cByte;
186 case Type::UShortTyID: case Type::ShortTyID: return cShort;
187 case Type::PointerTyID:
188 case Type::UIntTyID: case Type::IntTyID: return cInt;
189 case Type::ULongTyID: case Type::LongTyID: return cLong;
190 case Type::FloatTyID: return cFloat;
191 case Type::DoubleTyID: return cDouble;
193 assert (0 && "Type of unknown class passed to getClass?");
197 static TypeClass getClassB(const Type *T) {
198 if (T == Type::BoolTy) return cByte;
204 /// copyConstantToRegister - Output the instructions required to put the
205 /// specified constant into the specified register.
207 void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
208 MachineBasicBlock::iterator IP,
209 Constant *C, unsigned R) {
210 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);
216 case Instruction::Cast:
217 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
220 std::cerr << "Copying this constant expr not yet handled: " << *CE;
225 if (C->getType()->isIntegral ()) {
227 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();
234 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
236 copyConstantToRegister(MBB, IP, ConstantUInt::get(Type::UIntTy,
237 Val & 0xffffffffU), R+1);
241 assert(Class <= cInt && "Type not handled yet!");
243 if (C->getType() == Type::BoolTy) {
244 Val = (C == ConstantBool::True);
246 ConstantInt *CI = cast<ConstantInt> (C);
247 Val = CI->getRawValue ();
250 case cByte: Val = (int8_t) Val; break;
251 case cShort: Val = (int16_t) Val; break;
252 case cInt: Val = (int32_t) Val; break;
254 std::cerr << "Offending constant: " << *C << "\n";
255 assert (0 && "Can't copy this kind of constant into register yet");
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);
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);
270 } 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();
275 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
276 unsigned AddrReg = makeAnotherReg (Type::UIntTy);
278 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
279 unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
280 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);
283 } else if (isa<ConstantPointerNull>(C)) {
284 // Copy zero (null pointer) to the register.
285 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
286 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
287 // 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 ());
291 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress(GV);
292 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg(TmpReg).addGlobalAddress(GV);
294 std::cerr << "Offending constant: " << *C << "\n";
295 assert (0 && "Can't copy this kind of constant into register yet");
299 void V8ISel::LoadArgumentsToVirtualRegs (Function *LF) {
301 static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
302 V8::I3, V8::I4, V8::I5 };
303 // Add IMPLICIT_DEFs of input regs.
305 for (Function::aiterator I = LF->abegin(), E = LF->aend();
306 I != E && ArgOffset < 6; ++I, ++ArgOffset) {
307 unsigned Reg = getReg(*I);
308 switch (getClassB(I->getType())) {
313 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
317 // Double and Long use register pairs.
318 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
321 BuildMI(BB, V8::IMPLICIT_DEF, 0, IncomingArgRegs[ArgOffset]);
324 assert (0 && "type not handled");
330 for (Function::aiterator I = LF->abegin(), E = LF->aend(); I != E;
332 unsigned Reg = getReg(*I);
335 switch (getClassB(I->getType())) {
339 BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
340 .addReg (IncomingArgRegs[ArgOffset]);
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);
353 // Double-fp args are passed in pairs of integer registers; go through
354 // memory to get them into FP registers. (Double bleh!)
355 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
356 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
357 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (0)
358 .addReg (IncomingArgRegs[ArgOffset]);
360 BuildMI (BB, V8::ST, 3).addFrameIndex (FI).addSImm (4)
361 .addReg (IncomingArgRegs[ArgOffset]);
362 BuildMI (BB, V8::LDDFri, 2, Reg).addFrameIndex (FI).addSImm (0);
366 // FIXME: handle cLong
367 assert (0 && "64-bit int (long/ulong) function args not handled");
373 switch (getClassB(I->getType())) {
377 int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset));
378 BuildMI (BB, V8::LD, 2, Reg).addFrameIndex (FI).addSImm(0);
382 int FI = F->getFrameInfo()->CreateFixedObject(4, 68 + (4 * ArgOffset));
383 BuildMI (BB, V8::LDFri, 2, Reg).addFrameIndex (FI).addSImm(0);
387 int FI = F->getFrameInfo()->CreateFixedObject(8, 68 + (4 * ArgOffset));
388 BuildMI (BB, V8::LDDFri, 2, Reg).addFrameIndex (FI).addSImm(0);
392 // FIXME: handle cLong
393 assert (0 && "64-bit integer (long/ulong) function args not handled");
401 void V8ISel::SelectPHINodes() {
402 const TargetInstrInfo &TII = *TM.getInstrInfo();
403 const Function &LF = *F->getFunction(); // The LLVM function...
404 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
405 const BasicBlock *BB = I;
406 MachineBasicBlock &MBB = *MBBMap[I];
408 // Loop over all of the PHI nodes in the LLVM basic block...
409 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
410 for (BasicBlock::const_iterator I = BB->begin();
411 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
413 // Create a new machine instr PHI node, and insert it.
414 unsigned PHIReg = getReg(*PN);
415 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
416 V8::PHI, PN->getNumOperands(), PHIReg);
418 MachineInstr *LongPhiMI = 0;
419 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
420 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
421 V8::PHI, PN->getNumOperands(), PHIReg+1);
423 // PHIValues - Map of blocks to incoming virtual registers. We use this
424 // so that we only initialize one incoming value for a particular block,
425 // even if the block has multiple entries in the PHI node.
427 std::map<MachineBasicBlock*, unsigned> PHIValues;
429 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
430 MachineBasicBlock *PredMBB = 0;
431 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
432 PE = MBB.pred_end (); PI != PE; ++PI)
433 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
437 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
440 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
441 PHIValues.lower_bound(PredMBB);
443 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
444 // We already inserted an initialization of the register for this
445 // predecessor. Recycle it.
446 ValReg = EntryIt->second;
449 // Get the incoming value into a virtual register.
451 Value *Val = PN->getIncomingValue(i);
453 // If this is a constant or GlobalValue, we may have to insert code
454 // into the basic block to compute it into a virtual register.
455 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
456 isa<GlobalValue>(Val)) {
457 // Simple constants get emitted at the end of the basic block,
458 // before any terminator instructions. We "know" that the code to
459 // move a constant into a register will never clobber any flags.
460 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
462 // Because we don't want to clobber any values which might be in
463 // physical registers with the computation of this constant (which
464 // might be arbitrarily complex if it is a constant expression),
465 // just insert the computation at the top of the basic block.
466 MachineBasicBlock::iterator PI = PredMBB->begin();
468 // Skip over any PHI nodes though!
469 while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
472 ValReg = getReg(Val, PredMBB, PI);
475 // Remember that we inserted a value for this PHI for this predecessor
476 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
479 PhiMI->addRegOperand(ValReg);
480 PhiMI->addMachineBasicBlockOperand(PredMBB);
482 LongPhiMI->addRegOperand(ValReg+1);
483 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
487 // Now that we emitted all of the incoming values for the PHI node, make
488 // sure to reposition the InsertPoint after the PHI that we just added.
489 // This is needed because we might have inserted a constant into this
490 // block, right after the PHI's which is before the old insert point!
491 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
497 bool V8ISel::runOnFunction(Function &Fn) {
498 // First pass over the function, lower any unknown intrinsic functions
499 // with the IntrinsicLowering class.
500 LowerUnknownIntrinsicFunctionCalls(Fn);
502 F = &MachineFunction::construct(&Fn, TM);
504 // Create all of the machine basic blocks for the function...
505 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
506 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
510 // Set up a frame object for the return address. This is used by the
511 // llvm.returnaddress & llvm.frameaddress intrinisics.
512 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
514 // Copy incoming arguments off of the stack and out of fixed registers.
515 LoadArgumentsToVirtualRegs(&Fn);
517 // Instruction select everything except PHI nodes
520 // Select the PHI nodes
526 // We always build a machine code representation for the function
530 void V8ISel::visitCastInst(CastInst &I) {
531 Value *Op = I.getOperand(0);
532 unsigned DestReg = getReg(I);
533 MachineBasicBlock::iterator MI = BB->end();
534 emitCastOperation(BB, MI, Op, I.getType(), DestReg);
537 /// emitCastOperation - Common code shared between visitCastInst and constant
538 /// expression cast support.
540 void V8ISel::emitCastOperation(MachineBasicBlock *BB,
541 MachineBasicBlock::iterator IP,
542 Value *Src, const Type *DestTy,
544 const Type *SrcTy = Src->getType();
545 unsigned SrcClass = getClassB(SrcTy);
546 unsigned DestClass = getClassB(DestTy);
547 unsigned SrcReg = getReg(Src, BB, IP);
549 const Type *oldTy = SrcTy;
550 const Type *newTy = DestTy;
551 unsigned oldTyClass = SrcClass;
552 unsigned newTyClass = DestClass;
554 if (oldTyClass < cLong && newTyClass < cLong) {
555 if (oldTyClass >= newTyClass) {
556 // Emit a reg->reg copy to do a equal-size or narrowing cast,
557 // and do sign/zero extension (necessary if we change signedness).
558 unsigned TmpReg1 = makeAnotherReg (newTy);
559 unsigned TmpReg2 = makeAnotherReg (newTy);
560 BuildMI (*BB, IP, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg);
561 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
562 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
563 if (newTy->isSigned ()) { // sign-extend with SRA
564 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
565 } else { // zero-extend with SRL
566 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
569 unsigned TmpReg1 = makeAnotherReg (oldTy);
570 unsigned TmpReg2 = makeAnotherReg (newTy);
571 unsigned TmpReg3 = makeAnotherReg (newTy);
572 // Widening integer cast. Make sure it's fully sign/zero-extended
573 // wrt the input type, then make sure it's fully sign/zero-extended wrt
574 // the output type. Kind of stupid, but simple...
575 unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy));
576 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg);
577 if (oldTy->isSigned ()) { // sign-extend with SRA
578 BuildMI(*BB, IP, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
579 } else { // zero-extend with SRL
580 BuildMI(*BB, IP, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
582 shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
583 BuildMI (*BB, IP, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2);
584 if (newTy->isSigned ()) { // sign-extend with SRA
585 BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
586 } else { // zero-extend with SRL
587 BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
591 if (newTyClass == cFloat) {
592 assert (oldTyClass != cLong && "cast long to float not implemented yet");
593 switch (oldTyClass) {
595 BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
598 BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
601 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
602 // cast int to float. Store it to a stack slot and then load
603 // it using ldf into a floating point register. then do fitos.
604 unsigned TmpReg = makeAnotherReg (newTy);
605 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
606 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
608 BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
609 BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
613 } else if (newTyClass == cDouble) {
614 assert (oldTyClass != cLong && "cast long to double not implemented yet");
615 switch (oldTyClass) {
617 BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
619 case cDouble: // use double move pseudo-instr
620 BuildMI (*BB, IP, V8::FpMOVD, 1, DestReg).addReg (SrcReg);
623 unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
624 unsigned TmpReg = makeAnotherReg (newTy);
625 int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
626 BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
628 BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
629 BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
633 } else if (newTyClass == cLong) {
634 if (oldTyClass == cLong) {
636 BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
637 BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
640 std::cerr << "Cast still unsupported: SrcTy = "
641 << *SrcTy << ", DestTy = " << *DestTy << "\n";
645 std::cerr << "Cast still unsupported: SrcTy = "
646 << *SrcTy << ", DestTy = " << *DestTy << "\n";
652 void V8ISel::visitLoadInst(LoadInst &I) {
653 unsigned DestReg = getReg (I);
654 unsigned PtrReg = getReg (I.getOperand (0));
655 switch (getClassB (I.getType ())) {
657 if (I.getType ()->isSigned ())
658 BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
660 BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
663 if (I.getType ()->isSigned ())
664 BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
666 BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
669 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
672 BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
673 BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
676 BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
679 BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
682 std::cerr << "Load instruction not handled: " << I;
688 void V8ISel::visitStoreInst(StoreInst &I) {
689 Value *SrcVal = I.getOperand (0);
690 unsigned SrcReg = getReg (SrcVal);
691 unsigned PtrReg = getReg (I.getOperand (1));
692 switch (getClassB (SrcVal->getType ())) {
694 BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
697 BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
700 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
703 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
704 BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
707 BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
710 BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
713 std::cerr << "Store instruction not handled: " << I;
719 void V8ISel::visitCallInst(CallInst &I) {
720 MachineInstr *TheCall;
721 // Is it an intrinsic function call?
722 if (Function *F = I.getCalledFunction()) {
723 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
724 visitIntrinsicCall(ID, I); // Special intrinsics are not handled here
730 assert (I.getNumOperands () < 8
731 && "Can't handle pushing excess call args on the stack yet");
732 static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
734 for (unsigned i = 1; i < 7; ++i)
735 if (i < I.getNumOperands ()) {
736 unsigned ArgReg = getReg (I.getOperand (i));
737 if (getClassB (I.getOperand (i)->getType ()) < cLong) {
738 // Schlep it over into the incoming arg register
739 BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
741 } else if (getClassB (I.getOperand (i)->getType ()) == cFloat) {
742 // Single-fp args are passed in integer registers; go through
743 // memory to get them out of FP registers. (Bleh!)
744 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
745 int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
746 BuildMI (BB, V8::STFri, 3).addFrameIndex (FI).addSImm (0)
748 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i - 1]).addFrameIndex (FI)
750 } else if (getClassB (I.getOperand (i)->getType ()) == cDouble) {
751 // Double-fp args are passed in pairs of integer registers; go through
752 // memory to get them out of FP registers. (Bleh!)
753 assert (i <= 5 && "Can't deal with double-fp args past #5 yet");
754 unsigned DblAlign = TM.getTargetData().getDoubleAlignment();
755 int FI = F->getFrameInfo()->CreateStackObject(8, DblAlign);
756 BuildMI (BB, V8::STDFri, 3).addFrameIndex (FI).addSImm (0)
758 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i - 1]).addFrameIndex (FI)
760 BuildMI (BB, V8::LD, 2, OutgoingArgRegs[i]).addFrameIndex (FI)
763 assert (0 && "64-bit (double, long, etc.) 'call' opnds not handled");
767 // Emit call instruction
768 if (Function *F = I.getCalledFunction ()) {
769 BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
770 } else { // Emit an indirect call...
771 unsigned Reg = getReg (I.getCalledValue ());
772 BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
775 // Deal w/ return value: schlep it over into the destination register
776 if (I.getType () == Type::VoidTy)
778 unsigned DestReg = getReg (I);
779 switch (getClass (I.getType ())) {
783 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
786 BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
789 BuildMI (BB, V8::FpMOVD, 2, DestReg).addReg(V8::D0);
792 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
793 BuildMI (BB, V8::ORrr, 2, DestReg+1).addReg(V8::G0).addReg(V8::O1);
796 std::cerr << "Return type of call instruction not handled: " << I;
801 void V8ISel::visitReturnInst(ReturnInst &I) {
802 if (I.getNumOperands () == 1) {
803 unsigned RetValReg = getReg (I.getOperand (0));
804 switch (getClass (I.getOperand (0)->getType ())) {
808 // Schlep it over into i0 (where it will become o0 after restore).
809 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
812 BuildMI (BB, V8::FMOVS, 1, V8::F0).addReg(RetValReg);
815 BuildMI (BB, V8::FpMOVD, 1, V8::D0).addReg(RetValReg);
818 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
819 BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
822 std::cerr << "Return instruction of this type not handled: " << I;
827 // Just emit a 'retl' instruction to return.
828 BuildMI(BB, V8::RETL, 0);
832 static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
833 Function::iterator I = BB; ++I; // Get iterator to next block
834 return I != BB->getParent()->end() ? &*I : 0;
837 /// visitBranchInst - Handles conditional and unconditional branches.
839 void V8ISel::visitBranchInst(BranchInst &I) {
840 BasicBlock *takenSucc = I.getSuccessor (0);
841 MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
842 BB->addSuccessor (takenSuccMBB);
843 if (I.isConditional()) { // conditional branch
844 BasicBlock *notTakenSucc = I.getSuccessor (1);
845 MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
846 BB->addSuccessor (notTakenSuccMBB);
848 // CondReg=(<condition>);
849 // If (CondReg==0) goto notTakenSuccMBB;
850 unsigned CondReg = getReg (I.getCondition ());
851 BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
852 BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
854 // goto takenSuccMBB;
855 BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
858 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
859 /// constant expression GEP support.
861 void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
862 MachineBasicBlock::iterator IP,
863 Value *Src, User::op_iterator IdxBegin,
864 User::op_iterator IdxEnd, unsigned TargetReg) {
865 const TargetData &TD = TM.getTargetData ();
866 const Type *Ty = Src->getType ();
867 unsigned basePtrReg = getReg (Src, MBB, IP);
869 // GEPs have zero or more indices; we must perform a struct access
870 // or array access for each one.
871 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
874 unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
875 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
876 // It's a struct access. idx is the index into the structure,
877 // which names the field. Use the TargetData structure to
878 // pick out what the layout of the structure is in memory.
879 // Use the (constant) structure index's value to find the
880 // right byte offset from the StructLayout class's list of
881 // structure member offsets.
882 unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
883 unsigned memberOffset =
884 TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
885 // Emit an ADD to add memberOffset to the basePtr.
886 BuildMI (*MBB, IP, V8::ADDri, 2,
887 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
888 // The next type is the member of the structure selected by the
890 Ty = StTy->getElementType (fieldIndex);
891 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
892 // It's an array or pointer access: [ArraySize x ElementType].
893 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
894 // must find the size of the pointed-to type (Not coincidentally, the next
895 // type is the type of the elements in the array).
896 Ty = SqTy->getElementType ();
897 unsigned elementSize = TD.getTypeSize (Ty);
898 unsigned idxReg = getReg (idx, MBB, IP);
899 unsigned OffsetReg = makeAnotherReg (Type::IntTy);
900 unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
901 copyConstantToRegister (MBB, IP,
902 ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
903 // Emit a SMUL to multiply the register holding the index by
904 // elementSize, putting the result in OffsetReg.
905 BuildMI (*MBB, IP, V8::SMULrr, 2,
906 OffsetReg).addReg (elementSizeReg).addReg (idxReg);
907 // Emit an ADD to add OffsetReg to the basePtr.
908 BuildMI (*MBB, IP, V8::ADDrr, 2,
909 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
911 basePtrReg = nextBasePtrReg;
913 // After we have processed all the indices, the result is left in
914 // basePtrReg. Move it to the register where we were expected to
916 BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
919 void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
920 unsigned outputReg = getReg (I);
921 emitGEPOperation (BB, BB->end (), I.getOperand (0),
922 I.op_begin ()+1, I.op_end (), outputReg);
926 void V8ISel::visitBinaryOperator (Instruction &I) {
927 unsigned DestReg = getReg (I);
928 unsigned Op0Reg = getReg (I.getOperand (0));
929 unsigned Op1Reg = getReg (I.getOperand (1));
931 unsigned Class = getClassB (I.getType());
932 unsigned OpCase = ~0;
935 switch (I.getOpcode ()) {
936 case Instruction::Add: OpCase = 0; break;
937 case Instruction::Sub: OpCase = 1; break;
938 case Instruction::Mul: OpCase = 2; break;
939 case Instruction::Div: OpCase = 3; break;
940 default: visitInstruction (I); return;
942 static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
943 V8::FSUBS, V8::FSUBD,
944 V8::FMULS, V8::FMULD,
945 V8::FDIVS, V8::FDIVD };
946 BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
947 .addReg (Op0Reg).addReg (Op1Reg);
951 unsigned ResultReg = DestReg;
952 if (Class != cInt && Class != cLong)
953 ResultReg = makeAnotherReg (I.getType ());
955 if (Class == cLong) {
956 DEBUG (std::cerr << "Class = cLong\n");
957 DEBUG (std::cerr << "Op0Reg = " << Op0Reg << ", " << Op0Reg+1 << "\n");
958 DEBUG (std::cerr << "Op1Reg = " << Op1Reg << ", " << Op1Reg+1 << "\n");
959 DEBUG (std::cerr << "ResultReg = " << ResultReg << ", " << ResultReg+1 << "\n");
960 DEBUG (std::cerr << "DestReg = " << DestReg << ", " << DestReg+1 << "\n");
963 // FIXME: support long, ulong.
964 switch (I.getOpcode ()) {
965 case Instruction::Add: OpCase = 0; break;
966 case Instruction::Sub: OpCase = 1; break;
967 case Instruction::Mul: OpCase = 2; break;
968 case Instruction::And: OpCase = 3; break;
969 case Instruction::Or: OpCase = 4; break;
970 case Instruction::Xor: OpCase = 5; break;
971 case Instruction::Shl: OpCase = 6; break;
972 case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
974 case Instruction::Div:
975 case Instruction::Rem: {
976 unsigned Dest = ResultReg;
977 if (I.getOpcode() == Instruction::Rem)
978 Dest = makeAnotherReg(I.getType());
980 // FIXME: this is probably only right for 32 bit operands.
981 if (I.getType ()->isSigned()) {
982 unsigned Tmp = makeAnotherReg (I.getType ());
983 // Sign extend into the Y register
984 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
985 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
986 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
988 // Zero extend into the Y register, ie, just set it to zero
989 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
990 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
993 if (I.getOpcode() == Instruction::Rem) {
994 unsigned Tmp = makeAnotherReg (I.getType ());
995 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
996 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
1001 visitInstruction (I);
1005 static const unsigned Opcodes[] = {
1006 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
1007 V8::SLLrr, V8::SRLrr, V8::SRArr
1009 if (OpCase != ~0U) {
1010 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
1013 switch (getClassB (I.getType ())) {
1015 if (I.getType ()->isSigned ()) { // add byte
1016 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
1017 } else { // add ubyte
1018 unsigned TmpReg = makeAnotherReg (I.getType ());
1019 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
1020 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
1024 if (I.getType ()->isSigned ()) { // add short
1025 unsigned TmpReg = makeAnotherReg (I.getType ());
1026 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1027 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
1028 } else { // add ushort
1029 unsigned TmpReg = makeAnotherReg (I.getType ());
1030 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
1031 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
1035 // Nothing to do here.
1038 // Only support and, or, xor.
1039 if (OpCase < 3 || OpCase > 5) {
1040 visitInstruction (I);
1043 // Do the other half of the value:
1044 BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
1048 visitInstruction (I);
1052 void V8ISel::visitSetCondInst(SetCondInst &I) {
1053 unsigned Op0Reg = getReg (I.getOperand (0));
1054 unsigned Op1Reg = getReg (I.getOperand (1));
1055 unsigned DestReg = getReg (I);
1056 const Type *Ty = I.getOperand (0)->getType ();
1058 // Compare the two values.
1059 assert (getClass (Ty) != cLong && "can't setcc on longs yet");
1060 if (getClass (Ty) < cLong) {
1061 BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
1062 } else if (getClass (Ty) == cFloat) {
1063 BuildMI(BB, V8::FCMPS, 2).addReg(Op0Reg).addReg(Op1Reg);
1064 } else if (getClass (Ty) == cDouble) {
1065 BuildMI(BB, V8::FCMPD, 2).addReg(Op0Reg).addReg(Op1Reg);
1069 switch (I.getOpcode()) {
1070 default: assert(0 && "Unknown setcc instruction!");
1071 case Instruction::SetEQ: BranchIdx = 0; break;
1072 case Instruction::SetNE: BranchIdx = 1; break;
1073 case Instruction::SetLT: BranchIdx = 2; break;
1074 case Instruction::SetGT: BranchIdx = 3; break;
1075 case Instruction::SetLE: BranchIdx = 4; break;
1076 case Instruction::SetGE: BranchIdx = 5; break;
1078 unsigned Column = 0;
1079 if (Ty->isSigned()) ++Column;
1080 if (Ty->isFloatingPoint()) ++Column;
1081 static unsigned OpcodeTab[3*6] = {
1083 // unsigned signed fp
1084 V8::BE, V8::BE, V8::FBE, // seteq = be be fbe
1085 V8::BNE, V8::BNE, V8::FBNE, // setne = bne bne fbne
1086 V8::BCS, V8::BL, V8::FBL, // setlt = bcs bl fbl
1087 V8::BGU, V8::BG, V8::FBG, // setgt = bgu bg fbg
1088 V8::BLEU, V8::BLE, V8::FBLE, // setle = bleu ble fble
1089 V8::BCC, V8::BGE, V8::FBGE // setge = bcc bge fbge
1091 unsigned Opcode = OpcodeTab[3*BranchIdx + Column];
1093 MachineBasicBlock *thisMBB = BB;
1094 const BasicBlock *LLVM_BB = BB->getBasicBlock ();
1097 // subcc %reg0, %reg1, %g0
1101 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1102 // if we could insert other, non-terminator instructions after the
1103 // bCC. But MBB->getFirstTerminator() can't understand this.
1104 MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
1105 F->getBasicBlockList ().push_back (copy1MBB);
1106 BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
1107 MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
1108 F->getBasicBlockList ().push_back (copy0MBB);
1109 BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
1110 // Update machine-CFG edges
1111 BB->addSuccessor (copy1MBB);
1112 BB->addSuccessor (copy0MBB);
1115 // %FalseValue = or %G0, 0
1118 unsigned FalseValue = makeAnotherReg (I.getType ());
1119 BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1120 MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1121 F->getBasicBlockList ().push_back (sinkMBB);
1122 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1123 // Update machine-CFG edges
1124 BB->addSuccessor (sinkMBB);
1126 DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1127 DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1128 DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1129 DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1132 // %TrueValue = or %G0, 1
1135 unsigned TrueValue = makeAnotherReg (I.getType ());
1136 BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1137 BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1138 // Update machine-CFG edges
1139 BB->addSuccessor (sinkMBB);
1142 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1145 BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1146 .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
1149 void V8ISel::visitAllocaInst(AllocaInst &I) {
1150 // Find the data size of the alloca inst's getAllocatedType.
1151 const Type *Ty = I.getAllocatedType();
1152 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
1154 unsigned ArraySizeReg = getReg (I.getArraySize ());
1155 unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1156 unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1157 unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1158 unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
1160 // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1161 BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
1163 // Round up TmpReg1 to nearest doubleword boundary:
1164 BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1165 BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
1167 // Subtract size from stack pointer, thereby allocating some space.
1168 BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
1170 // Put a pointer to the space into the result register, by copying
1171 // the stack pointer.
1172 BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1174 // Inform the Frame Information that we have just allocated a variable-sized
1176 F->getFrameInfo()->CreateVariableSizedObject();
1179 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1180 /// function, lowering any calls to unknown intrinsic functions into the
1181 /// equivalent LLVM code.
1182 void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1183 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1184 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1185 if (CallInst *CI = dyn_cast<CallInst>(I++))
1186 if (Function *F = CI->getCalledFunction())
1187 switch (F->getIntrinsicID()) {
1188 case Intrinsic::not_intrinsic: break;
1190 // All other intrinsic calls we must lower.
1191 Instruction *Before = CI->getPrev();
1192 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1193 if (Before) { // Move iterator to instruction after call
1202 void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1203 unsigned TmpReg1, TmpReg2;
1205 default: assert(0 && "Intrinsic not supported!");