Add instructions to fold unary and binary instructions.
[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/Instruction.h"
40 #include "llvm/Type.h"
41
42 //===----------------------------------------------------------------------===//
43 //  Implement == directly...
44 //===----------------------------------------------------------------------===//
45
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));
50 }
51
52 //===----------------------------------------------------------------------===//
53 //  Implement all other operators indirectly through TypeRules system
54 //===----------------------------------------------------------------------===//
55
56 class ConstRules {
57 protected:
58   inline ConstRules() {}  // Can only be subclassed...
59 public:
60   // Unary Operators...
61   virtual ConstPoolVal *neg(const ConstPoolVal *V) const = 0;
62   virtual ConstPoolVal *not(const ConstPoolVal *V) const = 0;
63
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;
69
70   virtual ConstPoolBool *lessthan(const ConstPoolVal *V1, 
71                                   const ConstPoolVal *V2) const = 0;
72
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,
75   //  if possible...
76   //
77   static inline const ConstRules *get(const ConstPoolVal &V) {
78     const ConstRules *Result = V.getType()->getConstRules();
79     return Result ? Result : find(V.getType());
80   }
81 private :
82   static const ConstRules *find(const Type *Ty);
83
84   ConstRules(const ConstRules &);             // Do not implement
85   ConstRules &operator=(const ConstRules &);  // Do not implement
86 };
87
88
89 inline ConstPoolVal *operator-(const ConstPoolVal &V) {
90   return ConstRules::get(V)->neg(&V);
91 }
92
93 inline ConstPoolVal *operator!(const ConstPoolVal &V) {
94   return ConstRules::get(V)->not(&V);
95 }
96
97
98
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);
102 }
103
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);
107 }
108
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);
113 }
114
115
116 //===----------------------------------------------------------------------===//
117 //  Implement 'derived' operators based on what we already have...
118 //===----------------------------------------------------------------------===//
119
120 inline ConstPoolBool *operator>(const ConstPoolVal &V1, 
121                                 const ConstPoolVal &V2) {
122   return V2 < V1;
123 }
124
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)
130 }
131
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)
137 }
138
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)
144 }
145
146
147 //===----------------------------------------------------------------------===//
148 //  Implement higher level instruction folding type instructions
149 //===----------------------------------------------------------------------===//
150
151 inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, 
152                                                   ConstPoolVal *V) {
153   switch (Opcode) {
154   case Instruction::Not:  return !*V;
155   case Instruction::Neg:  return -*V;
156   }
157   return 0;
158 }
159
160 inline ConstPoolVal *ConstantFoldBinaryInstruction(unsigned Opcode,
161                                                    ConstPoolVal *V1, 
162                                                    ConstPoolVal *V2) {
163   switch (Opcode) {
164   case Instruction::Add:     return *V1 + *V2;
165   case Instruction::Sub:     return *V1 - *V2;
166
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;
173   }
174   return 0;
175 }
176
177 #endif