Lowerpacked and SCCP support for the insertelement operation.
authorRobert Bocchino <bocchino@illinois.edu>
Tue, 17 Jan 2006 20:06:55 +0000 (20:06 +0000)
committerRobert Bocchino <bocchino@illinois.edu>
Tue, 17 Jan 2006 20:06:55 +0000 (20:06 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25406 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LowerPacked.cpp
lib/Transforms/Scalar/SCCP.cpp

index e8338bb96f72f116c080ca2c8b0a29255a0aef1d..b24460c98877c57ec824de6b44bd4094837f74c4 100644 (file)
@@ -61,7 +61,11 @@ public:
 
    /// @brief Lowers packed extractelement instructions.
    /// @param EI the extractelement operator to convert
-   void visitExtractElementInst(ExtractElementInst& EI);
+   void visitExtractElementInst(ExtractElementInst& EE);
+
+   /// @brief Lowers packed insertelement instructions.
+   /// @param EI the insertelement operator to convert
+   void visitInsertElementInst(InsertElementInst& IE);
 
    /// This function asserts if the instruction is a PackedType but
    /// is handled by another function.
@@ -345,16 +349,19 @@ void LowerPacked::visitExtractElementInst(ExtractElementInst& EI)
   if (ConstantUInt *C = dyn_cast<ConstantUInt>(op1)) {
     EI.replaceAllUsesWith(op0Vals[C->getValue()]);
   } else {
-    AllocaInst *alloca = new AllocaInst(PTy->getElementType(),
-                                       ConstantUInt::get(Type::UIntTy, PTy->getNumElements()),
-                                       EI.getName() + ".alloca", &(EI.getParent()->getParent()->getEntryBlock().front()));
+    AllocaInst *alloca = 
+      new AllocaInst(PTy->getElementType(),
+                     ConstantUInt::get(Type::UIntTy, PTy->getNumElements()),
+                     EI.getName() + ".alloca", 
+                    EI.getParent()->getParent()->getEntryBlock().begin());
     for (unsigned i = 0; i < PTy->getNumElements(); ++i) {
-      GetElementPtrInst *GEP = new GetElementPtrInst(alloca, ConstantUInt::get(Type::UIntTy, i),
-                                                    "store.ge", &EI);
+      GetElementPtrInst *GEP = 
+        new GetElementPtrInst(alloca, ConstantUInt::get(Type::UIntTy, i),
+                              "store.ge", &EI);
       new StoreInst(op0Vals[i], GEP, &EI);
     }
-    GetElementPtrInst *GEP = new GetElementPtrInst(alloca, op1,
-                                                  EI.getName() + ".ge", &EI);
+    GetElementPtrInst *GEP = 
+      new GetElementPtrInst(alloca, op1, EI.getName() + ".ge", &EI);
     LoadInst *load = new LoadInst(GEP, EI.getName() + ".load", &EI);
     EI.replaceAllUsesWith(load);
   }
@@ -363,6 +370,36 @@ void LowerPacked::visitExtractElementInst(ExtractElementInst& EI)
   instrsToRemove.push_back(&EI);
 }
 
+void LowerPacked::visitInsertElementInst(InsertElementInst& IE)
+{
+  std::vector<Value*>& Vals = getValues(IE.getOperand(0));
+  Value *Elt = IE.getOperand(1);
+  Value *Idx = IE.getOperand(2);
+  std::vector<Value*> result;
+  result.reserve(Vals.size());
+
+  if (ConstantUInt *C = dyn_cast<ConstantUInt>(Idx)) {
+    unsigned idxVal = C->getValue();
+    for (unsigned i = 0; i != Vals.size(); ++i) {
+      result.push_back(i == idxVal ? Elt : Vals[i]);
+    }
+  } else {
+    for (unsigned i = 0; i != Vals.size(); ++i) {
+      SetCondInst *setcc =
+        new SetCondInst(Instruction::SetEQ, Idx, 
+                        ConstantUInt::get(Type::UIntTy, i),
+                        "setcc", &IE);
+      SelectInst *select =
+        new SelectInst(setcc, Elt, Vals[i], "select", &IE);
+      result.push_back(select);
+    }
+  }
+
+  setValues(&IE, result);
+  Changed = true;
+  instrsToRemove.push_back(&IE);
+}
+
 bool LowerPacked::runOnFunction(Function& F)
 {
    // initialize
index 1a76eb5e5e8dfce3e476ba2ae76de08cacdb06b2..9e98248cf8e8a50f32eac4c1cca0a17d4bc643e2 100644 (file)
@@ -323,6 +323,7 @@ private:
   void visitBinaryOperator(Instruction &I);
   void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
   void visitExtractElementInst(ExtractElementInst &I);
+  void visitInsertElementInst(InsertElementInst &I);
 
   // Instructions that cannot be folded away...
   void visitStoreInst     (Instruction &I);
@@ -740,6 +741,26 @@ void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
                                                      IdxState.getConstant()));
 }
 
+void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
+  LatticeVal &ValState = getValueState(I.getOperand(0));
+  LatticeVal &EltState = getValueState(I.getOperand(1));
+  LatticeVal &IdxState = getValueState(I.getOperand(2));
+
+  if (ValState.isOverdefined() || EltState.isOverdefined() ||
+      IdxState.isOverdefined())
+    markOverdefined(&I);
+  else if(ValState.isConstant() && EltState.isConstant() &&
+          IdxState.isConstant())
+    markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
+                                                    EltState.getConstant(),
+                                                    IdxState.getConstant()));
+  else if (ValState.isUndefined() && EltState.isConstant() &&
+           IdxState.isConstant())
+    markConstant(&I, ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
+                                                    EltState.getConstant(),
+                                                    IdxState.getConstant()));
+}
+
 // Handle getelementptr instructions... if all operands are constants then we
 // can turn this into a getelementptr ConstantExpr.
 //