Fix Bug: ConstProp/2003-05-12-DivideError.ll
[oota-llvm.git] / lib / VMCore / Constants.cpp
index 84f2566e46868da2caf1aba07208c41ee0a42653..c519da36a861ecaee3b26f4c775ebca72fc03c79 100644 (file)
@@ -5,6 +5,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Constants.h"
+#include "llvm/ConstantHandling.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
 #include "llvm/SymbolTable.h"
@@ -418,7 +419,7 @@ void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
 
-  ConstantExpr *Replacement = 0;
+  Constant *Replacement = 0;
   if (getOpcode() == Instruction::GetElementPtr) {
     std::vector<Constant*> Indices;
     Constant *Pointer = cast<Constant>(getOperand(0));
@@ -635,7 +636,9 @@ void ConstantPointerRef::destroyConstant() {
 typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
 static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
 
-ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
+Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
+  if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
+    return FC;          // Fold a few common cases...
 
   // Look up the constant in the table first to ensure uniqueness
   vector<Constant*> argVec(1, C);
@@ -649,7 +652,11 @@ ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
   return Result;
 }
 
-ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
+Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
+  
+  if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
+    return FC;          // Fold a few common cases...
+
   // Look up the constant in the table first to ensure uniqueness
   vector<Constant*> argVec(1, C1); argVec.push_back(C2);
   const ExprMapKeyType &Key = make_pair(Opcode, argVec);
@@ -670,8 +677,10 @@ ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
   return Result;
 }
 
-ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
-                                        const std::vector<Constant*> &IdxList) {
+Constant *ConstantExpr::getGetElementPtr(Constant *C,
+                                         const std::vector<Constant*> &IdxList){
+  if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
+    return FC;          // Fold a few common cases...
   const Type *Ty = C->getType();
 
   // Look up the constant in the table first to ensure uniqueness