1 //===-- ConstantHandling.h - Stuff for manipulating constants ----*- C++ -*--=//
3 // This file contains the declarations of some cool operators that allow you
4 // to do natural things with constant pool values.
6 // Unfortunately we can't overload operators on pointer types (like this:)
8 // inline bool operator==(const Constant *V1, const Constant *V2)
10 // so we must make due with references, even though it leads to some butt ugly
11 // looking code downstream. *sigh* (ex: Constant *Result = *V1 + *v2; )
13 //===----------------------------------------------------------------------===//
15 // WARNING: These operators may return a null object if I don't know how to
16 // perform the specified operation on the specified constant types.
18 //===----------------------------------------------------------------------===//
20 // Implementation notes:
21 // This library is implemented this way for a reason: In most cases, we do
22 // not want to have to link the constant mucking code into an executable.
23 // We do, however want to tie some of this into the main type system, as an
24 // optional component. By using a mutable cache member in the Type class, we
25 // get exactly the kind of behavior we want.
27 // In the end, we get performance almost exactly the same as having a virtual
28 // function dispatch, but we don't have to put our virtual functions into the
29 // "Type" class, and we can implement functionality with templates. Good deal.
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_CONSTANTHANDLING_H
34 #define LLVM_CONSTANTHANDLING_H
36 #include "llvm/Constants.h"
37 #include "llvm/Type.h"
40 //===----------------------------------------------------------------------===//
41 // Implement == and != directly...
42 //===----------------------------------------------------------------------===//
44 inline ConstantBool *operator==(const Constant &V1, const Constant &V2) {
45 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
46 return ConstantBool::get(&V1 == &V2);
49 inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) {
50 return ConstantBool::get(&V1 != &V2);
53 //===----------------------------------------------------------------------===//
54 // Implement all other operators indirectly through TypeRules system
55 //===----------------------------------------------------------------------===//
57 class ConstRules : public Annotation {
59 inline ConstRules() : Annotation(AID) {} // Can only be subclassed...
61 static AnnotationID AID; // AnnotationID for this class
63 // Binary Operators...
64 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
65 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
66 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
67 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
68 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
69 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
70 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
71 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
72 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
73 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
75 virtual ConstantBool *lessthan(const Constant *V1,
76 const Constant *V2) const = 0;
78 // Casting operators. ick
79 virtual ConstantBool *castToBool (const Constant *V) const = 0;
80 virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
81 virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
82 virtual ConstantSInt *castToShort (const Constant *V) const = 0;
83 virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
84 virtual ConstantSInt *castToInt (const Constant *V) const = 0;
85 virtual ConstantUInt *castToUInt (const Constant *V) const = 0;
86 virtual ConstantSInt *castToLong (const Constant *V) const = 0;
87 virtual ConstantUInt *castToULong (const Constant *V) const = 0;
88 virtual ConstantFP *castToFloat (const Constant *V) const = 0;
89 virtual ConstantFP *castToDouble(const Constant *V) const = 0;
90 virtual Constant *castToPointer(const Constant *V,
91 const PointerType *Ty) const = 0;
93 inline Constant *castTo(const Constant *V, const Type *Ty) const {
94 switch (Ty->getPrimitiveID()) {
95 case Type::BoolTyID: return castToBool(V);
96 case Type::UByteTyID: return castToUByte(V);
97 case Type::SByteTyID: return castToSByte(V);
98 case Type::UShortTyID: return castToUShort(V);
99 case Type::ShortTyID: return castToShort(V);
100 case Type::UIntTyID: return castToUInt(V);
101 case Type::IntTyID: return castToInt(V);
102 case Type::ULongTyID: return castToULong(V);
103 case Type::LongTyID: return castToLong(V);
104 case Type::FloatTyID: return castToFloat(V);
105 case Type::DoubleTyID: return castToDouble(V);
106 case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
111 // ConstRules::get - A type will cache its own type rules if one is needed...
112 // we just want to make sure to hit the cache instead of doing it indirectly,
115 static inline ConstRules *get(const Constant &V1, const Constant &V2) {
116 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2))
117 return getConstantExprRules();
118 return (ConstRules*)V1.getType()->getOrCreateAnnotation(AID);
121 static ConstRules *getConstantExprRules();
122 static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
124 ConstRules(const ConstRules &); // Do not implement
125 ConstRules &operator=(const ConstRules &); // Do not implement
128 // Unary operators...
129 inline Constant *operator~(const Constant &V) {
130 assert(V.getType()->isIntegral() && "Cannot invert non-intergral constant!");
131 return ConstRules::get(V, V)->op_xor(&V,
132 ConstantInt::getAllOnesValue(V.getType()));
135 // Standard binary operators...
136 inline Constant *operator+(const Constant &V1, const Constant &V2) {
137 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
138 return ConstRules::get(V1, V2)->add(&V1, &V2);
141 inline Constant *operator-(const Constant &V1, const Constant &V2) {
142 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
143 return ConstRules::get(V1, V2)->sub(&V1, &V2);
146 inline Constant *operator*(const Constant &V1, const Constant &V2) {
147 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
148 return ConstRules::get(V1, V2)->mul(&V1, &V2);
151 inline Constant *operator/(const Constant &V1, const Constant &V2) {
152 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
153 return ConstRules::get(V1, V2)->div(&V1, &V2);
156 inline Constant *operator%(const Constant &V1, const Constant &V2) {
157 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
158 return ConstRules::get(V1, V2)->rem(&V1, &V2);
161 // Logical Operators...
162 inline Constant *operator&(const Constant &V1, const Constant &V2) {
163 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
164 return ConstRules::get(V1, V2)->op_and(&V1, &V2);
167 inline Constant *operator|(const Constant &V1, const Constant &V2) {
168 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
169 return ConstRules::get(V1, V2)->op_or(&V1, &V2);
172 inline Constant *operator^(const Constant &V1, const Constant &V2) {
173 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
174 return ConstRules::get(V1, V2)->op_xor(&V1, &V2);
177 // Shift Instructions...
178 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
179 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
180 return ConstRules::get(V1, V2)->shl(&V1, &V2);
183 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
184 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
185 return ConstRules::get(V1, V2)->shr(&V1, &V2);
188 inline ConstantBool *operator<(const Constant &V1,
189 const Constant &V2) {
190 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
191 return ConstRules::get(V1, V2)->lessthan(&V1, &V2);
195 //===----------------------------------------------------------------------===//
196 // Implement 'derived' operators based on what we already have...
197 //===----------------------------------------------------------------------===//
199 inline ConstantBool *operator>(const Constant &V1,
200 const Constant &V2) {
204 inline ConstantBool *operator>=(const Constant &V1,
205 const Constant &V2) {
206 if (ConstantBool *V = (V1 < V2))
207 return V->inverted(); // !(V1 < V2)
211 inline ConstantBool *operator<=(const Constant &V1,
212 const Constant &V2) {
213 if (ConstantBool *V = (V1 > V2))
214 return V->inverted(); // !(V1 > V2)
219 //===----------------------------------------------------------------------===//
220 // Implement higher level instruction folding type instructions
221 //===----------------------------------------------------------------------===//
223 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
224 // If successful, the constant result is returned, if not, null is returned.
226 Constant *ConstantFoldInstruction(Instruction *I);
228 // Constant fold various types of instruction...
229 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
230 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
232 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
234 Constant *ConstantFoldGetElementPtr(const Constant *C,
235 const std::vector<Constant*> &IdxList);