Refactor the bitcast code into its own function.
authorDan Gohman <gohman@apple.com>
Tue, 26 Aug 2008 21:28:54 +0000 (21:28 +0000)
committerDan Gohman <gohman@apple.com>
Tue, 26 Aug 2008 21:28:54 +0000 (21:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55387 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/FastISel.h
lib/CodeGen/SelectionDAG/FastISel.cpp

index b990ecdd9ff08e5637b11708d8c45ad7d68d771a..591c8bde30ebbf1cdf2835c2e450a373b055ce0d 100644 (file)
@@ -167,6 +167,9 @@ private:
 
   bool SelectGetElementPtr(Instruction *I,
                            DenseMap<const Value*, unsigned> &ValueMap);
+
+  bool SelectBitCast(Instruction *I,
+                     DenseMap<const Value*, unsigned> &ValueMap);
 };
 
 }
index 4d4e1b8c2176c0090fdf5b53649bf17a34e0b222..80ccc52365c4a52135431598907290513585bf5e 100644 (file)
@@ -149,6 +149,72 @@ bool FastISel::SelectGetElementPtr(Instruction *I,
   return true;
 }
 
+bool FastISel::SelectBitCast(Instruction *I,
+                             DenseMap<const Value*, unsigned> &ValueMap) {
+  // BitCast consists of either an immediate to register move
+  // or a register to register move.
+  if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
+    if (I->getType()->isInteger()) {
+      MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
+      unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
+                                   ISD::Constant,
+                                   CI->getZExtValue());
+      if (!result)
+        return false;
+      
+      ValueMap[I] = result;
+      return true;
+    }
+
+    // TODO: Support vector and fp constants.
+    return false;
+  }
+
+  if (!isa<Constant>(I->getOperand(0))) {
+    // Bitcasts of non-constant values become reg-reg copies.
+    MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
+    MVT DstVT = MVT::getMVT(I->getType());
+    
+    if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
+        DstVT == MVT::Other || !DstVT.isSimple() ||
+        !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
+      // Unhandled type. Halt "fast" selection and bail.
+      return false;
+    
+    unsigned Op0 = ValueMap[I->getOperand(0)];
+    if (Op0 == 0)
+      // Unhandled operand. Halt "fast" selection and bail.
+      return false;
+    
+    // First, try to perform the bitcast by inserting a reg-reg copy.
+    unsigned ResultReg = 0;
+    if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
+      TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
+      TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
+      ResultReg = createResultReg(DstClass);
+      
+      bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
+                                           Op0, DstClass, SrcClass);
+      if (!InsertedCopy)
+        ResultReg = 0;
+    }
+    
+    // If the reg-reg copy failed, select a BIT_CONVERT opcode.
+    if (!ResultReg)
+      ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
+                             ISD::BIT_CONVERT, Op0);
+    
+    if (!ResultReg)
+      return false;
+    
+    ValueMap[I] = ResultReg;
+    return true;
+  }
+
+  // TODO: Casting a non-integral constant?
+  return false;
+}
+
 BasicBlock::iterator
 FastISel::SelectInstructions(BasicBlock::iterator Begin,
                              BasicBlock::iterator End,
@@ -231,64 +297,8 @@ FastISel::SelectInstructions(BasicBlock::iterator Begin,
       break;
       
     case Instruction::BitCast:
-      // BitCast consists of either an immediate to register move
-      // or a register to register move.
-      if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
-        if (I->getType()->isInteger()) {
-          MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
-          unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
-                                       ISD::Constant,
-                                       CI->getZExtValue());
-          if (!result)
-            return I;
-          
-          ValueMap[I] = result;
-          break;
-        } else
-          // TODO: Support vector and fp constants.
-          return I;
-      } else if (!isa<Constant>(I->getOperand(0))) {
-        // Bitcasts of non-constant values become reg-reg copies.
-        MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
-        MVT DstVT = MVT::getMVT(I->getType());
-        
-        if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
-            DstVT == MVT::Other || !DstVT.isSimple() ||
-            !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
-          // Unhandled type. Halt "fast" selection and bail.
-          return I;
-        
-        unsigned Op0 = ValueMap[I->getOperand(0)];
-        if (Op0 == 0)
-          // Unhandled operand. Halt "fast" selection and bail.
-          return false;
-        
-        // First, try to perform the bitcast by inserting a reg-reg copy.
-        unsigned ResultReg = 0;
-        if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
-          TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
-          TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
-          ResultReg = createResultReg(DstClass);
-          
-          bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
-                                               Op0, DstClass, SrcClass);
-          if (!InsertedCopy)
-            ResultReg = 0;
-        }
-        
-        // If the reg-reg copy failed, select a BIT_CONVERT opcode.
-        if (!ResultReg)
-          ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
-                                 ISD::BIT_CONVERT, Op0);
-        
-        if (!ResultReg)
-          return I;
-        
-        ValueMap[I] = ResultReg;
-        break;
-      } else
-        // TODO: Casting a non-integral constant?
-        return I;
+      if (!SelectBitCast(I, ValueMap)) return I;
+      break;
 
     case Instruction::FPToSI:
       if (!isa<ConstantFP>(I->getOperand(0))) {
@@ -348,6 +358,7 @@ FastISel::SelectInstructions(BasicBlock::iterator Begin,
       } else
         // TODO: Materialize constant and convert to FP.
         return I;
+
     default:
       // Unhandled instruction. Halt "fast" selection and bail.
       return I;