]> granicus.if.org Git - llvm/commitdiff
[ExecutionEngine] Add UnaryOperator visitor to the interpreter
authorCameron McInally <cameron.mcinally@nyu.edu>
Mon, 10 Jun 2019 14:38:48 +0000 (14:38 +0000)
committerCameron McInally <cameron.mcinally@nyu.edu>
Mon, 10 Jun 2019 14:38:48 +0000 (14:38 +0000)
This is to support the unary FNeg instruction.

Differential Revision: https://reviews.llvm.org/D62881

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362941 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/Interpreter/Execution.cpp
lib/ExecutionEngine/Interpreter/Interpreter.h
test/ExecutionEngine/test-interp-vec-arithm_float.ll

index 170f0155e5b756d25af5ad0ec2ec6609d77cd0fb..67180f2c21f7a611d5ff1a556ee046f3719b241d 100644 (file)
@@ -42,6 +42,58 @@ static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
   SF.Values[V] = Val;
 }
 
+//===----------------------------------------------------------------------===//
+//                    Unary Instruction Implementations
+//===----------------------------------------------------------------------===//
+
+static void executeFNegInst(GenericValue &Dest, GenericValue Src, Type *Ty) {
+  switch (Ty->getTypeID()) {
+  case Type::FloatTyID:
+    Dest.FloatVal = -Src.FloatVal;
+  case Type::DoubleTyID:
+    Dest.DoubleVal = -Src.DoubleVal;
+  default:
+    llvm_unreachable("Unhandled type for FNeg instruction");
+  }
+}
+
+void Interpreter::visitUnaryOperator(UnaryOperator &I) {
+  ExecutionContext &SF = ECStack.back();
+  Type *Ty = I.getOperand(0)->getType();
+  GenericValue Src = getOperandValue(I.getOperand(0), SF);
+  GenericValue R; // Result
+
+  // First process vector operation
+  if (Ty->isVectorTy()) {
+    R.AggregateVal.resize(Src.AggregateVal.size());
+
+    switch(I.getOpcode()) {
+    default:
+      llvm_unreachable("Don't know how to handle this unary operator");
+      break;
+    case Instruction::FNeg:
+      if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
+        for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
+          R.AggregateVal[i].FloatVal = -Src.AggregateVal[i].FloatVal;
+      } else if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) {
+        for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
+          R.AggregateVal[i].DoubleVal = -Src.AggregateVal[i].DoubleVal;
+      } else {
+        llvm_unreachable("Unhandled type for FNeg instruction");
+      }
+      break;
+    }
+  } else {
+    switch (I.getOpcode()) {
+    default:
+      llvm_unreachable("Don't know how to handle this unary operator");
+      break;
+    case Instruction::FNeg: executeFNegInst(R, Src, Ty); break;
+    }
+  }
+  SetValue(&I, R, SF);
+}
+
 //===----------------------------------------------------------------------===//
 //                    Binary Instruction Implementations
 //===----------------------------------------------------------------------===//
index 898f1d34ec1d36e12b73e54d1984c0fc49c65054..e72d778317d6baccfdd0b49548364a7001a5b54b 100644 (file)
@@ -124,6 +124,7 @@ public:
   void visitSwitchInst(SwitchInst &I);
   void visitIndirectBrInst(IndirectBrInst &I);
 
+  void visitUnaryOperator(UnaryOperator &I);
   void visitBinaryOperator(BinaryOperator &I);
   void visitICmpInst(ICmpInst &I);
   void visitFCmpInst(FCmpInst &I);
index d7f4ac90a9861984085d553076b66e2d7a125137..b01457d82b4a629dee19bd344fce56fdc0d79c02 100644 (file)
@@ -8,6 +8,7 @@ define i32 @main() {
     %C_float = fmul <4 x float> %B_float, %B_float
     %D_float = fdiv <4 x float> %C_float, %B_float
     %E_float = frem <4 x float> %D_float, %A_float
+    %F_float = fneg <4 x float> %E_float
 
 
     %A_double = fadd <3 x double> <double 0.0, double 111.0, double 222.0>, <double 444.0, double 555.0, double 665.0>
@@ -15,6 +16,7 @@ define i32 @main() {
     %C_double = fmul <3 x double> %B_double, %B_double
     %D_double = fdiv <3 x double> %C_double, %B_double
     %E_double = frem <3 x double> %D_double, %A_double
+    %F_double = fneg <3 x double> %E_double
 
     ret i32 0
 }