Initial revision
[oota-llvm.git] / lib / Target / PowerPC / PPC32ISelSimple.cpp
1 //===-- InstSelectSimple.cpp - A simple instruction selector for PowerPC --===//
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 #include "PowerPC.h"
11 #include "PowerPCInstrBuilder.h"
12 #include "PowerPCInstrInfo.h"
13 #include "llvm/Constants.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Function.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/IntrinsicLowering.h"
18 #include "llvm/Pass.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/SSARegMap.h"
23 #include "llvm/Target/MRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/GetElementPtrTypeIterator.h"
26 #include "llvm/Support/InstVisitor.h"
27 using namespace llvm;
28
29 namespace {
30   /// TypeClass - Used by the PowerPC backend to group LLVM types by their basic PPC
31   /// Representation.
32   ///
33   enum TypeClass {
34     cByte, cShort, cInt, cFP, cLong
35   };
36 }
37
38 /// getClass - Turn a primitive type into a "class" number which is based on the
39 /// size of the type, and whether or not it is floating point.
40 ///
41 static inline TypeClass getClass(const Type *Ty) {
42   switch (Ty->getPrimitiveID()) {
43   case Type::SByteTyID:
44   case Type::UByteTyID:   return cByte;      // Byte operands are class #0
45   case Type::ShortTyID:
46   case Type::UShortTyID:  return cShort;     // Short operands are class #1
47   case Type::IntTyID:
48   case Type::UIntTyID:
49   case Type::PointerTyID: return cInt;       // Int's and pointers are class #2
50
51   case Type::FloatTyID:
52   case Type::DoubleTyID:  return cFP;        // Floating Point is #3
53
54   case Type::LongTyID:
55   case Type::ULongTyID:   return cLong;      // Longs are class #4
56   default:
57     assert(0 && "Invalid type to getClass!");
58     return cByte;  // not reached
59   }
60 }
61
62 // getClassB - Just like getClass, but treat boolean values as ints.
63 static inline TypeClass getClassB(const Type *Ty) {
64   if (Ty == Type::BoolTy) return cInt;
65   return getClass(Ty);
66 }
67
68 namespace {
69   struct ISel : public FunctionPass, InstVisitor<ISel> {
70     TargetMachine &TM;
71     MachineFunction *F;                 // The function we are compiling into
72     MachineBasicBlock *BB;              // The current MBB we are compiling
73     int VarArgsFrameIndex;              // FrameIndex for start of varargs area
74     int ReturnAddressIndex;             // FrameIndex for the return address
75
76     std::map<Value*, unsigned> RegMap;  // Mapping between Val's and SSA Regs
77
78     // MBBMap - Mapping between LLVM BB -> Machine BB
79     std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
80
81     // AllocaMap - Mapping from fixed sized alloca instructions to the
82     // FrameIndex for the alloca.
83     std::map<AllocaInst*, unsigned> AllocaMap;
84
85     ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
86
87     /// runOnFunction - Top level implementation of instruction selection for
88     /// the entire function.
89     ///
90     bool runOnFunction(Function &Fn) {
91       // First pass over the function, lower any unknown intrinsic functions
92       // with the IntrinsicLowering class.
93       LowerUnknownIntrinsicFunctionCalls(Fn);
94
95       F = &MachineFunction::construct(&Fn, TM);
96
97       // Create all of the machine basic blocks for the function...
98       for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
99         F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
100
101       BB = &F->front();
102
103       // Set up a frame object for the return address.  This is used by the
104       // llvm.returnaddress & llvm.frameaddress intrinisics.
105       ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
106
107       // Copy incoming arguments off of the stack...
108       LoadArgumentsToVirtualRegs(Fn);
109
110       // Instruction select everything except PHI nodes
111       visit(Fn);
112
113       // Select the PHI nodes
114       SelectPHINodes();
115
116       RegMap.clear();
117       MBBMap.clear();
118       AllocaMap.clear();
119       F = 0;
120       // We always build a machine code representation for the function
121       return true;
122     }
123
124     virtual const char *getPassName() const {
125       return "PowerPC Simple Instruction Selection";
126     }
127
128     /// visitBasicBlock - This method is called when we are visiting a new basic
129     /// block.  This simply creates a new MachineBasicBlock to emit code into
130     /// and adds it to the current MachineFunction.  Subsequent visit* for
131     /// instructions will be invoked for all instructions in the basic block.
132     ///
133     void visitBasicBlock(BasicBlock &LLVM_BB) {
134       BB = MBBMap[&LLVM_BB];
135     }
136
137     /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
138     /// function, lowering any calls to unknown intrinsic functions into the
139     /// equivalent LLVM code.
140     ///
141     void LowerUnknownIntrinsicFunctionCalls(Function &F);
142
143     /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
144     /// from the stack into virtual registers.
145     ///
146     void LoadArgumentsToVirtualRegs(Function &F);
147
148     /// SelectPHINodes - Insert machine code to generate phis.  This is tricky
149     /// because we have to generate our sources into the source basic blocks,
150     /// not the current one.
151     ///
152     void SelectPHINodes();
153
154     // Visitation methods for various instructions.  These methods simply emit
155     // fixed PowerPC code for each instruction.
156
157     // Control flow operators
158     void visitReturnInst(ReturnInst &RI);
159     void visitBranchInst(BranchInst &BI);
160
161     struct ValueRecord {
162       Value *Val;
163       unsigned Reg;
164       const Type *Ty;
165       ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
166       ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
167     };
168     void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
169                 const std::vector<ValueRecord> &Args);
170     void visitCallInst(CallInst &I);
171     void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
172
173     // Arithmetic operators
174     void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
175     void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
176     void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
177     void visitMul(BinaryOperator &B);
178
179     void visitDiv(BinaryOperator &B) { visitDivRem(B); }
180     void visitRem(BinaryOperator &B) { visitDivRem(B); }
181     void visitDivRem(BinaryOperator &B);
182
183     // Bitwise operators
184     void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
185     void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
186     void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
187
188     // Comparison operators...
189     void visitSetCondInst(SetCondInst &I);
190     unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
191                             MachineBasicBlock *MBB,
192                             MachineBasicBlock::iterator MBBI);
193     void visitSelectInst(SelectInst &SI);
194     
195     
196     // Memory Instructions
197     void visitLoadInst(LoadInst &I);
198     void visitStoreInst(StoreInst &I);
199     void visitGetElementPtrInst(GetElementPtrInst &I);
200     void visitAllocaInst(AllocaInst &I);
201     void visitMallocInst(MallocInst &I);
202     void visitFreeInst(FreeInst &I);
203     
204     // Other operators
205     void visitShiftInst(ShiftInst &I);
206     void visitPHINode(PHINode &I) {}      // PHI nodes handled by second pass
207     void visitCastInst(CastInst &I);
208     void visitVANextInst(VANextInst &I);
209     void visitVAArgInst(VAArgInst &I);
210
211     void visitInstruction(Instruction &I) {
212       std::cerr << "Cannot instruction select: " << I;
213       abort();
214     }
215
216     /// promote32 - Make a value 32-bits wide, and put it somewhere.
217     ///
218     void promote32(unsigned targetReg, const ValueRecord &VR);
219
220     /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
221     /// constant expression GEP support.
222     ///
223     void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
224                           Value *Src, User::op_iterator IdxBegin,
225                           User::op_iterator IdxEnd, unsigned TargetReg);
226
227     /// emitCastOperation - Common code shared between visitCastInst and
228     /// constant expression cast support.
229     ///
230     void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
231                            Value *Src, const Type *DestTy, unsigned TargetReg);
232
233     /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
234     /// and constant expression support.
235     ///
236     void emitSimpleBinaryOperation(MachineBasicBlock *BB,
237                                    MachineBasicBlock::iterator IP,
238                                    Value *Op0, Value *Op1,
239                                    unsigned OperatorClass, unsigned TargetReg);
240
241     /// emitBinaryFPOperation - This method handles emission of floating point
242     /// Add (0), Sub (1), Mul (2), and Div (3) operations.
243     void emitBinaryFPOperation(MachineBasicBlock *BB,
244                                MachineBasicBlock::iterator IP,
245                                Value *Op0, Value *Op1,
246                                unsigned OperatorClass, unsigned TargetReg);
247
248     void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
249                       Value *Op0, Value *Op1, unsigned TargetReg);
250
251     void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
252                     unsigned DestReg, const Type *DestTy,
253                     unsigned Op0Reg, unsigned Op1Reg);
254     void doMultiplyConst(MachineBasicBlock *MBB, 
255                          MachineBasicBlock::iterator MBBI,
256                          unsigned DestReg, const Type *DestTy,
257                          unsigned Op0Reg, unsigned Op1Val);
258
259     void emitDivRemOperation(MachineBasicBlock *BB,
260                              MachineBasicBlock::iterator IP,
261                              Value *Op0, Value *Op1, bool isDiv,
262                              unsigned TargetReg);
263
264     /// emitSetCCOperation - Common code shared between visitSetCondInst and
265     /// constant expression support.
266     ///
267     void emitSetCCOperation(MachineBasicBlock *BB,
268                             MachineBasicBlock::iterator IP,
269                             Value *Op0, Value *Op1, unsigned Opcode,
270                             unsigned TargetReg);
271
272     /// emitShiftOperation - Common code shared between visitShiftInst and
273     /// constant expression support.
274     ///
275     void emitShiftOperation(MachineBasicBlock *MBB,
276                             MachineBasicBlock::iterator IP,
277                             Value *Op, Value *ShiftAmount, bool isLeftShift,
278                             const Type *ResultTy, unsigned DestReg);
279       
280     /// emitSelectOperation - Common code shared between visitSelectInst and the
281     /// constant expression support.
282     void emitSelectOperation(MachineBasicBlock *MBB,
283                              MachineBasicBlock::iterator IP,
284                              Value *Cond, Value *TrueVal, Value *FalseVal,
285                              unsigned DestReg);
286
287     /// copyConstantToRegister - Output the instructions required to put the
288     /// specified constant into the specified register.
289     ///
290     void copyConstantToRegister(MachineBasicBlock *MBB,
291                                 MachineBasicBlock::iterator MBBI,
292                                 Constant *C, unsigned Reg);
293
294     void emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
295                    unsigned LHS, unsigned RHS);
296
297     /// makeAnotherReg - This method returns the next register number we haven't
298     /// yet used.
299     ///
300     /// Long values are handled somewhat specially.  They are always allocated
301     /// as pairs of 32 bit integer values.  The register number returned is the
302     /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
303     /// of the long value.
304     ///
305     unsigned makeAnotherReg(const Type *Ty) {
306       assert(dynamic_cast<const PowerPCRegisterInfo*>(TM.getRegisterInfo()) &&
307              "Current target doesn't have PPC reg info??");
308       const PowerPCRegisterInfo *MRI =
309         static_cast<const PowerPCRegisterInfo*>(TM.getRegisterInfo());
310       if (Ty == Type::LongTy || Ty == Type::ULongTy) {
311         const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
312         // Create the lower part
313         F->getSSARegMap()->createVirtualRegister(RC);
314         // Create the upper part.
315         return F->getSSARegMap()->createVirtualRegister(RC)-1;
316       }
317
318       // Add the mapping of regnumber => reg class to MachineFunction
319       const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
320       return F->getSSARegMap()->createVirtualRegister(RC);
321     }
322
323     /// getReg - This method turns an LLVM value into a register number.
324     ///
325     unsigned getReg(Value &V) { return getReg(&V); }  // Allow references
326     unsigned getReg(Value *V) {
327       // Just append to the end of the current bb.
328       MachineBasicBlock::iterator It = BB->end();
329       return getReg(V, BB, It);
330     }
331     unsigned getReg(Value *V, MachineBasicBlock *MBB,
332                     MachineBasicBlock::iterator IPt);
333
334     /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
335     /// that is to be statically allocated with the initial stack frame
336     /// adjustment.
337     unsigned getFixedSizedAllocaFI(AllocaInst *AI);
338   };
339 }
340
341 /// dyn_castFixedAlloca - If the specified value is a fixed size alloca
342 /// instruction in the entry block, return it.  Otherwise, return a null
343 /// pointer.
344 static AllocaInst *dyn_castFixedAlloca(Value *V) {
345   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
346     BasicBlock *BB = AI->getParent();
347     if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
348       return AI;
349   }
350   return 0;
351 }
352
353 /// getReg - This method turns an LLVM value into a register number.
354 ///
355 unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB,
356                       MachineBasicBlock::iterator IPt) {
357   // If this operand is a constant, emit the code to copy the constant into
358   // the register here...
359   //
360   if (Constant *C = dyn_cast<Constant>(V)) {
361     unsigned Reg = makeAnotherReg(V->getType());
362     copyConstantToRegister(MBB, IPt, C, Reg);
363     return Reg;
364   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
365     unsigned Reg1 = makeAnotherReg(V->getType());
366         unsigned Reg2 = makeAnotherReg(V->getType());
367     // Move the address of the global into the register
368     BuildMI(*MBB, IPt, PPC32::LOADHiAddr, 2, Reg1).addReg(PPC32::R0).addGlobalAddress(GV);
369     BuildMI(*MBB, IPt, PPC32::LOADLoAddr, 2, Reg2).addReg(Reg1).addGlobalAddress(GV);
370     return Reg2;
371   } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
372     // Do not emit noop casts at all.
373     if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()))
374       return getReg(CI->getOperand(0), MBB, IPt);
375   } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
376     unsigned Reg = makeAnotherReg(V->getType());
377     unsigned FI = getFixedSizedAllocaFI(AI);
378     addFrameReference(BuildMI(*MBB, IPt, PPC32::ADDI, 2, Reg), FI, 0, false);
379     return Reg;
380   }
381
382   unsigned &Reg = RegMap[V];
383   if (Reg == 0) {
384     Reg = makeAnotherReg(V->getType());
385     RegMap[V] = Reg;
386   }
387
388   return Reg;
389 }
390
391 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
392 /// that is to be statically allocated with the initial stack frame
393 /// adjustment.
394 unsigned ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
395   // Already computed this?
396   std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
397   if (I != AllocaMap.end() && I->first == AI) return I->second;
398
399   const Type *Ty = AI->getAllocatedType();
400   ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
401   unsigned TySize = TM.getTargetData().getTypeSize(Ty);
402   TySize *= CUI->getValue();   // Get total allocated size...
403   unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
404       
405   // Create a new stack object using the frame manager...
406   int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
407   AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
408   return FrameIdx;
409 }
410
411
412 /// copyConstantToRegister - Output the instructions required to put the
413 /// specified constant into the specified register.
414 ///
415 void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
416                                   MachineBasicBlock::iterator IP,
417                                   Constant *C, unsigned R) {
418   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
419     unsigned Class = 0;
420     switch (CE->getOpcode()) {
421     case Instruction::GetElementPtr:
422       emitGEPOperation(MBB, IP, CE->getOperand(0),
423                        CE->op_begin()+1, CE->op_end(), R);
424       return;
425     case Instruction::Cast:
426       emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
427       return;
428
429     case Instruction::Xor: ++Class; // FALL THROUGH
430     case Instruction::Or:  ++Class; // FALL THROUGH
431     case Instruction::And: ++Class; // FALL THROUGH
432     case Instruction::Sub: ++Class; // FALL THROUGH
433     case Instruction::Add:
434       emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
435                                 Class, R);
436       return;
437
438     case Instruction::Mul:
439       emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
440       return;
441
442     case Instruction::Div:
443     case Instruction::Rem:
444       emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
445                           CE->getOpcode() == Instruction::Div, R);
446       return;
447
448     case Instruction::SetNE:
449     case Instruction::SetEQ:
450     case Instruction::SetLT:
451     case Instruction::SetGT:
452     case Instruction::SetLE:
453     case Instruction::SetGE:
454       emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
455                          CE->getOpcode(), R);
456       return;
457
458     case Instruction::Shl:
459     case Instruction::Shr:
460       emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
461                          CE->getOpcode() == Instruction::Shl, CE->getType(), R);
462       return;
463
464     case Instruction::Select:
465       emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
466                           CE->getOperand(2), R);
467       return;
468
469     default:
470       std::cerr << "Offending expr: " << C << "\n";
471       assert(0 && "Constant expression not yet handled!\n");
472     }
473   }
474
475   if (C->getType()->isIntegral()) {
476     unsigned Class = getClassB(C->getType());
477
478     if (Class == cLong) {
479       // Copy the value into the register pair.
480       uint64_t Val = cast<ConstantInt>(C)->getRawValue();
481           unsigned hiTmp = makeAnotherReg(Type::IntTy);
482           unsigned loTmp = makeAnotherReg(Type::IntTy);
483       BuildMI(*MBB, IP, PPC32::ADDIS, 2, loTmp).addReg(PPC32::R0).addImm(Val >> 48);
484       BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(loTmp).addImm((Val >> 32) & 0xFFFF);
485       BuildMI(*MBB, IP, PPC32::ADDIS, 2, hiTmp).addReg(PPC32::R0).addImm((Val >> 16) & 0xFFFF);
486       BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(hiTmp).addImm(Val & 0xFFFF);
487       return;
488     }
489
490     assert(Class <= cInt && "Type not handled yet!");
491
492     if (C->getType() == Type::BoolTy) {
493       BuildMI(*MBB, IP, PPC32::ADDI, 2, R).addReg(PPC32::R0).addImm(C == ConstantBool::True);
494     } else if (Class == cByte || Class == cShort) {
495       ConstantInt *CI = cast<ConstantInt>(C);
496       BuildMI(*MBB, IP, PPC32::ADDI, 2, R).addReg(PPC32::R0).addImm(CI->getRawValue());
497     } else {
498       ConstantInt *CI = cast<ConstantInt>(C);
499       int TheVal = CI->getRawValue() & 0xFFFFFFFF;
500       if (TheVal < 32768 && TheVal >= -32768) {
501                 BuildMI(*MBB, IP, PPC32::ADDI, 2, R).addReg(PPC32::R0).addImm(CI->getRawValue());
502           } else {
503                 unsigned TmpReg = makeAnotherReg(Type::IntTy);
504                 BuildMI(*MBB, IP, PPC32::ADDIS, 2, TmpReg).addReg(PPC32::R0).addImm(CI->getRawValue() >> 16);
505                 BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(TmpReg).addImm(CI->getRawValue() & 0xFFFF);
506           }
507     }
508   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
509       // We need to spill the constant to memory...
510       MachineConstantPool *CP = F->getConstantPool();
511       unsigned CPI = CP->getConstantPoolIndex(CFP);
512       const Type *Ty = CFP->getType();
513
514       assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); 
515       unsigned LoadOpcode = Ty == Type::FloatTy ? PPC32::LFS : PPC32::LFD;
516       addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 2, R), CPI);
517   } else if (isa<ConstantPointerNull>(C)) {
518     // Copy zero (null pointer) to the register.
519     BuildMI(*MBB, IP, PPC32::ADDI, 2, R).addReg(PPC32::R0).addImm(0);
520   } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
521     BuildMI(*MBB, IP, PPC32::ADDIS, 2, R).addReg(PPC32::R0).addGlobalAddress(CPR->getValue());
522     BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(PPC32::R0).addGlobalAddress(CPR->getValue());
523   } else {
524     std::cerr << "Offending constant: " << C << "\n";
525     assert(0 && "Type not handled yet!");
526   }
527 }
528
529 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
530 /// the stack into virtual registers.
531 ///
532 /// FIXME: When we can calculate which args are coming in via registers
533 /// source them from there instead.
534 void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
535   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
536   unsigned GPR_remaining = 8;
537   unsigned FPR_remaining = 13;
538   unsigned GPR_idx = 3;
539   unsigned FPR_idx = 1;
540         
541   MachineFrameInfo *MFI = F->getFrameInfo();
542
543   for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
544     bool ArgLive = !I->use_empty();
545     unsigned Reg = ArgLive ? getReg(*I) : 0;
546     int FI;          // Frame object index
547
548     switch (getClassB(I->getType())) {
549     case cByte:
550       if (ArgLive) {
551         FI = MFI->CreateFixedObject(1, ArgOffset);
552                 if (GPR_remaining > 0) {
553                         BuildMI(BB, PPC32::OR, 2, Reg).addReg(PPC32::R0+GPR_idx).addReg(PPC32::R0+GPR_idx);
554                 } else {
555                         addFrameReference(BuildMI(BB, PPC32::LBZ, 2, Reg), FI);
556                 }
557           }
558       break;
559     case cShort:
560       if (ArgLive) {
561         FI = MFI->CreateFixedObject(2, ArgOffset);
562                 if (GPR_remaining > 0) {
563                         BuildMI(BB, PPC32::OR, 2, Reg).addReg(PPC32::R0+GPR_idx).addReg(PPC32::R0+GPR_idx);
564                 } else {
565                         addFrameReference(BuildMI(BB, PPC32::LHZ, 2, Reg), FI);
566                 }
567           }
568       break;
569     case cInt:
570       if (ArgLive) {
571         FI = MFI->CreateFixedObject(4, ArgOffset);
572                 if (GPR_remaining > 0) {
573                         BuildMI(BB, PPC32::OR, 2, Reg).addReg(PPC32::R0+GPR_idx).addReg(PPC32::R0+GPR_idx);
574                 } else {
575                         addFrameReference(BuildMI(BB, PPC32::LWZ, 2, Reg), FI);
576                 }
577           }
578       break;
579     case cLong:
580       if (ArgLive) {
581         FI = MFI->CreateFixedObject(8, ArgOffset);
582                 if (GPR_remaining > 1) {
583                         BuildMI(BB, PPC32::OR, 2, Reg).addReg(PPC32::R0+GPR_idx).addReg(PPC32::R0+GPR_idx);
584                         BuildMI(BB, PPC32::OR, 2, Reg+1).addReg(PPC32::R0+GPR_idx+1).addReg(PPC32::R0+GPR_idx+1);
585                 } else {
586                         addFrameReference(BuildMI(BB, PPC32::LWZ, 2, Reg), FI);
587                         addFrameReference(BuildMI(BB, PPC32::LWZ, 2, Reg+1), FI, 4);
588                 }
589           }
590       ArgOffset += 4;   // longs require 4 additional bytes
591           if (GPR_remaining > 1) {
592                 GPR_remaining--;    // uses up 2 GPRs
593                 GPR_idx++;
594           }
595       break;
596     case cFP:
597       if (ArgLive) {
598         unsigned Opcode;
599         if (I->getType() == Type::FloatTy) {
600           Opcode = PPC32::LFS;
601           FI = MFI->CreateFixedObject(4, ArgOffset);
602         } else {
603           Opcode = PPC32::LFD;
604           FI = MFI->CreateFixedObject(8, ArgOffset);
605         }
606                 if (FPR_remaining > 0) {
607                         BuildMI(BB, PPC32::FMR, 1, Reg).addReg(PPC32::F0+FPR_idx);
608                         FPR_remaining--;
609                         FPR_idx++;
610                 } else {
611                         addFrameReference(BuildMI(BB, Opcode, 2, Reg), FI);
612                 }
613           }
614       if (I->getType() == Type::DoubleTy) {
615         ArgOffset += 4;   // doubles require 4 additional bytes
616                 if (GPR_remaining > 0) {
617                         GPR_remaining--;    // uses up 2 GPRs
618                         GPR_idx++;
619                 }
620           }
621       break;
622     default:
623       assert(0 && "Unhandled argument type!");
624     }
625     ArgOffset += 4;  // Each argument takes at least 4 bytes on the stack...
626         if (GPR_remaining > 0) {
627                 GPR_remaining--;    // uses up 2 GPRs
628                 GPR_idx++;
629         }
630   }
631
632   // If the function takes variable number of arguments, add a frame offset for
633   // the start of the first vararg value... this is used to expand
634   // llvm.va_start.
635   if (Fn.getFunctionType()->isVarArg())
636     VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
637 }
638
639
640 /// SelectPHINodes - Insert machine code to generate phis.  This is tricky
641 /// because we have to generate our sources into the source basic blocks, not
642 /// the current one.
643 ///
644 void ISel::SelectPHINodes() {
645   const TargetInstrInfo &TII = *TM.getInstrInfo();
646   const Function &LF = *F->getFunction();  // The LLVM function...
647   for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
648     const BasicBlock *BB = I;
649     MachineBasicBlock &MBB = *MBBMap[I];
650
651     // Loop over all of the PHI nodes in the LLVM basic block...
652     MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
653     for (BasicBlock::const_iterator I = BB->begin();
654          PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
655
656       // Create a new machine instr PHI node, and insert it.
657       unsigned PHIReg = getReg(*PN);
658       MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
659                                     PPC32::PHI, PN->getNumOperands(), PHIReg);
660
661       MachineInstr *LongPhiMI = 0;
662       if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
663         LongPhiMI = BuildMI(MBB, PHIInsertPoint,
664                             PPC32::PHI, PN->getNumOperands(), PHIReg+1);
665
666       // PHIValues - Map of blocks to incoming virtual registers.  We use this
667       // so that we only initialize one incoming value for a particular block,
668       // even if the block has multiple entries in the PHI node.
669       //
670       std::map<MachineBasicBlock*, unsigned> PHIValues;
671
672       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
673         MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
674         unsigned ValReg;
675         std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
676           PHIValues.lower_bound(PredMBB);
677
678         if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
679           // We already inserted an initialization of the register for this
680           // predecessor.  Recycle it.
681           ValReg = EntryIt->second;
682
683         } else {        
684           // Get the incoming value into a virtual register.
685           //
686           Value *Val = PN->getIncomingValue(i);
687
688           // If this is a constant or GlobalValue, we may have to insert code
689           // into the basic block to compute it into a virtual register.
690           if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
691               isa<GlobalValue>(Val)) {
692             // Simple constants get emitted at the end of the basic block,
693             // before any terminator instructions.  We "know" that the code to
694             // move a constant into a register will never clobber any flags.
695             ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
696           } else {
697             // Because we don't want to clobber any values which might be in
698             // physical registers with the computation of this constant (which
699             // might be arbitrarily complex if it is a constant expression),
700             // just insert the computation at the top of the basic block.
701             MachineBasicBlock::iterator PI = PredMBB->begin();
702             
703             // Skip over any PHI nodes though!
704             while (PI != PredMBB->end() && PI->getOpcode() == PPC32::PHI)
705               ++PI;
706             
707             ValReg = getReg(Val, PredMBB, PI);
708           }
709
710           // Remember that we inserted a value for this PHI for this predecessor
711           PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
712         }
713
714         PhiMI->addRegOperand(ValReg);
715         PhiMI->addMachineBasicBlockOperand(PredMBB);
716         if (LongPhiMI) {
717           LongPhiMI->addRegOperand(ValReg+1);
718           LongPhiMI->addMachineBasicBlockOperand(PredMBB);
719         }
720       }
721
722       // Now that we emitted all of the incoming values for the PHI node, make
723       // sure to reposition the InsertPoint after the PHI that we just added.
724       // This is needed because we might have inserted a constant into this
725       // block, right after the PHI's which is before the old insert point!
726       PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
727       ++PHIInsertPoint;
728     }
729   }
730 }
731
732
733 // canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
734 // it into the conditional branch or select instruction which is the only user
735 // of the cc instruction.  This is the case if the conditional branch is the
736 // only user of the setcc, and if the setcc is in the same basic block as the
737 // conditional branch.  We also don't handle long arguments below, so we reject
738 // them here as well.
739 //
740 static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
741   if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
742     if (SCI->hasOneUse()) {
743       Instruction *User = cast<Instruction>(SCI->use_back());
744       if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
745           SCI->getParent() == User->getParent() &&
746           (getClassB(SCI->getOperand(0)->getType()) != cLong ||
747            SCI->getOpcode() == Instruction::SetEQ ||
748            SCI->getOpcode() == Instruction::SetNE))
749         return SCI;
750     }
751   return 0;
752 }
753
754 // Return a fixed numbering for setcc instructions which does not depend on the
755 // order of the opcodes.
756 //
757 static unsigned getSetCCNumber(unsigned Opcode) {
758   switch(Opcode) {
759   default: assert(0 && "Unknown setcc instruction!");
760   case Instruction::SetEQ: return 0;
761   case Instruction::SetNE: return 1;
762   case Instruction::SetLT: return 2;
763   case Instruction::SetGE: return 3;
764   case Instruction::SetGT: return 4;
765   case Instruction::SetLE: return 5;
766   }
767 }
768
769 /// emitUCOM - emits an unordered FP compare.
770 void ISel::emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
771                      unsigned LHS, unsigned RHS) {
772         BuildMI(*MBB, IP, PPC32::FCMPU, 2, PPC32::CR0).addReg(LHS).addReg(RHS);
773 }
774
775 // EmitComparison - This function emits a comparison of the two operands,
776 // returning the extended setcc code to use.
777 unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
778                               MachineBasicBlock *MBB,
779                               MachineBasicBlock::iterator IP) {
780   // The arguments are already supposed to be of the same type.
781   const Type *CompTy = Op0->getType();
782   unsigned Class = getClassB(CompTy);
783   unsigned Op0r = getReg(Op0, MBB, IP);
784
785   // Special case handling of: cmp R, i
786   if (isa<ConstantPointerNull>(Op1)) {
787       BuildMI(*MBB, IP, PPC32::CMPI, 2, PPC32::CR0).addReg(Op0r).addImm(0);
788   } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
789     if (Class == cByte || Class == cShort || Class == cInt) {
790       unsigned Op1v = CI->getRawValue();
791
792       // Mask off any upper bits of the constant, if there are any...
793       Op1v &= (1ULL << (8 << Class)) - 1;
794
795           // Compare immediate or promote to reg?
796           if (Op1v <= 32767) {
797                 BuildMI(*MBB, IP, CompTy->isSigned() ? PPC32::CMPI : PPC32::CMPLI, 3, PPC32::CR0).addImm(0).addReg(Op0r).addImm(Op1v);
798           } else {
799                 unsigned Op1r = getReg(Op1, MBB, IP);
800                 BuildMI(*MBB, IP, CompTy->isSigned() ? PPC32::CMP : PPC32::CMPL, 3, PPC32::CR0).addImm(0).addReg(Op0r).addReg(Op1r);
801           }
802       return OpNum;
803     } else {
804       assert(Class == cLong && "Unknown integer class!");
805       unsigned LowCst = CI->getRawValue();
806       unsigned HiCst = CI->getRawValue() >> 32;
807       if (OpNum < 2) {    // seteq, setne
808         unsigned LoTmp = Op0r;
809         if (LowCst != 0) {
810                   unsigned LoLow = makeAnotherReg(Type::IntTy);
811           unsigned LoTmp = makeAnotherReg(Type::IntTy);
812           BuildMI(*MBB, IP, PPC32::XORI, 2, LoLow).addReg(Op0r).addImm(LowCst);
813           BuildMI(*MBB, IP, PPC32::XORIS, 2, LoTmp).addReg(LoLow).addImm(LowCst >> 16);
814         }
815         unsigned HiTmp = Op0r+1;
816         if (HiCst != 0) {
817                   unsigned HiLow = makeAnotherReg(Type::IntTy);
818           unsigned HiTmp = makeAnotherReg(Type::IntTy);
819           BuildMI(*MBB, IP, PPC32::XORI, 2, HiLow).addReg(Op0r+1).addImm(HiCst);
820           BuildMI(*MBB, IP, PPC32::XORIS, 2, HiTmp).addReg(HiLow).addImm(HiCst >> 16);
821         }
822         unsigned FinalTmp = makeAnotherReg(Type::IntTy);
823         BuildMI(*MBB, IP, PPC32::ORo, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
824         //BuildMI(*MBB, IP, PPC32::CMPLI, 2, PPC32::CR0).addReg(FinalTmp).addImm(0);
825         return OpNum;
826       } else {
827         // Emit a sequence of code which compares the high and low parts once
828         // each, then uses a conditional move to handle the overflow case.  For
829         // example, a setlt for long would generate code like this:
830         //
831         // AL = lo(op1) < lo(op2)   // Always unsigned comparison
832         // BL = hi(op1) < hi(op2)   // Signedness depends on operands
833         // dest = hi(op1) == hi(op2) ? BL : AL;
834         //
835
836         // FIXME: Not Yet Implemented
837                 return OpNum;
838       }
839     }
840   }
841
842   unsigned Op1r = getReg(Op1, MBB, IP);
843   switch (Class) {
844   default: assert(0 && "Unknown type class!");
845   case cByte:
846   case cShort:
847   case cInt:
848         BuildMI(*MBB, IP, CompTy->isSigned() ? PPC32::CMP : PPC32::CMPL, 2, PPC32::CR0).addReg(Op0r).addReg(Op1r);
849     break;
850   case cFP:
851     emitUCOM(MBB, IP, Op0r, Op1r);
852     break;
853
854   case cLong:
855     if (OpNum < 2) {    // seteq, setne
856       unsigned LoTmp = makeAnotherReg(Type::IntTy);
857       unsigned HiTmp = makeAnotherReg(Type::IntTy);
858       unsigned FinalTmp = makeAnotherReg(Type::IntTy);
859       BuildMI(*MBB, IP, PPC32::XOR, 2, LoTmp).addReg(Op0r).addReg(Op1r);
860       BuildMI(*MBB, IP, PPC32::XOR, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
861       BuildMI(*MBB, IP, PPC32::ORo,  2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
862       //BuildMI(*MBB, IP, PPC32::CMPLI, 2, PPC32::CR0).addReg(FinalTmp).addImm(0);
863       break;  // Allow the sete or setne to be generated from flags set by OR
864     } else {
865       // Emit a sequence of code which compares the high and low parts once
866       // each, then uses a conditional move to handle the overflow case.  For
867       // example, a setlt for long would generate code like this:
868       //
869       // AL = lo(op1) < lo(op2)   // Signedness depends on operands
870       // BL = hi(op1) < hi(op2)   // Always unsigned comparison
871       // dest = hi(op1) == hi(op2) ? BL : AL;
872       //
873
874       // FIXME: Not Yet Implemented
875       return OpNum;
876     }
877   }
878   return OpNum;
879 }
880
881 /// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
882 /// register, then move it to wherever the result should be. 
883 ///
884 void ISel::visitSetCondInst(SetCondInst &I) {
885   if (canFoldSetCCIntoBranchOrSelect(&I))
886     return;  // Fold this into a branch or select.
887
888   unsigned DestReg = getReg(I);
889   MachineBasicBlock::iterator MII = BB->end();
890   emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),DestReg);
891 }
892
893 /// emitSetCCOperation - Common code shared between visitSetCondInst and
894 /// constant expression support.
895 ///
896 /// FIXME: this is wrong.  we should figure out a way to guarantee
897 /// TargetReg is a CR and then make it a no-op
898 void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
899                               MachineBasicBlock::iterator IP,
900                               Value *Op0, Value *Op1, unsigned Opcode,
901                               unsigned TargetReg) {
902   unsigned OpNum = getSetCCNumber(Opcode);
903   OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
904
905   // The value is already in CR0 at this point, do nothing.
906 }
907
908
909 void ISel::visitSelectInst(SelectInst &SI) {
910   unsigned DestReg = getReg(SI);
911   MachineBasicBlock::iterator MII = BB->end();
912   emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),SI.getFalseValue(), DestReg);
913 }
914  
915 /// emitSelect - Common code shared between visitSelectInst and the constant
916 /// expression support.
917 /// FIXME: this is most likely broken in one or more ways.  Namely, PowerPC has
918 /// no select instruction.  FSEL only works for comparisons against zero.
919 void ISel::emitSelectOperation(MachineBasicBlock *MBB,
920                                MachineBasicBlock::iterator IP,
921                                Value *Cond, Value *TrueVal, Value *FalseVal,
922                                unsigned DestReg) {
923   unsigned SelectClass = getClassB(TrueVal->getType());
924
925   unsigned TrueReg  = getReg(TrueVal, MBB, IP);
926   unsigned FalseReg = getReg(FalseVal, MBB, IP);
927
928   if (TrueReg == FalseReg) {
929         if (SelectClass == cFP) {
930                 BuildMI(*MBB, IP, PPC32::FMR, 1, DestReg).addReg(TrueReg);
931         } else {
932                 BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(TrueReg).addReg(TrueReg);
933         }
934         
935     if (SelectClass == cLong)
936                 BuildMI(*MBB, IP, PPC32::OR, 2, DestReg+1).addReg(TrueReg+1).addReg(TrueReg+1);
937     return;
938   }
939
940   unsigned CondReg = getReg(Cond, MBB, IP);
941   unsigned numZeros = makeAnotherReg(Type::IntTy);
942   unsigned falseHi = makeAnotherReg(Type::IntTy);
943   unsigned falseAll = makeAnotherReg(Type::IntTy);
944   unsigned trueAll = makeAnotherReg(Type::IntTy);
945   unsigned Temp1 = makeAnotherReg(Type::IntTy);
946   unsigned Temp2 = makeAnotherReg(Type::IntTy);
947
948   BuildMI(*MBB, IP, PPC32::CNTLZW, 1, numZeros).addReg(CondReg);
949   BuildMI(*MBB, IP, PPC32::RLWINM, 4, falseHi).addReg(numZeros).addImm(26).addImm(0).addImm(0);
950   BuildMI(*MBB, IP, PPC32::SRAWI, 2, falseAll).addReg(falseHi).addImm(31);
951   BuildMI(*MBB, IP, PPC32::NOR, 2, trueAll).addReg(falseAll).addReg(falseAll);
952   BuildMI(*MBB, IP, PPC32::AND, 2, Temp1).addReg(TrueReg).addReg(trueAll);
953   BuildMI(*MBB, IP, PPC32::AND, 2, Temp2).addReg(FalseReg).addReg(falseAll);
954   BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(Temp1).addReg(Temp2);
955   
956   if (SelectClass == cLong) {
957         unsigned Temp3 = makeAnotherReg(Type::IntTy);
958         unsigned Temp4 = makeAnotherReg(Type::IntTy);
959         BuildMI(*MBB, IP, PPC32::AND, 2, Temp3).addReg(TrueReg+1).addReg(trueAll);
960         BuildMI(*MBB, IP, PPC32::AND, 2, Temp4).addReg(FalseReg+1).addReg(falseAll);
961         BuildMI(*MBB, IP, PPC32::OR, 2, DestReg+1).addReg(Temp3).addReg(Temp4);
962   }
963   
964   return;
965 }
966
967
968
969 /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
970 /// operand, in the specified target register.
971 ///
972 void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
973   bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
974
975   Value *Val = VR.Val;
976   const Type *Ty = VR.Ty;
977   if (Val) {
978     if (Constant *C = dyn_cast<Constant>(Val)) {
979       Val = ConstantExpr::getCast(C, Type::IntTy);
980       Ty = Type::IntTy;
981     }
982
983     // If this is a simple constant, just emit a load directly to avoid the copy.
984     if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
985       int TheVal = CI->getRawValue() & 0xFFFFFFFF;
986
987       if (TheVal < 32768 && TheVal >= -32768) {
988                 BuildMI(BB, PPC32::ADDI, 2, targetReg).addReg(PPC32::R0).addImm(TheVal);
989           } else {
990                 unsigned TmpReg = makeAnotherReg(Type::IntTy);
991                 BuildMI(BB, PPC32::ADDIS, 2, TmpReg).addReg(PPC32::R0).addImm(TheVal >> 16);
992                 BuildMI(BB, PPC32::ORI, 2, targetReg).addReg(TmpReg).addImm(TheVal & 0xFFFF);
993           }
994       return;
995     }
996   }
997
998   // Make sure we have the register number for this value...
999   unsigned Reg = Val ? getReg(Val) : VR.Reg;
1000
1001   switch (getClassB(Ty)) {
1002   case cByte:
1003     // Extend value into target register (8->32)
1004     if (isUnsigned)
1005       BuildMI(BB, PPC32::RLWINM, 4, targetReg).addReg(Reg).addZImm(0).addZImm(24).addZImm(31);
1006     else
1007       BuildMI(BB, PPC32::EXTSB, 1, targetReg).addReg(Reg);
1008     break;
1009   case cShort:
1010     // Extend value into target register (16->32)
1011     if (isUnsigned)
1012       BuildMI(BB, PPC32::RLWINM, 4, targetReg).addReg(Reg).addZImm(0).addZImm(16).addZImm(31);
1013     else
1014       BuildMI(BB, PPC32::EXTSH, 1, targetReg).addReg(Reg);
1015     break;
1016   case cInt:
1017     // Move value into target register (32->32)
1018     BuildMI(BB, PPC32::ORI, 2, targetReg).addReg(Reg).addReg(Reg);
1019     break;
1020   default:
1021     assert(0 && "Unpromotable operand class in promote32");
1022   }
1023 }
1024
1025 // just emit blr.
1026 void ISel::visitReturnInst(ReturnInst &I) {
1027   Value *RetVal = I.getOperand(0);
1028
1029   switch (getClassB(RetVal->getType())) {
1030   case cByte:   // integral return values: extend or move into r3 and return
1031   case cShort:
1032   case cInt:
1033     promote32(PPC32::R3, ValueRecord(RetVal));
1034     break;
1035   case cFP: {   // Floats & Doubles: Return in f1
1036     unsigned RetReg = getReg(RetVal);
1037     BuildMI(BB, PPC32::FMR, 1, PPC32::F1).addReg(RetReg);
1038     break;
1039   }
1040   case cLong: {
1041     unsigned RetReg = getReg(RetVal);
1042     BuildMI(BB, PPC32::OR, 2, PPC32::R3).addReg(RetReg).addReg(RetReg);
1043     BuildMI(BB, PPC32::OR, 2, PPC32::R4).addReg(RetReg+1).addReg(RetReg+1);
1044     break;
1045   }
1046   default:
1047     visitInstruction(I);
1048   }
1049   BuildMI(BB, PPC32::BLR, 1).addImm(0);
1050 }
1051
1052 // getBlockAfter - Return the basic block which occurs lexically after the
1053 // specified one.
1054 static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1055   Function::iterator I = BB; ++I;  // Get iterator to next block
1056   return I != BB->getParent()->end() ? &*I : 0;
1057 }
1058
1059 /// visitBranchInst - Handle conditional and unconditional branches here.  Note
1060 /// that since code layout is frozen at this point, that if we are trying to
1061 /// jump to a block that is the immediate successor of the current block, we can
1062 /// just make a fall-through (but we don't currently).
1063 ///
1064 void ISel::visitBranchInst(BranchInst &BI) {
1065         // Update machine-CFG edges
1066         BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1067         if (BI.isConditional())
1068                 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1069         
1070         BasicBlock *NextBB = getBlockAfter(BI.getParent());  // BB after current one
1071         
1072         if (!BI.isConditional()) {  // Unconditional branch?
1073                 if (BI.getSuccessor(0) != NextBB)
1074                         BuildMI(BB, PPC32::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1075                 return;
1076         }
1077         
1078   // See if we can fold the setcc into the branch itself...
1079   SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
1080   if (SCI == 0) {
1081     // Nope, cannot fold setcc into this branch.  Emit a branch on a condition
1082     // computed some other way...
1083     unsigned condReg = getReg(BI.getCondition());
1084         BuildMI(BB, PPC32::CMPLI, 3, PPC32::CR0).addImm(0).addReg(condReg).addImm(0);
1085     if (BI.getSuccessor(1) == NextBB) {
1086       if (BI.getSuccessor(0) != NextBB)
1087         BuildMI(BB, PPC32::BC, 3).addImm(4).addImm(2).addMBB(MBBMap[BI.getSuccessor(0)]);
1088     } else {
1089           BuildMI(BB, PPC32::BC, 3).addImm(12).addImm(2).addMBB(MBBMap[BI.getSuccessor(1)]);
1090       
1091       if (BI.getSuccessor(0) != NextBB)
1092         BuildMI(BB, PPC32::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1093     }
1094     return;
1095   }
1096
1097
1098   unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1099   MachineBasicBlock::iterator MII = BB->end();
1100   OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
1101
1102   const Type *CompTy = SCI->getOperand(0)->getType();
1103   bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1104   
1105   // LLVM  -> X86 signed  X86 unsigned
1106   // -----    ----------  ------------
1107   // seteq -> je          je
1108   // setne -> jne         jne
1109   // setlt -> jl          jb
1110   // setge -> jge         jae
1111   // setgt -> jg          ja
1112   // setle -> jle         jbe
1113
1114   static const unsigned BITab[6] = { 2, 2, 0, 0, 1, 1 };
1115   unsigned BO_true = (OpNum % 2 == 0) ? 12 : 4;
1116   unsigned BO_false = (OpNum % 2 == 0) ? 4 : 12;
1117   unsigned BIval = BITab[0];
1118
1119   if (BI.getSuccessor(0) != NextBB) {
1120                 BuildMI(BB, PPC32::BC, 3).addImm(BO_true).addImm(BIval).addMBB(MBBMap[BI.getSuccessor(0)]);
1121     if (BI.getSuccessor(1) != NextBB)
1122                 BuildMI(BB, PPC32::B, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
1123   } else {
1124     // Change to the inverse condition...
1125     if (BI.getSuccessor(1) != NextBB) {
1126           BuildMI(BB, PPC32::BC, 3).addImm(BO_false).addImm(BIval).addMBB(MBBMap[BI.getSuccessor(1)]);
1127     }
1128   }
1129 }
1130
1131
1132 /// doCall - This emits an abstract call instruction, setting up the arguments
1133 /// and the return value as appropriate.  For the actual function call itself,
1134 /// it inserts the specified CallMI instruction into the stream.
1135 ///
1136 /// FIXME: See Documentation at the following URL for "correct" behavior
1137 /// <http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/2rt_powerpc_abi/chapter_9_section_5.html>
1138 void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1139                   const std::vector<ValueRecord> &Args) {
1140   // Count how many bytes are to be pushed on the stack...
1141   unsigned NumBytes = 0;
1142
1143   if (!Args.empty()) {
1144     for (unsigned i = 0, e = Args.size(); i != e; ++i)
1145       switch (getClassB(Args[i].Ty)) {
1146       case cByte: case cShort: case cInt:
1147         NumBytes += 4; break;
1148       case cLong:
1149         NumBytes += 8; break;
1150       case cFP:
1151         NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1152         break;
1153       default: assert(0 && "Unknown class!");
1154       }
1155
1156     // Adjust the stack pointer for the new arguments...
1157     BuildMI(BB, PPC32::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
1158
1159     // Arguments go on the stack in reverse order, as specified by the ABI.
1160     unsigned ArgOffset = 0;
1161         unsigned GPR_remaining = 8;
1162         unsigned FPR_remaining = 13;
1163         unsigned GPR_idx = 3;
1164         unsigned FPR_idx = 1;
1165         
1166     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1167       unsigned ArgReg;
1168       switch (getClassB(Args[i].Ty)) {
1169       case cByte:
1170       case cShort:
1171         // Promote arg to 32 bits wide into a temporary register...
1172         ArgReg = makeAnotherReg(Type::UIntTy);
1173         promote32(ArgReg, Args[i]);
1174                   
1175                 // Reg or stack?
1176                 if (GPR_remaining > 0) {
1177                         BuildMI(BB, PPC32::OR, 2, PPC32::R0 + GPR_idx).addReg(ArgReg).addReg(ArgReg);
1178                 } else {
1179                         BuildMI(BB, PPC32::STW, 3).addReg(ArgReg).addImm(ArgOffset).addReg(PPC32::R1);
1180                 }
1181                 break;
1182       case cInt:
1183         ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1184
1185                 // Reg or stack?
1186                 if (GPR_remaining > 0) {
1187                     BuildMI(BB, PPC32::OR, 2, PPC32::R0 + GPR_idx).addReg(ArgReg).addReg(ArgReg);
1188                 } else {
1189                     BuildMI(BB, PPC32::STW, 3).addReg(ArgReg).addImm(ArgOffset).addReg(PPC32::R1);
1190                 }
1191                 break;
1192       case cLong:
1193                 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1194
1195                 // Reg or stack?
1196                 if (GPR_remaining > 1) {
1197                     BuildMI(BB, PPC32::OR, 2, PPC32::R0 + GPR_idx).addReg(ArgReg).addReg(ArgReg);
1198                     BuildMI(BB, PPC32::OR, 2, PPC32::R0 + GPR_idx + 1).addReg(ArgReg+1).addReg(ArgReg+1);
1199                 } else {
1200                     BuildMI(BB, PPC32::STW, 3).addReg(ArgReg).addImm(ArgOffset).addReg(PPC32::R1);
1201                     BuildMI(BB, PPC32::STW, 3).addReg(ArgReg+1).addImm(ArgOffset+4).addReg(PPC32::R1);
1202                 }
1203
1204         ArgOffset += 4;        // 8 byte entry, not 4.
1205                 if (GPR_remaining > 0) {
1206                         GPR_remaining -= 1;    // uses up 2 GPRs
1207                         GPR_idx += 1;
1208                 }
1209         break;
1210       case cFP:
1211         ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1212         if (Args[i].Ty == Type::FloatTy) {
1213                         // Reg or stack?
1214                         if (FPR_remaining > 0) {
1215                                 BuildMI(BB, PPC32::FMR, 1, PPC32::F0 + FPR_idx).addReg(ArgReg);
1216                                 FPR_remaining--;
1217                                 FPR_idx++;
1218                         } else {
1219                                 BuildMI(BB, PPC32::STFS, 3).addReg(ArgReg).addImm(ArgOffset).addReg(PPC32::R1);
1220                         }
1221         } else {
1222           assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
1223                         // Reg or stack?
1224                         if (FPR_remaining > 0) {
1225                                 BuildMI(BB, PPC32::FMR, 1, PPC32::F0 + FPR_idx).addReg(ArgReg);
1226                                 FPR_remaining--;
1227                                 FPR_idx++;
1228                         } else {
1229                                 BuildMI(BB, PPC32::STFD, 3).addReg(ArgReg).addImm(ArgOffset).addReg(PPC32::R1);
1230                         }
1231
1232                         ArgOffset += 4;       // 8 byte entry, not 4.
1233                         if (GPR_remaining > 0) {
1234                                 GPR_remaining--;    // uses up 2 GPRs
1235                                 GPR_idx++;
1236                         }
1237         }
1238         break;
1239
1240       default: assert(0 && "Unknown class!");
1241       }
1242       ArgOffset += 4;
1243           if (GPR_remaining > 0) {
1244                 GPR_remaining--;    // uses up 2 GPRs
1245                 GPR_idx++;
1246           }
1247     }
1248   } else {
1249     BuildMI(BB, PPC32::ADJCALLSTACKDOWN, 1).addImm(0);
1250   }
1251
1252   BB->push_back(CallMI);
1253
1254   BuildMI(BB, PPC32::ADJCALLSTACKUP, 1).addImm(NumBytes);
1255
1256   // If there is a return value, scavenge the result from the location the call
1257   // leaves it in...
1258   //
1259   if (Ret.Ty != Type::VoidTy) {
1260     unsigned DestClass = getClassB(Ret.Ty);
1261     switch (DestClass) {
1262     case cByte:
1263     case cShort:
1264     case cInt:
1265       // Integral results are in r3
1266           BuildMI(BB, PPC32::OR, 2, Ret.Reg).addReg(PPC32::R3).addReg(PPC32::R3);
1267     case cFP:     // Floating-point return values live in f1
1268       BuildMI(BB, PPC32::FMR, 1, Ret.Reg).addReg(PPC32::F1);
1269       break;
1270     case cLong:   // Long values are in r3:r4
1271           BuildMI(BB, PPC32::OR, 2, Ret.Reg).addReg(PPC32::R3).addReg(PPC32::R3);
1272           BuildMI(BB, PPC32::OR, 2, Ret.Reg+1).addReg(PPC32::R4).addReg(PPC32::R4);
1273       break;
1274     default: assert(0 && "Unknown class!");
1275     }
1276   }
1277 }
1278
1279
1280 /// visitCallInst - Push args on stack and do a procedure call instruction.
1281 void ISel::visitCallInst(CallInst &CI) {
1282   MachineInstr *TheCall;
1283   if (Function *F = CI.getCalledFunction()) {
1284     // Is it an intrinsic function call?
1285     if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
1286       visitIntrinsicCall(ID, CI);   // Special intrinsics are not handled here
1287       return;
1288     }
1289
1290     // Emit a CALL instruction with PC-relative displacement.
1291     TheCall = BuildMI(PPC32::CALLpcrel, 1).addGlobalAddress(F, true);
1292   } else {  // Emit an indirect call through the CTR
1293     unsigned Reg = getReg(CI.getCalledValue());
1294     BuildMI(PPC32::MTSPR, 2).addZImm(9).addReg(Reg);
1295     TheCall = BuildMI(PPC32::CALLindirect, 1).addZImm(20).addZImm(0);
1296   }
1297
1298   std::vector<ValueRecord> Args;
1299   for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
1300     Args.push_back(ValueRecord(CI.getOperand(i)));
1301
1302   unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1303   doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
1304 }         
1305
1306
1307 /// dyncastIsNan - Return the operand of an isnan operation if this is an isnan.
1308 ///
1309 static Value *dyncastIsNan(Value *V) {
1310   if (CallInst *CI = dyn_cast<CallInst>(V))
1311     if (Function *F = CI->getCalledFunction())
1312       if (F->getIntrinsicID() == Intrinsic::isnan)
1313         return CI->getOperand(1);
1314   return 0;
1315 }
1316
1317 /// isOnlyUsedByUnorderedComparisons - Return true if this value is only used by
1318 /// or's whos operands are all calls to the isnan predicate.
1319 static bool isOnlyUsedByUnorderedComparisons(Value *V) {
1320   assert(dyncastIsNan(V) && "The value isn't an isnan call!");
1321
1322   // Check all uses, which will be or's of isnans if this predicate is true.
1323   for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
1324     Instruction *I = cast<Instruction>(*UI);
1325     if (I->getOpcode() != Instruction::Or) return false;
1326     if (I->getOperand(0) != V && !dyncastIsNan(I->getOperand(0))) return false;
1327     if (I->getOperand(1) != V && !dyncastIsNan(I->getOperand(1))) return false;
1328   }
1329
1330   return true;
1331 }
1332
1333 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1334 /// function, lowering any calls to unknown intrinsic functions into the
1335 /// equivalent LLVM code.
1336 ///
1337 void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1338   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1339     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1340       if (CallInst *CI = dyn_cast<CallInst>(I++))
1341         if (Function *F = CI->getCalledFunction())
1342           switch (F->getIntrinsicID()) {
1343           case Intrinsic::not_intrinsic:
1344           case Intrinsic::vastart:
1345           case Intrinsic::vacopy:
1346           case Intrinsic::vaend:
1347           case Intrinsic::returnaddress:
1348           case Intrinsic::frameaddress:
1349           case Intrinsic::isnan:
1350             // We directly implement these intrinsics
1351             break;
1352           case Intrinsic::readio: {
1353             // On PPC, memory operations are in-order.  Lower this intrinsic
1354             // into a volatile load.
1355             Instruction *Before = CI->getPrev();
1356             LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1357             CI->replaceAllUsesWith(LI);
1358             BB->getInstList().erase(CI);
1359             break;
1360           }
1361           case Intrinsic::writeio: {
1362             // On PPC, memory operations are in-order.  Lower this intrinsic
1363             // into a volatile store.
1364             Instruction *Before = CI->getPrev();
1365             StoreInst *LI = new StoreInst(CI->getOperand(1),
1366                                           CI->getOperand(2), true, CI);
1367             CI->replaceAllUsesWith(LI);
1368             BB->getInstList().erase(CI);
1369             break;
1370           }
1371           default:
1372             // All other intrinsic calls we must lower.
1373             Instruction *Before = CI->getPrev();
1374             TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1375             if (Before) {        // Move iterator to instruction after call
1376               I = Before; ++I;
1377             } else {
1378               I = BB->begin();
1379             }
1380           }
1381 }
1382
1383 void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1384   unsigned TmpReg1, TmpReg2, TmpReg3;
1385   switch (ID) {
1386   case Intrinsic::vastart:
1387     // Get the address of the first vararg value...
1388     TmpReg1 = getReg(CI);
1389     addFrameReference(BuildMI(BB, PPC32::ADDI, 2, TmpReg1), VarArgsFrameIndex);
1390     return;
1391
1392   case Intrinsic::vacopy:
1393     TmpReg1 = getReg(CI);
1394     TmpReg2 = getReg(CI.getOperand(1));
1395     BuildMI(BB, PPC32::OR, 2, TmpReg1).addReg(TmpReg2).addReg(TmpReg2);
1396     return;
1397   case Intrinsic::vaend: return;
1398
1399   case Intrinsic::returnaddress:
1400   case Intrinsic::frameaddress:
1401     TmpReg1 = getReg(CI);
1402     if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1403       if (ID == Intrinsic::returnaddress) {
1404         // Just load the return address
1405         addFrameReference(BuildMI(BB, PPC32::LWZ, 2, TmpReg1),
1406                           ReturnAddressIndex);
1407       } else {
1408         addFrameReference(BuildMI(BB, PPC32::ADDI, 2, TmpReg1),
1409                           ReturnAddressIndex, -4, false);
1410       }
1411     } else {
1412       // Values other than zero are not implemented yet.
1413       BuildMI(BB, PPC32::ADDI, 2, TmpReg1).addReg(PPC32::R0).addImm(0);
1414     }
1415     return;
1416
1417   case Intrinsic::isnan:
1418     // If this is only used by 'isunordered' style comparisons, don't emit it.
1419     if (isOnlyUsedByUnorderedComparisons(&CI)) return;
1420     TmpReg1 = getReg(CI.getOperand(1));
1421     emitUCOM(BB, BB->end(), TmpReg1, TmpReg1);
1422         TmpReg2 = makeAnotherReg(Type::IntTy);
1423         BuildMI(BB, PPC32::MFCR, TmpReg2);
1424     TmpReg3 = getReg(CI);
1425     BuildMI(BB, PPC32::RLWINM, 4, TmpReg3).addReg(TmpReg2).addImm(4).addImm(31).addImm(31);
1426     return;
1427
1428   default: assert(0 && "Error: unknown intrinsics should have been lowered!");
1429   }
1430 }
1431
1432 /// visitSimpleBinary - Implement simple binary operators for integral types...
1433 /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1434 /// Xor.
1435 ///
1436 void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1437   unsigned DestReg = getReg(B);
1438   MachineBasicBlock::iterator MI = BB->end();
1439   Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1440   unsigned Class = getClassB(B.getType());
1441
1442   emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
1443 }
1444
1445 /// emitBinaryFPOperation - This method handles emission of floating point
1446 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
1447 void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
1448                                  MachineBasicBlock::iterator IP,
1449                                  Value *Op0, Value *Op1,
1450                                  unsigned OperatorClass, unsigned DestReg) {
1451
1452   // Special case: op Reg, <const fp>
1453   if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1454       // Create a constant pool entry for this constant.
1455       MachineConstantPool *CP = F->getConstantPool();
1456       unsigned CPI = CP->getConstantPoolIndex(Op1C);
1457       const Type *Ty = Op1->getType();
1458
1459       static const unsigned OpcodeTab[][4] = {
1460         { PPC32::FADDS, PPC32::FSUBS, PPC32::FMULS, PPC32::FDIVS },   // Float
1461         { PPC32::FADD, PPC32::FSUB, PPC32::FMUL, PPC32::FDIV },   // Double
1462       };
1463
1464       assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1465           unsigned TempReg = makeAnotherReg(Ty);
1466       unsigned LoadOpcode = Ty == Type::FloatTy ? PPC32::LFS : PPC32::LFD;
1467       addConstantPoolReference(BuildMI(*BB, IP, LoadOpcode, 2, TempReg), CPI);
1468
1469       unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1470       unsigned Op0r = getReg(Op0, BB, IP);
1471           BuildMI(*BB, IP, Opcode, DestReg).addReg(Op0r).addReg(TempReg);
1472       return;
1473     }
1474   
1475   // Special case: R1 = op <const fp>, R2
1476   if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1477     if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
1478       // -0.0 - X === -X
1479       unsigned op1Reg = getReg(Op1, BB, IP);
1480       BuildMI(*BB, IP, PPC32::FNEG, 1, DestReg).addReg(op1Reg);
1481       return;
1482     } else {
1483       // R1 = op CST, R2  -->  R1 = opr R2, CST
1484
1485       // Create a constant pool entry for this constant.
1486       MachineConstantPool *CP = F->getConstantPool();
1487       unsigned CPI = CP->getConstantPoolIndex(CFP);
1488       const Type *Ty = CFP->getType();
1489
1490       static const unsigned OpcodeTab[][4] = {
1491         { PPC32::FADDS, PPC32::FSUBS, PPC32::FMULS, PPC32::FDIVS },   // Float
1492         { PPC32::FADD, PPC32::FSUB, PPC32::FMUL, PPC32::FDIV },   // Double
1493       };
1494
1495       assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1496           unsigned TempReg = makeAnotherReg(Ty);
1497       unsigned LoadOpcode = Ty == Type::FloatTy ? PPC32::LFS : PPC32::LFD;
1498       addConstantPoolReference(BuildMI(*BB, IP, LoadOpcode, 2, TempReg), CPI);
1499
1500       unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1501       unsigned Op1r = getReg(Op1, BB, IP);
1502           BuildMI(*BB, IP, Opcode, DestReg).addReg(TempReg).addReg(Op1r);
1503       return;
1504     }
1505
1506   // General case.
1507   static const unsigned OpcodeTab[4] = {
1508     PPC32::FADD, PPC32::FSUB, PPC32::FMUL, PPC32::FDIV
1509   };
1510
1511   unsigned Opcode = OpcodeTab[OperatorClass];
1512   unsigned Op0r = getReg(Op0, BB, IP);
1513   unsigned Op1r = getReg(Op1, BB, IP);
1514   BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1515 }
1516
1517 /// emitSimpleBinaryOperation - Implement simple binary operators for integral
1518 /// types...  OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1519 /// Or, 4 for Xor.
1520 ///
1521 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1522 /// and constant expression support.
1523 ///
1524 void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
1525                                      MachineBasicBlock::iterator IP,
1526                                      Value *Op0, Value *Op1,
1527                                      unsigned OperatorClass, unsigned DestReg) {
1528   unsigned Class = getClassB(Op0->getType());
1529
1530     // Arithmetic and Bitwise operators
1531     static const unsigned OpcodeTab[5] = {
1532           PPC32::ADD, PPC32::SUB, PPC32::AND, PPC32::OR, PPC32::XOR
1533         };
1534     // Otherwise, code generate the full operation with a constant.
1535     static const unsigned BottomTab[] = {
1536       PPC32::ADDC, PPC32::SUBC, PPC32::AND, PPC32::OR, PPC32::XOR
1537     };
1538     static const unsigned TopTab[] = {
1539       PPC32::ADDE, PPC32::SUBFE, PPC32::AND, PPC32::OR, PPC32::XOR
1540     };
1541   
1542   if (Class == cFP) {
1543     assert(OperatorClass < 2 && "No logical ops for FP!");
1544     emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
1545     return;
1546   }
1547
1548   if (Op0->getType() == Type::BoolTy) {
1549     if (OperatorClass == 3)
1550       // If this is an or of two isnan's, emit an FP comparison directly instead
1551       // of or'ing two isnan's together.
1552       if (Value *LHS = dyncastIsNan(Op0))
1553         if (Value *RHS = dyncastIsNan(Op1)) {
1554           unsigned Op0Reg = getReg(RHS, MBB, IP), Op1Reg = getReg(LHS, MBB, IP);
1555                   unsigned TmpReg = makeAnotherReg(Type::IntTy);
1556           emitUCOM(MBB, IP, Op0Reg, Op1Reg);
1557                   BuildMI(*MBB, IP, PPC32::MFCR, TmpReg);
1558                   BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(TmpReg).addImm(4).addImm(31).addImm(31);
1559           return;
1560         }
1561   }
1562
1563   // sub 0, X -> neg X
1564   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
1565     if (OperatorClass == 1 && CI->isNullValue()) {
1566       unsigned op1Reg = getReg(Op1, MBB, IP);
1567       BuildMI(*MBB, IP, PPC32::NEG, 1, DestReg).addReg(op1Reg);
1568       
1569       if (Class == cLong) {
1570                 unsigned zeroes = makeAnotherReg(Type::IntTy);
1571                 unsigned overflow = makeAnotherReg(Type::IntTy);
1572         unsigned T = makeAnotherReg(Type::IntTy);
1573                 BuildMI(*MBB, IP, PPC32::CNTLZW, 1, zeroes).addReg(op1Reg);
1574                 BuildMI(*MBB, IP, PPC32::RLWINM, 4, overflow).addReg(zeroes).addImm(27).addImm(5).addImm(31);
1575                 BuildMI(*MBB, IP, PPC32::ADD, 2, T).addReg(op1Reg+1).addReg(overflow);
1576                 BuildMI(*MBB, IP, PPC32::NEG, 1, DestReg+1).addReg(T);
1577       }
1578       return;
1579     }
1580
1581   // Special case: op Reg, <const int>
1582   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1583     unsigned Op0r = getReg(Op0, MBB, IP);
1584
1585     // xor X, -1 -> not X
1586     if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1587       BuildMI(*MBB, IP, PPC32::NOR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1588       if (Class == cLong)  // Invert the top part too
1589         BuildMI(*MBB, IP, PPC32::NOR, 2, DestReg+1).addReg(Op0r+1).addReg(Op0r+1);
1590       return;
1591     }
1592
1593     unsigned Opcode = OpcodeTab[OperatorClass];
1594     unsigned Op1r = getReg(Op1, MBB, IP);
1595
1596     if (Class != cLong) {
1597       BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1598       return;
1599     }
1600     
1601     // If the constant is zero in the low 32-bits, just copy the low part
1602     // across and apply the normal 32-bit operation to the high parts.  There
1603     // will be no carry or borrow into the top.
1604     if (cast<ConstantInt>(Op1C)->getRawValue() == 0) {
1605       if (OperatorClass != 2) // All but and...
1606         BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1607       else
1608         BuildMI(*MBB, IP, PPC32::ADDI, 2, DestReg).addReg(PPC32::R0).addImm(0);
1609           BuildMI(*MBB, IP, Opcode, 2, DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
1610       return;
1611     }
1612     
1613     // If this is a long value and the high or low bits have a special
1614     // property, emit some special cases.
1615     unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
1616     
1617     // If this is a logical operation and the top 32-bits are zero, just
1618     // operate on the lower 32.
1619     if (Op1h == 0 && OperatorClass > 1) {
1620       BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1621       if (OperatorClass != 2)  // All but and
1622         BuildMI(*MBB, IP, PPC32::OR, 2, DestReg+1).addReg(Op0r+1).addReg(Op0r+1);
1623       else
1624         BuildMI(*MBB, IP, PPC32::ADDI, 2, DestReg+1).addReg(PPC32::R0).addImm(0);
1625       return;
1626     }
1627     
1628     // TODO: We could handle lots of other special cases here, such as AND'ing
1629     // with 0xFFFFFFFF00000000 -> noop, etc.
1630     
1631     BuildMI(*MBB, IP, BottomTab[OperatorClass], 2, DestReg).addReg(Op0r).addImm(Op1r);
1632     BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1).addReg(Op0r+1).addImm(Op1r+1);
1633     return;
1634   }
1635
1636   unsigned Op0r = getReg(Op0, MBB, IP);
1637   unsigned Op1r = getReg(Op1, MBB, IP);
1638
1639   if (Class != cLong) {
1640         unsigned Opcode = OpcodeTab[OperatorClass];
1641         BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1642   } else {
1643     BuildMI(*MBB, IP, BottomTab[OperatorClass], 2, DestReg).addReg(Op0r).addImm(Op1r);
1644     BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1).addReg(Op0r+1).addImm(Op1r+1);
1645   }
1646   return;
1647 }
1648
1649 /// doMultiply - Emit appropriate instructions to multiply together the
1650 /// registers op0Reg and op1Reg, and put the result in DestReg.  The type of the
1651 /// result should be given as DestTy.
1652 ///
1653 void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
1654                       unsigned DestReg, const Type *DestTy,
1655                       unsigned op0Reg, unsigned op1Reg) {
1656   unsigned Class = getClass(DestTy);
1657   switch (Class) {
1658   case cLong:
1659     BuildMI(*MBB, MBBI, PPC32::MULHW, 2, DestReg+1).addReg(op0Reg+1).addReg(op1Reg+1);
1660   case cInt:
1661   case cShort:
1662   case cByte:
1663     BuildMI(*MBB, MBBI, PPC32::MULLW, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
1664     return;
1665   default:
1666         assert(0 && "doMultiply cannot operate on unknown type!");
1667   }
1668 }
1669
1670 // ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N.  It
1671 // returns zero when the input is not exactly a power of two.
1672 static unsigned ExactLog2(unsigned Val) {
1673   if (Val == 0 || (Val & (Val-1))) return 0;
1674   unsigned Count = 0;
1675   while (Val != 1) {
1676     Val >>= 1;
1677     ++Count;
1678   }
1679   return Count+1;
1680 }
1681
1682
1683 /// doMultiplyConst - This function is specialized to efficiently codegen an 8,
1684 /// 16, or 32-bit integer multiply by a constant.
1685 void ISel::doMultiplyConst(MachineBasicBlock *MBB,
1686                            MachineBasicBlock::iterator IP,
1687                            unsigned DestReg, const Type *DestTy,
1688                            unsigned op0Reg, unsigned ConstRHS) {
1689   unsigned Class = getClass(DestTy);
1690   // Handle special cases here.
1691   switch (ConstRHS) {
1692   case 0:
1693     BuildMI(*MBB, IP, PPC32::ADDI, 2, DestReg).addReg(PPC32::R0).addImm(0);
1694     return;
1695   case 1:
1696     BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(op0Reg).addReg(op0Reg);
1697     return;
1698   case 2:
1699     BuildMI(*MBB, IP, PPC32::ADD, 2,DestReg).addReg(op0Reg).addReg(op0Reg);
1700     return;
1701   }
1702
1703   // If the element size is exactly a power of 2, use a shift to get it.
1704   if (unsigned Shift = ExactLog2(ConstRHS)) {
1705     switch (Class) {
1706     default: assert(0 && "Unknown class for this function!");
1707     case cByte:
1708     case cShort:
1709     case cInt:
1710       BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(op0Reg).addImm(Shift-1).addImm(0).addImm(31-Shift-1);
1711       return;
1712     }
1713   }
1714   
1715   // Most general case, emit a normal multiply...
1716   unsigned TmpReg1 = makeAnotherReg(Type::IntTy);
1717   unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
1718   BuildMI(*MBB, IP, PPC32::ADDIS, 2, TmpReg1).addReg(PPC32::R0).addImm(ConstRHS >> 16);
1719   BuildMI(*MBB, IP, PPC32::ORI, 2, TmpReg2).addReg(TmpReg1).addImm(ConstRHS);
1720   
1721   // Emit a MUL to multiply the register holding the index by
1722   // elementSize, putting the result in OffsetReg.
1723   doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg2);
1724 }
1725
1726 void ISel::visitMul(BinaryOperator &I) {
1727   unsigned ResultReg = getReg(I);
1728
1729   Value *Op0 = I.getOperand(0);
1730   Value *Op1 = I.getOperand(1);
1731
1732   MachineBasicBlock::iterator IP = BB->end();
1733   emitMultiply(BB, IP, Op0, Op1, ResultReg);
1734 }
1735
1736 void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
1737                         Value *Op0, Value *Op1, unsigned DestReg) {
1738   MachineBasicBlock &BB = *MBB;
1739   TypeClass Class = getClass(Op0->getType());
1740
1741   // Simple scalar multiply?
1742   unsigned Op0Reg  = getReg(Op0, &BB, IP);
1743   switch (Class) {
1744   case cByte:
1745   case cShort:
1746   case cInt:
1747     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1748       unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
1749       doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
1750     } else {
1751       unsigned Op1Reg  = getReg(Op1, &BB, IP);
1752       doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
1753     }
1754     return;
1755   case cFP:
1756     emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
1757     return;
1758   case cLong:
1759     break;
1760   }
1761
1762   // Long value.  We have to do things the hard way...
1763   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1764     unsigned CLow = CI->getRawValue();
1765     unsigned CHi  = CI->getRawValue() >> 32;
1766     
1767     if (CLow == 0) {
1768       // If the low part of the constant is all zeros, things are simple.
1769       BuildMI(BB, IP, PPC32::ADDI, 2, DestReg).addReg(PPC32::R0).addImm(0);
1770       doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
1771       return;
1772     }
1773     
1774     // Multiply the two low parts
1775     unsigned OverflowReg = 0;
1776     if (CLow == 1) {
1777       BuildMI(BB, IP, PPC32::OR, 2, DestReg).addReg(Op0Reg).addReg(Op0Reg);
1778     } else {
1779           unsigned TmpRegL = makeAnotherReg(Type::UIntTy);
1780       unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
1781       OverflowReg = makeAnotherReg(Type::UIntTy);
1782           BuildMI(BB, IP, PPC32::ADDIS, 2, TmpRegL).addReg(PPC32::R0).addImm(CLow >> 16);
1783           BuildMI(BB, IP, PPC32::ORI, 2, Op1RegL).addReg(TmpRegL).addImm(CLow);
1784           BuildMI(BB, IP, PPC32::MULLW, 2, DestReg).addReg(Op0Reg).addReg(Op1RegL);
1785           BuildMI(BB, IP, PPC32::MULHW, 2, OverflowReg).addReg(Op0Reg).addReg(Op1RegL);
1786     }
1787     
1788     unsigned AHBLReg = makeAnotherReg(Type::UIntTy);
1789     doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
1790     
1791     unsigned AHBLplusOverflowReg;
1792     if (OverflowReg) {
1793       AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1794       BuildMI(BB, IP, PPC32::ADD, 2,                // AH*BL+(AL*BL >> 32)
1795               AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
1796     } else {
1797       AHBLplusOverflowReg = AHBLReg;
1798     }
1799     
1800     if (CHi == 0) {
1801       BuildMI(BB, IP, PPC32::OR, 2, DestReg+1).addReg(AHBLplusOverflowReg).addReg(AHBLplusOverflowReg);
1802     } else {
1803       unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
1804       doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
1805       
1806       BuildMI(BB, IP, PPC32::ADD, 2,      // AL*BH + AH*BL + (AL*BL >> 32)
1807               DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
1808     }
1809     return;
1810   }
1811
1812   // General 64x64 multiply
1813
1814   unsigned Op1Reg  = getReg(Op1, &BB, IP);
1815   
1816   // Multiply the two low parts... capturing carry into EDX
1817   BuildMI(BB, IP, PPC32::MULLW, 2, DestReg).addReg(Op0Reg).addReg(Op1Reg);  // AL*BL
1818   
1819   unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
1820   BuildMI(BB, IP, PPC32::MULHW, 2, OverflowReg).addReg(Op0Reg).addReg(Op1Reg); // AL*BL >> 32
1821   
1822   unsigned AHBLReg = makeAnotherReg(Type::UIntTy);   // AH*BL
1823   BuildMI(BB, IP, PPC32::MULLW, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
1824   
1825   unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1826   BuildMI(BB, IP, PPC32::ADD, 2,                // AH*BL+(AL*BL >> 32)
1827           AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
1828   
1829   unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
1830   BuildMI(BB, IP, PPC32::MULLW, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
1831   
1832   BuildMI(BB, IP, PPC32::ADD, 2,      // AL*BH + AH*BL + (AL*BL >> 32)
1833           DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
1834 }
1835
1836
1837 /// visitDivRem - Handle division and remainder instructions... these
1838 /// instruction both require the same instructions to be generated, they just
1839 /// select the result from a different register.  Note that both of these
1840 /// instructions work differently for signed and unsigned operands.
1841 ///
1842 void ISel::visitDivRem(BinaryOperator &I) {
1843   unsigned ResultReg = getReg(I);
1844   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1845
1846   MachineBasicBlock::iterator IP = BB->end();
1847   emitDivRemOperation(BB, IP, Op0, Op1, I.getOpcode() == Instruction::Div, ResultReg);
1848 }
1849
1850 void ISel::emitDivRemOperation(MachineBasicBlock *BB,
1851                                MachineBasicBlock::iterator IP,
1852                                Value *Op0, Value *Op1, bool isDiv,
1853                                unsigned ResultReg) {
1854   const Type *Ty = Op0->getType();
1855   unsigned Class = getClass(Ty);
1856   switch (Class) {
1857   case cFP:              // Floating point divide
1858     if (isDiv) {
1859       emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
1860       return;
1861     } else {               // Floating point remainder...
1862       unsigned Op0Reg = getReg(Op0, BB, IP);
1863       unsigned Op1Reg = getReg(Op1, BB, IP);
1864       MachineInstr *TheCall =
1865         BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol("fmod", true);
1866       std::vector<ValueRecord> Args;
1867       Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1868       Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
1869       doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1870     }
1871     return;
1872   case cLong: {
1873     static const char *FnName[] =
1874       { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1875     unsigned Op0Reg = getReg(Op0, BB, IP);
1876     unsigned Op1Reg = getReg(Op1, BB, IP);
1877     unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
1878     MachineInstr *TheCall =
1879       BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol(FnName[NameIdx], true);
1880
1881     std::vector<ValueRecord> Args;
1882     Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
1883     Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
1884     doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1885     return;
1886   }
1887   case cByte: case cShort: case cInt:
1888     break;          // Small integrals, handled below...
1889   default: assert(0 && "Unknown class!");
1890   }
1891
1892   // Special case signed division by power of 2.
1893   if (isDiv)
1894     if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) {
1895       assert(Class != cLong && "This doesn't handle 64-bit divides!");
1896       int V = CI->getValue();
1897
1898       if (V == 1) {       // X /s 1 => X
1899         unsigned Op0Reg = getReg(Op0, BB, IP);
1900         BuildMI(*BB, IP, PPC32::OR, 2, ResultReg).addReg(Op0Reg).addReg(Op0Reg);
1901         return;
1902       }
1903
1904       if (V == -1) {      // X /s -1 => -X
1905         unsigned Op0Reg = getReg(Op0, BB, IP);
1906         BuildMI(*BB, IP, PPC32::NEG, 1, ResultReg).addReg(Op0Reg);
1907         return;
1908       }
1909
1910       bool isNeg = false;
1911       if (V < 0) {         // Not a positive power of 2?
1912         V = -V;
1913         isNeg = true;      // Maybe it's a negative power of 2.
1914       }
1915       if (unsigned Log = ExactLog2(V)) {
1916         --Log;
1917         unsigned Op0Reg = getReg(Op0, BB, IP);
1918         unsigned TmpReg = makeAnotherReg(Op0->getType());
1919         if (Log != 1) 
1920           BuildMI(*BB, IP, PPC32::SRAWI, 2, TmpReg).addReg(Op0Reg).addImm(Log-1);
1921         else
1922           BuildMI(*BB, IP, PPC32::OR, 2, TmpReg).addReg(Op0Reg).addReg(Op0Reg);
1923
1924         unsigned TmpReg2 = makeAnotherReg(Op0->getType());
1925         BuildMI(*BB, IP, PPC32::RLWINM, 4, TmpReg2).addReg(TmpReg).addImm(Log).addImm(32-Log).addImm(31);
1926
1927         unsigned TmpReg3 = makeAnotherReg(Op0->getType());
1928         BuildMI(*BB, IP, PPC32::ADD, 2, TmpReg3).addReg(Op0Reg).addReg(TmpReg2);
1929
1930         unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
1931         BuildMI(*BB, IP, PPC32::SRAWI, 2, TmpReg4).addReg(Op0Reg).addImm(Log);
1932
1933         if (isNeg)
1934           BuildMI(*BB, IP, PPC32::NEG, 1, ResultReg).addReg(TmpReg4);
1935         return;
1936       }
1937     }
1938
1939   unsigned Op0Reg = getReg(Op0, BB, IP);
1940   unsigned Op1Reg = getReg(Op1, BB, IP);
1941
1942   if (isDiv) {
1943         if (Ty->isSigned()) {
1944                 BuildMI(*BB, IP, PPC32::DIVW, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1945         } else {
1946                 BuildMI(*BB, IP, PPC32::DIVWU, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1947         }
1948   } else { // Remainder
1949         unsigned TmpReg1 = makeAnotherReg(Op0->getType());
1950         unsigned TmpReg2 = makeAnotherReg(Op0->getType());
1951         
1952         if (Ty->isSigned()) {
1953                 BuildMI(*BB, IP, PPC32::DIVW, 2, TmpReg1).addReg(Op0Reg).addReg(Op1Reg);
1954         } else {
1955                 BuildMI(*BB, IP, PPC32::DIVWU, 2, TmpReg1).addReg(Op0Reg).addReg(Op1Reg);
1956         }
1957         BuildMI(*BB, IP, PPC32::MULLW, 2, TmpReg2).addReg(TmpReg1).addReg(Op1Reg);
1958         BuildMI(*BB, IP, PPC32::SUBF, 2, ResultReg).addReg(TmpReg2).addReg(Op0Reg);
1959   }
1960 }
1961
1962
1963 /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1964 /// for constant immediate shift values, and for constant immediate
1965 /// shift values equal to 1. Even the general case is sort of special,
1966 /// because the shift amount has to be in CL, not just any old register.
1967 ///
1968 void ISel::visitShiftInst(ShiftInst &I) {
1969   MachineBasicBlock::iterator IP = BB->end ();
1970   emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
1971                       I.getOpcode () == Instruction::Shl, I.getType (),
1972                       getReg (I));
1973 }
1974
1975 /// emitShiftOperation - Common code shared between visitShiftInst and
1976 /// constant expression support.
1977 void ISel::emitShiftOperation(MachineBasicBlock *MBB,
1978                               MachineBasicBlock::iterator IP,
1979                               Value *Op, Value *ShiftAmount, bool isLeftShift,
1980                               const Type *ResultTy, unsigned DestReg) {
1981   unsigned SrcReg = getReg (Op, MBB, IP);
1982   bool isSigned = ResultTy->isSigned ();
1983   unsigned Class = getClass (ResultTy);
1984   
1985   // Longs, as usual, are handled specially...
1986   if (Class == cLong) {
1987     // If we have a constant shift, we can generate much more efficient code
1988     // than otherwise...
1989     //
1990     if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
1991       unsigned Amount = CUI->getValue();
1992       if (Amount < 32) {
1993         if (isLeftShift) {
1994                   // FIXME: RLWIMI is a use-and-def of DestReg+1, but that violates SSA
1995           BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg+1).addReg(SrcReg+1).addImm(Amount).addImm(0).addImm(31-Amount);
1996           BuildMI(*MBB, IP, PPC32::RLWIMI, 5).addReg(DestReg+1).addReg(SrcReg).addImm(Amount).addImm(32-Amount).addImm(31);
1997           BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addImm(Amount).addImm(0).addImm(31-Amount);
1998         } else {
1999                   // FIXME: RLWIMI is a use-and-def of DestReg, but that violates SSA
2000           BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addImm(32-Amount).addImm(Amount).addImm(31);
2001           BuildMI(*MBB, IP, PPC32::RLWIMI, 5).addReg(DestReg).addReg(SrcReg+1).addImm(32-Amount).addImm(0).addImm(Amount-1);
2002           BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg+1).addReg(SrcReg+1).addImm(32-Amount).addImm(Amount).addImm(31);
2003         }
2004       } else {                 // Shifting more than 32 bits
2005         Amount -= 32;
2006         if (isLeftShift) {
2007           if (Amount != 0) {
2008                         BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg+1).addReg(SrcReg).addImm(Amount).addImm(0).addImm(31-Amount);
2009           } else {
2010             BuildMI(*MBB, IP, PPC32::OR, 2, DestReg+1).addReg(SrcReg).addReg(SrcReg);
2011           }
2012           BuildMI(*MBB, IP, PPC32::ADDI, 2, DestReg).addReg(PPC32::R0).addImm(0);
2013         } else {
2014           if (Amount != 0) {
2015                         if (isSigned)
2016                                 BuildMI(*MBB, IP, PPC32::SRAWI, 2, DestReg).addReg(SrcReg+1).addImm(Amount);
2017                         else
2018                                 BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg+1).addImm(32-Amount).addImm(Amount).addImm(31);
2019           } else {
2020             BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(SrcReg+1).addReg(SrcReg+1);
2021           }
2022           BuildMI(*MBB, IP, PPC32::ADDI, 2, DestReg+1).addReg(PPC32::R0).addImm(0);
2023         }
2024       }
2025     } else {
2026       unsigned TmpReg1 = makeAnotherReg(Type::IntTy);
2027       unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2028           unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2029           unsigned TmpReg4 = makeAnotherReg(Type::IntTy);
2030           unsigned TmpReg5 = makeAnotherReg(Type::IntTy);
2031           unsigned TmpReg6 = makeAnotherReg(Type::IntTy);
2032           unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
2033           
2034       if (isLeftShift) {
2035                 BuildMI(*MBB, IP, PPC32::SUBFIC, 2, TmpReg1).addReg(ShiftAmountReg).addImm(32);
2036                 BuildMI(*MBB, IP, PPC32::SLW, 2, TmpReg2).addReg(SrcReg+1).addReg(ShiftAmountReg);
2037                 BuildMI(*MBB, IP, PPC32::SRW, 2, TmpReg3).addReg(SrcReg).addReg(TmpReg1);
2038                 BuildMI(*MBB, IP, PPC32::OR, 2, TmpReg4).addReg(TmpReg2).addReg(TmpReg3);
2039                 BuildMI(*MBB, IP, PPC32::ADDI, 2, TmpReg5).addReg(ShiftAmountReg).addImm(-32);
2040                 BuildMI(*MBB, IP, PPC32::SLW, 2, TmpReg6).addReg(SrcReg).addReg(TmpReg5);
2041                 BuildMI(*MBB, IP, PPC32::OR, 2, DestReg+1).addReg(TmpReg4).addReg(TmpReg6);
2042                 BuildMI(*MBB, IP, PPC32::SLW, 2, DestReg).addReg(SrcReg).addReg(ShiftAmountReg);
2043           } else {
2044                 if (isSigned) {
2045                         // FIXME: Unimplmented
2046                         // Page C-3 of the PowerPC 32bit Programming Environments Manual
2047                 } else {
2048                         BuildMI(*MBB, IP, PPC32::SUBFIC, 2, TmpReg1).addReg(ShiftAmountReg).addImm(32);
2049                         BuildMI(*MBB, IP, PPC32::SRW, 2, TmpReg2).addReg(SrcReg).addReg(ShiftAmountReg);
2050                         BuildMI(*MBB, IP, PPC32::SLW, 2, TmpReg3).addReg(SrcReg+1).addReg(TmpReg1);
2051                         BuildMI(*MBB, IP, PPC32::OR, 2, TmpReg4).addReg(TmpReg2).addReg(TmpReg3);
2052                         BuildMI(*MBB, IP, PPC32::ADDI, 2, TmpReg5).addReg(ShiftAmountReg).addImm(-32);
2053                         BuildMI(*MBB, IP, PPC32::SRW, 2, TmpReg6).addReg(SrcReg+1).addReg(TmpReg5);
2054                         BuildMI(*MBB, IP, PPC32::OR, 2, DestReg).addReg(TmpReg4).addReg(TmpReg6);
2055                         BuildMI(*MBB, IP, PPC32::SRW, 2, DestReg+1).addReg(SrcReg+1).addReg(ShiftAmountReg);
2056                 }
2057           }
2058     }
2059     return;
2060   }
2061
2062   if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
2063     // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2064     assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
2065     unsigned Amount = CUI->getValue();
2066
2067         if (isLeftShift) {
2068                 BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addImm(Amount).addImm(0).addImm(31-Amount);
2069         } else {
2070                 if (isSigned) {
2071                         BuildMI(*MBB, IP, PPC32::SRAWI, 2, DestReg).addReg(SrcReg).addImm(Amount);
2072                 } else {
2073                         BuildMI(*MBB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addImm(32-Amount).addImm(Amount).addImm(31);
2074                 }
2075         }
2076   } else {                  // The shift amount is non-constant.
2077     unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
2078
2079         if (isLeftShift) {
2080                 BuildMI(*MBB, IP, PPC32::SLW, 2, DestReg).addReg(SrcReg).addReg(ShiftAmountReg);
2081         } else {
2082                 BuildMI(*MBB, IP, isSigned ? PPC32::SRAW : PPC32::SRW, 2, DestReg).addReg(SrcReg).addReg(ShiftAmountReg);
2083         }
2084   }
2085 }
2086
2087
2088 /// visitLoadInst - Implement LLVM load instructions
2089 ///
2090 void ISel::visitLoadInst(LoadInst &I) {
2091   static const unsigned Opcodes[] = { PPC32::LBZ, PPC32::LHZ, PPC32::LWZ, PPC32::LFS };
2092   unsigned Class = getClassB(I.getType());
2093   unsigned Opcode = Opcodes[Class];
2094   if (I.getType() == Type::DoubleTy) Opcode = PPC32::LFD;
2095
2096   unsigned DestReg = getReg(I);
2097
2098   if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2099         unsigned FI = getFixedSizedAllocaFI(AI);
2100     if (Class == cLong) {
2101                 addFrameReference(BuildMI(BB, PPC32::LWZ, 2, DestReg), FI);
2102                 addFrameReference(BuildMI(BB, PPC32::LWZ, 2, DestReg+1), FI, 4);
2103     } else {
2104                 addFrameReference(BuildMI(BB, Opcode, 2, DestReg), FI);
2105         }
2106   } else {
2107         unsigned SrcAddrReg = getReg(I.getOperand(0));
2108     
2109     if (Class == cLong) {
2110       BuildMI(BB, PPC32::LWZ, 2, DestReg).addImm(0).addReg(SrcAddrReg);
2111       BuildMI(BB, PPC32::LWZ, 2, DestReg+1).addImm(4).addReg(SrcAddrReg);
2112     } else {
2113       BuildMI(BB, Opcode, 2, DestReg).addImm(0).addReg(SrcAddrReg);
2114     }
2115   }
2116 }
2117
2118 /// visitStoreInst - Implement LLVM store instructions
2119 ///
2120 void ISel::visitStoreInst(StoreInst &I) {
2121   unsigned ValReg      = getReg(I.getOperand(0));
2122   unsigned AddressReg  = getReg(I.getOperand(1));
2123  
2124   const Type *ValTy = I.getOperand(0)->getType();
2125   unsigned Class = getClassB(ValTy);
2126
2127   if (Class == cLong) {
2128         BuildMI(BB, PPC32::STW, 3).addReg(ValReg).addImm(0).addReg(AddressReg);
2129         BuildMI(BB, PPC32::STW, 3).addReg(ValReg+1).addImm(4).addReg(AddressReg);
2130     return;
2131   }
2132
2133   static const unsigned Opcodes[] = {
2134     PPC32::STB, PPC32::STH, PPC32::STW, PPC32::STFS
2135   };
2136   unsigned Opcode = Opcodes[Class];
2137   if (ValTy == Type::DoubleTy) Opcode = PPC32::STFD;
2138   BuildMI(BB, Opcode, 3).addReg(ValReg).addImm(0).addReg(AddressReg);
2139 }
2140
2141
2142 /// visitCastInst - Here we have various kinds of copying with or without sign
2143 /// extension going on.
2144 ///
2145 void ISel::visitCastInst(CastInst &CI) {
2146   Value *Op = CI.getOperand(0);
2147
2148   unsigned SrcClass = getClassB(Op->getType());
2149   unsigned DestClass = getClassB(CI.getType());
2150   // Noop casts are not emitted: getReg will return the source operand as the
2151   // register to use for any uses of the noop cast.
2152   if (DestClass == SrcClass)
2153     return;
2154
2155   // If this is a cast from a 32-bit integer to a Long type, and the only uses
2156   // of the case are GEP instructions, then the cast does not need to be
2157   // generated explicitly, it will be folded into the GEP.
2158   if (DestClass == cLong && SrcClass == cInt) {
2159     bool AllUsesAreGEPs = true;
2160     for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2161       if (!isa<GetElementPtrInst>(*I)) {
2162         AllUsesAreGEPs = false;
2163         break;
2164       }        
2165
2166     // No need to codegen this cast if all users are getelementptr instrs...
2167     if (AllUsesAreGEPs) return;
2168   }
2169
2170   unsigned DestReg = getReg(CI);
2171   MachineBasicBlock::iterator MI = BB->end();
2172   emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
2173 }
2174
2175 /// emitCastOperation - Common code shared between visitCastInst and constant
2176 /// expression cast support.
2177 ///
2178 void ISel::emitCastOperation(MachineBasicBlock *BB,
2179                              MachineBasicBlock::iterator IP,
2180                              Value *Src, const Type *DestTy,
2181                              unsigned DestReg) {
2182   const Type *SrcTy = Src->getType();
2183   unsigned SrcClass = getClassB(SrcTy);
2184   unsigned DestClass = getClassB(DestTy);
2185   unsigned SrcReg = getReg(Src, BB, IP);
2186
2187   // Implement casts to bool by using compare on the operand followed by set if
2188   // not zero on the result.
2189   if (DestTy == Type::BoolTy) {
2190     switch (SrcClass) {
2191     case cByte:
2192         case cShort:
2193     case cInt: {
2194       unsigned TmpReg = makeAnotherReg(Type::IntTy);
2195           BuildMI(*BB, IP, PPC32::ADDIC, 2, TmpReg).addReg(SrcReg).addImm(-1);
2196           BuildMI(*BB, IP, PPC32::SUBFE, 2, DestReg).addReg(TmpReg).addReg(SrcReg);
2197       break;
2198     }
2199     case cLong: {
2200       unsigned TmpReg = makeAnotherReg(Type::IntTy);
2201       unsigned SrcReg2 = makeAnotherReg(Type::IntTy);
2202       BuildMI(*BB, IP, PPC32::OR, 2, SrcReg2).addReg(SrcReg).addReg(SrcReg+1);
2203           BuildMI(*BB, IP, PPC32::ADDIC, 2, TmpReg).addReg(SrcReg2).addImm(-1);
2204           BuildMI(*BB, IP, PPC32::SUBFE, 2, DestReg).addReg(TmpReg).addReg(SrcReg2);
2205       break;
2206     }
2207     case cFP:
2208       // FIXME
2209           // Load -0.0
2210           // Compare
2211           // move to CR1
2212           // Negate -0.0
2213           // Compare
2214           // CROR
2215           // MFCR
2216           // Left-align
2217           // SRA ?
2218       break;
2219     }
2220     return;
2221   }
2222
2223   // Implement casts between values of the same type class (as determined by
2224   // getClass) by using a register-to-register move.
2225   if (SrcClass == DestClass) {
2226         if (SrcClass <= cInt) {
2227           BuildMI(*BB, IP, PPC32::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2228         } else if (SrcClass == cFP && SrcTy == DestTy) {
2229       BuildMI(*BB, IP, PPC32::FMR, 1, DestReg).addReg(SrcReg);
2230     } else if (SrcClass == cFP) {
2231       if (SrcTy == Type::FloatTy) {  // float -> double
2232         assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
2233         BuildMI(*BB, IP, PPC32::FMR, 1, DestReg).addReg(SrcReg);
2234       } else {                       // double -> float
2235         assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2236                "Unknown cFP member!");
2237                 BuildMI(*BB, IP, PPC32::FRSP, 1, DestReg).addReg(SrcReg);
2238       }
2239     } else if (SrcClass == cLong) {
2240           BuildMI(*BB, IP, PPC32::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2241           BuildMI(*BB, IP, PPC32::OR, 2, DestReg+1).addReg(SrcReg+1).addReg(SrcReg+1);
2242     } else {
2243       assert(0 && "Cannot handle this type of cast instruction!");
2244       abort();
2245     }
2246     return;
2247   }
2248
2249   // Handle cast of SMALLER int to LARGER int using a move with sign extension
2250   // or zero extension, depending on whether the source type was signed.
2251   if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2252       SrcClass < DestClass) {
2253     bool isLong = DestClass == cLong;
2254     if (isLong) DestClass = cInt;
2255
2256     bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
2257     if (SrcClass < cInt) {
2258       if (isUnsigned) {
2259         unsigned shift = (SrcClass == cByte) ? 24 : 16;
2260         BuildMI(*BB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addZImm(0).addImm(shift).addImm(31);
2261       } else {
2262         BuildMI(*BB, IP, (SrcClass == cByte) ? PPC32::EXTSB : PPC32::EXTSH, 1, DestReg).addReg(SrcReg);
2263           }
2264         } else {
2265           BuildMI(*BB, IP, PPC32::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2266         }
2267
2268     if (isLong) {  // Handle upper 32 bits as appropriate...
2269       if (isUnsigned)     // Zero out top bits...
2270         BuildMI(*BB, IP, PPC32::ADDI, 2, DestReg+1).addReg(PPC32::R0).addImm(0);
2271       else                // Sign extend bottom half...
2272         BuildMI(*BB, IP, PPC32::SRAWI, 2, DestReg+1).addReg(DestReg).addImm(31);
2273     }
2274     return;
2275   }
2276
2277   // Special case long -> int ...
2278   if (SrcClass == cLong && DestClass == cInt) {
2279     BuildMI(*BB, IP, PPC32::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2280     return;
2281   }
2282   
2283   // Handle cast of LARGER int to SMALLER int with a clear or sign extend
2284   if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2285       && SrcClass > DestClass) {
2286     bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
2287         if (isUnsigned) {
2288         unsigned shift = (SrcClass == cByte) ? 24 : 16;
2289         BuildMI(*BB, IP, PPC32::RLWINM, 4, DestReg).addReg(SrcReg).addZImm(0).addImm(shift).addImm(31);
2290         } else {
2291         BuildMI(*BB, IP, (SrcClass == cByte) ? PPC32::EXTSB : PPC32::EXTSH, 1, DestReg).addReg(SrcReg);
2292         }
2293     return;
2294   }
2295
2296   // Handle casts from integer to floating point now...
2297   if (DestClass == cFP) {
2298
2299         // Emit a library call for long to float conversion
2300         if (SrcClass == cLong) {
2301                 std::vector<ValueRecord> Args;
2302                 Args.push_back(ValueRecord(SrcReg, SrcTy));
2303                 MachineInstr *TheCall = BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol("__floatdidf", true);
2304                 doCall(ValueRecord(DestReg, DestTy), TheCall, Args);
2305                 return;
2306         }
2307
2308     unsigned TmpReg = makeAnotherReg(Type::IntTy);
2309     switch (SrcTy->getPrimitiveID()) {
2310     case Type::BoolTyID:
2311     case Type::SByteTyID:
2312       BuildMI(*BB, IP, PPC32::EXTSB, 1, TmpReg).addReg(SrcReg);
2313       break;
2314     case Type::UByteTyID:
2315           BuildMI(*BB, IP, PPC32::RLWINM, 4, TmpReg).addReg(SrcReg).addZImm(0).addImm(24).addImm(31);
2316       break;
2317     case Type::ShortTyID:
2318       BuildMI(*BB, IP, PPC32::EXTSB, 1, TmpReg).addReg(SrcReg);
2319       break;
2320     case Type::UShortTyID:
2321           BuildMI(*BB, IP, PPC32::RLWINM, 4, TmpReg).addReg(SrcReg).addZImm(0).addImm(16).addImm(31);
2322       break;
2323         case Type::IntTyID:
2324           BuildMI(*BB, IP, PPC32::OR, 2, TmpReg).addReg(SrcReg).addReg(SrcReg);
2325           break;
2326         case Type::UIntTyID:
2327           BuildMI(*BB, IP, PPC32::OR, 2, TmpReg).addReg(SrcReg).addReg(SrcReg);
2328           break;
2329     default:  // No promotion needed...
2330       break;
2331     }
2332     
2333     SrcReg = TmpReg;
2334         
2335     // Spill the integer to memory and reload it from there.
2336         // Also spill room for a special conversion constant
2337         int ConstantFrameIndex = 
2338       F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2339     int ValueFrameIdx =
2340       F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2341
2342         unsigned constantHi = makeAnotherReg(Type::IntTy);
2343         unsigned constantLo = makeAnotherReg(Type::IntTy);
2344         unsigned ConstF = makeAnotherReg(Type::DoubleTy);
2345         unsigned TempF = makeAnotherReg(Type::DoubleTy);
2346         
2347     if (!SrcTy->isSigned()) {
2348                 BuildMI(*BB, IP, PPC32::ADDIS, 2, constantHi).addReg(PPC32::R0).addImm(0x4330);
2349                 BuildMI(*BB, IP, PPC32::ADDI, 2, constantLo).addReg(PPC32::R0).addImm(0);
2350                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantHi), ConstantFrameIndex);
2351                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantLo), ConstantFrameIndex, 4);
2352                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantHi), ValueFrameIdx);
2353                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(SrcReg), ValueFrameIdx, 4);
2354                 addFrameReference(BuildMI(*BB, IP, PPC32::LFD, 2, ConstF), ConstantFrameIndex);
2355                 addFrameReference(BuildMI(*BB, IP, PPC32::LFD, 2, TempF), ValueFrameIdx);
2356                 BuildMI(*BB, IP, PPC32::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF);
2357         } else {
2358                 unsigned TempLo = makeAnotherReg(Type::IntTy);
2359                 BuildMI(*BB, IP, PPC32::ADDIS, 2, constantHi).addReg(PPC32::R0).addImm(0x4330);
2360                 BuildMI(*BB, IP, PPC32::ADDIS, 2, constantLo).addReg(PPC32::R0).addImm(0x8000);
2361                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantHi), ConstantFrameIndex);
2362                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantLo), ConstantFrameIndex, 4);
2363                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(constantHi), ValueFrameIdx);
2364                 BuildMI(*BB, IP, PPC32::XORIS, 2, TempLo).addReg(SrcReg).addImm(0x8000);
2365                 addFrameReference(BuildMI(*BB, IP, PPC32::STW, 3).addReg(TempLo), ValueFrameIdx, 4);
2366                 addFrameReference(BuildMI(*BB, IP, PPC32::LFD, 2, ConstF), ConstantFrameIndex);
2367                 addFrameReference(BuildMI(*BB, IP, PPC32::LFD, 2, TempF), ValueFrameIdx);
2368                 BuildMI(*BB, IP, PPC32::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF);
2369         }
2370     return;
2371   }
2372
2373   // Handle casts from floating point to integer now...
2374   if (SrcClass == cFP) {
2375
2376         // emit library call
2377         if (DestClass == cLong) {
2378                 std::vector<ValueRecord> Args;
2379                 Args.push_back(ValueRecord(SrcReg, SrcTy));
2380                 MachineInstr *TheCall = BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol("__fixdfdi", true);
2381                 doCall(ValueRecord(DestReg, DestTy), TheCall, Args);
2382                 return;
2383         }
2384
2385     int ValueFrameIdx =
2386       F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2387
2388         // load into 32 bit value, and then truncate as necessary
2389         // FIXME: This is wrong for unsigned dest types
2390         //if (DestTy->isSigned()) {
2391                 unsigned TempReg = makeAnotherReg(Type::DoubleTy);
2392                 BuildMI(*BB, IP, PPC32::FCTIWZ, 1, TempReg).addReg(SrcReg);
2393                 addFrameReference(BuildMI(*BB, IP, PPC32::STFD, 3).addReg(TempReg), ValueFrameIdx);
2394                 addFrameReference(BuildMI(*BB, IP, PPC32::LWZ, 2, DestReg), ValueFrameIdx+4);
2395         //} else {
2396         //}
2397         
2398         // FIXME: Truncate return value
2399     return;
2400   }
2401
2402   // Anything we haven't handled already, we can't (yet) handle at all.
2403   assert(0 && "Unhandled cast instruction!");
2404   abort();
2405 }
2406
2407 /// visitVANextInst - Implement the va_next instruction...
2408 ///
2409 void ISel::visitVANextInst(VANextInst &I) {
2410   unsigned VAList = getReg(I.getOperand(0));
2411   unsigned DestReg = getReg(I);
2412
2413   unsigned Size;
2414   switch (I.getArgType()->getPrimitiveID()) {
2415   default:
2416     std::cerr << I;
2417     assert(0 && "Error: bad type for va_next instruction!");
2418     return;
2419   case Type::PointerTyID:
2420   case Type::UIntTyID:
2421   case Type::IntTyID:
2422     Size = 4;
2423     break;
2424   case Type::ULongTyID:
2425   case Type::LongTyID:
2426   case Type::DoubleTyID:
2427     Size = 8;
2428     break;
2429   }
2430
2431   // Increment the VAList pointer...
2432   BuildMI(BB, PPC32::ADDI, 2, DestReg).addReg(VAList).addImm(Size);
2433 }
2434
2435 void ISel::visitVAArgInst(VAArgInst &I) {
2436   unsigned VAList = getReg(I.getOperand(0));
2437   unsigned DestReg = getReg(I);
2438
2439   switch (I.getType()->getPrimitiveID()) {
2440   default:
2441     std::cerr << I;
2442     assert(0 && "Error: bad type for va_next instruction!");
2443     return;
2444   case Type::PointerTyID:
2445   case Type::UIntTyID:
2446   case Type::IntTyID:
2447     BuildMI(BB, PPC32::LWZ, 2, DestReg).addImm(0).addReg(VAList);
2448     break;
2449   case Type::ULongTyID:
2450   case Type::LongTyID:
2451     BuildMI(BB, PPC32::LWZ, 2, DestReg).addImm(0).addReg(VAList);
2452     BuildMI(BB, PPC32::LWZ, 2, DestReg+1).addImm(4).addReg(VAList);
2453     break;
2454   case Type::DoubleTyID:
2455     BuildMI(BB, PPC32::LFD, 2, DestReg).addImm(0).addReg(VAList);
2456     break;
2457   }
2458 }
2459
2460 /// visitGetElementPtrInst - instruction-select GEP instructions
2461 ///
2462 void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
2463   unsigned outputReg = getReg(I);
2464   emitGEPOperation(BB, BB->end(), I.getOperand(0),I.op_begin()+1, I.op_end(), outputReg);
2465 }
2466
2467 void ISel::emitGEPOperation(MachineBasicBlock *MBB,
2468                             MachineBasicBlock::iterator IP,
2469                             Value *Src, User::op_iterator IdxBegin,
2470                             User::op_iterator IdxEnd, unsigned TargetReg) {
2471   const TargetData &TD = TM.getTargetData();
2472   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2473     Src = CPR->getValue();
2474
2475   std::vector<Value*> GEPOps;
2476   GEPOps.resize(IdxEnd-IdxBegin+1);
2477   GEPOps[0] = Src;
2478   std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2479   
2480   std::vector<const Type*> GEPTypes;
2481   GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2482                   gep_type_end(Src->getType(), IdxBegin, IdxEnd));
2483
2484   // Keep emitting instructions until we consume the entire GEP instruction.
2485   while (!GEPOps.empty()) {
2486       // It's an array or pointer access: [ArraySize x ElementType].
2487       const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2488       Value *idx = GEPOps.back();
2489       GEPOps.pop_back();        // Consume a GEP operand
2490       GEPTypes.pop_back();
2491
2492       // Many GEP instructions use a [cast (int/uint) to LongTy] as their
2493       // operand on X86.  Handle this case directly now...
2494       if (CastInst *CI = dyn_cast<CastInst>(idx))
2495         if (CI->getOperand(0)->getType() == Type::IntTy ||
2496             CI->getOperand(0)->getType() == Type::UIntTy)
2497           idx = CI->getOperand(0);
2498
2499       // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
2500       // must find the size of the pointed-to type (Not coincidentally, the next
2501       // type is the type of the elements in the array).
2502       const Type *ElTy = SqTy->getElementType();
2503       unsigned elementSize = TD.getTypeSize(ElTy);
2504
2505      if (elementSize == 1) {
2506         // If the element size is 1, we don't have to multiply, just add
2507         unsigned idxReg = getReg(idx, MBB, IP);
2508         unsigned Reg = makeAnotherReg(Type::UIntTy);
2509         BuildMI(*MBB, IP, PPC32::ADD, 2,TargetReg).addReg(Reg).addReg(idxReg);
2510         --IP;            // Insert the next instruction before this one.
2511         TargetReg = Reg; // Codegen the rest of the GEP into this
2512       } else {
2513         unsigned idxReg = getReg(idx, MBB, IP);
2514         unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
2515
2516         // Make sure we can back the iterator up to point to the first
2517         // instruction emitted.
2518         MachineBasicBlock::iterator BeforeIt = IP;
2519         if (IP == MBB->begin())
2520           BeforeIt = MBB->end();
2521         else
2522           --BeforeIt;
2523         doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2524
2525         // Emit an ADD to add OffsetReg to the basePtr.
2526         unsigned Reg = makeAnotherReg(Type::UIntTy);
2527         BuildMI(*MBB, IP, PPC32::ADD, 2, TargetReg).addReg(Reg).addReg(OffsetReg);
2528
2529         // Step to the first instruction of the multiply.
2530         if (BeforeIt == MBB->end())
2531           IP = MBB->begin();
2532         else
2533           IP = ++BeforeIt;
2534
2535         TargetReg = Reg; // Codegen the rest of the GEP into this
2536       }
2537     }
2538 }
2539
2540 /// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2541 /// frame manager, otherwise do it the hard way.
2542 ///
2543 void ISel::visitAllocaInst(AllocaInst &I) {
2544   // If this is a fixed size alloca in the entry block for the function, we
2545   // statically stack allocate the space, so we don't need to do anything here.
2546   //
2547   if (dyn_castFixedAlloca(&I)) return;
2548   
2549   // Find the data size of the alloca inst's getAllocatedType.
2550   const Type *Ty = I.getAllocatedType();
2551   unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2552
2553   // Create a register to hold the temporary result of multiplying the type size
2554   // constant by the variable amount.
2555   unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2556   unsigned SrcReg1 = getReg(I.getArraySize());
2557   
2558   // TotalSizeReg = mul <numelements>, <TypeSize>
2559   MachineBasicBlock::iterator MBBI = BB->end();
2560   doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
2561
2562   // AddedSize = add <TotalSizeReg>, 15
2563   unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2564   BuildMI(BB, PPC32::ADD, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
2565
2566   // AlignedSize = and <AddedSize>, ~15
2567   unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2568   BuildMI(BB, PPC32::RLWNM, 4, AlignedSize).addReg(AddedSizeReg).addImm(0).addImm(0).addImm(27);
2569   
2570   // Subtract size from stack pointer, thereby allocating some space.
2571   BuildMI(BB, PPC32::SUB, 2, PPC32::R1).addReg(PPC32::R1).addReg(AlignedSize);
2572
2573   // Put a pointer to the space into the result register, by copying
2574   // the stack pointer.
2575   BuildMI(BB, PPC32::OR, 2, getReg(I)).addReg(PPC32::R1).addReg(PPC32::R1);
2576
2577   // Inform the Frame Information that we have just allocated a variable-sized
2578   // object.
2579   F->getFrameInfo()->CreateVariableSizedObject();
2580 }
2581
2582 /// visitMallocInst - Malloc instructions are code generated into direct calls
2583 /// to the library malloc.
2584 ///
2585 void ISel::visitMallocInst(MallocInst &I) {
2586   unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2587   unsigned Arg;
2588
2589   if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2590     Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2591   } else {
2592     Arg = makeAnotherReg(Type::UIntTy);
2593     unsigned Op0Reg = getReg(I.getOperand(0));
2594     MachineBasicBlock::iterator MBBI = BB->end();
2595     doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
2596   }
2597
2598   std::vector<ValueRecord> Args;
2599   Args.push_back(ValueRecord(Arg, Type::UIntTy));
2600   MachineInstr *TheCall = BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol("malloc", true);
2601   doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2602 }
2603
2604
2605 /// visitFreeInst - Free instructions are code gen'd to call the free libc
2606 /// function.
2607 ///
2608 void ISel::visitFreeInst(FreeInst &I) {
2609   std::vector<ValueRecord> Args;
2610   Args.push_back(ValueRecord(I.getOperand(0)));
2611   MachineInstr *TheCall = BuildMI(PPC32::CALLpcrel, 1).addExternalSymbol("free", true);
2612   doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2613 }
2614    
2615 /// createPPC32SimpleInstructionSelector - This pass converts an LLVM function
2616 /// into a machine code representation is a very simple peep-hole fashion.  The
2617 /// generated code sucks but the implementation is nice and simple.
2618 ///
2619 FunctionPass *llvm::createPPCSimpleInstructionSelector(TargetMachine &TM) {
2620   return new ISel(TM);
2621 }