clean this function up some
[oota-llvm.git] / lib / Target / Alpha / AlphaISelDAGToDAG.cpp
index 8e2e39e7cdeb80e74e2fa1160083bbe91ba0c674..dd29e912529bc67bf98c434986236b9d7396b8fb 100644 (file)
@@ -35,17 +35,17 @@ namespace {
   //===--------------------------------------------------------------------===//
   /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
   /// instructions for SelectionDAG operations.
-  ///
   class AlphaDAGToDAGISel : public SelectionDAGISel {
     AlphaTargetLowering AlphaLowering;
 
-    static const int IMM_LOW  = -32768;
-    static const int IMM_HIGH = 32767;
-    static const int IMM_MULT = 65536;
+    static const int64_t IMM_LOW  = -32768;
+    static const int64_t IMM_HIGH = 32767;
+    static const int64_t IMM_MULT = 65536;
     
   public:
     AlphaDAGToDAGISel(TargetMachine &TM)
-      : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {}
+      : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) 
+    {}
 
     /// getI64Imm - Return a target constant with the specified value, of type
     /// i64.
@@ -155,6 +155,38 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
     return SDOperand(Result.Val, Op.ResNo);
   }
   case ISD::BRCOND: {
+    if (N->getOperand(1).getOpcode() == ISD::SETCC &&
+       MVT::isFloatingPoint(N->getOperand(1).getOperand(0).getValueType())) {
+      SDOperand Chain = Select(N->getOperand(0));
+      SDOperand CC1 = Select(N->getOperand(1).getOperand(0));
+      SDOperand CC2 = Select(N->getOperand(1).getOperand(1));
+      ISD::CondCode cCode= cast<CondCodeSDNode>(N->getOperand(1).getOperand(2))->get();
+
+      bool rev = false;
+      bool isNE = false;
+      unsigned Opc = Alpha::WTF;
+      switch(cCode) {
+      default: N->dump(); assert(0 && "Unknown FP comparison!");
+      case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
+      case ISD::SETLT: Opc = Alpha::CMPTLT; break;
+      case ISD::SETLE: Opc = Alpha::CMPTLE; break;
+      case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
+      case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
+      case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
+      };
+      SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64, 
+                                            rev?CC2:CC1,
+                                            rev?CC1:CC2);
+
+      MachineBasicBlock *Dest =
+       cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
+      if(isNE)
+       return CurDAG->SelectNodeTo(N, Alpha::FBEQ, MVT::Other, cmp, 
+                                   CurDAG->getBasicBlock(Dest), Chain);
+      else
+       return CurDAG->SelectNodeTo(N, Alpha::FBNE, MVT::Other, cmp, 
+                                   CurDAG->getBasicBlock(Dest), Chain);
+    }
     SDOperand Chain = Select(N->getOperand(0));
     SDOperand CC = Select(N->getOperand(1));
     MachineBasicBlock *Dest =
@@ -162,67 +194,6 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
     return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC, 
                                 CurDAG->getBasicBlock(Dest), Chain);
   }
-  case ISD::LOAD:
-  case ISD::EXTLOAD:
-  case ISD::ZEXTLOAD:
-  case ISD::SEXTLOAD: {
-    SDOperand Chain = Select(N->getOperand(0));
-    SDOperand Address = Select(N->getOperand(1));
-    unsigned opcode = N->getOpcode();
-    unsigned Opc = Alpha::WTF;
-    if (opcode == ISD::LOAD)
-      switch (N->getValueType(0)) {
-      default: N->dump(); assert(0 && "Bad load!");
-      case MVT::i64: Opc = Alpha::LDQ; break;
-      case MVT::f64: Opc = Alpha::LDT; break;
-      case MVT::f32: Opc = Alpha::LDS; break;
-      }
-    else
-      switch (cast<VTSDNode>(N->getOperand(3))->getVT()) {
-      default: N->dump(); assert(0 && "Bad sign extend!");
-      case MVT::i32: Opc = Alpha::LDL;
-        assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
-      case MVT::i16: Opc = Alpha::LDWU;
-        assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
-      case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
-      case MVT::i8: Opc = Alpha::LDBU;
-          assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
-      }
-
-    return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
-                                getI64Imm(0), Address, 
-                                Chain).getValue(Op.ResNo);
-  }
-  case ISD::STORE:
-  case ISD::TRUNCSTORE: {
-    SDOperand Chain = Select(N->getOperand(0));
-    SDOperand Value = Select(N->getOperand(1));
-    SDOperand Address = Select(N->getOperand(2));
-
-    unsigned Opc = Alpha::WTF;
-
-    if (N->getOpcode() == ISD::STORE) {
-      switch (N->getOperand(1).getValueType()) {
-      case MVT::i64: Opc = Alpha::STQ; break;
-      case MVT::f64: Opc = Alpha::STT; break;
-      case MVT::f32: Opc = Alpha::STS; break;
-      default: assert(0 && "Bad store!");
-      };
-    } else { //TRUNCSTORE
-      switch (cast<VTSDNode>(N->getOperand(4))->getVT()) {
-      case MVT::i32: Opc = Alpha::STL; break;
-      case MVT::i16: Opc = Alpha::STW; break;
-      case MVT::i8: Opc = Alpha::STB; break;
-      default: assert(0 && "Bad truncstore!");
-      };
-    }
-    return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Value, getI64Imm(0),
-                                Address, Chain);
-  }
-
-  case ISD::BR:
-    return CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
-                                Select(N->getOperand(0)));
 
   case ISD::FrameIndex: {
     int FI = cast<FrameIndexSDNode>(N)->getIndex();
@@ -230,31 +201,24 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
                                 CurDAG->getTargetFrameIndex(FI, MVT::i32),
                                 getI64Imm(0));
   }
-  case ISD::ConstantPool: {
-    Constant *C = cast<ConstantPoolSDNode>(N)->get();
-    SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
-    Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
-    return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
-  }
-  case ISD::GlobalAddress: {
-    GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
-    SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
-    return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA, 
-                                getGlobalBaseReg());
-  }
-  case ISD::ExternalSymbol:
-    return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, 
-                                CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
-                                getGlobalBaseReg());
-
-  case ISD::CALLSEQ_START:
-  case ISD::CALLSEQ_END: {
-    unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
-    unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
-                       Alpha::ADJUSTSTACKDOWN : Alpha::ADJUSTSTACKUP;
-    return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
-                                getI64Imm(Amt), Select(N->getOperand(0)));
+  case AlphaISD::GlobalBaseReg: 
+    return getGlobalBaseReg();
+  
+  case AlphaISD::DivCall: {
+    SDOperand Chain = CurDAG->getEntryNode();
+    Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, Select(Op.getOperand(1)), 
+                                SDOperand(0,0));
+    Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, Select(Op.getOperand(2)), 
+                                Chain.getValue(1));
+    Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Select(Op.getOperand(0)), 
+                                Chain.getValue(1));
+    Chain = CurDAG->getTargetNode(Alpha::JSRs, MVT::Other, MVT::Flag, 
+                                 Chain, Chain.getValue(1));
+    Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64, 
+                                 Chain.getValue(1));
+    return CurDAG->SelectNodeTo(N, Alpha::BIS, MVT::i64, Chain, Chain);
   }
+
   case ISD::RET: {
     SDOperand Chain = Select(N->getOperand(0));     // Token chain.
     SDOperand InFlag;
@@ -264,6 +228,10 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
       if (N->getOperand(1).getValueType() == MVT::i64) {
         Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
         InFlag = Chain.getValue(1);
+      } else if (N->getOperand(1).getValueType() == MVT::f64 ||
+                 N->getOperand(1).getValueType() == MVT::f32) {
+        Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag);
+        InFlag = Chain.getValue(1);
       }
     }
     Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
@@ -273,17 +241,24 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
     return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
   }
   case ISD::Constant: {
-    int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
-    if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT ||
-        val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
-      MachineConstantPool *CP = BB->getParent()->getConstantPool();
-      ConstantUInt *C =
-        ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
-      SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
-      Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
-      return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, CPI, Tmp);
-    }
-    break;
+    uint64_t uval = cast<ConstantSDNode>(N)->getValue();
+    int64_t val = (int64_t)uval;
+    int32_t val32 = (int32_t)val;
+    if (val <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
+       val >= IMM_LOW  + IMM_LOW  * IMM_MULT)
+      break; //(LDAH (LDA))
+    if ((uval >> 32) == 0 && //empty upper bits
+       val32 <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
+       val32 >= IMM_LOW  + IMM_LOW  * IMM_MULT)
+      break; //(zext (LDAH (LDA)))
+    //Else use the constant pool
+    MachineConstantPool *CP = BB->getParent()->getConstantPool();
+    ConstantUInt *C =
+      ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , uval);
+    SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
+    Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
+    return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, MVT::Other, 
+                               CPI, Tmp, CurDAG->getEntryNode());
   }
   case ISD::ConstantFP:
     if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
@@ -302,35 +277,6 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
       }
       break;
     }
-  case ISD::SDIV:
-  case ISD::UDIV:
-  case ISD::UREM:
-  case ISD::SREM:
-    if (MVT::isInteger(N->getValueType(0))) {
-      const char* opstr = 0;
-      switch(N->getOpcode()) {
-      case ISD::UREM: opstr = "__remqu"; break;
-      case ISD::SREM: opstr = "__remq";  break;
-      case ISD::UDIV: opstr = "__divqu"; break;
-      case ISD::SDIV: opstr = "__divq";  break;
-      }
-      SDOperand Tmp1 = Select(N->getOperand(0)),
-        Tmp2 = Select(N->getOperand(1)),
-        Addr = CurDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
-      SDOperand Tmp3 = Select(Addr);
-      SDOperand Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R24, 
-                                             Tmp1, SDOperand());
-      Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R25, 
-                                   Tmp2, Chain.getValue(1));
-      Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R27, 
-                                   Tmp3, Chain.getValue(1));
-      Chain = CurDAG->getTargetNode(Alpha::JSRs, MVT::i64, MVT::Flag,
-                                    CurDAG->getRegister(Alpha::R27, MVT::i64),
-                                    getI64Imm(0));
-      return CurDAG->getCopyFromReg(Chain.getValue(1), Alpha::R27, MVT::i64, 
-                                    Chain.getValue(1));
-    }
-    break;
 
   case ISD::SETCC:
     if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
@@ -377,28 +323,33 @@ SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
     break;
 
   case ISD::SELECT:
-    if (MVT::isFloatingPoint(N->getValueType(0))) {
+    if (MVT::isFloatingPoint(N->getValueType(0)) &&
+       (N->getOperand(0).getOpcode() != ISD::SETCC ||
+        !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) {
+      //This should be the condition not covered by the Patterns
+      //FIXME: Don't have SelectCode die, but rather return something testable
+      // so that things like this can be caught in fall though code
       //move int to fp
       bool isDouble = N->getValueType(0) == MVT::f64;
       SDOperand LD,
-        cond = Select(N->getOperand(0)),
-        TV = Select(N->getOperand(1)),
-        FV = Select(N->getOperand(2));
+       cond = Select(N->getOperand(0)),
+       TV = Select(N->getOperand(1)),
+       FV = Select(N->getOperand(2));
       
       if (AlphaLowering.hasITOF()) {
-        LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
+       LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
       } else {
-        int FrameIdx =
-          CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
-        SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
-        SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other, 
-                                             cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
-        LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI, 
-                                   CurDAG->getRegister(Alpha::R31, MVT::i64),
-                                   ST);
+       int FrameIdx =
+         CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
+       SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
+       SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
+                                            cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
+       LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
+                                  CurDAG->getRegister(Alpha::R31, MVT::i64),
+                                  ST);
       }
-      SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVEQT:Alpha::FCMOVEQS,
-                                           MVT::f64, TV, FV, LD);
+      SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
+                                          MVT::f64, FV, TV, LD);
       return FP;
     }
     break;
@@ -413,7 +364,7 @@ SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
 
   SDNode *N = Op.Val;
   SDOperand Chain = Select(N->getOperand(0));
-  SDOperand Addr = Select(N->getOperand(1));
+  SDOperand Addr = N->getOperand(1);
   SDOperand InFlag;  // Null incoming flag value.
 
    std::vector<SDOperand> CallOperands;
@@ -458,12 +409,20 @@ SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
    }
 
 
-   Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
-   InFlag = Chain.getValue(1);
    // Finally, once everything is in registers to pass to the call, emit the
    // call itself.
-   Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, MVT::Flag, 
-                                 Chain, InFlag );
+   if (Addr.getOpcode() == AlphaISD::GPRelLo) {
+     SDOperand GOT = getGlobalBaseReg();
+     Chain = CurDAG->getCopyToReg(Chain, Alpha::R29, GOT, InFlag);
+     InFlag = Chain.getValue(1);
+     Chain = CurDAG->getTargetNode(Alpha::BSR, MVT::Other, MVT::Flag, 
+                                  Addr.getOperand(0), Chain, InFlag);
+   } else {
+     Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Select(Addr), InFlag);
+     InFlag = Chain.getValue(1);
+     Chain = CurDAG->getTargetNode(Alpha::JSR, MVT::Other, MVT::Flag, 
+                                  Chain, InFlag );
+   }
    InFlag = Chain.getValue(1);
 
    std::vector<SDOperand> CallResults;