3227e3995f61f2c099863c936107cbf9f5ee8683
[oota-llvm.git] / lib / VMCore / ConstantFolding.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/Type.h"
40
41 //===----------------------------------------------------------------------===//
42 //  Implement == directly...
43 //===----------------------------------------------------------------------===//
44
45 inline ConstPoolBool *operator==(const ConstPoolVal &V1, 
46                                  const ConstPoolVal &V2) {
47   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
48   return new ConstPoolBool(V1.equals(&V2));
49 }
50
51 //===----------------------------------------------------------------------===//
52 //  Implement all other operators indirectly through TypeRules system
53 //===----------------------------------------------------------------------===//
54
55 class ConstRules {
56 protected:
57   inline ConstRules() {}  // Can only be subclassed...
58 public:
59   // Unary Operators...
60   virtual ConstPoolVal *neg(const ConstPoolVal *V) const = 0;
61   virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
62
63   // Binary Operators...
64   virtual ConstPoolVal *add(const ConstPoolVal *V1, 
65                             const ConstPoolVal *V2) const = 0;
66   virtual ConstPoolVal *sub(const ConstPoolVal *V1, 
67                             const ConstPoolVal *V2) const = 0;
68
69   virtual ConstPoolBool *lessthan(const ConstPoolVal *V1, 
70                                   const ConstPoolVal *V2) const = 0;
71
72   // ConstRules::get - A type will cache its own type rules if one is needed...
73   // we just want to make sure to hit the cache instead of doing it indirectly,
74   //  if possible...
75   //
76   static inline const ConstRules *get(const ConstPoolVal &V) {
77     const ConstRules *Result = V.getType()->getConstRules();
78     return Result ? Result : find(V.getType());
79   }
80 private :
81   static const ConstRules *find(const Type *Ty);
82
83   ConstRules(const ConstRules &);             // Do not implement
84   ConstRules &operator=(const ConstRules &);  // Do not implement
85 };
86
87
88 inline ConstPoolVal *operator-(const ConstPoolVal &V) {
89   return ConstRules::get(V)->neg(&V);
90 }
91
92 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
93   return ConstRules::get(V)->not(&V);
94 }
95
96
97
98 inline ConstPoolVal *operator+(const ConstPoolVal &V1, const ConstPoolVal &V2) {
99   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
100   return ConstRules::get(V1)->add(&V1, &V2);
101 }
102
103 inline ConstPoolVal *operator-(const ConstPoolVal &V1, const ConstPoolVal &V2) {
104   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
105   return ConstRules::get(V1)->sub(&V1, &V2);
106 }
107
108 inline ConstPoolBool *operator<(const ConstPoolVal &V1, 
109                                 const ConstPoolVal &V2) {
110   assert(V1.getType() == V2.getType() && "Constant types must be identical!");
111   return ConstRules::get(V1)->lessthan(&V1, &V2);
112 }
113
114
115 //===----------------------------------------------------------------------===//
116 //  Implement 'derived' operators based on what we already have...
117 //===----------------------------------------------------------------------===//
118
119 inline ConstPoolBool *operator>(const ConstPoolVal &V1, 
120                                 const ConstPoolVal &V2) {
121   return V2 < V1;
122 }
123
124 inline ConstPoolBool *operator!=(const ConstPoolVal &V1, 
125                                  const ConstPoolVal &V2) {
126   ConstPoolBool *Result = V1 == V2;
127   Result->setValue(!Result->getValue());     // Invert value
128   return Result;     // !(V1 == V2)
129 }
130
131 inline ConstPoolBool *operator>=(const ConstPoolVal &V1, 
132                                  const ConstPoolVal &V2) {
133   ConstPoolBool *Result = V1 < V2;
134   Result->setValue(!Result->getValue());     // Invert value
135   return Result;      // !(V1 < V2)
136 }
137
138 inline ConstPoolBool *operator<=(const ConstPoolVal &V1, 
139                                  const ConstPoolVal &V2) {
140   ConstPoolBool *Result = V1 > V2;
141   Result->setValue(!Result->getValue());     // Invert value
142   return Result;      // !(V1 > V2)
143 }
144
145 #endif