Print "circular" warning message only in debug mode.
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9InstrSelection.cpp
index 3f31e3b90be9246cf4eab25d0f07950685a6c19f..5195f4ac29faef839c3603f35fff817e89f62238 100644 (file)
 #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;
 
-//************************* Forward Declarations ***************************/
-
-
-static void SetMemOperands_Internal     (vector<MachineInstr*>& mvec,
-                                         vector<MachineInstr*>::iterator mvecI,
-                                         const InstructionNode* vmInstrNode,
-                                         Value* ptrVal,
-                                         std::vector<Value*>& idxVec,
-                                         bool allConstantIndices,
-                                         const TargetMachine& target);
-
-
 //************************ Internal Functions ******************************/
 
 
@@ -168,7 +157,8 @@ ChooseBccInstruction(const InstructionNode* instrNode,
                      bool& isFPBranch)
 {
   InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
-  BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
+  assert(setCCNode->getOpLabel() == SetCCOp);
+  BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
   const Type* setCCType = setCCInstr->getOperand(0)->getType();
   
   isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
@@ -279,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);
@@ -324,35 +309,49 @@ CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
   return M;
 }
 
-// CreateCodeToConvertIntToFloat: Convert FP value to signed or unsigned integer
+// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
 // The FP value must be converted to the dest type in an FP register,
 // and the result is then copied from FP to int register via memory.
+//
+// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
+// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
+// *only* when converting to an unsigned int.  (Unsigned byte, short or long
+// don't have this problem.)
+// For unsigned int, we therefore have to generate the code sequence:
+// 
+//      if (V > (float) MAXINT) {
+//        unsigned result = (unsigned) (V  - (float) MAXINT);
+//        result = result + (unsigned) MAXINT;
+//      }
+//      else
+//        result = (unsigned int) V;
+// 
 static void
-CreateCodeToConvertIntToFloat (const TargetMachine& target,
-                               Value* opVal,
-                               Instruction* destI,
-                               std::vector<MachineInstr*>& mvec,
-                               MachineCodeForInstruction& mcfi)
+CreateCodeToConvertFloatToInt(const TargetMachine& target,
+                              Value* opVal,
+                              Instruction* destI,
+                              std::vector<MachineInstr*>& mvec,
+                              MachineCodeForInstruction& mcfi)
 {
   // Create a temporary to represent the FP register into which the
   // int value will placed after conversion.  The type of this temporary
   // depends on the type of FP register to use: single-prec for a 32-bit
   // int or smaller; double-prec for a 64-bit int.
   // 
-  const Type* destTypeToUse = (destI->getType() == Type::LongTy)? Type::DoubleTy
-                                                                : Type::FloatTy;
-  Value* destForCast = new TmpInstruction(destTypeToUse, opVal);
+  size_t destSize = target.DataLayout.getTypeSize(destI->getType());
+  const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
+  TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
   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
   target.getInstrInfo().
     CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
-                               (TmpInstruction*)destForCast, destI, mvec, mcfi);
+                               destForCast, destI, mvec, mcfi);
 }
 
 
@@ -404,7 +403,7 @@ ChooseSubInstructionByType(const Type* resultType)
 {
   MachineOpCode opCode = INVALID_OPCODE;
   
-  if (resultType->isIntegral() || isa<PointerType>(resultType))
+  if (resultType->isInteger() || isa<PointerType>(resultType))
     {
       opCode = SUB;
     }
@@ -482,7 +481,7 @@ ChooseMulInstructionByType(const Type* resultType)
 {
   MachineOpCode opCode = INVALID_OPCODE;
   
-  if (resultType->isIntegral())
+  if (resultType->isInteger())
     opCode = MULX;
   else
     switch(resultType->getPrimitiveID())
@@ -537,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())
@@ -555,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);
     }
 }
 
@@ -585,7 +583,7 @@ CreateMulConstInstruction(const TargetMachine &target, Function* F,
   // 
   const Type* resultType = destVal->getType();
   
-  if (resultType->isIntegral() || isa<PointerType>(resultType))
+  if (resultType->isInteger() || isa<PointerType>(resultType))
     {
       bool isValidConst;
       int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
@@ -667,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);
@@ -727,7 +717,7 @@ ChooseDivInstruction(TargetMachine &target,
   
   const Type* resultType = instrNode->getInstruction()->getType();
   
-  if (resultType->isIntegral())
+  if (resultType->isInteger())
     opCode = resultType->isSigned()? SDIVX : UDIVX;
   else
     switch(resultType->getPrimitiveID())
@@ -760,7 +750,7 @@ CreateDivConstInstruction(TargetMachine &target,
   // 
   const Type* resultType = instrNode->getInstruction()->getType();
   
-  if (resultType->isIntegral())
+  if (resultType->isInteger())
     {
       unsigned pow;
       bool isValidConst;
@@ -844,7 +834,8 @@ CreateCodeForVariableSizeAlloca(const TargetMachine& target,
                                 vector<MachineInstr*>& getMvec)
 {
   MachineInstr* M;
-  
+  MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
+
   // Create a Value to hold the (constant) element size
   Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
 
@@ -861,14 +852,11 @@ CreateCodeForVariableSizeAlloca(const TargetMachine& target,
 
   // Create a temporary value to hold the result of MUL
   TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
-  MachineCodeForInstruction::get(result).addTemp(tmpProd);
+  mcfi.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);
+  CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
+                       mcfi, INVALID_MACHINE_OPCODE);
         
   // Instruction 2: sub %sp, tmpProd -> %sp
   M = new MachineInstr(SUB);
@@ -893,6 +881,7 @@ 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();
@@ -931,15 +920,6 @@ CreateCodeForFixedSizeAlloca(const TargetMachine& target,
 }
 
 
-
-// Check for a constant (uint) 0.
-inline bool
-IsZero(Value* idx)
-{
-  return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
-}
-
-
 //------------------------------------------------------------------------ 
 // Function SetOperandsForMemInstr
 //
@@ -958,74 +938,19 @@ IsZero(Value* idx)
 
 static void
 SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
-                       vector<MachineInstr*>::iterator mvecI,
-                       const InstructionNode* vmInstrNode,
+                       InstructionNode* vmInstrNode,
                        const TargetMachine& target)
 {
-  MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
-  
-  // Variables to hold the index vector and ptr value.
-  // The major work here is to extract these for all 3 instruction types
-  // and to try to fold chains of constant indices into a single offset.
-  // After that, we call SetMemOperands_Internal(), which creates the
-  // appropriate operands for the machine instruction.
-  vector<Value*> idxVec;
-  bool allConstantIndices = true;
-  Value* ptrVal = memInst->getPointerOperand();
-
-  // If there is a GetElemPtr instruction to fold in to this instr,
-  // it must be in the left child for Load and GetElemPtr, and in the
-  // right child for Store instructions.
-  InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
-                             ? vmInstrNode->rightChild()
-                             : vmInstrNode->leftChild()); 
-
-  // Check if all indices are constant for this instruction
-  for (MemAccessInst::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
-       allConstantIndices && OI != OE; ++OI)
-    if (! isa<Constant>(*OI))
-      allConstantIndices = false; 
-
-  // If we have only constant indices, fold chains of constant indices
-  // in this and any preceding GetElemPtr instructions.
-  bool foldedGEPs = false;
-  if (allConstantIndices &&
-      (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
-       ptrChild->getOpLabel() == GetElemPtrIdx))
-    if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
-      ptrVal = newPtr;
-      foldedGEPs = true;
-    }
-
-  // Append the index vector of the current instruction, if any.
-  // Skip the leading [0] index if preceding GEPs were folded into this.
-  if (memInst->getNumIndices() > 0) {
-    assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
-    idxVec.insert(idxVec.end(),
-                  memInst->idx_begin() + foldedGEPs, memInst->idx_end());
-  }
-
-  // Now create the appropriate operands for the machine instruction
-  SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
-                          ptrVal, idxVec, allConstantIndices, target);
-}
+  Instruction* memInst = vmInstrNode->getInstruction();
+  vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
 
+  // Index vector, ptr value, and flag if all indices are const.
+  vector<Value*> idxVec;
+  bool allConstantIndices;
+  Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
 
-// Generate the correct operands (and additional instructions if needed)
-// for the given pointer and given index vector.
-//
-static void
-SetMemOperands_Internal(vector<MachineInstr*>& mvec,
-                        vector<MachineInstr*>::iterator mvecI,
-                        const InstructionNode* vmInstrNode,
-                        Value* ptrVal,
-                        vector<Value*>& idxVec,
-                        bool allConstantIndices,
-                        const TargetMachine& target)
-{
-  MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
-  
-  // Initialize so we default to storing the offset in a register.
+  // Now create the appropriate operands for the machine instruction.
+  // First, initialize so we default to storing the offset in a register.
   int64_t smallConstOffset = 0;
   Value* valueForRegOffset = NULL;
   MachineOperand::MachineOperandType offsetOpType =
@@ -1034,7 +959,7 @@ SetMemOperands_Internal(vector<MachineInstr*>& mvec,
   // Check if there is an index vector and if so, compute the
   // right offset for structures and for arrays 
   // 
-  if (idxVec.size() > 0)
+  if (!idxVec.empty())
     {
       const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
       
@@ -1053,29 +978,34 @@ SetMemOperands_Internal(vector<MachineInstr*>& mvec,
           // offset.  (An extra leading zero offset, if any, can be ignored.)
           // Generate code sequence to compute address from index.
           // 
-          assert(idxVec.size() == 1U + IsZero(idxVec[0])
+          bool firstIdxIsZero =
+            (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
+          assert(idxVec.size() == 1U + firstIdxIsZero 
                  && "Array refs must be lowered before Instruction Selection");
 
-          Value* idxVal = idxVec[IsZero(idxVec[0])];
+          Value* idxVal = idxVec[firstIdxIsZero];
 
           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.
           // The call to getTypeSize() will fail if size is not constant.
-          unsigned int eltSize =
-            target.DataLayout.getTypeSize(ptrType->getElementType());
-          assert(eltSize > 0 && "Invalid or non-const array element size");
-          ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
+          const Type* vecType = (firstIdxIsZero
+                                 ? GetElementPtrInst::getIndexedType(ptrType,
+                                           std::vector<Value*>(1U, idxVec[0]),
+                                           /*AllowCompositeLeaf*/ true)
+                                 : ptrType);
+          const Type* eltType = cast<SequentialType>(vecType)->getElementType();
+          ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
+                                       target.DataLayout.getTypeSize(eltType));
 
           // CreateMulInstruction() folds constants intelligently enough.
-          CreateMulInstruction(target,
-                               memInst->getParent()->getParent(),
-                               idxVal,         /* lval, not likely const */
-                               eltVal,         /* rval, likely constant */
-                               addr,           /* result*/
-                               mulVec,
-                               MachineCodeForInstruction::get(memInst),
+          CreateMulInstruction(target, memInst->getParent()->getParent(),
+                               idxVal,         /* lval, not likely to be const*/
+                               eltSizeVal,     /* rval, likely to be constant */
+                               addr,           /* result */
+                               mulVec, MachineCodeForInstruction::get(memInst),
                                INVALID_MACHINE_OPCODE);
 
           // Insert mulVec[] before *mvecI in mvec[] and update mvecI
@@ -1218,7 +1148,6 @@ ThisIsAChainRule(int eruleno)
   switch(eruleno)
     {
     case 111:  // stmt:  reg
-    case 113:  // stmt:  bool
     case 123:
     case 124:
     case 125:
@@ -1237,9 +1166,10 @@ ThisIsAChainRule(int eruleno)
     case 242:
     case 243:
     case 244:
+    case 245:
     case 321:
       return true; break;
-      
+
     default:
       return false; break;
     }
@@ -1330,7 +1260,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         mvec.push_back(new MachineInstr(
                          ChooseStoreInstruction(
                             subtreeRoot->leftChild()->getValue()->getType())));
-        SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
+        SetOperandsForMemInstr(mvec, subtreeRoot, target);
         break;
 
       case 5:  // stmt:   BrUncond
@@ -1355,7 +1285,7 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         Constant *constVal = cast<Constant>(constNode->getValue());
         bool isValidConst;
         
-        if ((constVal->getType()->isIntegral()
+        if ((constVal->getType()->isInteger()
              || isa<PointerType>(constVal->getType()))
             && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
             && isValidConst)
@@ -1394,18 +1324,16 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         // ELSE FALL THROUGH
       }
 
-      case 6:  // stmt:   BrCond(bool)
-      { // bool => boolean was computed with some boolean operator
-        // (SetCC, Not, ...).  We need to check whether the type was a FP,
-        // signed int or unsigned int, and check the branching condition in
-        // order to choose the branch to use.
+      case 6:  // stmt:   BrCond(setCC)
+      { // bool => boolean was computed with SetCC.
+        // The branch to use depends on whether it is FP, signed, or unsigned.
         // If it is an integer CC, we also need to find the unique
         // TmpInstruction representing that CC.
         // 
         BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
         bool isFPBranch;
         M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
-        
+
         Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
                                      brInst->getParent()->getParent(),
                                      isFPBranch? Type::FloatTy : Type::IntTy);
@@ -1414,16 +1342,16 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
                                    brInst->getSuccessor(0));
         mvec.push_back(M);
-        
+
         // delay slot
         mvec.push_back(new MachineInstr(NOP));
-        
+
         // false branch
         M = new MachineInstr(BA);
         M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
                                    brInst->getSuccessor(1));
         mvec.push_back(M);
-        
+
         // delay slot
         mvec.push_back(new MachineInstr(NOP));
         break;
@@ -1490,128 +1418,128 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         break;
       }
 
-      case 322:        // reg:   ToBoolTy(bool):
       case 22: // reg:   ToBoolTy(reg):
       {
         const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
-        assert(opType->isIntegral() || isa<PointerType>(opType)
-               || opType == Type::BoolTy);
+        assert(opType->isIntegral() || isa<PointerType>(opType));
         forwardOperandNum = 0;          // forward first operand to user
         break;
       }
       
       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();
-        if (opType->isIntegral()
-            || isa<PointerType>(opType)
-            || opType == Type::BoolTy)
+        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
           }
         else if (opType->isFloatingPoint())
-          CreateCodeToConvertIntToFloat(target, opVal, destI, mvec,
-                                        MachineCodeForInstruction::get(destI));
+          {
+            CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
+                                         MachineCodeForInstruction::get(destI));
+            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)
-            || opType == Type::BoolTy)
+        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())
-          CreateCodeToConvertIntToFloat(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):
-        
+      
         // If this instruction has a parent (a user) in the tree 
         // and the user is translated as an FsMULd instruction,
         // then the cast is unnecessary.  So check that first.
         // In the future, we'll want to do the same for the FdMULq instruction,
         // so do the check here instead of only for ToFloatTy(reg).
         // 
-        if (subtreeRoot->parent() != NULL &&
-            MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
+        if (subtreeRoot->parent() != NULL)
           {
-            forwardOperandNum = 0;          // forward first operand to user
+            const MachineCodeForInstruction& mcfi =
+              MachineCodeForInstruction::get(
+                cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
+            if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
+              forwardOperandNum = 0;    // forward first operand to user
           }
-        else
+
+        if (forwardOperandNum != 0)     // we do need the cast
           {
             Value* leftVal = subtreeRoot->leftChild()->getValue();
             const Type* opType = leftVal->getType();
@@ -1636,23 +1564,23 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
                     // register used: single-prec for a 32-bit int or smaller,
                     // double-prec for a 64-bit int.
                     // 
-                    const Type* srcTypeToUse =
-                      (leftVal->getType() == Type::LongTy)? Type::DoubleTy
-                                                          : Type::FloatTy;
-                    
-                    srcForCast = new TmpInstruction(srcTypeToUse, dest);
+                    uint64_t srcSize =
+                      target.DataLayout.getTypeSize(leftVal->getType());
+                    Type* tmpTypeToUse =
+                      (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
+                    srcForCast = new TmpInstruction(tmpTypeToUse, dest);
                     MachineCodeForInstruction &destMCFI = 
                       MachineCodeForInstruction::get(dest);
                     destMCFI.addTemp(srcForCast);
-                    
+
                     target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
                          dest->getParent()->getParent(),
-                         leftVal, (TmpInstruction*) srcForCast,
+                         leftVal, cast<Instruction>(srcForCast),
                          mvec, destMCFI);
                   }
                 else
                   srcForCast = leftVal;
-                
+
                 M = new MachineInstr(opCode);
                 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
                                            srcForCast);
@@ -1973,17 +1901,11 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         break;
       }    
 
-      case 43: // boolreg: VReg
-      case 44: // boolreg: Constant
-        break;
-
       case 51: // reg:   Load(reg)
       case 52: // reg:   Load(ptrreg)
-      case 53: // reg:   LoadIdx(reg,reg)
-      case 54: // reg:   LoadIdx(ptrreg,reg)
         mvec.push_back(new MachineInstr(ChooseLoadInstruction(
                                      subtreeRoot->getValue()->getType())));
-        SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
+        SetOperandsForMemInstr(mvec, subtreeRoot, target);
         break;
 
       case 55: // reg:   GetElemPtr(reg)
@@ -1991,20 +1913,20 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         // If the GetElemPtr was folded into the user (parent), it will be
         // caught above.  For other cases, we have to compute the address.
         mvec.push_back(new MachineInstr(ADD));
-        SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
+        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
@@ -2014,7 +1936,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();
         
@@ -2031,13 +1953,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
@@ -2045,34 +1967,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 =
@@ -2107,7 +2017,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);   
@@ -2156,9 +2066,8 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
         Instruction* shlInstr = subtreeRoot->getInstruction();
         
         const Type* opType = argVal1->getType();
-        assert(opType->isIntegral()
-               || opType == Type::BoolTy
-               || isa<PointerType>(opType)&&"Shl unsupported for other types");
+        assert((opType->isInteger() || isa<PointerType>(opType)) &&
+               "Shl unsupported for other types");
         
         CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
                                 (opType == Type::LongTy)? SLLX : SLL,
@@ -2169,8 +2078,8 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
       
       case 63: // reg:   Shr(reg, reg)
       { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
-        assert(opType->isIntegral()
-               || isa<PointerType>(opType)&&"Shr unsupported for other types");
+        assert((opType->isInteger() || isa<PointerType>(opType)) &&
+               "Shr unsupported for other types");
         mvec.push_back(new MachineInstr((opType->isSigned()
                                    ? ((opType == Type::LongTy)? SRAX : SRA)
                                    : ((opType == Type::LongTy)? SRLX : SRL))));
@@ -2218,10 +2127,10 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
       // we need to clear high bits of result value.
       assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
       Instruction* dest = subtreeRoot->getInstruction();
-      if (! dest->getType()->isSigned())
+      if (dest->getType()->isUnsigned())
         {
           unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
-          if (destSize < target.DataLayout.getIntegerRegize())
+          if (destSize <= 4)
             { // Mask high bits.  Use a TmpInstruction to represent the
               // intermediate result before masking.  Since those instructions
               // have already been generated, go back and substitute tmpI
@@ -2233,12 +2142,11 @@ GetInstructionsByRule(InstructionNode* subtreeRoot,
               for (unsigned i=0, N=mvec.size(); i < N; ++i)
                 mvec[i]->substituteValue(dest, tmpI);
 
-              M = Create3OperandInstr(AND, tmpI,
-                                      ConstantUInt::get(Type::ULongTy,
-                                              ((uint64_t) 1 << 8*destSize) - 1),
-                                      dest);
+              M = Create3OperandInstr_UImmed(SRL, tmpI, 8*(4-destSize), dest);
               mvec.push_back(M);
             }
+          else if (destSize < target.DataLayout.getIntegerRegize())
+            assert(0 && "Unsupported type size: 32 < size < 64 bits");
         }
     }
 }