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 return pointers to newly 'new'd objects. You MUST
16 // make sure to free them if you don't want them hanging around. Also,
17 // note that these may return a null object if I don't know how to
18 // perform those operations on the specified constant types.
20 //===----------------------------------------------------------------------===//
22 // Implementation notes:
23 // This library is implemented this way for a reason: In most cases, we do
24 // not want to have to link the constant mucking code into an executable.
25 // We do, however want to tie some of this into the main type system, as an
26 // optional component. By using a mutable cache member in the Type class, we
27 // get exactly the kind of behavior we want.
29 // In the end, we get performance almost exactly the same as having a virtual
30 // function dispatch, but we don't have to put our virtual functions into the
31 // "Type" class, and we can implement functionality with templates. Good deal.
33 //===----------------------------------------------------------------------===//
35 #ifndef LLVM_OPT_CONSTANTHANDLING_H
36 #define LLVM_OPT_CONSTANTHANDLING_H
38 #include "llvm/ConstPoolVals.h"
39 #include "llvm/Instruction.h"
40 #include "llvm/Type.h"
44 //===----------------------------------------------------------------------===//
45 // Implement == directly...
46 //===----------------------------------------------------------------------===//
48 inline ConstPoolBool *operator==(const ConstPoolVal &V1,
49 const ConstPoolVal &V2) {
50 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
51 return new ConstPoolBool(V1.equals(&V2));
54 //===----------------------------------------------------------------------===//
55 // Implement all other operators indirectly through TypeRules system
56 //===----------------------------------------------------------------------===//
60 inline ConstRules() {} // Can only be subclassed...
63 virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
65 // Binary Operators...
66 virtual ConstPoolVal *add(const ConstPoolVal *V1,
67 const ConstPoolVal *V2) const = 0;
68 virtual ConstPoolVal *sub(const ConstPoolVal *V1,
69 const ConstPoolVal *V2) const = 0;
71 virtual ConstPoolBool *lessthan(const ConstPoolVal *V1,
72 const ConstPoolVal *V2) const = 0;
74 // ConstRules::get - A type will cache its own type rules if one is needed...
75 // we just want to make sure to hit the cache instead of doing it indirectly,
78 static inline const ConstRules *get(const ConstPoolVal &V) {
79 const ConstRules *Result = V.getType()->getConstRules();
80 return Result ? Result : find(V.getType());
83 static const ConstRules *find(const Type *Ty);
85 ConstRules(const ConstRules &); // Do not implement
86 ConstRules &operator=(const ConstRules &); // Do not implement
90 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
91 return ConstRules::get(V)->not(&V);
96 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
97 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
98 return ConstRules::get(V1)->add(&V1, &V2);
101 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
102 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
103 return ConstRules::get(V1)->sub(&V1, &V2);
106 inline ConstPoolBool *operator<(const ConstPoolVal &V1,
107 const ConstPoolVal &V2) {
108 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
109 return ConstRules::get(V1)->lessthan(&V1, &V2);
113 //===----------------------------------------------------------------------===//
114 // Implement 'derived' operators based on what we already have...
115 //===----------------------------------------------------------------------===//
117 inline ConstPoolBool *operator>(const ConstPoolVal &V1,
118 const ConstPoolVal &V2) {
122 inline ConstPoolBool *operator!=(const ConstPoolVal &V1,
123 const ConstPoolVal &V2) {
124 ConstPoolBool *Result = V1 == V2;
125 Result->setValue(!Result->getValue()); // Invert value
126 return Result; // !(V1 == V2)
129 inline ConstPoolBool *operator>=(const ConstPoolVal &V1,
130 const ConstPoolVal &V2) {
131 ConstPoolBool *Result = V1 < V2;
132 Result->setValue(!Result->getValue()); // Invert value
133 return Result; // !(V1 < V2)
136 inline ConstPoolBool *operator<=(const ConstPoolVal &V1,
137 const ConstPoolVal &V2) {
138 ConstPoolBool *Result = V1 > V2;
139 Result->setValue(!Result->getValue()); // Invert value
140 return Result; // !(V1 > V2)
144 //===----------------------------------------------------------------------===//
145 // Implement higher level instruction folding type instructions
146 //===----------------------------------------------------------------------===//
148 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
151 case Instruction::Not: return !*V;
156 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
160 case Instruction::Add: return *V1 + *V2;
161 case Instruction::Sub: return *V1 - *V2;
163 case Instruction::SetEQ: return *V1 == *V2;
164 case Instruction::SetNE: return *V1 != *V2;
165 case Instruction::SetLE: return *V1 <= *V2;
166 case Instruction::SetGE: return *V1 >= *V2;
167 case Instruction::SetLT: return *V1 < *V2;
168 case Instruction::SetGT: return *V1 > *V2;
173 } // end namespace opt