1 //===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
3 // This file implements the various intrinsic operations, on constant values.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/ConstantHandling.h"
8 #include "llvm/iPHINode.h"
9 #include "llvm/DerivedTypes.h"
12 AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
15 // ConstantFoldInstruction - Attempt to constant fold the specified instruction.
16 // If successful, the constant result is returned, if not, null is returned.
18 Constant *ConstantFoldInstruction(Instruction *I) {
19 if (PHINode *PN = dyn_cast<PHINode>(I)) {
20 if (PN->getNumIncomingValues() == 0)
21 return Constant::getNullValue(PN->getType());
23 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
24 if (Result == 0) return 0;
26 // Handle PHI nodes specially here...
27 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
28 if (PN->getIncomingValue(i) != Result)
29 return 0; // Not all the same incoming constants...
31 // If we reach here, all incoming values are the same constant.
38 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
39 Op0 = dyn_cast<Constant>(I->getOperand(0));
40 if (Op0 == 0) return 0; // Not a constant?, can't fold
42 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
43 Op1 = dyn_cast<Constant>(I->getOperand(1));
44 if (Op1 == 0) return 0; // Not a constant?, can't fold
48 switch (I->getOpcode()) {
49 case Instruction::Cast:
50 return ConstRules::get(*Op0)->castTo(Op0, I->getType());
51 case Instruction::Add: return *Op0 + *Op1;
52 case Instruction::Sub: return *Op0 - *Op1;
53 case Instruction::Mul: return *Op0 * *Op1;
54 case Instruction::Div: return *Op0 / *Op1;
55 case Instruction::Rem: return *Op0 % *Op1;
56 case Instruction::And: return *Op0 & *Op1;
57 case Instruction::Or: return *Op0 | *Op1;
58 case Instruction::Xor: return *Op0 ^ *Op1;
60 case Instruction::SetEQ: return *Op0 == *Op1;
61 case Instruction::SetNE: return *Op0 != *Op1;
62 case Instruction::SetLE: return *Op0 <= *Op1;
63 case Instruction::SetGE: return *Op0 >= *Op1;
64 case Instruction::SetLT: return *Op0 < *Op1;
65 case Instruction::SetGT: return *Op0 > *Op1;
66 case Instruction::Shl: return *Op0 << *Op1;
67 case Instruction::Shr: return *Op0 >> *Op1;
68 case Instruction::GetElementPtr: {
69 std::vector<Constant*> IdxList;
70 IdxList.reserve(I->getNumOperands()-1);
71 if (Op1) IdxList.push_back(Op1);
72 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
73 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
76 return 0; // Non-constant operand
77 return ConstantFoldGetElementPtr(Op0, IdxList);
84 static unsigned getSize(const Type *Ty) {
85 unsigned S = Ty->getPrimitiveSize();
86 return S ? S : 8; // Treat pointers at 8 bytes
89 Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
90 if (V->getType() == DestTy) return (Constant*)V;
92 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
93 if (CE->getOpcode() == Instruction::Cast) {
94 Constant *Op = (Constant*)cast<Constant>(CE->getOperand(0));
95 // Try to not produce a cast of a cast, which is almost always redundant.
96 if (!Op->getType()->isFloatingPoint() &&
97 !CE->getType()->isFloatingPoint() &&
98 !DestTy->getType()->isFloatingPoint()) {
99 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
100 unsigned S3 = getSize(DestTy);
101 if (Op->getType() == DestTy && S3 >= S2)
103 if (S1 >= S2 && S2 >= S3)
104 return ConstantExpr::getCast(Op, DestTy);
105 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
106 return ConstantExpr::getCast(Op, DestTy);
110 return ConstRules::get(*V)->castTo(V, DestTy);
113 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
114 const Constant *V2) {
116 case Instruction::Add: return *V1 + *V2;
117 case Instruction::Sub: return *V1 - *V2;
118 case Instruction::Mul: return *V1 * *V2;
119 case Instruction::Div: return *V1 / *V2;
120 case Instruction::Rem: return *V1 % *V2;
121 case Instruction::And: return *V1 & *V2;
122 case Instruction::Or: return *V1 | *V2;
123 case Instruction::Xor: return *V1 ^ *V2;
125 case Instruction::SetEQ: return *V1 == *V2;
126 case Instruction::SetNE: return *V1 != *V2;
127 case Instruction::SetLE: return *V1 <= *V2;
128 case Instruction::SetGE: return *V1 >= *V2;
129 case Instruction::SetLT: return *V1 < *V2;
130 case Instruction::SetGT: return *V1 > *V2;
135 Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
136 const Constant *V2) {
138 case Instruction::Shl: return *V1 << *V2;
139 case Instruction::Shr: return *V1 >> *V2;
144 Constant *ConstantFoldGetElementPtr(const Constant *C,
145 const std::vector<Constant*> &IdxList) {
146 if (IdxList.size() == 0 ||
147 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
148 return const_cast<Constant*>(C);
150 // If C is null and all idx's are null, return null of the right type.
152 // FIXME: Implement folding of GEP constant exprs the same as instcombine does
157 //===----------------------------------------------------------------------===//
158 // TemplateRules Class
159 //===----------------------------------------------------------------------===//
161 // TemplateRules - Implement a subclass of ConstRules that provides all
162 // operations as noops. All other rules classes inherit from this class so
163 // that if functionality is needed in the future, it can simply be added here
164 // and to ConstRules without changing anything else...
166 // This class also provides subclasses with typesafe implementations of methods
167 // so that don't have to do type casting.
169 template<class ArgType, class SubClassName>
170 class TemplateRules : public ConstRules {
172 //===--------------------------------------------------------------------===//
173 // Redirecting functions that cast to the appropriate types
174 //===--------------------------------------------------------------------===//
176 virtual Constant *add(const Constant *V1, const Constant *V2) const {
177 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
179 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
180 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
182 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
183 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
185 virtual Constant *div(const Constant *V1, const Constant *V2) const {
186 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
188 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
189 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
191 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
192 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
194 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
195 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
197 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
198 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
200 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
201 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
203 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
204 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
207 virtual ConstantBool *lessthan(const Constant *V1,
208 const Constant *V2) const {
209 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
212 // Casting operators. ick
213 virtual ConstantBool *castToBool(const Constant *V) const {
214 return SubClassName::CastToBool((const ArgType*)V);
216 virtual ConstantSInt *castToSByte(const Constant *V) const {
217 return SubClassName::CastToSByte((const ArgType*)V);
219 virtual ConstantUInt *castToUByte(const Constant *V) const {
220 return SubClassName::CastToUByte((const ArgType*)V);
222 virtual ConstantSInt *castToShort(const Constant *V) const {
223 return SubClassName::CastToShort((const ArgType*)V);
225 virtual ConstantUInt *castToUShort(const Constant *V) const {
226 return SubClassName::CastToUShort((const ArgType*)V);
228 virtual ConstantSInt *castToInt(const Constant *V) const {
229 return SubClassName::CastToInt((const ArgType*)V);
231 virtual ConstantUInt *castToUInt(const Constant *V) const {
232 return SubClassName::CastToUInt((const ArgType*)V);
234 virtual ConstantSInt *castToLong(const Constant *V) const {
235 return SubClassName::CastToLong((const ArgType*)V);
237 virtual ConstantUInt *castToULong(const Constant *V) const {
238 return SubClassName::CastToULong((const ArgType*)V);
240 virtual ConstantFP *castToFloat(const Constant *V) const {
241 return SubClassName::CastToFloat((const ArgType*)V);
243 virtual ConstantFP *castToDouble(const Constant *V) const {
244 return SubClassName::CastToDouble((const ArgType*)V);
246 virtual Constant *castToPointer(const Constant *V,
247 const PointerType *Ty) const {
248 return SubClassName::CastToPointer((const ArgType*)V, Ty);
251 //===--------------------------------------------------------------------===//
252 // Default "noop" implementations
253 //===--------------------------------------------------------------------===//
255 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
256 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
257 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
258 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
259 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
260 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
261 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
262 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
263 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
264 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
265 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
269 // Casting operators. ick
270 static ConstantBool *CastToBool (const Constant *V) { return 0; }
271 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
272 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
273 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
274 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
275 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
276 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
277 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
278 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
279 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
280 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
281 static Constant *CastToPointer(const Constant *,
282 const PointerType *) {return 0;}
287 //===----------------------------------------------------------------------===//
289 //===----------------------------------------------------------------------===//
291 // EmptyRules provides a concrete base class of ConstRules that does nothing
293 struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
298 //===----------------------------------------------------------------------===//
300 //===----------------------------------------------------------------------===//
302 // BoolRules provides a concrete base class of ConstRules for the 'bool' type.
304 struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
306 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
307 return ConstantBool::get(V1->getValue() < V2->getValue());
310 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
311 return ConstantBool::get(V1->getValue() & V2->getValue());
314 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
315 return ConstantBool::get(V1->getValue() | V2->getValue());
318 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
319 return ConstantBool::get(V1->getValue() ^ V2->getValue());
324 //===----------------------------------------------------------------------===//
325 // PointerRules Class
326 //===----------------------------------------------------------------------===//
328 // PointerRules provides a concrete base class of ConstRules for pointer types
330 struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
331 static ConstantBool *CastToBool (const Constant *V) {
332 if (V->isNullValue()) return ConstantBool::False;
333 return 0; // Can't const prop other types of pointers
335 static ConstantSInt *CastToSByte (const Constant *V) {
336 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
337 return 0; // Can't const prop other types of pointers
339 static ConstantUInt *CastToUByte (const Constant *V) {
340 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
341 return 0; // Can't const prop other types of pointers
343 static ConstantSInt *CastToShort (const Constant *V) {
344 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
345 return 0; // Can't const prop other types of pointers
347 static ConstantUInt *CastToUShort(const Constant *V) {
348 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
349 return 0; // Can't const prop other types of pointers
351 static ConstantSInt *CastToInt (const Constant *V) {
352 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
353 return 0; // Can't const prop other types of pointers
355 static ConstantUInt *CastToUInt (const Constant *V) {
356 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
357 return 0; // Can't const prop other types of pointers
359 static ConstantSInt *CastToLong (const Constant *V) {
360 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
361 return 0; // Can't const prop other types of pointers
363 static ConstantUInt *CastToULong (const Constant *V) {
364 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
365 return 0; // Can't const prop other types of pointers
367 static ConstantFP *CastToFloat (const Constant *V) {
368 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
369 return 0; // Can't const prop other types of pointers
371 static ConstantFP *CastToDouble(const Constant *V) {
372 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
373 return 0; // Can't const prop other types of pointers
376 static Constant *CastToPointer(const ConstantPointer *V,
377 const PointerType *PTy) {
378 if (V->getType() == PTy)
379 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
380 if (V->isNullValue())
381 return ConstantPointerNull::get(PTy);
382 return 0; // Can't const prop other types of pointers
387 //===----------------------------------------------------------------------===//
389 //===----------------------------------------------------------------------===//
391 // DirectRules provides a concrete base classes of ConstRules for a variety of
392 // different types. This allows the C++ compiler to automatically generate our
393 // constant handling operations in a typesafe and accurate manner.
395 template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
396 struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
397 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
398 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
399 return ConstantClass::get(*Ty, R);
402 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
403 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
404 return ConstantClass::get(*Ty, R);
407 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
408 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
409 return ConstantClass::get(*Ty, R);
412 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
413 if (V2->isNullValue()) return 0;
414 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
415 return ConstantClass::get(*Ty, R);
418 static ConstantBool *LessThan(const ConstantClass *V1,
419 const ConstantClass *V2) {
420 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
421 return ConstantBool::get(R);
424 static Constant *CastToPointer(const ConstantClass *V,
425 const PointerType *PTy) {
426 if (V->isNullValue()) // Is it a FP or Integral null value?
427 return ConstantPointerNull::get(PTy);
428 return 0; // Can't const prop other types of pointers
431 // Casting operators. ick
432 #define DEF_CAST(TYPE, CLASS, CTYPE) \
433 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
434 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
437 DEF_CAST(Bool , ConstantBool, bool)
438 DEF_CAST(SByte , ConstantSInt, signed char)
439 DEF_CAST(UByte , ConstantUInt, unsigned char)
440 DEF_CAST(Short , ConstantSInt, signed short)
441 DEF_CAST(UShort, ConstantUInt, unsigned short)
442 DEF_CAST(Int , ConstantSInt, signed int)
443 DEF_CAST(UInt , ConstantUInt, unsigned int)
444 DEF_CAST(Long , ConstantSInt, int64_t)
445 DEF_CAST(ULong , ConstantUInt, uint64_t)
446 DEF_CAST(Float , ConstantFP , float)
447 DEF_CAST(Double, ConstantFP , double)
452 //===----------------------------------------------------------------------===//
453 // DirectIntRules Class
454 //===----------------------------------------------------------------------===//
456 // DirectIntRules provides implementations of functions that are valid on
457 // integer types, but not all types in general.
459 template <class ConstantClass, class BuiltinType, Type **Ty>
460 struct DirectIntRules
461 : public DirectRules<ConstantClass, BuiltinType, Ty,
462 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
464 static Constant *Rem(const ConstantClass *V1,
465 const ConstantClass *V2) {
466 if (V2->isNullValue()) return 0;
467 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
468 return ConstantClass::get(*Ty, R);
471 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
472 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
473 return ConstantClass::get(*Ty, R);
475 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
476 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
477 return ConstantClass::get(*Ty, R);
479 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
480 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
481 return ConstantClass::get(*Ty, R);
484 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
485 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
486 return ConstantClass::get(*Ty, R);
489 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
490 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
491 return ConstantClass::get(*Ty, R);
496 //===----------------------------------------------------------------------===//
497 // DirectFPRules Class
498 //===----------------------------------------------------------------------===//
500 // DirectFPRules provides implementations of functions that are valid on
501 // floating point types, but not all types in general.
503 template <class ConstantClass, class BuiltinType, Type **Ty>
505 : public DirectRules<ConstantClass, BuiltinType, Ty,
506 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
507 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
508 if (V2->isNullValue()) return 0;
509 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
510 (BuiltinType)V2->getValue());
511 return ConstantClass::get(*Ty, Result);
515 //===----------------------------------------------------------------------===//
516 // DirectRules Subclasses
517 //===----------------------------------------------------------------------===//
519 // Given the DirectRules class we can now implement lots of types with little
520 // code. Thank goodness C++ compilers are great at stomping out layers of
521 // templates... can you imagine having to do this all by hand? (/me is lazy :)
524 // ConstRules::find - Return the constant rules that take care of the specified
527 Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
528 assert(AID == ConstRules::AID && "Bad annotation for factory!");
529 const Type *Ty = cast<Type>((const Value*)TyA);
531 switch (Ty->getPrimitiveID()) {
532 case Type::BoolTyID: return new BoolRules();
533 case Type::PointerTyID: return new PointerRules();
534 case Type::SByteTyID:
535 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
536 case Type::UByteTyID:
537 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
538 case Type::ShortTyID:
539 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
540 case Type::UShortTyID:
541 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
543 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
545 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
547 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
548 case Type::ULongTyID:
549 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
550 case Type::FloatTyID:
551 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
552 case Type::DoubleTyID:
553 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
555 return new EmptyRules();