1 //===-- ConstantHandling.h - Stuff for manipulating constants ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations of some cool operators that allow you
11 // to do natural things with constant pool values.
13 // Unfortunately we can't overload operators on pointer types (like this:)
15 // inline bool operator==(const Constant *V1, const Constant *V2)
17 // so we must make due with references, even though it leads to some butt ugly
18 // looking code downstream. *sigh* (ex: Constant *Result = *V1 + *v2; )
20 //===----------------------------------------------------------------------===//
22 // WARNING: These operators may return a null object if I don't know how to
23 // perform the specified operation on the specified constant types.
25 //===----------------------------------------------------------------------===//
27 // Implementation notes:
28 // This library is implemented this way for a reason: In most cases, we do
29 // not want to have to link the constant mucking code into an executable.
30 // We do, however want to tie some of this into the main type system, as an
31 // optional component. By using a mutable cache member in the Type class, we
32 // get exactly the kind of behavior we want.
34 // In the end, we get performance almost exactly the same as having a virtual
35 // function dispatch, but we don't have to put our virtual functions into the
36 // "Type" class, and we can implement functionality with templates. Good deal.
38 //===----------------------------------------------------------------------===//
40 #ifndef LLVM_CONSTANTHANDLING_H
41 #define LLVM_CONSTANTHANDLING_H
43 #include "llvm/Constants.h"
44 #include "llvm/Type.h"
50 //===----------------------------------------------------------------------===//
51 // Implement == and != directly...
52 //===----------------------------------------------------------------------===//
54 inline ConstantBool *operator==(const Constant &V1, const Constant &V2) {
55 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
56 return ConstantBool::get(&V1 == &V2);
59 inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) {
60 return ConstantBool::get(&V1 != &V2);
63 //===----------------------------------------------------------------------===//
64 // Implement all other operators indirectly through TypeRules system
65 //===----------------------------------------------------------------------===//
70 // Binary Operators...
71 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
72 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
73 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
74 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
75 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
76 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
77 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
78 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
79 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
80 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
82 virtual ConstantBool *lessthan(const Constant *V1,
83 const Constant *V2) const = 0;
85 // Casting operators. ick
86 virtual ConstantBool *castToBool (const Constant *V) const = 0;
87 virtual ConstantSInt *castToSByte (const Constant *V) const = 0;
88 virtual ConstantUInt *castToUByte (const Constant *V) const = 0;
89 virtual ConstantSInt *castToShort (const Constant *V) const = 0;
90 virtual ConstantUInt *castToUShort(const Constant *V) const = 0;
91 virtual ConstantSInt *castToInt (const Constant *V) const = 0;
92 virtual ConstantUInt *castToUInt (const Constant *V) const = 0;
93 virtual ConstantSInt *castToLong (const Constant *V) const = 0;
94 virtual ConstantUInt *castToULong (const Constant *V) const = 0;
95 virtual ConstantFP *castToFloat (const Constant *V) const = 0;
96 virtual ConstantFP *castToDouble(const Constant *V) const = 0;
97 virtual Constant *castToPointer(const Constant *V,
98 const PointerType *Ty) const = 0;
100 inline Constant *castTo(const Constant *V, const Type *Ty) const {
101 switch (Ty->getPrimitiveID()) {
102 case Type::BoolTyID: return castToBool(V);
103 case Type::UByteTyID: return castToUByte(V);
104 case Type::SByteTyID: return castToSByte(V);
105 case Type::UShortTyID: return castToUShort(V);
106 case Type::ShortTyID: return castToShort(V);
107 case Type::UIntTyID: return castToUInt(V);
108 case Type::IntTyID: return castToInt(V);
109 case Type::ULongTyID: return castToULong(V);
110 case Type::LongTyID: return castToLong(V);
111 case Type::FloatTyID: return castToFloat(V);
112 case Type::DoubleTyID: return castToDouble(V);
113 case Type::PointerTyID:
114 return castToPointer(V, reinterpret_cast<const PointerType*>(Ty));
119 // ConstRules::get - Return an instance of ConstRules for the specified
120 // constant operands.
122 static ConstRules &get(const Constant &V1, const Constant &V2);
124 ConstRules(const ConstRules &); // Do not implement
125 ConstRules &operator=(const ConstRules &); // Do not implement
128 // Unary operators...
129 inline Constant *operator~(const Constant &V) {
130 assert(V.getType()->isIntegral() && "Cannot invert non-integral constant!");
131 return ConstRules::get(V, V).op_xor(&V,
132 ConstantInt::getAllOnesValue(V.getType()));
135 inline Constant *operator-(const Constant &V) {
136 return ConstRules::get(V, V).sub(Constant::getNullValue(V.getType()), &V);
139 // Standard binary operators...
140 inline Constant *operator+(const Constant &V1, const Constant &V2) {
141 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
142 return ConstRules::get(V1, V2).add(&V1, &V2);
145 inline Constant *operator-(const Constant &V1, const Constant &V2) {
146 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
147 return ConstRules::get(V1, V2).sub(&V1, &V2);
150 inline Constant *operator*(const Constant &V1, const Constant &V2) {
151 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
152 return ConstRules::get(V1, V2).mul(&V1, &V2);
155 inline Constant *operator/(const Constant &V1, const Constant &V2) {
156 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
157 return ConstRules::get(V1, V2).div(&V1, &V2);
160 inline Constant *operator%(const Constant &V1, const Constant &V2) {
161 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
162 return ConstRules::get(V1, V2).rem(&V1, &V2);
165 // Logical Operators...
166 inline Constant *operator&(const Constant &V1, const Constant &V2) {
167 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
168 return ConstRules::get(V1, V2).op_and(&V1, &V2);
171 inline Constant *operator|(const Constant &V1, const Constant &V2) {
172 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
173 return ConstRules::get(V1, V2).op_or(&V1, &V2);
176 inline Constant *operator^(const Constant &V1, const Constant &V2) {
177 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
178 return ConstRules::get(V1, V2).op_xor(&V1, &V2);
181 // Shift Instructions...
182 inline Constant *operator<<(const Constant &V1, const Constant &V2) {
183 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
184 return ConstRules::get(V1, V2).shl(&V1, &V2);
187 inline Constant *operator>>(const Constant &V1, const Constant &V2) {
188 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
189 return ConstRules::get(V1, V2).shr(&V1, &V2);
192 inline ConstantBool *operator<(const Constant &V1,
193 const Constant &V2) {
194 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
195 return ConstRules::get(V1, V2).lessthan(&V1, &V2);
199 //===----------------------------------------------------------------------===//
200 // Implement 'derived' operators based on what we already have...
201 //===----------------------------------------------------------------------===//
203 inline ConstantBool *operator>(const Constant &V1,
204 const Constant &V2) {
208 inline ConstantBool *operator>=(const Constant &V1,
209 const Constant &V2) {
210 if (ConstantBool *V = (V1 < V2))
211 return V->inverted(); // !(V1 < V2)
215 inline ConstantBool *operator<=(const Constant &V1,
216 const Constant &V2) {
217 if (ConstantBool *V = (V1 > V2))
218 return V->inverted(); // !(V1 > V2)
223 //===----------------------------------------------------------------------===//
224 // Implement higher level instruction folding type instructions
225 //===----------------------------------------------------------------------===//
227 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
228 // If successful, the constant result is returned, if not, null is returned.
230 Constant *ConstantFoldInstruction(Instruction *I);
232 // Constant fold various types of instruction...
233 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
234 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
236 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
238 Constant *ConstantFoldGetElementPtr(const Constant *C,
239 const std::vector<Constant*> &IdxList);
241 } // End llvm namespace