summaryrefslogtreecommitdiff
path: root/lib/VMCore/ConstPoolVals.cpp
blob: f778a3716c7b29a9e94393f0e84fc88ac4a98593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===-- llvm/ConstPoolVals.cpp - Implement Constant Value nodes -*- C++ -*--=//
//
// This file implements the ConstPoolVal class and associated functions.
//
//===---------------------------------------------------------------------===//

#include "llvm/Type.h"
#include "llvm/Value.h"
#include "llvm/ConstPoolVals.h"


//===--------------------------------------------------------------------------
// External functions
//

// Convenience functions to get the value of an integer constant, for an
// appropriate integer or non-integer type that can be held in an integer.
// The type of the argument must be the following:
//   GetSignedIntConstantValue:   signed integer or bool
//   GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
//   GetConstantValueAsSignedInt: any of the above, but the value
//				  must fit into a int64_t.
// 
// isValidConstant is set to true if a valid constant was found.
// 

int64_t
GetSignedIntConstantValue(const Value* val, bool& isValidConstant)
{
  int64_t intValue = 0;
  isValidConstant = false;
  
  if (val->getValueType() == Value::ConstantVal)
    {
      switch(val->getType()->getPrimitiveID())
	{
	case Type::BoolTyID:
	  intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
	  isValidConstant = true;
	  break;
	case Type::SByteTyID:
	case Type::ShortTyID:
	case Type::IntTyID:
	case Type::LongTyID:
	  intValue = ((ConstPoolSInt*) val)->getValue();
	  isValidConstant = true;
	  break;
	default:
	  break;
	}
    }
  
  return intValue;
}

uint64_t
GetUnsignedIntConstantValue(const Value* val, bool& isValidConstant)
{
  uint64_t intValue = 0;
  isValidConstant = false;
  
  if (val->getValueType() == Value::ConstantVal)
    {
      switch(val->getType()->getPrimitiveID())
	{
	case Type::BoolTyID:
	  intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
	  isValidConstant = true;
	  break;
	case Type::UByteTyID:
	case Type::UShortTyID:
	case Type::UIntTyID:
	case Type::ULongTyID:
	case Type::PointerTyID:
	  intValue = ((ConstPoolUInt*) val)->getValue();
	  isValidConstant = true;
	  break;
	default:
	  break;
	}
    }
  
  return intValue;
}


int64_t
GetConstantValueAsSignedInt(const Value* val, bool& isValidConstant)
{
  int64_t intValue = 0;
  
  if (val->getType()->isSigned())
    {
      intValue = GetSignedIntConstantValue(val, isValidConstant);
    }
  else				// non-numeric types will fall here
    {
      uint64_t uintValue = GetUnsignedIntConstantValue(val, isValidConstant);
      if (isValidConstant && uintValue < INT64_MAX)	// then safe to cast to signed
	intValue = (int64_t) uintValue;
      else 
	isValidConstant = false;
    }
  
  return intValue;
}

//===--------------------------------------------------------------------------