[llvm-commits] CVS: llvm/lib/ExecutionEngine/Interpreter/Execution.cpp

Reid Spencer reid at x10sys.com
Sat Jan 20 12:12:45 PST 2007



Changes in directory llvm/lib/ExecutionEngine/Interpreter:

Execution.cpp updated: 1.163 -> 1.164
---
Log message:

Implement the signed icmp instructions properly. To do this we introduce
a small inline function to sign extend a uint64_t value based on its 
type's bitwidth. This function is then used in both executeSExtInst and
the various executeICMP_S** functions.


---
Diffs of the changes:  (+27 -17)

 Execution.cpp |   44 +++++++++++++++++++++++++++-----------------
 1 files changed, 27 insertions(+), 17 deletions(-)


Index: llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
diff -u llvm/lib/ExecutionEngine/Interpreter/Execution.cpp:1.163 llvm/lib/ExecutionEngine/Interpreter/Execution.cpp:1.164
--- llvm/lib/ExecutionEngine/Interpreter/Execution.cpp:1.163	Sat Jan 20 02:32:52 2007
+++ llvm/lib/ExecutionEngine/Interpreter/Execution.cpp	Sat Jan 20 14:12:29 2007
@@ -67,6 +67,15 @@
 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
                                       GenericValue Src3);
 
+inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
+  // Determine if the value is signed or not
+  bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
+  // If its signed, extend the sign bits
+  if (isSigned)
+    Val |= ~ITy->getBitMask();
+  return Val;
+}
+
 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
                                                 ExecutionContext &SF) {
   switch (CE->getOpcode()) {
@@ -385,22 +394,26 @@
 
 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
    case Type::IntegerTyID: {  \
-     unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
-     if (BitWidth == 1) \
-       Dest.Int1Val = ((int8_t)Src1.Int1Val)   OP ((int8_t)Src2.Int1Val); \
-     else if (BitWidth <= 8) \
-       Dest.Int1Val = ((int8_t)Src1.Int8Val)   OP ((int8_t)Src2.Int8Val); \
-     else if (BitWidth <= 16) \
-       Dest.Int1Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
-     else if (BitWidth <= 32) \
-       Dest.Int1Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
-     else if (BitWidth <= 64) \
-       Dest.Int1Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
-     else { \
+     const IntegerType* ITy = cast<IntegerType>(TY); \
+     unsigned BitWidth = ITy->getBitWidth(); \
+     int64_t LHS = 0, RHS = 0; \
+     if (BitWidth <= 8) { \
+       LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
+       RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
+     } else if (BitWidth <= 16) { \
+       LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
+       RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
+    } else if (BitWidth <= 32) { \
+       LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
+       RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
+    } else if (BitWidth <= 64) { \
+       LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
+       RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
+    } else { \
       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
        abort(); \
      } \
-     maskToBitWidth(Dest, BitWidth); \
+     Dest.Int1Val = LHS OP RHS; \
      break; \
    }
 
@@ -1359,10 +1372,7 @@
   else 
     Normalized = Src.Int64Val;
 
-  // Now do the bit-accurate sign extension manually.
-  bool isSigned = (Normalized & (1 << (SBitWidth-1))) != 0;
-  if (isSigned)
-    Normalized |= ~SITy->getBitMask();
+  Normalized = doSignExtension(Normalized, SITy);
 
   // Now that we have a sign extended value, assign it to the destination
   INTEGER_ASSIGN(Dest, DBitWidth, Normalized);






More information about the llvm-commits mailing list