[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Chris Lattner lattner at cs.uiuc.edu
Thu Mar 11 23:55:17 PST 2004


Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.81 -> 1.82

---
Log message:

Add support for select constant expressions.  Use reserve a bit more to avoid
memory wasteage.


---
Diffs of the changes:  (+35 -0)

Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.81 llvm/lib/VMCore/Constants.cpp:1.82
--- llvm/lib/VMCore/Constants.cpp:1.81	Mon Mar  8 00:11:10 2004
+++ llvm/lib/VMCore/Constants.cpp	Thu Mar 11 23:54:04 2004
@@ -267,14 +267,26 @@
 
 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
   : Constant(GV->getType()) {
+  Operands.reserve(1);
   Operands.push_back(Use(GV, this));
 }
 
 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
   : Constant(Ty), iType(Opcode) {
+  Operands.reserve(1);
   Operands.push_back(Use(C, this));
 }
 
+// Select instruction creation ctor
+ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
+  : Constant(V1->getType()), iType(Instruction::Select) {
+  Operands.reserve(3);
+  Operands.push_back(Use(C, this));
+  Operands.push_back(Use(V1, this));
+  Operands.push_back(Use(V2, this));
+}
+
+
 static bool isSetCC(unsigned Opcode) {
   return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
          Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
@@ -283,6 +295,7 @@
 
 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
   : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
+  Operands.reserve(2);
   Operands.push_back(Use(C1, this));
   Operands.push_back(Use(C2, this));
 }
@@ -997,6 +1010,11 @@
       case Instruction::Cast:
         New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
         break;
+      case Instruction::Select:
+        New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
+                                        OldC->getOperand(1),
+                                        OldC->getOperand(2));
+        break;
       case Instruction::Shl:
       case Instruction::Shr:
         New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
@@ -1056,6 +1074,23 @@
 
   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
+  return ExprConstants.getOrCreate(ReqTy, Key);
+}
+
+Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
+                                    Constant *V1, Constant *V2) {
+  assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
+  assert(V1->getType() == V2->getType() && "Select value types must match!");
+  assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
+
+  if (ReqTy == V1->getType())
+    if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
+      return SC;        // Fold common cases
+
+  std::vector<Constant*> argVec(3, C);
+  argVec[1] = V1;
+  argVec[2] = V2;
+  ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
   return ExprConstants.getOrCreate(ReqTy, Key);
 }
 





More information about the llvm-commits mailing list