blob: ef19cd257f8fdb360e188782bf0efbbed3e3f97e [file] [log] [blame]
Chris Lattner48486892003-09-30 18:37:50 +00001//===-- ConstantHandling.h - Stuff for manipulating constants ---*- C++ -*-===//
John Criswell6fbcc262003-10-20 20:19:47 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file contains the declarations of some cool operators that allow you
11// to do natural things with constant pool values.
12//
13// Unfortunately we can't overload operators on pointer types (like this:)
14//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000015// inline bool operator==(const Constant *V1, const Constant *V2)
Chris Lattner00950542001-06-06 20:29:01 +000016//
17// so we must make due with references, even though it leads to some butt ugly
Chris Lattnere9bb2df2001-12-03 22:26:30 +000018// looking code downstream. *sigh* (ex: Constant *Result = *V1 + *v2; )
Chris Lattner00950542001-06-06 20:29:01 +000019//
20//===----------------------------------------------------------------------===//
21//
Chris Lattner539a4bf2001-09-07 16:29:18 +000022// 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.
Chris Lattner00950542001-06-06 20:29:01 +000024//
25//===----------------------------------------------------------------------===//
26//
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.
33//
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.
37//
38//===----------------------------------------------------------------------===//
39
Chris Lattner66160422002-04-08 20:15:12 +000040#ifndef LLVM_CONSTANTHANDLING_H
41#define LLVM_CONSTANTHANDLING_H
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattner31bcdb82002-04-28 19:55:58 +000043#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000044#include "llvm/Type.h"
Chris Lattner76ac1a42001-11-01 05:55:13 +000045class PointerType;
Chris Lattner00950542001-06-06 20:29:01 +000046
47//===----------------------------------------------------------------------===//
Chris Lattner539a4bf2001-09-07 16:29:18 +000048// Implement == and != directly...
Chris Lattner00950542001-06-06 20:29:01 +000049//===----------------------------------------------------------------------===//
50
Chris Lattnerfd73cf82003-04-17 19:22:47 +000051inline ConstantBool *operator==(const Constant &V1, const Constant &V2) {
Chris Lattner00950542001-06-06 20:29:01 +000052 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000053 return ConstantBool::get(&V1 == &V2);
Chris Lattner539a4bf2001-09-07 16:29:18 +000054}
55
Chris Lattnerfd73cf82003-04-17 19:22:47 +000056inline ConstantBool *operator!=(const Constant &V1, const Constant &V2) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000057 return ConstantBool::get(&V1 != &V2);
Chris Lattner00950542001-06-06 20:29:01 +000058}
59
60//===----------------------------------------------------------------------===//
61// Implement all other operators indirectly through TypeRules system
62//===----------------------------------------------------------------------===//
63
Chris Lattner78914e72001-09-09 21:00:23 +000064class ConstRules : public Annotation {
Chris Lattner00950542001-06-06 20:29:01 +000065protected:
Chris Lattner78914e72001-09-09 21:00:23 +000066 inline ConstRules() : Annotation(AID) {} // Can only be subclassed...
Chris Lattner00950542001-06-06 20:29:01 +000067public:
Chris Lattner78914e72001-09-09 21:00:23 +000068 static AnnotationID AID; // AnnotationID for this class
69
Chris Lattner00950542001-06-06 20:29:01 +000070 // Binary Operators...
Chris Lattnerbdd15ad2002-05-03 21:40:37 +000071 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;
Chris Lattnere56096a2002-07-30 16:24:25 +000076 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;
Chris Lattner4c1061f2002-05-06 03:01:37 +000079 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
80 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
Chris Lattner00950542001-06-06 20:29:01 +000081
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 virtual ConstantBool *lessthan(const Constant *V1,
83 const Constant *V2) const = 0;
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattner7e314d22001-07-21 19:10:33 +000085 // Casting operators. ick
Chris Lattnere9bb2df2001-12-03 22:26:30 +000086 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;
Chris Lattnerfd73cf82003-04-17 19:22:47 +000097 virtual Constant *castToPointer(const Constant *V,
98 const PointerType *Ty) const = 0;
Chris Lattner7e314d22001-07-21 19:10:33 +000099
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000100 inline Constant *castTo(const Constant *V, const Type *Ty) const {
Chris Lattner7e314d22001-07-21 19:10:33 +0000101 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);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000113 case Type::PointerTyID:return castToPointer(V, (PointerType*)Ty);
Chris Lattner7e314d22001-07-21 19:10:33 +0000114 default: return 0;
115 }
116 }
117
Chris Lattner00950542001-06-06 20:29:01 +0000118 // ConstRules::get - A type will cache its own type rules if one is needed...
119 // we just want to make sure to hit the cache instead of doing it indirectly,
120 // if possible...
121 //
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000122 static inline ConstRules *get(const Constant &V1, const Constant &V2) {
123 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2))
124 return getConstantExprRules();
125 return (ConstRules*)V1.getType()->getOrCreateAnnotation(AID);
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
Chris Lattnerfd73cf82003-04-17 19:22:47 +0000127private:
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000128 static ConstRules *getConstantExprRules();
Chris Lattner78914e72001-09-09 21:00:23 +0000129 static Annotation *find(AnnotationID AID, const Annotable *Ty, void *);
Chris Lattner00950542001-06-06 20:29:01 +0000130
131 ConstRules(const ConstRules &); // Do not implement
132 ConstRules &operator=(const ConstRules &); // Do not implement
133};
134
Chris Lattner30780cc2003-07-23 17:21:17 +0000135// Unary operators...
136inline Constant *operator~(const Constant &V) {
137 assert(V.getType()->isIntegral() && "Cannot invert non-intergral constant!");
138 return ConstRules::get(V, V)->op_xor(&V,
139 ConstantInt::getAllOnesValue(V.getType()));
140}
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattnere56096a2002-07-30 16:24:25 +0000142// Standard binary operators...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000143inline Constant *operator+(const Constant &V1, const Constant &V2) {
Chris Lattner00950542001-06-06 20:29:01 +0000144 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000145 return ConstRules::get(V1, V2)->add(&V1, &V2);
Chris Lattner00950542001-06-06 20:29:01 +0000146}
147
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148inline Constant *operator-(const Constant &V1, const Constant &V2) {
Chris Lattner00950542001-06-06 20:29:01 +0000149 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000150 return ConstRules::get(V1, V2)->sub(&V1, &V2);
Chris Lattner00950542001-06-06 20:29:01 +0000151}
152
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000153inline Constant *operator*(const Constant &V1, const Constant &V2) {
Chris Lattnera137f872001-07-20 19:14:41 +0000154 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000155 return ConstRules::get(V1, V2)->mul(&V1, &V2);
Chris Lattnera137f872001-07-20 19:14:41 +0000156}
157
Chris Lattner05c05ea2002-04-07 08:10:14 +0000158inline Constant *operator/(const Constant &V1, const Constant &V2) {
159 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000160 return ConstRules::get(V1, V2)->div(&V1, &V2);
Chris Lattner05c05ea2002-04-07 08:10:14 +0000161}
162
Chris Lattnerbdd15ad2002-05-03 21:40:37 +0000163inline Constant *operator%(const Constant &V1, const Constant &V2) {
164 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000165 return ConstRules::get(V1, V2)->rem(&V1, &V2);
Chris Lattnerbdd15ad2002-05-03 21:40:37 +0000166}
167
Chris Lattnere56096a2002-07-30 16:24:25 +0000168// Logical Operators...
169inline Constant *operator&(const Constant &V1, const Constant &V2) {
170 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000171 return ConstRules::get(V1, V2)->op_and(&V1, &V2);
Chris Lattnere56096a2002-07-30 16:24:25 +0000172}
173
174inline Constant *operator|(const Constant &V1, const Constant &V2) {
175 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000176 return ConstRules::get(V1, V2)->op_or(&V1, &V2);
Chris Lattnere56096a2002-07-30 16:24:25 +0000177}
178
179inline Constant *operator^(const Constant &V1, const Constant &V2) {
180 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000181 return ConstRules::get(V1, V2)->op_xor(&V1, &V2);
Chris Lattnere56096a2002-07-30 16:24:25 +0000182}
183
184// Shift Instructions...
Chris Lattner4c1061f2002-05-06 03:01:37 +0000185inline Constant *operator<<(const Constant &V1, const Constant &V2) {
Chris Lattnerce8a1492002-09-03 01:05:48 +0000186 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000187 return ConstRules::get(V1, V2)->shl(&V1, &V2);
Chris Lattner4c1061f2002-05-06 03:01:37 +0000188}
189
190inline Constant *operator>>(const Constant &V1, const Constant &V2) {
Chris Lattnerce8a1492002-09-03 01:05:48 +0000191 assert(V1.getType()->isInteger() && V2.getType() == Type::UByteTy);
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000192 return ConstRules::get(V1, V2)->shr(&V1, &V2);
Chris Lattner4c1061f2002-05-06 03:01:37 +0000193}
194
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000195inline ConstantBool *operator<(const Constant &V1,
196 const Constant &V2) {
Chris Lattner00950542001-06-06 20:29:01 +0000197 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000198 return ConstRules::get(V1, V2)->lessthan(&V1, &V2);
Chris Lattner00950542001-06-06 20:29:01 +0000199}
200
201
202//===----------------------------------------------------------------------===//
203// Implement 'derived' operators based on what we already have...
204//===----------------------------------------------------------------------===//
205
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000206inline ConstantBool *operator>(const Constant &V1,
207 const Constant &V2) {
Chris Lattner00950542001-06-06 20:29:01 +0000208 return V2 < V1;
209}
210
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000211inline ConstantBool *operator>=(const Constant &V1,
212 const Constant &V2) {
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000213 if (ConstantBool *V = (V1 < V2))
214 return V->inverted(); // !(V1 < V2)
215 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000216}
217
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000218inline ConstantBool *operator<=(const Constant &V1,
219 const Constant &V2) {
Chris Lattnereca8e8f2003-04-25 02:51:46 +0000220 if (ConstantBool *V = (V1 > V2))
221 return V->inverted(); // !(V1 > V2)
222 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000223}
224
Chris Lattner6bb09d92001-06-27 23:31:34 +0000225
226//===----------------------------------------------------------------------===//
227// Implement higher level instruction folding type instructions
228//===----------------------------------------------------------------------===//
229
Chris Lattnera8038992002-05-06 17:54:50 +0000230// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
231// If successful, the constant result is returned, if not, null is returned.
232//
233Constant *ConstantFoldInstruction(Instruction *I);
Chris Lattner37aabf22001-10-31 05:07:57 +0000234
Chris Lattnera8038992002-05-06 17:54:50 +0000235// Constant fold various types of instruction...
236Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy);
Chris Lattnera8038992002-05-06 17:54:50 +0000237Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
238 const Constant *V2);
239Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
240 const Constant *V2);
Chris Lattnerfd73cf82003-04-17 19:22:47 +0000241Constant *ConstantFoldGetElementPtr(const Constant *C,
242 const std::vector<Constant*> &IdxList);
Chris Lattner00950542001-06-06 20:29:01 +0000243#endif