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"
42 //===----------------------------------------------------------------------===//
43 // Implement == 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 new ConstPoolBool(V1.equals(&V2));
52 //===----------------------------------------------------------------------===//
53 // Implement all other operators indirectly through TypeRules system
54 //===----------------------------------------------------------------------===//
58 inline ConstRules() {} // Can only be subclassed...
61 virtual ConstPoolVal *neg(const ConstPoolVal *V) const = 0;
62 virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
64 // Binary Operators...
65 virtual ConstPoolVal *add(const ConstPoolVal *V1,
66 const ConstPoolVal *V2) const = 0;
67 virtual ConstPoolVal *sub(const ConstPoolVal *V1,
68 const ConstPoolVal *V2) const = 0;
70 virtual ConstPoolBool *lessthan(const ConstPoolVal *V1,
71 const ConstPoolVal *V2) const = 0;
73 // ConstRules::get - A type will cache its own type rules if one is needed...
74 // we just want to make sure to hit the cache instead of doing it indirectly,
77 static inline const ConstRules *get(const ConstPoolVal &V) {
78 const ConstRules *Result = V.getType()->getConstRules();
79 return Result ? Result : find(V.getType());
82 static const ConstRules *find(const Type *Ty);
84 ConstRules(const ConstRules &); // Do not implement
85 ConstRules &operator=(const ConstRules &); // Do not implement
89 inline ConstPoolVal *operator-(const ConstPoolVal &V) {
90 return ConstRules::get(V)->neg(&V);
93 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
94 return ConstRules::get(V)->not(&V);
99 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
100 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
101 return ConstRules::get(V1)->add(&V1, &V2);
104 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
105 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
106 return ConstRules::get(V1)->sub(&V1, &V2);
109 inline ConstPoolBool *operator<(const ConstPoolVal &V1,
110 const ConstPoolVal &V2) {
111 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
112 return ConstRules::get(V1)->lessthan(&V1, &V2);
116 //===----------------------------------------------------------------------===//
117 // Implement 'derived' operators based on what we already have...
118 //===----------------------------------------------------------------------===//
120 inline ConstPoolBool *operator>(const ConstPoolVal &V1,
121 const ConstPoolVal &V2) {
125 inline ConstPoolBool *operator!=(const ConstPoolVal &V1,
126 const ConstPoolVal &V2) {
127 ConstPoolBool *Result = V1 == V2;
128 Result->setValue(!Result->getValue()); // Invert value
129 return Result; // !(V1 == V2)
132 inline ConstPoolBool *operator>=(const ConstPoolVal &V1,
133 const ConstPoolVal &V2) {
134 ConstPoolBool *Result = V1 < V2;
135 Result->setValue(!Result->getValue()); // Invert value
136 return Result; // !(V1 < V2)
139 inline ConstPoolBool *operator<=(const ConstPoolVal &V1,
140 const ConstPoolVal &V2) {
141 ConstPoolBool *Result = V1 > V2;
142 Result->setValue(!Result->getValue()); // Invert value
143 return Result; // !(V1 > V2)
147 //===----------------------------------------------------------------------===//
148 // Implement higher level instruction folding type instructions
149 //===----------------------------------------------------------------------===//
151 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
154 case Instruction::Not: return !*V;
155 case Instruction::Neg: return -*V;
160 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
164 case Instruction::Add: return *V1 + *V2;
165 case Instruction::Sub: return *V1 - *V2;
167 case Instruction::SetEQ: return *V1 == *V2;
168 case Instruction::SetNE: return *V1 != *V2;
169 case Instruction::SetLE: return *V1 <= *V2;
170 case Instruction::SetGE: return *V1 >= *V2;
171 case Instruction::SetLT: return *V1 < *V2;
172 case Instruction::SetGT: return *V1 > *V2;