[llvm-commits] CVS: llvm-gcc/gcc/llvm-expand.c

Chris Lattner lattner at cs.uiuc.edu
Sat Apr 9 20:07:02 PDT 2005



Changes in directory llvm-gcc/gcc:

llvm-expand.c updated: 1.93 -> 1.94
---
Log message:

Implement - and ~ for complex numbers, fixing CFrontend/2005-05-09-ComplexOps.c


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

 llvm-expand.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+)


Index: llvm-gcc/gcc/llvm-expand.c
diff -u llvm-gcc/gcc/llvm-expand.c:1.93 llvm-gcc/gcc/llvm-expand.c:1.94
--- llvm-gcc/gcc/llvm-expand.c:1.93	Fri Apr  8 23:42:55 2005
+++ llvm-gcc/gcc/llvm-expand.c	Sat Apr  9 22:06:48 2005
@@ -5964,6 +5964,46 @@
     InitializeComplex(Fn, DestLoc, ResultR, ResultI, 0);
 }
 
+/* llvm_expand_complex_unary_expr - Expand one of the supported unary
+ * expressions on complex numbers (including both complex floating point and
+ * complex integer).  Supported operations are ~ and -.
+ */
+static void llvm_expand_complex_unary_expr(llvm_function *Fn, tree exp,
+                                           llvm_value *DestLoc) {
+  llvm_type *DestTy = llvm_type_get_from_tree(TREE_TYPE(exp));  
+  llvm_value *Op0 = D2V(make_temporary_alloca(Fn, DestTy));
+  llvm_value *Op0r, *Op0i;   /* real and imaginary components. */
+  llvm_value *ResultR, *ResultI;
+  llvm_value *Zero;
+  /* Expand the operand, loading real/imag parts... */
+
+  llvm_expand_expr(Fn, TREE_OPERAND(exp, 0), Op0);
+  Op0r = llvm_load_complex_part(Fn, Op0, 0, 0);
+  Op0i = llvm_load_complex_part(Fn, Op0, 1, 0);
+
+  if (llvm_type_is_fp(Op0r->Ty)) {
+    Zero = llvm_constant_new(Op0i->Ty, "-0.0");
+  } else {
+    Zero = llvm_constant_get_null(Op0i->Ty);
+  }
+  switch (TREE_CODE(exp)) {
+  case NEGATE_EXPR:  /* -(a+ib) = -a + i*-b */
+    ResultR = append_inst(Fn, create_binary_inst("tmp", O_Sub, Zero, Op0r));
+    ResultI = append_inst(Fn, create_binary_inst("tmp", O_Sub, Zero, Op0i));
+    break;
+  case CONJ_EXPR:  /* ~(a+ib) = a + i*-b */
+    ResultR = Op0r;
+    ResultI = append_inst(Fn, create_binary_inst("tmp", O_Sub, Zero, Op0i));
+    break;
+  default:
+    debug_tree(exp);
+    assert(0 && "Unknown complex expression!");
+  }
+
+  if (DestLoc)
+    InitializeComplex(Fn, DestLoc, ResultR, ResultI, 0);
+}
+
 /* isSimpleEnoughToEvaluateUnconditionally - Return true if this expression is
  * simple enough to evaluate unconditionally in a case where it would normally
  * be only conditionally executed.
@@ -6349,7 +6389,17 @@
     TREE_OPERAND(exp, 0) = lang_hooks.unsave_expr_now(TREE_OPERAND(exp, 0));
     break;
 
+  case CONJ_EXPR:      /* ~A = Ar + i*(-Ai)*/
+    assert(llvm_type_is_composite(DestTy) && "conj of noncomplex?");
+    llvm_expand_complex_unary_expr(Fn, exp, DestLoc);
+    break;
+
   case NEGATE_EXPR:    /* -A === 0-A */
+    if (llvm_type_is_composite(DestTy)) {
+      llvm_expand_complex_unary_expr(Fn, exp, DestLoc);
+      break;
+    }
+
     op1 = llvm_expand_expr(Fn, TREE_OPERAND(exp, 0), 0);
     if (op1->Ty->ID == PointerTyID)
       op1 = cast_if_type_not_equal(Fn, op1, IntPtrTy);






More information about the llvm-commits mailing list