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 ConstPoolVal *V1, const ConstPoolVal *V2)
10 // so we must make due with references, even though it leads to some butt ugly
11 // looking code downstream. *sigh* (ex: ConstPoolVal *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/ConstPoolVals.h"
37 #include "llvm/Instruction.h"
38 #include "llvm/Type.h"
42 //===----------------------------------------------------------------------===//
43 // Implement == and != directly...
44 //===----------------------------------------------------------------------===//
46 inline ConstPoolBool *operator==(const ConstPoolVal &V1,
47 const ConstPoolVal &V2) {
48 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
49 return ConstPoolBool::get(&V1 == &V2);
52 inline ConstPoolBool *operator!=(const ConstPoolVal &V1,
53 const ConstPoolVal &V2) {
54 return ConstPoolBool::get(&V1 != &V2);
57 //===----------------------------------------------------------------------===//
58 // Implement all other operators indirectly through TypeRules system
59 //===----------------------------------------------------------------------===//
63 inline ConstRules() {} // Can only be subclassed...
66 virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
68 // Binary Operators...
69 virtual ConstPoolVal *add(const ConstPoolVal *V1,
70 const ConstPoolVal *V2) const = 0;
71 virtual ConstPoolVal *sub(const ConstPoolVal *V1,
72 const ConstPoolVal *V2) const = 0;
73 virtual ConstPoolVal *mul(const ConstPoolVal *V1,
74 const ConstPoolVal *V2) const = 0;
76 virtual ConstPoolBool *lessthan(const ConstPoolVal *V1,
77 const ConstPoolVal *V2) const = 0;
79 // Casting operators. ick
80 virtual ConstPoolBool *castToBool (const ConstPoolVal *V) const = 0;
81 virtual ConstPoolSInt *castToSByte (const ConstPoolVal *V) const = 0;
82 virtual ConstPoolUInt *castToUByte (const ConstPoolVal *V) const = 0;
83 virtual ConstPoolSInt *castToShort (const ConstPoolVal *V) const = 0;
84 virtual ConstPoolUInt *castToUShort(const ConstPoolVal *V) const = 0;
85 virtual ConstPoolSInt *castToInt (const ConstPoolVal *V) const = 0;
86 virtual ConstPoolUInt *castToUInt (const ConstPoolVal *V) const = 0;
87 virtual ConstPoolSInt *castToLong (const ConstPoolVal *V) const = 0;
88 virtual ConstPoolUInt *castToULong (const ConstPoolVal *V) const = 0;
89 virtual ConstPoolFP *castToFloat (const ConstPoolVal *V) const = 0;
90 virtual ConstPoolFP *castToDouble(const ConstPoolVal *V) const = 0;
92 inline ConstPoolVal *castTo(const ConstPoolVal *V, const Type *Ty) const {
93 switch (Ty->getPrimitiveID()) {
94 case Type::BoolTyID: return castToBool(V);
95 case Type::UByteTyID: return castToUByte(V);
96 case Type::SByteTyID: return castToSByte(V);
97 case Type::UShortTyID: return castToUShort(V);
98 case Type::ShortTyID: return castToShort(V);
99 case Type::UIntTyID: return castToUInt(V);
100 case Type::IntTyID: return castToInt(V);
101 case Type::ULongTyID: return castToULong(V);
102 case Type::LongTyID: return castToLong(V);
103 case Type::FloatTyID: return castToFloat(V);
104 case Type::DoubleTyID: return castToDouble(V);
109 // ConstRules::get - A type will cache its own type rules if one is needed...
110 // we just want to make sure to hit the cache instead of doing it indirectly,
113 static inline const ConstRules *get(const ConstPoolVal &V) {
114 const ConstRules *Result = V.getType()->getConstRules();
115 return Result ? Result : find(V.getType());
118 static const ConstRules *find(const Type *Ty);
120 ConstRules(const ConstRules &); // Do not implement
121 ConstRules &operator=(const ConstRules &); // Do not implement
125 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
126 return ConstRules::get(V)->not(&V);
131 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
132 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
133 return ConstRules::get(V1)->add(&V1, &V2);
136 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
137 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
138 return ConstRules::get(V1)->sub(&V1, &V2);
141 inline ConstPoolVal *operator*(const ConstPoolVal &V1, const ConstPoolVal &V2) {
142 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
143 return ConstRules::get(V1)->mul(&V1, &V2);
146 inline ConstPoolBool *operator<(const ConstPoolVal &V1,
147 const ConstPoolVal &V2) {
148 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
149 return ConstRules::get(V1)->lessthan(&V1, &V2);
153 //===----------------------------------------------------------------------===//
154 // Implement 'derived' operators based on what we already have...
155 //===----------------------------------------------------------------------===//
157 inline ConstPoolBool *operator>(const ConstPoolVal &V1,
158 const ConstPoolVal &V2) {
162 inline ConstPoolBool *operator>=(const ConstPoolVal &V1,
163 const ConstPoolVal &V2) {
164 return (V1 < V2)->inverted(); // !(V1 < V2)
167 inline ConstPoolBool *operator<=(const ConstPoolVal &V1,
168 const ConstPoolVal &V2) {
169 return (V1 > V2)->inverted(); // !(V1 > V2)
173 //===----------------------------------------------------------------------===//
174 // Implement higher level instruction folding type instructions
175 //===----------------------------------------------------------------------===//
177 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
180 case Instruction::Not: return !*V;
185 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
189 case Instruction::Add: return *V1 + *V2;
190 case Instruction::Sub: return *V1 - *V2;
192 case Instruction::SetEQ: return *V1 == *V2;
193 case Instruction::SetNE: return *V1 != *V2;
194 case Instruction::SetLE: return *V1 <= *V2;
195 case Instruction::SetGE: return *V1 >= *V2;
196 case Instruction::SetLT: return *V1 < *V2;
197 case Instruction::SetGT: return *V1 > *V2;
202 } // end namespace opt