1 //===-- llvm/ConstPoolVals.cpp - Implement Constant Value nodes -*- C++ -*--=//
3 // This file implements the ConstPoolVal class and associated functions.
5 //===---------------------------------------------------------------------===//
8 #include "llvm/Value.h"
9 #include "llvm/ConstPoolVals.h"
12 //===--------------------------------------------------------------------------
16 // Convenience functions to get the value of an integer constant, for an
17 // appropriate integer or non-integer type that can be held in an integer.
18 // The type of the argument must be the following:
19 // GetSignedIntConstantValue: signed integer or bool
20 // GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
21 // GetConstantValueAsSignedInt: any of the above, but the value
22 // must fit into a int64_t.
24 // isValidConstant is set to true if a valid constant was found.
28 GetSignedIntConstantValue(const Value* val, bool& isValidConstant)
31 isValidConstant = false;
33 if (val->getValueType() == Value::ConstantVal)
35 switch(val->getType()->getPrimitiveID())
38 intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
39 isValidConstant = true;
45 intValue = ((ConstPoolSInt*) val)->getValue();
46 isValidConstant = true;
57 GetUnsignedIntConstantValue(const Value* val, bool& isValidConstant)
59 uint64_t intValue = 0;
60 isValidConstant = false;
62 if (val->getValueType() == Value::ConstantVal)
64 switch(val->getType()->getPrimitiveID())
67 intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
68 isValidConstant = true;
71 case Type::UShortTyID:
74 case Type::PointerTyID:
75 intValue = ((ConstPoolUInt*) val)->getValue();
76 isValidConstant = true;
88 GetConstantValueAsSignedInt(const Value* val, bool& isValidConstant)
92 if (val->getType()->isSigned())
94 intValue = GetSignedIntConstantValue(val, isValidConstant);
96 else // non-numeric types will fall here
98 uint64_t uintValue = GetUnsignedIntConstantValue(val, isValidConstant);
99 if (isValidConstant && uintValue < INT64_MAX) // then safe to cast to signed
100 intValue = (int64_t) uintValue;
102 isValidConstant = false;
108 //===--------------------------------------------------------------------------