* Switch over to cleaner TmpInstruction model
[oota-llvm.git] / lib / CodeGen / InstrSelection / InstrSelectionSupport.cpp
index d43e688dd762f7cdf9eabecf5326a9aade7a3335..ff655fc98da6ee089cdf61753de9010fcbb79bdb 100644 (file)
 #include "llvm/CodeGen/InstrSelectionSupport.h"
 #include "llvm/CodeGen/InstrSelection.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineCodeForInstruction.h"
+#include "llvm/CodeGen/MachineCodeForMethod.h"
+#include "llvm/CodeGen/InstrForest.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/MachineRegInfo.h"
-#include "llvm/ConstPoolVals.h"
+#include "llvm/ConstantVals.h"
 #include "llvm/Method.h"
 #include "llvm/BasicBlock.h"
-#include "llvm/Instruction.h"
 #include "llvm/Type.h"
 #include "llvm/iMemory.h"
-
+using std::vector;
 
 //*************************** Local Functions ******************************/
 
@@ -36,16 +38,16 @@ InsertCodeToLoadConstant(Value* opValue,
   vector<TmpInstruction*> tempVec;
   
   // Create a tmp virtual register to hold the constant.
-  TmpInstruction* tmpReg =
-    new TmpInstruction(TMP_INSTRUCTION_OPCODE, opValue, NULL);
-  vmInstr->getMachineInstrVec().addTempValue(tmpReg);
+  TmpInstruction* tmpReg = new TmpInstruction(opValue);
+  MachineCodeForInstruction &MCFI = MachineCodeForInstruction::get(vmInstr);
+  MCFI.addTemp(tmpReg);
   
   target.getInstrInfo().CreateCodeToLoadConst(opValue, tmpReg,
                                               loadConstVec, tempVec);
   
   // Register the new tmp values created for this m/c instruction sequence
   for (unsigned i=0; i < tempVec.size(); i++)
-    vmInstr->getMachineInstrVec().addTempValue(tempVec[i]);
+    MCFI.addTemp(tempVec[i]);
   
   // Record the mapping from the tmp VM instruction to machine instruction.
   // Do this for all machine instructions that were not mapped to any
@@ -73,7 +75,7 @@ int64_t
 GetConstantValueAsSignedInt(const Value *V,
                             bool &isValidConstant)
 {
-  if (!isa<ConstPoolVal>(V))
+  if (!isa<Constant>(V))
     {
       isValidConstant = false;
       return 0;
@@ -82,15 +84,15 @@ GetConstantValueAsSignedInt(const Value *V,
   isValidConstant = true;
   
   if (V->getType() == Type::BoolTy)
-    return (int64_t) ((ConstPoolBool*)V)->getValue();
+    return (int64_t) cast<ConstantBool>(V)->getValue();
   
   if (V->getType()->isIntegral())
     {
       if (V->getType()->isSigned())
-        return ((ConstPoolSInt*)V)->getValue();
+        return cast<ConstantSInt>(V)->getValue();
       
       assert(V->getType()->isUnsigned());
-      uint64_t Val = ((ConstPoolUInt*)V)->getValue();
+      uint64_t Val = cast<ConstantUInt>(V)->getValue();
       if (Val < INT64_MAX)     // then safe to cast to signed
         return (int64_t)Val;
     }
@@ -111,14 +113,14 @@ GetConstantValueAsSignedInt(const Value *V,
 
 Value*
 FoldGetElemChain(const InstructionNode* getElemInstrNode,
-                vector<ConstPoolVal*>& chainIdxVec)
+                vector<Value*>& chainIdxVec)
 {
   MemAccessInst* getElemInst = (MemAccessInst*)
     getElemInstrNode->getInstruction();
   
   // Initialize return values from the incoming instruction
   Value* ptrVal = getElemInst->getPointerOperand();
-  chainIdxVec = getElemInst->getIndicesBROKEN(); // copies index vector values
+  chainIdxVec = getElemInst->copyIndices();
   
   // Now chase the chain of getElementInstr instructions, if any
   InstrTreeNode* ptrChild = getElemInstrNode->leftChild();
@@ -128,7 +130,7 @@ FoldGetElemChain(const InstructionNode* getElemInstrNode,
       // Child is a GetElemPtr instruction
       getElemInst = (MemAccessInst*)
        ((InstructionNode*) ptrChild)->getInstruction();
-      const vector<ConstPoolVal*>& idxVec = getElemInst->getIndicesBROKEN();
+      const vector<Value*>& idxVec = getElemInst->copyIndices();
       
       // Get the pointer value out of ptrChild and *prepend* its index vector
       ptrVal = getElemInst->getPointerOperand();
@@ -225,12 +227,11 @@ ChooseRegOrImmed(Value* val,
   
   // Check for the common case first: argument is not constant
   // 
-  ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(val);
+  Constant *CPV = dyn_cast<Constant>(val);
   if (!CPV) return opType;
 
-  if (CPV->getType() == Type::BoolTy)
+  if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV))
     {
-      ConstPoolBool *CPB = (ConstPoolBool*)CPV;
       if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0)
        {
          getMachineRegNum = target.getRegInfo().getZeroRegNum();
@@ -259,11 +260,11 @@ ChooseRegOrImmed(Value* val,
     }
   else if (CPV->getType()->isSigned())
     {
-      intValue = ((ConstPoolSInt*)CPV)->getValue();
+      intValue = cast<ConstantSInt>(CPV)->getValue();
     }
   else
     {
-      uint64_t V = ((ConstPoolUInt*)CPV)->getValue();
+      uint64_t V = cast<ConstantUInt>(CPV)->getValue();
       if (V >= INT64_MAX) return opType;
       intValue = (int64_t)V;
     }
@@ -327,8 +328,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
       Value* opValue = mop.getVRegValue();
       bool constantThatMustBeLoaded = false;
       
-      if (isa<ConstPoolVal>(opValue))
-        {
+      if (Constant *OpConst = dyn_cast<Constant>(opValue)) {
           unsigned int machineRegNum;
           int64_t immedValue;
           MachineOperand::MachineOperandType opType =
@@ -345,8 +345,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
 
           if (constantThatMustBeLoaded)
             { // register the value so it is emitted in the assembly
-              MachineCodeForMethod::get(method).addToConstantPool(
-                                                 cast<ConstPoolVal>(opValue));
+              MachineCodeForMethod::get(method).addToConstantPool(OpConst);
             }
         }
       
@@ -370,7 +369,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
   // into a register.
   // 
   for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
-    if (isa<ConstPoolVal>(minstr->getImplicitRef(i)) ||
+    if (isa<Constant>(minstr->getImplicitRef(i)) ||
         isa<GlobalValue>(minstr->getImplicitRef(i)))
       {
         Value* oldVal = minstr->getImplicitRef(i);
@@ -378,10 +377,9 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
           InsertCodeToLoadConstant(oldVal, vmInstr, loadConstVec, target);
         minstr->setImplicitRef(i, tmpReg);
         
-        if (isa<ConstPoolVal>(oldVal))
+        if (Constant *C = dyn_cast<Constant>(oldVal))
           { // register the value so it is emitted in the assembly
-            MachineCodeForMethod::get(method).addToConstantPool(
-                                               cast<ConstPoolVal>(oldVal));
+            MachineCodeForMethod::get(method).addToConstantPool(C);
           }
       }