Printing support for more stuff
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9InstrSelection.cpp
index ce7e4dc0d925fb10deccdeb16b0fa05f288ddad4..3e2731c002bd985a87aec92568e51bbffa81ab03 100644 (file)
@@ -12,7 +12,7 @@
 #include "llvm/CodeGen/MachineInstrAnnot.h"
 #include "llvm/CodeGen/InstrForest.h"
 #include "llvm/CodeGen/InstrSelection.h"
-#include "llvm/CodeGen/MachineCodeForMethod.h"
+#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineCodeForInstruction.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iTerminators.h"
@@ -20,6 +20,7 @@
 #include "llvm/iOther.h"
 #include "llvm/Function.h"
 #include "llvm/Constants.h"
+#include "llvm/ConstantHandling.h"
 #include "Support/MathExtras.h"
 #include <math.h>
 using std::vector;
@@ -268,43 +269,38 @@ ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
 }
 
 static inline MachineOpCode 
-ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
+ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
 {
   MachineOpCode opCode = INVALID_OPCODE;;
-  
-  if (tid==Type::SByteTyID || tid==Type::ShortTyID  || tid==Type::IntTyID ||
-      tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
+
+  assert((opType == Type::FloatTy || opType == Type::DoubleTy)
+         && "This function should only be called for FLOAT or DOUBLE");
+
+  if (tid==Type::UIntTyID)
     {
-      switch (opType->getPrimitiveID())
-        {
-        case Type::FloatTyID:   opCode = FSTOI; break;
-        case Type::DoubleTyID:  opCode = FDTOI; break;
-        default:
-          assert(0 && "Non-numeric non-bool type cannot be converted to Int");
-          break;
-        }
+      assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
+             " into FP->long->uint for SPARC v9:  SO RUN PRESELECTION PASS!");
+    }
+  else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
+           tid==Type::UByteTyID || tid==Type::UShortTyID)
+    {
+      opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
     }
   else if (tid==Type::LongTyID || tid==Type::ULongTyID)
     {
-      switch (opType->getPrimitiveID())
-        {
-        case Type::FloatTyID:   opCode = FSTOX; break;
-        case Type::DoubleTyID:  opCode = FDTOX; break;
-        default:
-          assert(0 && "Non-numeric non-bool type cannot be converted to Long");
-          break;
-        }
+      opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
     }
   else
       assert(0 && "Should not get here, Mo!");
-  
+
   return opCode;
 }
 
 MachineInstr*
-CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
+CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
+                          Value* srcVal, Value* destVal)
 {
-  MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
+  MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
   assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
   
   MachineInstr* M = new MachineInstr(opCode);
@@ -348,8 +344,8 @@ CreateCodeToConvertFloatToInt(const TargetMachine& target,
   mcfi.addTemp(destForCast);
 
   // Create the fp-to-int conversion code
-  MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
-                                            opVal, destForCast);
+  MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
+                                             opVal, destForCast);
   mvec.push_back(M);
 
   // Create the fpreg-to-intreg copy code
@@ -540,7 +536,6 @@ CreateShiftInstructions(const TargetMachine& target,
   // of dest, so we need to put the result of the SLL into a temporary.
   // 
   Value* shiftDest = destVal;
-  const Type* opType = argVal1->getType();
   unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
   if ((shiftOpCode == SLL || shiftOpCode == SLLX)
       && opSize < target.DataLayout.getIntegerRegize())
@@ -558,8 +553,8 @@ CreateShiftInstructions(const TargetMachine& target,
     { // extend the sign-bit of the result into all upper bits of dest
       assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
       target.getInstrInfo().
-        CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
-                                        destVal, mvec, mcfi);
+        CreateSignExtensionInstructions(target, F, shiftDest, destVal,
+                                        8*opSize, mvec, mcfi);
     }
 }
 
@@ -670,18 +665,10 @@ CreateCheapestMulConstInstruction(const TargetMachine &target,
 {
   Value* constOp;
   if (isa<Constant>(lval) && isa<Constant>(rval))
-    { // both operands are constant: try both orders!
-      vector<MachineInstr*> mvec1, mvec2;
-      unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
-                                                     destVal, mvec1, mcfi);
-      unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
-                                                     destVal, mvec2, mcfi);
-      vector<MachineInstr*>& mincostMvec =  (lcost <= rcost)? mvec1 : mvec2;
-      vector<MachineInstr*>& maxcostMvec =  (lcost <= rcost)? mvec2 : mvec1;
-      mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end()); 
-
-      for (unsigned int i=0; i < maxcostMvec.size(); ++i)
-        delete maxcostMvec[i];
+    { // both operands are constant: evaluate and "set" in dest
+      Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
+                                  cast<Constant>(lval), cast<Constant>(rval));
+      target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
     }
   else if (isa<Constant>(rval))         // rval is constant, but not lval
     CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
@@ -846,40 +833,65 @@ CreateCodeForVariableSizeAlloca(const TargetMachine& target,
                                 Value* numElementsVal,
                                 vector<MachineInstr*>& getMvec)
 {
+  Value* totalSizeVal;
   MachineInstr* M;
-  
-  // Create a Value to hold the (constant) element size
-  Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
+  MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
+  Function *F = result->getParent()->getParent();
+
+  // Enforce the alignment constraints on the stack pointer at
+  // compile time if the total size is a known constant.
+  if (isa<Constant>(numElementsVal))
+    {
+      bool isValid;
+      int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
+      assert(isValid && "Unexpectedly large array dimension in alloca!");
+      int64_t total = numElem * tsize;
+      if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
+        total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
+      totalSizeVal = ConstantSInt::get(Type::IntTy, total);
+    }
+  else
+    {
+      // The size is not a constant.  Generate code to compute it and
+      // code to pad the size for stack alignment.
+      // Create a Value to hold the (constant) element size
+      Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
+
+      // Create temporary values to hold the result of MUL, SLL, SRL
+      // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
+      TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
+      TmpInstruction* tmpSLL  = new TmpInstruction(numElementsVal, tmpProd);
+      TmpInstruction* tmpSRL  = new TmpInstruction(numElementsVal, tmpSLL);
+      mcfi.addTemp(tmpProd);
+      mcfi.addTemp(tmpSLL);
+      mcfi.addTemp(tmpSRL);
+
+      // Instruction 1: mul numElements, typeSize -> tmpProd
+      // This will optimize the MUL as far as possible.
+      CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
+                           mcfi, INVALID_MACHINE_OPCODE);
+
+      assert(0 && "Need to insert padding instructions here!");
+
+      totalSizeVal = tmpProd;
+    }
 
   // Get the constant offset from SP for dynamically allocated storage
   // and create a temporary Value to hold it.
-  assert(result && result->getParent() && "Result value is not part of a fn?");
-  Function *F = result->getParent()->getParent();
-  MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
+  MachineFunction& mcInfo = MachineFunction::get(F);
   bool growUp;
   ConstantSInt* dynamicAreaOffset =
     ConstantSInt::get(Type::IntTy,
-                      target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
+                     target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
   assert(! growUp && "Has SPARC v9 stack frame convention changed?");
 
-  // Create a temporary value to hold the result of MUL
-  TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
-  MachineCodeForInstruction::get(result).addTemp(tmpProd);
-  
-  // Instruction 1: mul numElements, typeSize -> tmpProd
-  M = new MachineInstr(MULX);
-  M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
-  M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
-  M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
-  getMvec.push_back(M);
-        
-  // Instruction 2: sub %sp, tmpProd -> %sp
+  // Instruction 2: sub %sp, totalSizeVal -> %sp
   M = new MachineInstr(SUB);
   M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
-  M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
+  M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, totalSizeVal);
   M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
   getMvec.push_back(M);
-  
+
   // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
   M = new MachineInstr(ADD);
   M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
@@ -896,10 +908,11 @@ CreateCodeForFixedSizeAlloca(const TargetMachine& target,
                              unsigned int numElements,
                              vector<MachineInstr*>& getMvec)
 {
+  assert(tsize > 0 && "Illegal (zero) type size for alloca");
   assert(result && result->getParent() &&
          "Result value is not part of a function?");
   Function *F = result->getParent()->getParent();
-  MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
+  MachineFunction &mcInfo = MachineFunction::get(F);
 
   // Check if the offset would small enough to use as an immediate in
   // load/stores (check LDX because all load/stores have the same-size immediate
@@ -952,7 +965,7 @@ CreateCodeForFixedSizeAlloca(const TargetMachine& target,
 
 static void
 SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
-                       const InstructionNode* vmInstrNode,
+                       InstructionNode* vmInstrNode,
                        const TargetMachine& target)
 {
   Instruction* memInst = vmInstrNode->getInstruction();
@@ -998,10 +1011,9 @@ SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
                  && "Array refs must be lowered before Instruction Selection");
 
           Value* idxVal = idxVec[firstIdxIsZero];
-          assert(! isa<Constant>(idxVal) && "Need to sign-extend uint to 64b!");
 
           vector<MachineInstr*> mulVec;
-          Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
+          Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
           MachineCodeForInstruction::get(memInst).addTemp(addr);
 
           // Get the array type indexed by idxVal, and compute its element size.
@@ -1012,22 +1024,17 @@ SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
                                            /*AllowCompositeLeaf*/ true)
                                  : ptrType);
           const Type* eltType = cast<SequentialType>(vecType)->getElementType();
-          ConstantUInt* eltSizeVal = ConstantUInt::get(Type::UIntTy,
+          ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
                                        target.DataLayout.getTypeSize(eltType));
 
           // CreateMulInstruction() folds constants intelligently enough.
-          CreateMulInstruction(target,
-                               memInst->getParent()->getParent(),
+          CreateMulInstruction(target, memInst->getParent()->getParent(),
                                idxVal,         /* lval, not likely to be const*/
                                eltSizeVal,     /* rval, likely to be constant */
                                addr,           /* result */
-                               mulVec,
-                               MachineCodeForInstruction::get(memInst),
+                               mulVec, MachineCodeForInstruction::get(memInst),
                                INVALID_MACHINE_OPCODE);
 
-          // Sign-extend the result of MUL  from 32 to 64 bits.
-          target.getInstrInfo().CreateSignExtensionInstructions(target, memInst->getParent()->getParent(), addr, /*srcSizeInBits*/32, addr, mulVec, MachineCodeForInstruction::get(memInst));
-
           // Insert mulVec[] before *mvecI in mvec[] and update mvecI
           // to point to the same instruction it pointed to before.
           assert(mulVec.size() > 0 && "No multiply code created?");
@@ -1126,7 +1133,7 @@ ForwardOperand(InstructionNode* treeNode,
           for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
             {
               const MachineOperand& mop = minstr->getOperand(i);
-              if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
+              if (mop.getType() == MachineOperand::MO_VirtualRegister &&
                   mop.getVRegValue() == unusedOp)
                 minstr->SetMachineOperandVal(i,
                                 MachineOperand::MO_VirtualRegister, fwdOp);
@@ -1260,10 +1267,10 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
         
         M = new MachineInstr(JMPLRET);
-        M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
-                                      returnReg);
+        M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
+                                returnReg);
         M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
-                                   (int64_t)8);
+                                  (int64_t)8);
         M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
         
         if (returnInstr->getReturnValue() != NULL)
@@ -1447,29 +1454,64 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
       }
       
       case 23: // reg:   ToUByteTy(reg)
+      case 24: // reg:   ToSByteTy(reg)
       case 25: // reg:   ToUShortTy(reg)
+      case 26: // reg:   ToShortTy(reg)
       case 27: // reg:   ToUIntTy(reg)
-      case 29: // reg:   ToULongTy(reg)
+      case 28: // reg:   ToIntTy(reg)
       {
+        //======================================================================
+        // Rules for integer conversions:
+        // 
+        //--------
+        // From ISO 1998 C++ Standard, Sec. 4.7:
+        //
+        // 2. If the destination type is unsigned, the resulting value is
+        // the least unsigned integer congruent to the source integer
+        // (modulo 2n where n is the number of bits used to represent the
+        // unsigned type). [Note: In a two s complement representation,
+        // this conversion is conceptual and there is no change in the
+        // bit pattern (if there is no truncation). ]
+        // 
+        // 3. If the destination type is signed, the value is unchanged if
+        // it can be represented in the destination type (and bitfield width);
+        // otherwise, the value is implementation-defined.
+        //--------
+        // 
+        // Since we assume 2s complement representations, this implies:
+        // 
+        // -- if operand is smaller than destination, zero-extend or sign-extend
+        //    according to the signedness of the *operand*: source decides.
+        //    ==> we have to do nothing here!
+        // 
+        // -- if operand is same size as or larger than destination, and the
+        //    destination is *unsigned*, zero-extend the operand: dest. decides
+        // 
+        // -- if operand is same size as or larger than destination, and the
+        //    destination is *signed*, the choice is implementation defined:
+        //    we sign-extend the operand: i.e., again dest. decides.
+        //    Note: this matches both Sun's cc and gcc3.2.
+        //======================================================================
+
         Instruction* destI =  subtreeRoot->getInstruction();
         Value* opVal = subtreeRoot->leftChild()->getValue();
-        const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
+        const Type* opType = opVal->getType();
         if (opType->isIntegral() || isa<PointerType>(opType))
           {
             unsigned opSize = target.DataLayout.getTypeSize(opType);
             unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
-            if (opSize > destSize ||
-                (opType->isSigned()
-                 && destSize < target.DataLayout.getIntegerRegize()))
-              { // operand is larger than dest,
-                //    OR both are equal but smaller than the full register size
-                //       AND operand is signed, so it may have extra sign bits:
-                // mask high bits using AND
-                M = Create3OperandInstr(AND, opVal,
-                                        ConstantUInt::get(Type::ULongTy,
-                                              ((uint64_t) 1 << 8*destSize) - 1),
-                                        destI);
-                mvec.push_back(M);
+            if (opSize >= destSize)
+              { // Operand is same size as or larger than dest:
+                // zero- or sign-extend, according to the signeddness of
+                // the destination (see above).
+                if (destI->getType()->isSigned())
+                  target.getInstrInfo().CreateSignExtensionInstructions(target,
+                    destI->getParent()->getParent(), opVal, destI, 8*destSize,
+                    mvec, MachineCodeForInstruction::get(destI));
+                else
+                  target.getInstrInfo().CreateZeroExtensionInstructions(target,
+                    destI->getParent()->getParent(), opVal, destI, 8*destSize,
+                    mvec, MachineCodeForInstruction::get(destI));
               }
             else
               forwardOperandNum = 0;          // forward first operand to user
@@ -1478,69 +1520,33 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
           {
             CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
                                          MachineCodeForInstruction::get(destI));
-            maskUnsignedResult = true;  // not handled by convert code
+            if (destI->getType()->isUnsigned())
+              maskUnsignedResult = true; // not handled by fp->int code
           }
         else
           assert(0 && "Unrecognized operand type for convert-to-unsigned");
 
         break;
       }
-      
-      case 24: // reg:   ToSByteTy(reg)
-      case 26: // reg:   ToShortTy(reg)
-      case 28: // reg:   ToIntTy(reg)
+
+      case 29: // reg:   ToULongTy(reg)
       case 30: // reg:   ToLongTy(reg)
       {
-        Instruction* destI =  subtreeRoot->getInstruction();
         Value* opVal = subtreeRoot->leftChild()->getValue();
-        MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
-
         const Type* opType = opVal->getType();
         if (opType->isIntegral() || isa<PointerType>(opType))
+          forwardOperandNum = 0;          // forward first operand to user
+        else if (opType->isFloatingPoint())
           {
-            // These operand types have the same format as the destination,
-            // but may have different size: add sign bits or mask as needed.
-            // 
-            const Type* destType = destI->getType();
-            unsigned opSize = target.DataLayout.getTypeSize(opType);
-            unsigned destSize = target.DataLayout.getTypeSize(destType);
-            
-            if (opSize < destSize ||
-                (opSize == destSize &&
-                 opSize == target.DataLayout.getIntegerRegize()))
-              { // operand is smaller or both operand and result fill register
-                forwardOperandNum = 0;          // forward first operand to user
-              }
-            else
-              { // need to mask (possibly) and then sign-extend (definitely)
-                Value* srcForSignExt = opVal;
-                unsigned srcSizeForSignExt = 8 * opSize;
-                if (opSize > destSize)
-                  { // operand is larger than dest: mask high bits
-                    TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
-                                                              destI, "maskHi");
-                    mcfi.addTemp(tmpI);
-                    M = Create3OperandInstr(AND, opVal,
-                                            ConstantUInt::get(Type::ULongTy,
-                                              ((uint64_t) 1 << 8*destSize)-1),
-                                            tmpI);
-                    mvec.push_back(M);
-                    srcForSignExt = tmpI;
-                    srcSizeForSignExt = 8 * destSize;
-                  }
-                
-                // sign-extend
-                target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
-              }
+            Instruction* destI =  subtreeRoot->getInstruction();
+            CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
+                                         MachineCodeForInstruction::get(destI));
           }
-        else if (opType->isFloatingPoint())
-          CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
         else
           assert(0 && "Unrecognized operand type for convert-to-signed");
-
         break;
-      }  
-
+      }
+      
       case  31:        // reg:   ToFloatTy(reg):
       case  32:        // reg:   ToDoubleTy(reg):
       case 232:        // reg:   ToDoubleTy(Constant):
@@ -1601,7 +1607,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
                   }
                 else
                   srcForCast = leftVal;
-                
+
                 M = new MachineInstr(opCode);
                 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
                                            srcForCast);
@@ -1936,18 +1942,18 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         mvec.push_back(new MachineInstr(ADD));
         SetOperandsForMemInstr(mvec, subtreeRoot, target);
         break;
-        
+
       case 57: // reg:  Alloca: Implement as 1 instruction:
       {         //         add %fp, offsetFromFP -> result
         AllocationInst* instr =
           cast<AllocationInst>(subtreeRoot->getInstruction());
         unsigned int tsize =
-          target.findOptimalStorageSize(instr->getAllocatedType());
+          target.DataLayout.getTypeSize(instr->getAllocatedType());
         assert(tsize != 0);
         CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
         break;
       }
-      
+
       case 58: // reg:   Alloca(reg): Implement as 3 instructions:
                 //     mul num, typeSz -> tmp
                 //     sub %sp, tmp    -> %sp
@@ -1957,7 +1963,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         const Type* eltType = instr->getAllocatedType();
         
         // If #elements is constant, use simpler code for fixed-size allocas
-        int tsize = (int) target.findOptimalStorageSize(eltType);
+        int tsize = (int) target.DataLayout.getTypeSize(eltType);
         Value* numElementsVal = NULL;
         bool isArray = instr->isArrayAllocation();
         
@@ -1974,13 +1980,13 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
                                           numElementsVal, mvec);
         break;
       }
-      
+
       case 61: // reg:   Call
-      {         // Generate a direct (CALL) or indirect (JMPL). depending
-                // Mark the return-address register and the indirection
-                // register (if any) as hidden virtual registers.
-                // Also, mark the operands of the Call and return value (if
-                // any) as implicit operands of the CALL machine instruction.
+      {         // Generate a direct (CALL) or indirect (JMPL) call.
+                // Mark the return-address register, the indirection
+                // register (for indirect calls), the operands of the Call,
+                // and the return value (if any) as implicit operands
+                // of the machine instruction.
                 // 
                 // If this is a varargs function, floating point arguments
                 // have to passed in integer registers so insert
@@ -1988,34 +1994,22 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
                 // 
         CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
         Value *callee = callInstr->getCalledValue();
-        
-        // Create hidden virtual register for return address, with type void*. 
+
+        // Create hidden virtual register for return address with type void*
         TmpInstruction* retAddrReg =
           new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
         MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
-        
+
         // Generate the machine instruction and its operands.
         // Use CALL for direct function calls; this optimistically assumes
         // the PC-relative address fits in the CALL address field (22 bits).
         // Use JMPL for indirect calls.
         // 
-        if (isa<Function>(callee))
-          { // direct function call
-            M = new MachineInstr(CALL);
-            M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
-                                    callee);
-          } 
-        else
-          { // indirect function call
-            M = new MachineInstr(JMPLCALL);
-            M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
-                                    callee);
-            M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
-                                      (int64_t) 0);
-            M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
-                                    retAddrReg);
-          }
-        
+        if (isa<Function>(callee))      // direct function call
+          M = Create1OperandInstr_Addr(CALL, callee);
+        else                            // indirect function call
+          M = Create3OperandInstr_SImmed(JMPLCALL, callee,
+                                         (int64_t) 0, retAddrReg);
         mvec.push_back(M);
 
         const FunctionType* funcType =
@@ -2024,11 +2018,12 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         bool isVarArgs = funcType->isVarArg();
         bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
         
-        // Use an annotation to pass information about call arguments
-        // to the register allocator.
+        // Use a descriptor to pass information about call arguments
+        // to the register allocator.  This descriptor will be "owned"
+        // and freed automatically when the MachineCodeForInstruction
+        // object for the callInstr goes away.
         CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
                                          retAddrReg, isVarArgs, noPrototype);
-        M->addAnnotation(argDesc);
         
         assert(callInstr->getOperand(0) == callee
                && "This is assumed in the loop below!");
@@ -2050,7 +2045,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
                 
                 // If this arg. is in the first $K$ regs, add a copy
                 // float-to-int instruction to pass the value as an integer.
-                if (i < target.getRegInfo().GetNumOfIntArgRegs())
+                if (i <= target.getRegInfo().GetNumOfIntArgRegs())
                   {
                     MachineCodeForInstruction &destMCFI = 
                       MachineCodeForInstruction::get(callInstr);   
@@ -2175,7 +2170,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
               for (unsigned i=0, N=mvec.size(); i < N; ++i)
                 mvec[i]->substituteValue(dest, tmpI);
 
-              M = Create3OperandInstr_UImmed(SRL, tmpI, 4-destSize, dest);
+              M = Create3OperandInstr_UImmed(SRL, tmpI, 8*(4-destSize), dest);
               mvec.push_back(M);
             }
           else if (destSize < target.DataLayout.getIntegerRegize())