cf18ef8cea498059eed4fe2889e6b05938440da2
[oota-llvm.git] / include / llvm / ConstantHandling.h
1 //===-- ConstantHandling.h - Stuff for manipulating constants ----*- C++ -*--=//
2 //
3 // This file contains the declarations of some cool operators that allow you
4 // to do natural things with constant pool values.
5 //
6 // Unfortunately we can't overload operators on pointer types (like this:)
7 //
8 //      inline bool operator==(const ConstPoolVal *V1, const ConstPoolVal *V2)
9 //
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; )
12 //
13 //===----------------------------------------------------------------------===//
14 //
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.
19 //
20 //===----------------------------------------------------------------------===//
21 //
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.
28 //
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.
32 //
33 //===----------------------------------------------------------------------===//
34
35 #ifndef LLVM_OPT_CONSTANTHANDLING_H
36 #define LLVM_OPT_CONSTANTHANDLING_H
37
38 #include "llvm/ConstPoolVals.h"
39 #include "llvm/Instruction.h"
40 #include "llvm/Type.h"
41
42 namespace opt {
43
44 //===----------------------------------------------------------------------===//
45 //  Implement == directly...
46 //===----------------------------------------------------------------------===//
47
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));
52 }
53
54 //===----------------------------------------------------------------------===//
55 //  Implement all other operators indirectly through TypeRules system
56 //===----------------------------------------------------------------------===//
57
58 class ConstRules {
59 protected:
60   inline ConstRules() {}  // Can only be subclassed...
61 public:
62   // Unary Operators...
63   virtual ConstPoolVal *neg(const ConstPoolVal *V) const = 0;
64   virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
65
66   // Binary Operators...
67   virtual ConstPoolVal *add(const ConstPoolVal *V1, 
68                             const ConstPoolVal *V2) const = 0;
69   virtual ConstPoolVal *sub(const ConstPoolVal *V1, 
70                             const ConstPoolVal *V2) const = 0;
71
72   virtual ConstPoolBool *lessthan(const ConstPoolVal *V1, 
73                                   const ConstPoolVal *V2) const = 0;
74
75   // ConstRules::get - A type will cache its own type rules if one is needed...
76   // we just want to make sure to hit the cache instead of doing it indirectly,
77   //  if possible...
78   //
79   static inline const ConstRules *get(const ConstPoolVal &V) {
80     const ConstRules *Result = V.getType()->getConstRules();
81     return Result ? Result : find(V.getType());
82   }
83 private :
84   static const ConstRules *find(const Type *Ty);
85
86   ConstRules(const ConstRules &);             // Do not implement
87   ConstRules &operator=(const ConstRules &);  // Do not implement
88 };
89
90
91 inline ConstPoolVal *operator-(const ConstPoolVal &V) {
92   return ConstRules::get(V)->neg(&V);
93 }
94
95 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
96   return ConstRules::get(V)->not(&V);
97 }
98
99
100
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)->add(&V1, &V2);
104 }
105
106 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
107   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
108   return ConstRules::get(V1)->sub(&V1, &V2);
109 }
110
111 inline ConstPoolBool *operator<(const ConstPoolVal &V1, 
112                                 const ConstPoolVal &V2) {
113   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
114   return ConstRules::get(V1)->lessthan(&V1, &V2);
115 }
116
117
118 //===----------------------------------------------------------------------===//
119 //  Implement 'derived' operators based on what we already have...
120 //===----------------------------------------------------------------------===//
121
122 inline ConstPoolBool *operator>(const ConstPoolVal &V1, 
123                                 const ConstPoolVal &V2) {
124   return V2 < V1;
125 }
126
127 inline ConstPoolBool *operator!=(const ConstPoolVal &V1, 
128                                  const ConstPoolVal &V2) {
129   ConstPoolBool *Result = V1 == V2;
130   Result->setValue(!Result->getValue());     // Invert value
131   return Result;     // !(V1 == V2)
132 }
133
134 inline ConstPoolBool *operator>=(const ConstPoolVal &V1, 
135                                  const ConstPoolVal &V2) {
136   ConstPoolBool *Result = V1 < V2;
137   Result->setValue(!Result->getValue());     // Invert value
138   return Result;      // !(V1 < V2)
139 }
140
141 inline ConstPoolBool *operator<=(const ConstPoolVal &V1, 
142                                  const ConstPoolVal &V2) {
143   ConstPoolBool *Result = V1 > V2;
144   Result->setValue(!Result->getValue());     // Invert value
145   return Result;      // !(V1 > V2)
146 }
147
148
149 //===----------------------------------------------------------------------===//
150 //  Implement higher level instruction folding type instructions
151 //===----------------------------------------------------------------------===//
152
153 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, 
154                                                   ConstPoolVal *V) {
155   switch (Opcode) {
156   case Instruction::Not:  return !*V;
157   case Instruction::Neg:  return -*V;
158   }
159   return 0;
160 }
161
162 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
163                                                    ConstPoolVal *V1, 
164                                                    ConstPoolVal *V2) {
165   switch (Opcode) {
166   case Instruction::Add:     return *V1 + *V2;
167   case Instruction::Sub:     return *V1 - *V2;
168
169   case Instruction::SetEQ:   return *V1 == *V2;
170   case Instruction::SetNE:   return *V1 != *V2;
171   case Instruction::SetLE:   return *V1 <= *V2;
172   case Instruction::SetGE:   return *V1 >= *V2;
173   case Instruction::SetLT:   return *V1 <  *V2;
174   case Instruction::SetGT:   return *V1 >  *V2;
175   }
176   return 0;
177 }
178
179 } // end namespace opt
180 #endif