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,
46 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
47 return ConstantBool::get(&V1 == &V2);
50 inline ConstantBool *operator!=(const Constant &V1,
52 return ConstantBool::get(&V1 != &V2);
55 //===----------------------------------------------------------------------===//
56 // Implement all other operators indirectly through TypeRules system
57 //===----------------------------------------------------------------------===//
59 class ConstRules : public Annotation {
61 inline ConstRules() : Annotation(AID) {} // Can only be subclassed...
63 static AnnotationID AID; // AnnotationID for this class
66 virtual Constant *op_not(const Constant *V) const = 0;
68 // Binary Operators...
69 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
70 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
71 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
72 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
73 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
74 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
75 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
76 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
77 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
78 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
80 virtual ConstantBool *lessthan(const Constant *V1,
81 const Constant *V2) const = 0;
83 // Casting operators. ick
84 virtual ConstantBool *castToBool (const Constant *V) const = 0;
85 virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
86 virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
87 virtual ConstantSInt *castToShort (const Constant *V) const = 0;
88 virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
89 virtual ConstantSInt *castToInt (const Constant *V) const = 0;
90 virtual ConstantUInt *castToUInt (const Constant *V) const = 0;
91 virtual ConstantSInt *castToLong (const Constant *V) const = 0;
92 virtual ConstantUInt *castToULong (const Constant *V) const = 0;
93 virtual ConstantFP *castToFloat (const Constant *V) const = 0;
94 virtual ConstantFP *castToDouble(const Constant *V) const = 0;
95 virtual ConstantPointer *castToPointer(const Constant *V,
96 const PointerType *Ty) const = 0;
98 inline Constant *castTo(const Constant *V, const Type *Ty) const {
99 switch (Ty->getPrimitiveID()) {
100 case Type::BoolTyID: return castToBool(V);
101 case Type::UByteTyID: return castToUByte(V);
102 case Type::SByteTyID: return castToSByte(V);
103 case Type::UShortTyID: return castToUShort(V);
104 case Type::ShortTyID: return castToShort(V);
105 case Type::UIntTyID: return castToUInt(V);
106 case Type::IntTyID: return castToInt(V);
107 case Type::ULongTyID: return castToULong(V);
108 case Type::LongTyID: return castToLong(V);
109 case Type::FloatTyID: return castToFloat(V);
110 case Type::DoubleTyID: return castToDouble(V);
111 case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
116 // ConstRules::get - A type will cache its own type rules if one is needed...
117 // we just want to make sure to hit the cache instead of doing it indirectly,
120 static inline ConstRules *get(const Constant &V) {
121 return (ConstRules*)V.getType()->getOrCreateAnnotation(AID);
124 static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
126 ConstRules(const ConstRules &); // Do not implement
127 ConstRules &operator=(const ConstRules &); // Do not implement
131 inline Constant *operator~(const Constant &V) {
132 return ConstRules::get(V)->op_not(&V);
136 // Standard binary operators...
137 inline Constant *operator+(const Constant &V1, const Constant &V2) {
138 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
139 return ConstRules::get(V1)->add(&V1, &V2);
142 inline Constant *operator-(const Constant &V1, const Constant &V2) {
143 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
144 return ConstRules::get(V1)->sub(&V1, &V2);
147 inline Constant *operator*(const Constant &V1, const Constant &V2) {
148 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
149 return ConstRules::get(V1)->mul(&V1, &V2);
152 inline Constant *operator/(const Constant &V1, const Constant &V2) {
153 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
154 return ConstRules::get(V1)->div(&V1, &V2);
157 inline Constant *operator%(const Constant &V1, const Constant &V2) {
158 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
159 return ConstRules::get(V1)->rem(&V1, &V2);
162 // Logical Operators...
163 inline Constant *operator&(const Constant &V1, const Constant &V2) {
164 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
165 return ConstRules::get(V1)->op_and(&V1, &V2);
168 inline Constant *operator|(const Constant &V1, const Constant &V2) {
169 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
170 return ConstRules::get(V1)->op_or(&V1, &V2);
173 inline Constant *operator^(const Constant &V1, const Constant &V2) {
174 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
175 return ConstRules::get(V1)->op_xor(&V1, &V2);
178 // Shift Instructions...
179 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
180 assert(V1.getType()->isIntegral() && V2.getType() == Type::UByteTy);
181 return ConstRules::get(V1)->shl(&V1, &V2);
184 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
185 assert(V1.getType()->isIntegral() && V2.getType() == Type::UByteTy);
186 return ConstRules::get(V1)->shr(&V1, &V2);
189 inline ConstantBool *operator<(const Constant &V1,
190 const Constant &V2) {
191 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
192 return ConstRules::get(V1)->lessthan(&V1, &V2);
196 //===----------------------------------------------------------------------===//
197 // Implement 'derived' operators based on what we already have...
198 //===----------------------------------------------------------------------===//
200 inline ConstantBool *operator>(const Constant &V1,
201 const Constant &V2) {
205 inline ConstantBool *operator>=(const Constant &V1,
206 const Constant &V2) {
207 return (V1 < V2)->inverted(); // !(V1 < V2)
210 inline ConstantBool *operator<=(const Constant &V1,
211 const Constant &V2) {
212 return (V1 > V2)->inverted(); // !(V1 > V2)
216 //===----------------------------------------------------------------------===//
217 // Implement higher level instruction folding type instructions
218 //===----------------------------------------------------------------------===//
220 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
221 // If successful, the constant result is returned, if not, null is returned.
223 Constant *ConstantFoldInstruction(Instruction *I);
225 // Constant fold various types of instruction...
226 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
227 Constant *ConstantFoldUnaryInstruction(unsigned Opcode, const Constant *V);
228 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
230 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,