Support ICmp/FCmp constant expression reading and writing.
authorReid Spencer <rspencer@reidspencer.com>
Sun, 3 Dec 2006 17:17:02 +0000 (17:17 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sun, 3 Dec 2006 17:17:02 +0000 (17:17 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32160 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bytecode/Reader/Reader.cpp
lib/Bytecode/Writer/Writer.cpp

index d294e9e025e4c3e0b82926191a7e8081ea194ed5..9286279216e39abddc5b5b58052aeb6dcb694e53 100644 (file)
@@ -701,6 +701,8 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
     }
     case Instruction::ICmp:
     case Instruction::FCmp:
+      if (Oprnds.size() != 3)
+        error("Cmp instructions requires 3 operands");
       // These instructions encode the comparison predicate as the 3rd operand.
       Result = CmpInst::create(Instruction::OtherOps(Opcode),
           static_cast<unsigned short>(Oprnds[2]),
@@ -1351,6 +1353,16 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
         ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]);
       if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
       return Result;
+    } else if (Opcode == Instruction::ICmp) {
+      if (ArgVec.size() != 2) 
+        error("Invalid ICmp constant expr arguments");
+      unsigned short pred = read_vbr_uint();
+      return ConstantExpr::getICmp(pred, ArgVec[0], ArgVec[1]);
+    } else if (Opcode == Instruction::FCmp) {
+      if (ArgVec.size() != 2) 
+        error("Invalid FCmp constant expr arguments");
+      unsigned short pred = read_vbr_uint();
+      return ConstantExpr::getFCmp(pred, ArgVec[0], ArgVec[1]);
     } else {                            // All other 2-operand expressions
       Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
       if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
index 9cf61c6ee21539aca0035088b24d4a95c1b7bd2e..127a3bac003ce06a54057c8fc82d75ac896e175c 100644 (file)
@@ -791,6 +791,13 @@ void BytecodeWriter::outputInstruction(const Instruction &I) {
     }
   }
 
+  // In the weird case of the ICmp or FCmp instructions, we need to also put
+  // out the instruction's predicate value. We do that here, after the
+  // instruction's type and operands have been written so we can reuse the
+  // code above.
+  if (const CmpInst* CI = dyn_cast<CmpInst>(&I))
+    output_vbr((unsigned)CI->getPredicate());
+
   // If we weren't handled before here, we either have a large number of
   // operands or a large operand index that we are referring to.
   outputInstructionFormat0(&I, Opcode, Table, Type);