Print -X like this:
authorChris Lattner <sabre@nondot.org>
Thu, 3 Mar 2005 21:12:04 +0000 (21:12 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 3 Mar 2005 21:12:04 +0000 (21:12 +0000)
double test(double l1_X) {
  return (-l1_X);
}

instead of like this:

double test(double l1_X) {
  return (-0x0p+0 - l1_X);
}

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

lib/Target/CBackend/CBackend.cpp
lib/Target/CBackend/Writer.cpp

index 8505917a44e1670cd87d24233a3d8076f018d20b..af478d9bfdbb0df12f637c809dfc4ba569acc1bc 100644 (file)
@@ -1358,30 +1358,38 @@ void CWriter::visitBinaryOperator(Instruction &I) {
     printType(Out, I.getType());
     Out << ")(";
   }
-      
-  writeOperand(I.getOperand(0));
 
-  switch (I.getOpcode()) {
-  case Instruction::Add: Out << " + "; break;
-  case Instruction::Sub: Out << " - "; break;
-  case Instruction::Mul: Out << '*'; break;
-  case Instruction::Div: Out << '/'; break;
-  case Instruction::Rem: Out << '%'; break;
-  case Instruction::And: Out << " & "; break;
-  case Instruction::Or: Out << " | "; break;
-  case Instruction::Xor: Out << " ^ "; break;
-  case Instruction::SetEQ: Out << " == "; break;
-  case Instruction::SetNE: Out << " != "; break;
-  case Instruction::SetLE: Out << " <= "; break;
-  case Instruction::SetGE: Out << " >= "; break;
-  case Instruction::SetLT: Out << " < "; break;
-  case Instruction::SetGT: Out << " > "; break;
-  case Instruction::Shl : Out << " << "; break;
-  case Instruction::Shr : Out << " >> "; break;
-  default: std::cerr << "Invalid operator type!" << I; abort();
-  }
+  // If this is a negation operation, print it out as such.  For FP, we don't
+  // want to print "-0.0 - X".
+  if (BinaryOperator::isNeg(&I)) {
+    Out << "-";
+    writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
 
-  writeOperand(I.getOperand(1));
+  } else {
+    writeOperand(I.getOperand(0));
+
+    switch (I.getOpcode()) {
+    case Instruction::Add: Out << " + "; break;
+    case Instruction::Sub: Out << " - "; break;
+    case Instruction::Mul: Out << '*'; break;
+    case Instruction::Div: Out << '/'; break;
+    case Instruction::Rem: Out << '%'; break;
+    case Instruction::And: Out << " & "; break;
+    case Instruction::Or: Out << " | "; break;
+    case Instruction::Xor: Out << " ^ "; break;
+    case Instruction::SetEQ: Out << " == "; break;
+    case Instruction::SetNE: Out << " != "; break;
+    case Instruction::SetLE: Out << " <= "; break;
+    case Instruction::SetGE: Out << " >= "; break;
+    case Instruction::SetLT: Out << " < "; break;
+    case Instruction::SetGT: Out << " > "; break;
+    case Instruction::Shl : Out << " << "; break;
+    case Instruction::Shr : Out << " >> "; break;
+    default: std::cerr << "Invalid operator type!" << I; abort();
+    }
+
+    writeOperand(I.getOperand(1));
+  }
 
   if (needsCast) {
     Out << "))";
index 8505917a44e1670cd87d24233a3d8076f018d20b..af478d9bfdbb0df12f637c809dfc4ba569acc1bc 100644 (file)
@@ -1358,30 +1358,38 @@ void CWriter::visitBinaryOperator(Instruction &I) {
     printType(Out, I.getType());
     Out << ")(";
   }
-      
-  writeOperand(I.getOperand(0));
 
-  switch (I.getOpcode()) {
-  case Instruction::Add: Out << " + "; break;
-  case Instruction::Sub: Out << " - "; break;
-  case Instruction::Mul: Out << '*'; break;
-  case Instruction::Div: Out << '/'; break;
-  case Instruction::Rem: Out << '%'; break;
-  case Instruction::And: Out << " & "; break;
-  case Instruction::Or: Out << " | "; break;
-  case Instruction::Xor: Out << " ^ "; break;
-  case Instruction::SetEQ: Out << " == "; break;
-  case Instruction::SetNE: Out << " != "; break;
-  case Instruction::SetLE: Out << " <= "; break;
-  case Instruction::SetGE: Out << " >= "; break;
-  case Instruction::SetLT: Out << " < "; break;
-  case Instruction::SetGT: Out << " > "; break;
-  case Instruction::Shl : Out << " << "; break;
-  case Instruction::Shr : Out << " >> "; break;
-  default: std::cerr << "Invalid operator type!" << I; abort();
-  }
+  // If this is a negation operation, print it out as such.  For FP, we don't
+  // want to print "-0.0 - X".
+  if (BinaryOperator::isNeg(&I)) {
+    Out << "-";
+    writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
 
-  writeOperand(I.getOperand(1));
+  } else {
+    writeOperand(I.getOperand(0));
+
+    switch (I.getOpcode()) {
+    case Instruction::Add: Out << " + "; break;
+    case Instruction::Sub: Out << " - "; break;
+    case Instruction::Mul: Out << '*'; break;
+    case Instruction::Div: Out << '/'; break;
+    case Instruction::Rem: Out << '%'; break;
+    case Instruction::And: Out << " & "; break;
+    case Instruction::Or: Out << " | "; break;
+    case Instruction::Xor: Out << " ^ "; break;
+    case Instruction::SetEQ: Out << " == "; break;
+    case Instruction::SetNE: Out << " != "; break;
+    case Instruction::SetLE: Out << " <= "; break;
+    case Instruction::SetGE: Out << " >= "; break;
+    case Instruction::SetLT: Out << " < "; break;
+    case Instruction::SetGT: Out << " > "; break;
+    case Instruction::Shl : Out << " << "; break;
+    case Instruction::Shr : Out << " >> "; break;
+    default: std::cerr << "Invalid operator type!" << I; abort();
+    }
+
+    writeOperand(I.getOperand(1));
+  }
 
   if (needsCast) {
     Out << "))";