Fix bug in copying long constants to register pairs. We were getting
[oota-llvm.git] / lib / Target / SparcV8 / SparcV8ISelSimple.cpp
1 //===-- 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"
15 #include "SparcV8InstrInfo.h"
16 #include "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"
30 #include <iostream>
31 using namespace llvm;
32
33 namespace {
34   struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
35     TargetMachine &TM;
36     MachineFunction *F;                 // The function we are compiling into
37     MachineBasicBlock *BB;              // The current MBB we are compiling
38
39     std::map<Value*, unsigned> RegMap;  // Mapping between Val's and SSA Regs
40
41     // MBBMap - Mapping between LLVM BB -> Machine BB
42     std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
43
44     V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
45
46     /// runOnFunction - Top level implementation of instruction selection for
47     /// the entire function.
48     ///
49     bool runOnFunction(Function &Fn);
50
51     virtual const char *getPassName() const {
52       return "SparcV8 Simple Instruction Selection";
53     }
54
55     /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
56     /// constant expression GEP support.
57     ///
58     void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
59                           Value *Src, User::op_iterator IdxBegin,
60                           User::op_iterator IdxEnd, unsigned TargetReg);
61
62     /// emitCastOperation - Common code shared between visitCastInst and
63     /// constant expression cast support.
64     ///
65     void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
66                            Value *Src, const Type *DestTy, unsigned TargetReg);
67
68     /// visitBasicBlock - This method is called when we are visiting a new basic
69     /// block.  This simply creates a new MachineBasicBlock to emit code into
70     /// and adds it to the current MachineFunction.  Subsequent visit* for
71     /// instructions will be invoked for all instructions in the basic block.
72     ///
73     void visitBasicBlock(BasicBlock &LLVM_BB) {
74       BB = MBBMap[&LLVM_BB];
75     }
76
77     void visitBinaryOperator(Instruction &I);
78     void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); }
79     void visitSetCondInst(SetCondInst &I);
80     void visitCallInst(CallInst &I);
81     void visitReturnInst(ReturnInst &I);
82     void visitBranchInst(BranchInst &I);
83     void visitCastInst(CastInst &I);
84     void visitLoadInst(LoadInst &I);
85     void visitStoreInst(StoreInst &I);
86     void visitPHINode(PHINode &I) {}      // PHI nodes handled by second pass
87     void visitGetElementPtrInst(GetElementPtrInst &I);
88     void visitAllocaInst(AllocaInst &I);
89
90     void visitInstruction(Instruction &I) {
91       std::cerr << "Unhandled instruction: " << I;
92       abort();
93     }
94
95     /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
96     /// function, lowering any calls to unknown intrinsic functions into the
97     /// equivalent LLVM code.
98     void LowerUnknownIntrinsicFunctionCalls(Function &F);
99     void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
100
101     void LoadArgumentsToVirtualRegs(Function *F);
102
103     /// SelectPHINodes - Insert machine code to generate phis.  This is tricky
104     /// because we have to generate our sources into the source basic blocks,
105     /// not the current one.
106     ///
107     void SelectPHINodes();
108
109     /// copyConstantToRegister - Output the instructions required to put the
110     /// specified constant into the specified register.
111     ///
112     void copyConstantToRegister(MachineBasicBlock *MBB,
113                                 MachineBasicBlock::iterator IP,
114                                 Constant *C, unsigned R);
115
116     /// makeAnotherReg - This method returns the next register number we haven't
117     /// yet used.
118     ///
119     /// Long values are handled somewhat specially.  They are always allocated
120     /// as pairs of 32 bit integer values.  The register number returned is the
121     /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
122     /// of the long value.
123     ///
124     unsigned makeAnotherReg(const Type *Ty) {
125       assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
126              "Current target doesn't have SparcV8 reg info??");
127       const SparcV8RegisterInfo *MRI =
128         static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
129       if (Ty == Type::LongTy || Ty == Type::ULongTy) {
130         const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
131         // Create the lower part
132         F->getSSARegMap()->createVirtualRegister(RC);
133         // Create the upper part.
134         return F->getSSARegMap()->createVirtualRegister(RC)-1;
135       }
136
137       // Add the mapping of regnumber => reg class to MachineFunction
138       const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
139       return F->getSSARegMap()->createVirtualRegister(RC);
140     }
141
142     unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
143     unsigned getReg(Value *V) {
144       // Just append to the end of the current bb.
145       MachineBasicBlock::iterator It = BB->end();
146       return getReg(V, BB, It);
147     }
148     unsigned getReg(Value *V, MachineBasicBlock *MBB,
149                     MachineBasicBlock::iterator IPt) {
150       unsigned &Reg = RegMap[V];
151       if (Reg == 0) {
152         Reg = makeAnotherReg(V->getType());
153         RegMap[V] = Reg;
154       }
155       // If this operand is a constant, emit the code to copy the constant into
156       // the register here...
157       //
158       if (Constant *C = dyn_cast<Constant>(V)) {
159         copyConstantToRegister(MBB, IPt, C, Reg);
160         RegMap.erase(V);  // Assign a new name to this constant if ref'd again
161       } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
162         // Move the address of the global into the register
163         unsigned TmpReg = makeAnotherReg(V->getType());
164         BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
165         BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
166           .addGlobalAddress (GV);
167         RegMap.erase(V);  // Assign a new name to this address if ref'd again
168       }
169
170       return Reg;
171     }
172
173   };
174 }
175
176 FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
177   return new V8ISel(TM);
178 }
179
180 enum TypeClass {
181   cByte, cShort, cInt, cLong, cFloat, cDouble
182 };
183
184 static TypeClass getClass (const Type *T) {
185   switch (T->getTypeID()) {
186     case Type::UByteTyID:  case Type::SByteTyID:  return cByte;
187     case Type::UShortTyID: case Type::ShortTyID:  return cShort;
188     case Type::PointerTyID:
189     case Type::UIntTyID:   case Type::IntTyID:    return cInt;
190     case Type::ULongTyID:  case Type::LongTyID:   return cLong;
191     case Type::FloatTyID:                         return cFloat;
192     case Type::DoubleTyID:                        return cDouble;
193     default:
194       assert (0 && "Type of unknown class passed to getClass?");
195       return cByte;
196   }
197 }
198 static TypeClass getClassB(const Type *T) {
199   if (T == Type::BoolTy) return cByte;
200   return getClass(T);
201 }
202
203
204
205 /// copyConstantToRegister - Output the instructions required to put the
206 /// specified constant into the specified register.
207 ///
208 void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
209                                     MachineBasicBlock::iterator IP,
210                                     Constant *C, unsigned R) {
211   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
212     switch (CE->getOpcode()) {
213     case Instruction::GetElementPtr:
214       emitGEPOperation(MBB, IP, CE->getOperand(0),
215                        CE->op_begin()+1, CE->op_end(), R);
216       return;
217     case Instruction::Cast:
218       emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
219       return;
220     default:
221       std::cerr << "Copying this constant expr not yet handled: " << *CE;
222       abort();
223     }
224   }
225
226   if (C->getType()->isIntegral ()) {
227     uint64_t Val;
228     unsigned Class = getClassB (C->getType ());
229     if (Class == cLong) {
230       unsigned TmpReg = makeAnotherReg (Type::IntTy);
231       unsigned TmpReg2 = makeAnotherReg (Type::IntTy);
232       // Copy the value into the register pair.
233       // R = top(more-significant) half, R+1 = bottom(less-significant) half
234       uint64_t Val = cast<ConstantInt>(C)->getRawValue();
235       unsigned bottomHalf = Val & 0xffffffffU;
236       unsigned topHalf = Val >> 32;
237       unsigned HH = topHalf >> 10;
238       unsigned HM = topHalf & 0x03ff;
239       unsigned LM = bottomHalf >> 10;
240       unsigned LO = bottomHalf & 0x03ff;
241       BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addZImm(HH);
242       BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
243         .addSImm (HM);
244       BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg2).addZImm(LM);
245       BuildMI (*MBB, IP, V8::ORri, 2, R+1).addReg (TmpReg2)
246         .addSImm (LO);
247       return;
248     }
249
250     assert(Class <= cInt && "Type not handled yet!");
251
252     if (C->getType() == Type::BoolTy) {
253       Val = (C == ConstantBool::True);
254     } else {
255       ConstantInt *CI = cast<ConstantInt> (C);
256       Val = CI->getRawValue ();
257     }
258     switch (Class) {
259       case cByte:  Val =  (int8_t) Val; break;
260       case cShort: Val = (int16_t) Val; break;
261       case cInt:   Val = (int32_t) Val; break;
262       default:
263         std::cerr << "Offending constant: " << *C << "\n";
264         assert (0 && "Can't copy this kind of constant into register yet");
265         return;
266     }
267     if (Val == 0) {
268       BuildMI (*MBB, IP, V8::ORrr, 2, R).addReg (V8::G0).addReg(V8::G0);
269     } else if (((int64_t)Val >= -4096) && ((int64_t)Val <= 4095)) {
270       BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm(Val);
271     } else {
272       unsigned TmpReg = makeAnotherReg (C->getType ());
273       BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg)
274         .addSImm (((uint32_t) Val) >> 10);
275       BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
276         .addSImm (((uint32_t) Val) & 0x03ff);
277       return;
278     }
279   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
280     // We need to spill the constant to memory...
281     MachineConstantPool *CP = F->getConstantPool();
282     unsigned CPI = CP->getConstantPoolIndex(CFP);
283     const Type *Ty = CFP->getType();
284
285     assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
286     unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri;
287     BuildMI (*MBB, IP, LoadOpcode, 2, R).addConstantPoolIndex (CPI).addSImm (0);
288   } else if (isa<ConstantPointerNull>(C)) {
289     // Copy zero (null pointer) to the register.
290     BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addSImm (0);
291   } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
292     // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize
293     // that SETHI %reg,global == SETHI %reg,%hi(global) and 
294     // OR %reg,global,%reg == OR %reg,%lo(global),%reg.
295     unsigned TmpReg = makeAnotherReg (C->getType ());
296     BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress (CPR->getValue());
297     BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg)
298       .addGlobalAddress (CPR->getValue ());
299   } else {
300     std::cerr << "Offending constant: " << *C << "\n";
301     assert (0 && "Can't copy this kind of constant into register yet");
302   }
303 }
304
305 void V8ISel::LoadArgumentsToVirtualRegs (Function *F) {
306   unsigned ArgOffset = 0;
307   static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2,
308     V8::I3, V8::I4, V8::I5 };
309   assert (F->asize () < 7
310           && "Can't handle loading excess call args off the stack yet");
311
312   for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) {
313     unsigned Reg = getReg(*I);
314     switch (getClassB(I->getType())) {
315     case cByte:
316     case cShort:
317     case cInt:
318       BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0)
319         .addReg (IncomingArgRegs[ArgOffset]);
320       break;
321     default:
322       assert (0 && "Only <=32-bit, integral arguments currently handled");
323       return;
324     }
325     ++ArgOffset;
326   }
327 }
328
329 void V8ISel::SelectPHINodes() {
330   const TargetInstrInfo &TII = *TM.getInstrInfo();
331   const Function &LF = *F->getFunction();  // The LLVM function...
332   for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
333     const BasicBlock *BB = I;
334     MachineBasicBlock &MBB = *MBBMap[I];
335
336     // Loop over all of the PHI nodes in the LLVM basic block...
337     MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
338     for (BasicBlock::const_iterator I = BB->begin();
339          PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
340
341       // Create a new machine instr PHI node, and insert it.
342       unsigned PHIReg = getReg(*PN);
343       MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
344                                     V8::PHI, PN->getNumOperands(), PHIReg);
345
346       MachineInstr *LongPhiMI = 0;
347       if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
348         LongPhiMI = BuildMI(MBB, PHIInsertPoint,
349                             V8::PHI, PN->getNumOperands(), PHIReg+1);
350
351       // PHIValues - Map of blocks to incoming virtual registers.  We use this
352       // so that we only initialize one incoming value for a particular block,
353       // even if the block has multiple entries in the PHI node.
354       //
355       std::map<MachineBasicBlock*, unsigned> PHIValues;
356
357       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
358         MachineBasicBlock *PredMBB = 0;
359         for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
360              PE = MBB.pred_end (); PI != PE; ++PI)
361           if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
362             PredMBB = *PI;
363             break;
364           }
365         assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
366         
367         unsigned ValReg;
368         std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
369           PHIValues.lower_bound(PredMBB);
370
371         if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
372           // We already inserted an initialization of the register for this
373           // predecessor.  Recycle it.
374           ValReg = EntryIt->second;
375
376         } else {        
377           // Get the incoming value into a virtual register.
378           //
379           Value *Val = PN->getIncomingValue(i);
380
381           // If this is a constant or GlobalValue, we may have to insert code
382           // into the basic block to compute it into a virtual register.
383           if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
384               isa<GlobalValue>(Val)) {
385             // Simple constants get emitted at the end of the basic block,
386             // before any terminator instructions.  We "know" that the code to
387             // move a constant into a register will never clobber any flags.
388             ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
389           } else {
390             // Because we don't want to clobber any values which might be in
391             // physical registers with the computation of this constant (which
392             // might be arbitrarily complex if it is a constant expression),
393             // just insert the computation at the top of the basic block.
394             MachineBasicBlock::iterator PI = PredMBB->begin();
395             
396             // Skip over any PHI nodes though!
397             while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI)
398               ++PI;
399             
400             ValReg = getReg(Val, PredMBB, PI);
401           }
402
403           // Remember that we inserted a value for this PHI for this predecessor
404           PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
405         }
406
407         PhiMI->addRegOperand(ValReg);
408         PhiMI->addMachineBasicBlockOperand(PredMBB);
409         if (LongPhiMI) {
410           LongPhiMI->addRegOperand(ValReg+1);
411           LongPhiMI->addMachineBasicBlockOperand(PredMBB);
412         }
413       }
414
415       // Now that we emitted all of the incoming values for the PHI node, make
416       // sure to reposition the InsertPoint after the PHI that we just added.
417       // This is needed because we might have inserted a constant into this
418       // block, right after the PHI's which is before the old insert point!
419       PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
420       ++PHIInsertPoint;
421     }
422   }
423 }
424
425 bool V8ISel::runOnFunction(Function &Fn) {
426   // First pass over the function, lower any unknown intrinsic functions
427   // with the IntrinsicLowering class.
428   LowerUnknownIntrinsicFunctionCalls(Fn);
429   
430   F = &MachineFunction::construct(&Fn, TM);
431   
432   // Create all of the machine basic blocks for the function...
433   for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
434     F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
435   
436   BB = &F->front();
437   
438   // Set up a frame object for the return address.  This is used by the
439   // llvm.returnaddress & llvm.frameaddress intrinisics.
440   //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
441   
442   // Copy incoming arguments off of the stack and out of fixed registers.
443   LoadArgumentsToVirtualRegs(&Fn);
444   
445   // Instruction select everything except PHI nodes
446   visit(Fn);
447   
448   // Select the PHI nodes
449   SelectPHINodes();
450   
451   RegMap.clear();
452   MBBMap.clear();
453   F = 0;
454   // We always build a machine code representation for the function
455   return true;
456 }
457
458 void V8ISel::visitCastInst(CastInst &I) {
459   Value *Op = I.getOperand(0);
460   unsigned DestReg = getReg(I);
461   MachineBasicBlock::iterator MI = BB->end();
462   emitCastOperation(BB, MI, Op, I.getType(), DestReg);
463 }
464
465 /// emitCastOperation - Common code shared between visitCastInst and constant
466 /// expression cast support.
467 ///
468 void V8ISel::emitCastOperation(MachineBasicBlock *BB,
469                              MachineBasicBlock::iterator IP,
470                              Value *Src, const Type *DestTy,
471                              unsigned DestReg) {
472   const Type *SrcTy = Src->getType();
473   unsigned SrcClass = getClassB(SrcTy);
474   unsigned DestClass = getClassB(DestTy);
475   unsigned SrcReg = getReg(Src, BB, IP);
476
477   const Type *oldTy = SrcTy;
478   const Type *newTy = DestTy;
479   unsigned oldTyClass = SrcClass;
480   unsigned newTyClass = DestClass;
481
482   if (oldTyClass < cLong && newTyClass < cLong) {
483     if (oldTyClass >= newTyClass) {
484       // Emit a reg->reg copy to do a equal-size or narrowing cast,
485       // and do sign/zero extension (necessary if we change signedness).
486       unsigned TmpReg1 = makeAnotherReg (newTy);
487       unsigned TmpReg2 = makeAnotherReg (newTy);
488       BuildMI (*BB, IP, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg);
489       unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
490       BuildMI (*BB, IP, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
491       if (newTy->isSigned ()) { // sign-extend with SRA
492         BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
493       } else { // zero-extend with SRL
494         BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2);
495       }
496     } else {
497       unsigned TmpReg1 = makeAnotherReg (oldTy);
498       unsigned TmpReg2 = makeAnotherReg (newTy);
499       unsigned TmpReg3 = makeAnotherReg (newTy);
500       // Widening integer cast. Make sure it's fully sign/zero-extended
501       // wrt the input type, then make sure it's fully sign/zero-extended wrt
502       // the output type. Kind of stupid, but simple...
503       unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy));
504       BuildMI (*BB, IP, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg);
505       if (oldTy->isSigned ()) { // sign-extend with SRA
506         BuildMI(*BB, IP, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
507       } else { // zero-extend with SRL
508         BuildMI(*BB, IP, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1);
509       }
510       shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy));
511       BuildMI (*BB, IP, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2);
512       if (newTy->isSigned ()) { // sign-extend with SRA
513         BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
514       } else { // zero-extend with SRL
515         BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3);
516       }
517     }
518   } else {
519     if (newTyClass == cFloat) {
520       assert (oldTyClass != cLong && "cast long to float not implemented yet");
521       switch (oldTyClass) {
522       case cFloat:
523         BuildMI (*BB, IP, V8::FMOVS, 1, DestReg).addReg (SrcReg);
524         break;
525       case cDouble:
526         BuildMI (*BB, IP, V8::FDTOS, 1, DestReg).addReg (SrcReg);
527         break;
528       default: {
529         unsigned FltAlign = TM.getTargetData().getFloatAlignment();
530         // cast int to float.  Store it to a stack slot and then load
531         // it using ldf into a floating point register. then do fitos.
532         unsigned TmpReg = makeAnotherReg (newTy);
533         int FI = F->getFrameInfo()->CreateStackObject(4, FltAlign);
534         BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
535           .addReg (SrcReg);
536         BuildMI (*BB, IP, V8::LDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
537         BuildMI (*BB, IP, V8::FITOS, 1, DestReg).addReg(TmpReg);
538         break;
539       }
540       }
541     } else if (newTyClass == cDouble) {
542       assert (oldTyClass != cLong && "cast long to double not implemented yet");
543       switch (oldTyClass) {
544       case cFloat:
545         BuildMI (*BB, IP, V8::FSTOD, 1, DestReg).addReg (SrcReg);
546         break;
547       case cDouble: {
548         // go through memory, for now
549         unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
550         int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
551         BuildMI (*BB, IP, V8::STDFri, 3).addFrameIndex (FI).addSImm (0)
552           .addReg (SrcReg);
553         BuildMI (*BB, IP, V8::LDDFri, 2, DestReg).addFrameIndex (FI)
554           .addSImm (0);
555         break;
556       }
557       default: {
558         unsigned DoubleAlignment = TM.getTargetData().getDoubleAlignment();
559         unsigned TmpReg = makeAnotherReg (newTy);
560         int FI = F->getFrameInfo()->CreateStackObject(8, DoubleAlignment);
561         BuildMI (*BB, IP, V8::ST, 3).addFrameIndex (FI).addSImm (0)
562           .addReg (SrcReg);
563         BuildMI (*BB, IP, V8::LDDFri, 2, TmpReg).addFrameIndex (FI).addSImm (0);
564         BuildMI (*BB, IP, V8::FITOD, 1, DestReg).addReg(TmpReg);
565         break;
566       }
567       }
568     } else if (newTyClass == cLong) {
569       if (oldTyClass == cLong) {
570         // Just copy it
571         BuildMI (*BB, IP, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
572         BuildMI (*BB, IP, V8::ORrr, 2, DestReg+1).addReg (V8::G0)
573           .addReg (SrcReg+1);
574       } else {
575         std::cerr << "Cast still unsupported: SrcTy = "
576                   << *SrcTy << ", DestTy = " << *DestTy << "\n";
577         abort ();
578       }
579     } else {
580       std::cerr << "Cast still unsupported: SrcTy = "
581                 << *SrcTy << ", DestTy = " << *DestTy << "\n";
582       abort ();
583     }
584   }
585 }
586
587 void V8ISel::visitLoadInst(LoadInst &I) {
588   unsigned DestReg = getReg (I);
589   unsigned PtrReg = getReg (I.getOperand (0));
590   switch (getClassB (I.getType ())) {
591    case cByte:
592     if (I.getType ()->isSigned ())
593       BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0);
594     else
595       BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0);
596     return;
597    case cShort:
598     if (I.getType ()->isSigned ())
599       BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0);
600     else
601       BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0);
602     return;
603    case cInt:
604     BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
605     return;
606    case cLong:
607     BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0);
608     BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4);
609     return;
610    case cFloat:
611     BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
612     return;
613    case cDouble:
614     BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0);
615     return;
616    default:
617     std::cerr << "Load instruction not handled: " << I;
618     abort ();
619     return;
620   }
621 }
622
623 void V8ISel::visitStoreInst(StoreInst &I) {
624   Value *SrcVal = I.getOperand (0);
625   unsigned SrcReg = getReg (SrcVal);
626   unsigned PtrReg = getReg (I.getOperand (1));
627   switch (getClassB (SrcVal->getType ())) {
628    case cByte:
629     BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
630     return;
631    case cShort:
632     BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
633     return;
634    case cInt:
635     BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
636     return;
637    case cLong:
638     BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
639     BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1);
640     return;
641    case cFloat:
642     BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
643     return;
644    case cDouble:
645     BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg);
646     return;
647    default:
648     std::cerr << "Store instruction not handled: " << I;
649     abort ();
650     return;
651   }
652 }
653
654 void V8ISel::visitCallInst(CallInst &I) {
655   MachineInstr *TheCall;
656   // Is it an intrinsic function call?
657   if (Function *F = I.getCalledFunction()) {
658     if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
659       visitIntrinsicCall(ID, I);   // Special intrinsics are not handled here
660       return;
661     }
662   }
663
664   // Deal with args
665   assert (I.getNumOperands () < 8
666           && "Can't handle pushing excess call args on the stack yet");
667   static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3,
668     V8::O4, V8::O5 };
669   for (unsigned i = 1; i < 7; ++i)
670     if (i < I.getNumOperands ()) {
671       assert (getClassB (I.getOperand (i)->getType ()) < cLong
672               && "Can't handle long or fp function call arguments yet");
673       unsigned ArgReg = getReg (I.getOperand (i));
674       // Schlep it over into the incoming arg register
675       BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0)
676         .addReg (ArgReg);
677     }
678
679   // Emit call instruction
680   if (Function *F = I.getCalledFunction ()) {
681     BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true);
682   } else {  // Emit an indirect call...
683     unsigned Reg = getReg (I.getCalledValue ());
684     BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0);
685   }
686
687   // Deal w/ return value: schlep it over into the destination register
688   if (I.getType () == Type::VoidTy)
689     return;
690   unsigned DestReg = getReg (I);
691   switch (getClass (I.getType ())) {
692     case cByte:
693     case cShort:
694     case cInt:
695       BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
696       break;
697     case cFloat:
698       BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0);
699       break;
700     default:
701       std::cerr << "Return type of call instruction not handled: " << I;
702       abort ();
703   }
704 }
705
706 void V8ISel::visitReturnInst(ReturnInst &I) {
707   if (I.getNumOperands () == 1) {
708     unsigned RetValReg = getReg (I.getOperand (0));
709     switch (getClass (I.getOperand (0)->getType ())) {
710       case cByte:
711       case cShort:
712       case cInt:
713         // Schlep it over into i0 (where it will become o0 after restore).
714         BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
715         break;
716       case cFloat:
717         BuildMI (BB, V8::FMOVS, 2, V8::F0).addReg(RetValReg);
718         break;
719       case cLong:
720         BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
721         BuildMI (BB, V8::ORrr, 2, V8::I1).addReg(V8::G0).addReg(RetValReg+1);
722         break;
723       default:
724         std::cerr << "Return instruction of this type not handled: " << I;
725         abort ();
726     }
727   }
728
729   // Just emit a 'retl' instruction to return.
730   BuildMI(BB, V8::RETL, 0);
731   return;
732 }
733
734 static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
735   Function::iterator I = BB; ++I;  // Get iterator to next block
736   return I != BB->getParent()->end() ? &*I : 0;
737 }
738
739 /// visitBranchInst - Handles conditional and unconditional branches.
740 ///
741 void V8ISel::visitBranchInst(BranchInst &I) {
742   BasicBlock *takenSucc = I.getSuccessor (0);
743   MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc];
744   BB->addSuccessor (takenSuccMBB);
745   if (I.isConditional()) {  // conditional branch
746     BasicBlock *notTakenSucc = I.getSuccessor (1);
747     MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc];
748     BB->addSuccessor (notTakenSuccMBB);
749
750     // CondReg=(<condition>);
751     // If (CondReg==0) goto notTakenSuccMBB;
752     unsigned CondReg = getReg (I.getCondition ());
753     BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg);
754     BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB);
755   }
756   // goto takenSuccMBB;
757   BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB);
758 }
759
760 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
761 /// constant expression GEP support.
762 ///
763 void V8ISel::emitGEPOperation (MachineBasicBlock *MBB,
764                                MachineBasicBlock::iterator IP,
765                                Value *Src, User::op_iterator IdxBegin,
766                                User::op_iterator IdxEnd, unsigned TargetReg) {
767   const TargetData &TD = TM.getTargetData ();
768   const Type *Ty = Src->getType ();
769   unsigned basePtrReg = getReg (Src, MBB, IP);
770
771   // GEPs have zero or more indices; we must perform a struct access
772   // or array access for each one.
773   for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
774        ++oi) {
775     Value *idx = *oi;
776     unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy);
777     if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
778       // It's a struct access.  idx is the index into the structure,
779       // which names the field. Use the TargetData structure to
780       // pick out what the layout of the structure is in memory.
781       // Use the (constant) structure index's value to find the
782       // right byte offset from the StructLayout class's list of
783       // structure member offsets.
784       unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue ();
785       unsigned memberOffset =
786         TD.getStructLayout (StTy)->MemberOffsets[fieldIndex];
787       // Emit an ADD to add memberOffset to the basePtr.
788       BuildMI (*MBB, IP, V8::ADDri, 2,
789                nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
790       // The next type is the member of the structure selected by the
791       // index.
792       Ty = StTy->getElementType (fieldIndex);
793     } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
794       // It's an array or pointer access: [ArraySize x ElementType].
795       // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
796       // must find the size of the pointed-to type (Not coincidentally, the next
797       // type is the type of the elements in the array).
798       Ty = SqTy->getElementType ();
799       unsigned elementSize = TD.getTypeSize (Ty);
800       unsigned idxReg = getReg (idx, MBB, IP);
801       unsigned OffsetReg = makeAnotherReg (Type::IntTy);
802       unsigned elementSizeReg = makeAnotherReg (Type::UIntTy);
803       copyConstantToRegister (MBB, IP,
804         ConstantUInt::get(Type::UIntTy, elementSize), elementSizeReg);
805       // Emit a SMUL to multiply the register holding the index by
806       // elementSize, putting the result in OffsetReg.
807       BuildMI (*MBB, IP, V8::SMULrr, 2,
808                OffsetReg).addReg (elementSizeReg).addReg (idxReg);
809       // Emit an ADD to add OffsetReg to the basePtr.
810       BuildMI (*MBB, IP, V8::ADDrr, 2,
811                nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
812     }
813     basePtrReg = nextBasePtrReg;
814   }
815   // After we have processed all the indices, the result is left in
816   // basePtrReg.  Move it to the register where we were expected to
817   // put the answer.
818   BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg);
819 }
820
821 void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) {
822   unsigned outputReg = getReg (I);
823   emitGEPOperation (BB, BB->end (), I.getOperand (0),
824                     I.op_begin ()+1, I.op_end (), outputReg);
825 }
826
827
828 void V8ISel::visitBinaryOperator (Instruction &I) {
829   unsigned DestReg = getReg (I);
830   unsigned Op0Reg = getReg (I.getOperand (0));
831   unsigned Op1Reg = getReg (I.getOperand (1));
832
833   unsigned Class = getClassB (I.getType());
834   unsigned OpCase = ~0;
835
836   if (Class > cLong) {
837     switch (I.getOpcode ()) {
838     case Instruction::Add: OpCase = 0; break;
839     case Instruction::Sub: OpCase = 1; break;
840     case Instruction::Mul: OpCase = 2; break;
841     case Instruction::Div: OpCase = 3; break;
842     default: visitInstruction (I); return;
843     }
844     static unsigned Opcodes[] = { V8::FADDS, V8::FADDD,
845                                   V8::FSUBS, V8::FSUBD,
846                                   V8::FMULS, V8::FMULD,
847                                   V8::FDIVS, V8::FDIVD };
848     BuildMI (BB, Opcodes[2*OpCase + (Class - cFloat)], 2, DestReg)
849       .addReg (Op0Reg).addReg (Op1Reg);
850     return;
851   }
852
853   unsigned ResultReg = DestReg;
854   if (Class != cInt)
855     ResultReg = makeAnotherReg (I.getType ());
856
857   // FIXME: support long, ulong, fp.
858   switch (I.getOpcode ()) {
859   case Instruction::Add: OpCase = 0; break;
860   case Instruction::Sub: OpCase = 1; break;
861   case Instruction::Mul: OpCase = 2; break;
862   case Instruction::And: OpCase = 3; break;
863   case Instruction::Or:  OpCase = 4; break;
864   case Instruction::Xor: OpCase = 5; break;
865   case Instruction::Shl: OpCase = 6; break;
866   case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break;
867
868   case Instruction::Div:
869   case Instruction::Rem: {
870     unsigned Dest = ResultReg;
871     if (I.getOpcode() == Instruction::Rem)
872       Dest = makeAnotherReg(I.getType());
873
874     // FIXME: this is probably only right for 32 bit operands.
875     if (I.getType ()->isSigned()) {
876       unsigned Tmp = makeAnotherReg (I.getType ());
877       // Sign extend into the Y register
878       BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
879       BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
880       BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
881     } else {
882       // Zero extend into the Y register, ie, just set it to zero
883       BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
884       BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
885     }
886
887     if (I.getOpcode() == Instruction::Rem) {
888       unsigned Tmp = makeAnotherReg (I.getType ());
889       BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
890       BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
891     }
892     break;
893   }
894   default:
895     visitInstruction (I);
896     return;
897   }
898
899   static const unsigned Opcodes[] = {
900     V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr,
901     V8::SLLrr, V8::SRLrr, V8::SRArr
902   };
903   if (OpCase != ~0U) {
904     BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
905   }
906
907   switch (getClass (I.getType ())) {
908     case cByte: 
909       if (I.getType ()->isSigned ()) { // add byte
910         BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
911       } else { // add ubyte
912         unsigned TmpReg = makeAnotherReg (I.getType ());
913         BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
914         BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
915       }
916       break;
917     case cShort:
918       if (I.getType ()->isSigned ()) { // add short
919         unsigned TmpReg = makeAnotherReg (I.getType ());
920         BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
921         BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
922       } else { // add ushort
923         unsigned TmpReg = makeAnotherReg (I.getType ());
924         BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
925         BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
926       }
927       break;
928     case cInt:
929       // Nothing todo here.
930       break;
931     case cLong:
932       // Only support and, or, xor.
933       if (OpCase < 3 || OpCase > 5) {
934         visitInstruction (I);
935         return;
936       }
937       // Do the other half of the value:
938       BuildMI (BB, Opcodes[OpCase], 2, ResultReg+1).addReg (Op0Reg+1)
939         .addReg (Op1Reg+1);
940       break;
941     default:
942       visitInstruction (I);
943   }
944 }
945
946 void V8ISel::visitSetCondInst(SetCondInst &I) {
947   unsigned Op0Reg = getReg (I.getOperand (0));
948   unsigned Op1Reg = getReg (I.getOperand (1));
949   unsigned DestReg = getReg (I);
950   const Type *Ty = I.getOperand (0)->getType ();
951   
952   assert (getClass (Ty) < cLong && "can't setcc on longs or fp yet");
953   // Compare the two values.
954   BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg);
955
956   unsigned BranchIdx;
957   switch (I.getOpcode()) {
958   default: assert(0 && "Unknown setcc instruction!");
959   case Instruction::SetEQ: BranchIdx = 0; break;
960   case Instruction::SetNE: BranchIdx = 1; break;
961   case Instruction::SetLT: BranchIdx = 2; break;
962   case Instruction::SetGT: BranchIdx = 3; break;
963   case Instruction::SetLE: BranchIdx = 4; break;
964   case Instruction::SetGE: BranchIdx = 5; break;
965   }
966   static unsigned OpcodeTab[12] = {
967                               // LLVM       SparcV8
968                               //        unsigned signed
969     V8::BE,   V8::BE,         // seteq = be      be
970     V8::BNE,  V8::BNE,        // setne = bne     bne
971     V8::BCS,  V8::BL,         // setlt = bcs     bl
972     V8::BGU,  V8::BG,         // setgt = bgu     bg
973     V8::BLEU, V8::BLE,        // setle = bleu    ble
974     V8::BCC,  V8::BGE         // setge = bcc     bge
975   };
976   unsigned Opcode = OpcodeTab[2*BranchIdx + (Ty->isSigned() ? 1 : 0)];
977
978   MachineBasicBlock *thisMBB = BB;
979   const BasicBlock *LLVM_BB = BB->getBasicBlock ();
980   //  thisMBB:
981   //  ...
982   //   subcc %reg0, %reg1, %g0
983   //   bCC copy1MBB
984   //   ba copy0MBB
985
986   // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
987   // if we could insert other, non-terminator instructions after the
988   // bCC. But MBB->getFirstTerminator() can't understand this.
989   MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB);
990   F->getBasicBlockList ().push_back (copy1MBB);
991   BuildMI (BB, Opcode, 1).addMBB (copy1MBB);
992   MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB);
993   F->getBasicBlockList ().push_back (copy0MBB);
994   BuildMI (BB, V8::BA, 1).addMBB (copy0MBB);
995   // Update machine-CFG edges
996   BB->addSuccessor (copy1MBB);
997   BB->addSuccessor (copy0MBB);
998
999   //  copy0MBB:
1000   //   %FalseValue = or %G0, 0
1001   //   ba sinkMBB
1002   BB = copy0MBB;
1003   unsigned FalseValue = makeAnotherReg (I.getType ());
1004   BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0);
1005   MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB);
1006   F->getBasicBlockList ().push_back (sinkMBB);
1007   BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1008   // Update machine-CFG edges
1009   BB->addSuccessor (sinkMBB);
1010
1011   DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n");
1012   DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n");
1013   DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n");
1014   DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n");
1015
1016   //  copy1MBB:
1017   //   %TrueValue = or %G0, 1
1018   //   ba sinkMBB
1019   BB = copy1MBB;
1020   unsigned TrueValue = makeAnotherReg (I.getType ());
1021   BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1);
1022   BuildMI (BB, V8::BA, 1).addMBB (sinkMBB);
1023   // Update machine-CFG edges
1024   BB->addSuccessor (sinkMBB);
1025
1026   //  sinkMBB:
1027   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1028   //  ...
1029   BB = sinkMBB;
1030   BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue)
1031     .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB);
1032 }
1033
1034 void V8ISel::visitAllocaInst(AllocaInst &I) {
1035   // Find the data size of the alloca inst's getAllocatedType.
1036   const Type *Ty = I.getAllocatedType();
1037   unsigned TySize = TM.getTargetData().getTypeSize(Ty);
1038
1039   unsigned ArraySizeReg = getReg (I.getArraySize ());
1040   unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize));
1041   unsigned TmpReg1 = makeAnotherReg (Type::UIntTy);
1042   unsigned TmpReg2 = makeAnotherReg (Type::UIntTy);
1043   unsigned StackAdjReg = makeAnotherReg (Type::UIntTy);
1044
1045   // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary
1046   BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg);
1047
1048   // Round up TmpReg1 to nearest doubleword boundary:
1049   BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7);
1050   BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8);
1051
1052   // Subtract size from stack pointer, thereby allocating some space.
1053   BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg);
1054
1055   // Put a pointer to the space into the result register, by copying
1056   // the stack pointer.
1057   BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96);
1058
1059   // Inform the Frame Information that we have just allocated a variable-sized
1060   // object.
1061   F->getFrameInfo()->CreateVariableSizedObject();
1062 }
1063
1064 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1065 /// function, lowering any calls to unknown intrinsic functions into the
1066 /// equivalent LLVM code.
1067 void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1068   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1069     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1070       if (CallInst *CI = dyn_cast<CallInst>(I++))
1071         if (Function *F = CI->getCalledFunction())
1072           switch (F->getIntrinsicID()) {
1073           case Intrinsic::not_intrinsic: break;
1074           default:
1075             // All other intrinsic calls we must lower.
1076             Instruction *Before = CI->getPrev();
1077             TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1078             if (Before) {        // Move iterator to instruction after call
1079               I = Before;  ++I;
1080             } else {
1081               I = BB->begin();
1082             }
1083           }
1084 }
1085
1086
1087 void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1088   unsigned TmpReg1, TmpReg2;
1089   switch (ID) {
1090   default: assert(0 && "Intrinsic not supported!");
1091   }
1092 }