Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines an instruction selector for the MIPS target. |
| 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
| 14 | #define DEBUG_TYPE "mips-isel" |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 15 | #include "MipsISelDAGToDAG.h" |
| 16 | #include "Mips16ISelDAGToDAG.h" |
| 17 | #include "MipsSEISelDAGToDAG.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 18 | #include "Mips.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/MipsBaseInfo.h" |
Akira Hatanaka | 57fa382 | 2012-01-25 03:01:35 +0000 | [diff] [blame] | 20 | #include "MipsAnalyzeImmediate.h" |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 21 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 22 | #include "MipsRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Akira Hatanaka | 44b6c71 | 2012-02-28 02:55:02 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GlobalValue.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/Type.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CFG.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 41 | // Instruction Selector Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 43 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 45 | // MipsDAGToDAGISel - MIPS specific code to select MIPS machine |
| 46 | // instructions for SelectionDAG operations. |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 48 | |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 49 | bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { |
| 50 | bool Ret = SelectionDAGISel::runOnMachineFunction(MF); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 51 | |
Akira Hatanaka | f283512 | 2013-03-14 18:33:23 +0000 | [diff] [blame^] | 52 | processFunctionAfterISel(MF); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 53 | |
| 54 | return Ret; |
| 55 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 56 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 57 | /// getGlobalBaseReg - Output the instructions required to put the |
| 58 | /// GOT address into a register. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 59 | SDNode *MipsDAGToDAGISel::getGlobalBaseReg() { |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 60 | unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg(); |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 61 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 64 | /// ComplexPattern used on MipsInstrInfo |
| 65 | /// Used on Mips Load/Store instructions |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 66 | bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base, |
| 67 | SDValue &Offset) const { |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 68 | llvm_unreachable("Unimplemented function."); |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 72 | bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base, |
| 73 | SDValue &Offset) const { |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 74 | llvm_unreachable("Unimplemented function."); |
| 75 | return false; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 78 | bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base, |
| 79 | SDValue &Offset) const { |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 80 | llvm_unreachable("Unimplemented function."); |
| 81 | return false; |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Akira Hatanaka | f283512 | 2013-03-14 18:33:23 +0000 | [diff] [blame^] | 84 | bool MipsDAGToDAGISel::selectAddr16(SDNode *Parent, SDValue N, SDValue &Base, |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 85 | SDValue &Offset, SDValue &Alias) { |
| 86 | llvm_unreachable("Unimplemented function."); |
| 87 | return false; |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 88 | } |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 89 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 90 | /// Select instructions not customized! Used for |
| 91 | /// expanded, promoted and normal instructions |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 92 | SDNode* MipsDAGToDAGISel::Select(SDNode *Node) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 93 | unsigned Opcode = Node->getOpcode(); |
Akira Hatanaka | f283512 | 2013-03-14 18:33:23 +0000 | [diff] [blame^] | 94 | DebugLoc DL = Node->getDebugLoc(); |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 95 | EVT NodeTy = Node->getValueType(0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 96 | |
| 97 | // Dump information about the Node being selected |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 98 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 99 | |
| 100 | // If we have a custom node, we already have selected! |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 101 | if (Node->isMachineOpcode()) { |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 102 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
| 105 | |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 106 | // See if subclasses can handle this node. |
Akira Hatanaka | f283512 | 2013-03-14 18:33:23 +0000 | [diff] [blame^] | 107 | std::pair<bool, SDNode*> Ret = selectNode(Node); |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 108 | |
| 109 | if (Ret.first) |
| 110 | return Ret.second; |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 111 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 112 | switch(Opcode) { |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 113 | default: break; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 114 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 115 | // Get target GOT address. |
| 116 | case ISD::GLOBAL_OFFSET_TABLE: |
| 117 | return getGlobalBaseReg(); |
Akira Hatanaka | ca07479 | 2011-12-08 20:34:32 +0000 | [diff] [blame] | 118 | |
Akira Hatanaka | 5a7dd43 | 2012-09-15 01:52:08 +0000 | [diff] [blame] | 119 | #ifndef NDEBUG |
| 120 | case ISD::LOAD: |
| 121 | case ISD::STORE: |
| 122 | assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <= |
| 123 | cast<MemSDNode>(Node)->getAlignment() && |
| 124 | "Unexpected unaligned loads/stores."); |
| 125 | break; |
| 126 | #endif |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Select the default instruction |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 130 | SDNode *ResNode = SelectCode(Node); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 132 | DEBUG(errs() << "=> "); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 133 | if (ResNode == NULL || ResNode == Node) |
| 134 | DEBUG(Node->dump(CurDAG)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 135 | else |
| 136 | DEBUG(ResNode->dump(CurDAG)); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 137 | DEBUG(errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 138 | return ResNode; |
| 139 | } |
| 140 | |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 141 | bool MipsDAGToDAGISel:: |
| 142 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
| 143 | std::vector<SDValue> &OutOps) { |
| 144 | assert(ConstraintCode == 'm' && "unexpected asm memory constraint"); |
| 145 | OutOps.push_back(Op); |
| 146 | return false; |
| 147 | } |
| 148 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 149 | /// createMipsISelDag - This pass converts a legalized DAG into a |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 150 | /// MIPS-specific DAG, ready for instruction scheduling. |
| 151 | FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) { |
Akira Hatanaka | 554d931 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 152 | if (TM.getSubtargetImpl()->inMips16Mode()) |
| 153 | return llvm::createMips16ISelDag(TM); |
| 154 | |
| 155 | return llvm::createMipsSEISelDag(TM); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 156 | } |