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_OPT_CONSTANTHANDLING_H
34 #define LLVM_OPT_CONSTANTHANDLING_H
36 #include "llvm/ConstantVals.h"
37 #include "llvm/Instruction.h"
38 #include "llvm/Type.h"
41 //===----------------------------------------------------------------------===//
42 // Implement == and != directly...
43 //===----------------------------------------------------------------------===//
45 inline ConstantBool *operator==(const Constant &V1,
47 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
48 return ConstantBool::get(&V1 == &V2);
51 inline ConstantBool *operator!=(const Constant &V1,
53 return ConstantBool::get(&V1 != &V2);
56 //===----------------------------------------------------------------------===//
57 // Implement all other operators indirectly through TypeRules system
58 //===----------------------------------------------------------------------===//
60 class ConstRules : public Annotation {
62 inline ConstRules() : Annotation(AID) {} // Can only be subclassed...
64 static AnnotationID AID; // AnnotationID for this class
67 virtual Constant *op_not(const Constant *V) const = 0;
69 // Binary Operators...
70 virtual Constant *add(const Constant *V1,
71 const Constant *V2) const = 0;
72 virtual Constant *sub(const Constant *V1,
73 const Constant *V2) const = 0;
74 virtual Constant *mul(const Constant *V1,
75 const Constant *V2) const = 0;
77 virtual ConstantBool *lessthan(const Constant *V1,
78 const Constant *V2) const = 0;
80 // Casting operators. ick
81 virtual ConstantBool *castToBool (const Constant *V) const = 0;
82 virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
83 virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
84 virtual ConstantSInt *castToShort (const Constant *V) const = 0;
85 virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
86 virtual ConstantSInt *castToInt (const Constant *V) const = 0;
87 virtual ConstantUInt *castToUInt (const Constant *V) const = 0;
88 virtual ConstantSInt *castToLong (const Constant *V) const = 0;
89 virtual ConstantUInt *castToULong (const Constant *V) const = 0;
90 virtual ConstantFP *castToFloat (const Constant *V) const = 0;
91 virtual ConstantFP *castToDouble(const Constant *V) const = 0;
92 virtual ConstantPointer *castToPointer(const Constant *V,
93 const PointerType *Ty) const = 0;
95 inline Constant *castTo(const Constant *V, const Type *Ty) const {
96 switch (Ty->getPrimitiveID()) {
97 case Type::BoolTyID: return castToBool(V);
98 case Type::UByteTyID: return castToUByte(V);
99 case Type::SByteTyID: return castToSByte(V);
100 case Type::UShortTyID: return castToUShort(V);
101 case Type::ShortTyID: return castToShort(V);
102 case Type::UIntTyID: return castToUInt(V);
103 case Type::IntTyID: return castToInt(V);
104 case Type::ULongTyID: return castToULong(V);
105 case Type::LongTyID: return castToLong(V);
106 case Type::FloatTyID: return castToFloat(V);
107 case Type::DoubleTyID: return castToDouble(V);
108 case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
113 // ConstRules::get - A type will cache its own type rules if one is needed...
114 // we just want to make sure to hit the cache instead of doing it indirectly,
117 static inline ConstRules *get(const Constant &V) {
118 return (ConstRules*)V.getType()->getOrCreateAnnotation(AID);
121 static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
123 ConstRules(const ConstRules &); // Do not implement
124 ConstRules &operator=(const ConstRules &); // Do not implement
128 inline Constant *operator!(const Constant &V) {
129 return ConstRules::get(V)->op_not(&V);
134 inline Constant *operator+(const Constant &V1, const Constant &V2) {
135 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
136 return ConstRules::get(V1)->add(&V1, &V2);
139 inline Constant *operator-(const Constant &V1, const Constant &V2) {
140 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
141 return ConstRules::get(V1)->sub(&V1, &V2);
144 inline Constant *operator*(const Constant &V1, const Constant &V2) {
145 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
146 return ConstRules::get(V1)->mul(&V1, &V2);
149 inline ConstantBool *operator<(const Constant &V1,
150 const Constant &V2) {
151 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
152 return ConstRules::get(V1)->lessthan(&V1, &V2);
156 //===----------------------------------------------------------------------===//
157 // Implement 'derived' operators based on what we already have...
158 //===----------------------------------------------------------------------===//
160 inline ConstantBool *operator>(const Constant &V1,
161 const Constant &V2) {
165 inline ConstantBool *operator>=(const Constant &V1,
166 const Constant &V2) {
167 return (V1 < V2)->inverted(); // !(V1 < V2)
170 inline ConstantBool *operator<=(const Constant &V1,
171 const Constant &V2) {
172 return (V1 > V2)->inverted(); // !(V1 > V2)
176 //===----------------------------------------------------------------------===//
177 // Implement higher level instruction folding type instructions
178 //===----------------------------------------------------------------------===//
180 inline Constant *ConstantFoldCastInstruction(const Constant *V,
181 const Type *DestTy) {
182 return ConstRules::get(*V)->castTo(V, DestTy);
185 inline Constant *ConstantFoldUnaryInstruction(unsigned Opcode,
188 case Instruction::Not: return !*V;
189 // TODO: Handle get element ptr instruction here in the future? GEP null?
194 inline Constant *ConstantFoldBinaryInstruction(unsigned Opcode,
196 const Constant *V2) {
198 case Instruction::Add: return *V1 + *V2;
199 case Instruction::Sub: return *V1 - *V2;
201 case Instruction::SetEQ: return *V1 == *V2;
202 case Instruction::SetNE: return *V1 != *V2;
203 case Instruction::SetLE: return *V1 <= *V2;
204 case Instruction::SetGE: return *V1 >= *V2;
205 case Instruction::SetLT: return *V1 < *V2;
206 case Instruction::SetGT: return *V1 > *V2;