[llvm-commits] [dragonegg] r158262 - in /dragonegg/trunk: include/dragonegg/Trees.h src/ConstantConversion.cpp src/Convert.cpp src/Debug.cpp src/Trees.cpp

Duncan Sands baldrick at free.fr
Sat Jun 9 02:39:18 PDT 2012


Author: baldrick
Date: Sat Jun  9 04:39:18 2012
New Revision: 158262

URL: http://llvm.org/viewvc/llvm-project?rev=158262&view=rev
Log:
Rename getIntegerValue to getAPIntValue.  Enhance it so that the user can
specify a particular bitwidth if they want.

Modified:
    dragonegg/trunk/include/dragonegg/Trees.h
    dragonegg/trunk/src/ConstantConversion.cpp
    dragonegg/trunk/src/Convert.cpp
    dragonegg/trunk/src/Debug.cpp
    dragonegg/trunk/src/Trees.cpp

Modified: dragonegg/trunk/include/dragonegg/Trees.h
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/include/dragonegg/Trees.h?rev=158262&r1=158261&r2=158262&view=diff
==============================================================================
--- dragonegg/trunk/include/dragonegg/Trees.h (original)
+++ dragonegg/trunk/include/dragonegg/Trees.h Sat Jun  9 04:39:18 2012
@@ -92,9 +92,14 @@
   return !TYPE_UNSIGNED(type) && TYPE_OVERFLOW_UNDEFINED(type);
 }
 
-/// getIntegerValue - Return the specified INTEGER_CST as an APInt (the bitwidth
-/// is the precision of the constant's type, aka TYPE_PRECISION).
-llvm::APInt getIntegerValue(const_tree exp);
+/// getAPIntValue - Return the specified INTEGER_CST as an APInt.  The default
+/// bitwidth used for the result is the precision of the constant's type, aka
+/// TYPE_PRECISION.  If a larger bitwidth is specified then the value is sign-
+/// or zero-extended to the larger size, following the signedness of the type.
+/// If a smaller bitwidth is specified then the value is truncated.  This will
+/// however result in an error if truncating changes the numerical value, i.e.
+/// the truncated value must sign-/zero-extend to the original.
+llvm::APInt getAPIntValue(const_tree exp, unsigned Bitwidth = 0);
 
 /// isInt64 - Return true if t is an INTEGER_CST that fits in a 64 bit integer.
 /// If Unsigned is false, returns whether it fits in a int64_t.  If Unsigned is

Modified: dragonegg/trunk/src/ConstantConversion.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/src/ConstantConversion.cpp?rev=158262&r1=158261&r2=158262&view=diff
==============================================================================
--- dragonegg/trunk/src/ConstantConversion.cpp (original)
+++ dragonegg/trunk/src/ConstantConversion.cpp Sat Jun  9 04:39:18 2012
@@ -1626,7 +1626,7 @@
 
   // Convert to a byte pointer and displace by the offset.
   Addr = Folder.CreateBitCast(Addr, GetUnitPointerType(Context));
-  APInt Delta = getIntegerValue(TREE_OPERAND(exp, 1));
+  APInt Delta = getAPIntValue(TREE_OPERAND(exp, 1));
   Constant *Offset = ConstantInt::get(Context, Delta);
   // The address is always inside the referenced object, so "inbounds".
   return Folder.CreateInBoundsGetElementPtr(Addr, Offset);

Modified: dragonegg/trunk/src/Convert.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/src/Convert.cpp?rev=158262&r1=158261&r2=158262&view=diff
==============================================================================
--- dragonegg/trunk/src/Convert.cpp (original)
+++ dragonegg/trunk/src/Convert.cpp Sat Jun  9 04:39:18 2012
@@ -318,17 +318,8 @@
 
   unsigned BitWidth = GET_MODE_BITSIZE(TYPE_MODE(type));
 
-  APInt Lo = getIntegerValue(min);
-  assert(Lo.getBitWidth() <= BitWidth && "Min value doesn't fit in type!");
-  if (Lo.getBitWidth() != BitWidth)
-    Lo = TYPE_UNSIGNED(TREE_TYPE(min)) ?
-      Lo.zext(BitWidth) : Lo.sext(BitWidth);
-
-  APInt Hi = getIntegerValue(max);
-  assert(Hi.getBitWidth() <= BitWidth && "Max value doesn't fit in type!");
-  if (Hi.getBitWidth() != BitWidth)
-    Hi = TYPE_UNSIGNED(TREE_TYPE(max)) ?
-      Hi.zext(BitWidth) : Hi.sext(BitWidth);
+  APInt Lo = getAPIntValue(min, BitWidth);
+  APInt Hi = getAPIntValue(max, BitWidth);
 
   // Unlike GCC's, LLVM ranges do not include the upper end point.
   ++Hi;
@@ -6195,7 +6186,7 @@
   if (!integer_zerop(TREE_OPERAND(exp, 1))) {
     // Convert to a byte pointer and displace by the offset.
     Addr = Builder.CreateBitCast(Addr, GetUnitPointerType(Context));
-    APInt Offset = getIntegerValue(TREE_OPERAND(exp, 1));
+    APInt Offset = getAPIntValue(TREE_OPERAND(exp, 1));
     // The address is always inside the referenced object, so "inbounds".
     Addr = Builder.CreateInBoundsGEP(Addr, ConstantInt::get(Context, Offset));
   }
@@ -6302,7 +6293,7 @@
   }
 
   if (TMR_OFFSET(exp) && !integer_zerop (TMR_OFFSET(exp))) {
-    Constant *Off = ConstantInt::get(Context, getIntegerValue(TMR_OFFSET(exp)));
+    Constant *Off = ConstantInt::get(Context, getAPIntValue(TMR_OFFSET(exp)));
     Delta = Delta ? Builder.CreateAdd(Delta, Off) : Off;
   }
 
@@ -6452,7 +6443,7 @@
 /// EmitIntegerRegisterConstant - Turn the given INTEGER_CST into an LLVM
 /// constant of the corresponding register type.
 Constant *TreeToLLVM::EmitIntegerRegisterConstant(tree reg) {
-  ConstantInt *CI = ConstantInt::get(Context, getIntegerValue(reg));
+  ConstantInt *CI = ConstantInt::get(Context, getAPIntValue(reg));
   // The destination can be a pointer, integer or floating point type so we need
   // a generalized cast here
   Type *Ty = getRegType(TREE_TYPE(reg));

Modified: dragonegg/trunk/src/Debug.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/src/Debug.cpp?rev=158262&r1=158261&r2=158262&view=diff
==============================================================================
--- dragonegg/trunk/src/Debug.cpp (original)
+++ dragonegg/trunk/src/Debug.cpp Sat Jun  9 04:39:18 2012
@@ -677,7 +677,7 @@
       tree EnumValue = TREE_VALUE(Link);
       if (isa<CONST_DECL>(EnumValue))
         EnumValue = DECL_INITIAL(EnumValue);
-      uint64_t Value = getIntegerValue(EnumValue).getZExtValue();
+      uint64_t Value = getAPIntValue(EnumValue, 64).getZExtValue();
       const char *EnumName = IDENTIFIER_POINTER(TREE_PURPOSE(Link));
       Elements.push_back(DebugFactory.CreateEnumerator(EnumName, Value));
     }

Modified: dragonegg/trunk/src/Trees.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/src/Trees.cpp?rev=158262&r1=158261&r2=158262&view=diff
==============================================================================
--- dragonegg/trunk/src/Trees.cpp (original)
+++ dragonegg/trunk/src/Trees.cpp Sat Jun  9 04:39:18 2012
@@ -136,20 +136,44 @@
   return std::string();
 }
 
-/// getIntegerValue - Return the specified INTEGER_CST as an APInt.
-APInt getIntegerValue(const_tree exp) {
+/// getAPIntValue - Return the specified INTEGER_CST as an APInt.  The default
+/// bitwidth used for the result is the precision of the constant's type, aka
+/// TYPE_PRECISION.  If a larger bitwidth is specified then the value is sign-
+/// or zero-extended to the larger size, following the signedness of the type.
+/// If a smaller bitwidth is specified then the value is truncated.  This will
+/// however result in an error if truncating changes the numerical value, i.e.
+/// the truncated value must sign-/zero-extend to the original.
+APInt getAPIntValue(const_tree exp, unsigned Bitwidth) {
   assert(isa<INTEGER_CST>(exp) && "Expected an integer constant!");
   double_int val = tree_to_double_int(exp);
-  unsigned NumBits = TYPE_PRECISION(TREE_TYPE(exp));
+  unsigned DefaultWidth = TYPE_PRECISION(TREE_TYPE(exp));
 
-  if (integerPartWidth == HOST_BITS_PER_WIDE_INT)
-    return APInt(NumBits, /*numWords*/2, (integerPart*)&val);
-  assert(integerPartWidth == 2 * HOST_BITS_PER_WIDE_INT &&
-         "Unsupported host integer width!");
-  unsigned ShiftAmt = HOST_BITS_PER_WIDE_INT;
-  integerPart Part = integerPart((unsigned HOST_WIDE_INT)val.low) +
-    (integerPart((unsigned HOST_WIDE_INT)val.high) << ShiftAmt);
-  return APInt(NumBits, Part);
+  APInt DefaultValue;
+  if (integerPartWidth == HOST_BITS_PER_WIDE_INT) {
+    DefaultValue = APInt(DefaultWidth, /*numWords*/2, (integerPart*)&val);
+  } else {
+    assert(integerPartWidth == 2 * HOST_BITS_PER_WIDE_INT &&
+           "Unsupported host integer width!");
+    unsigned ShiftAmt = HOST_BITS_PER_WIDE_INT;
+    integerPart Part = integerPart((unsigned HOST_WIDE_INT)val.low) +
+      (integerPart((unsigned HOST_WIDE_INT)val.high) << ShiftAmt);
+    DefaultValue = APInt(DefaultWidth, Part);
+  }
+
+  if (!Bitwidth || Bitwidth == DefaultWidth)
+    return DefaultValue;
+
+  if (Bitwidth > DefaultWidth)
+    return TYPE_UNSIGNED(TREE_TYPE(exp)) ?
+      DefaultValue.zext(Bitwidth) : DefaultValue.sext(Bitwidth);
+
+  assert((TYPE_UNSIGNED(TREE_TYPE(exp)) ||
+          DefaultValue.trunc(Bitwidth).sext(DefaultWidth) == DefaultValue) &&
+         "Truncating changed signed value!");
+  assert((!TYPE_UNSIGNED(TREE_TYPE(exp)) ||
+          DefaultValue.trunc(Bitwidth).zext(DefaultWidth) == DefaultValue) &&
+         "Truncating changed unsigned value!");
+  return DefaultValue.trunc(Bitwidth);
 }
 
 /// isInt64 - Return true if t is an INTEGER_CST that fits in a 64 bit integer.





More information about the llvm-commits mailing list