Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 1 | //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the ARM target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ARM.h" |
| 15 | #include "ARMTargetMachine.h" |
Rafael Espindola | 84b19be | 2006-07-16 01:02:57 +0000 | [diff] [blame^] | 16 | #include "llvm/CallingConv.h" |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Intrinsics.h" |
| 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include <iostream> |
| 29 | #include <set> |
| 30 | using namespace llvm; |
| 31 | |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class ARMTargetLowering : public TargetLowering { |
| 34 | public: |
| 35 | ARMTargetLowering(TargetMachine &TM); |
| 36 | virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG); |
Rafael Espindola | 84b19be | 2006-07-16 01:02:57 +0000 | [diff] [blame^] | 37 | virtual const char *getTargetNodeName(unsigned Opcode) const; |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | } |
| 41 | |
| 42 | ARMTargetLowering::ARMTargetLowering(TargetMachine &TM) |
| 43 | : TargetLowering(TM) { |
| 44 | setOperationAction(ISD::RET, MVT::Other, Custom); |
| 45 | } |
| 46 | |
Rafael Espindola | 84b19be | 2006-07-16 01:02:57 +0000 | [diff] [blame^] | 47 | namespace llvm { |
| 48 | namespace ARMISD { |
| 49 | enum NodeType { |
| 50 | // Start the numbering where the builting ops and target ops leave off. |
| 51 | FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END, |
| 52 | /// CALL - A direct function call. |
| 53 | CALL |
| 54 | }; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 59 | switch (Opcode) { |
| 60 | default: return 0; |
| 61 | case ARMISD::CALL: return "ARMISD::CALL"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // This transforms a ISD::CALL node into a |
| 66 | // callseq_star <- ARMISD:CALL <- callseq_end |
| 67 | // chain |
Rafael Espindola | c3c1a86 | 2006-05-25 11:00:18 +0000 | [diff] [blame] | 68 | static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
Rafael Espindola | 84b19be | 2006-07-16 01:02:57 +0000 | [diff] [blame^] | 69 | SDOperand Chain = Op.getOperand(0); |
| 70 | unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 71 | assert(CallConv == CallingConv::C && "unknown calling convention"); |
| 72 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 73 | assert(isVarArg == false && "VarArg not supported"); |
| 74 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 75 | assert(isTailCall == false && "tail call not supported"); |
| 76 | SDOperand Callee = Op.getOperand(4); |
| 77 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 78 | assert(NumOps == 0); |
| 79 | |
| 80 | // Count how many bytes are to be pushed on the stack. Initially |
| 81 | // only the link register. |
| 82 | unsigned NumBytes = 4; |
| 83 | |
| 84 | // Adjust the stack pointer for the new arguments... |
| 85 | // These operations are automatically eliminated by the prolog/epilog pass |
| 86 | Chain = DAG.getCALLSEQ_START(Chain, |
| 87 | DAG.getConstant(NumBytes, MVT::i32)); |
| 88 | |
| 89 | std::vector<MVT::ValueType> NodeTys; |
| 90 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 91 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 92 | |
| 93 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 94 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 95 | // node so that legalize doesn't hack it. |
| 96 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 97 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType()); |
| 98 | |
| 99 | // If this is a direct call, pass the chain and the callee. |
| 100 | assert (Callee.Val); |
| 101 | std::vector<SDOperand> Ops; |
| 102 | Ops.push_back(Chain); |
| 103 | Ops.push_back(Callee); |
| 104 | |
| 105 | unsigned CallOpc = ARMISD::CALL; |
| 106 | Chain = DAG.getNode(CallOpc, NodeTys, Ops); |
| 107 | |
| 108 | assert(Op.Val->getValueType(0) == MVT::Other); |
| 109 | |
| 110 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
| 111 | DAG.getConstant(NumBytes, MVT::i32)); |
| 112 | |
| 113 | return Chain; |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 117 | SDOperand Copy; |
Rafael Espindola | 4b02367 | 2006-06-05 22:26:14 +0000 | [diff] [blame] | 118 | SDOperand Chain = Op.getOperand(0); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 119 | switch(Op.getNumOperands()) { |
| 120 | default: |
| 121 | assert(0 && "Do not know how to return this many arguments!"); |
| 122 | abort(); |
Rafael Espindola | 4b02367 | 2006-06-05 22:26:14 +0000 | [diff] [blame] | 123 | case 1: { |
| 124 | SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32); |
| 125 | return DAG.getNode(ISD::BRIND, MVT::Other, Chain, LR); |
| 126 | } |
Evan Cheng | 6848be1 | 2006-05-26 23:10:12 +0000 | [diff] [blame] | 127 | case 3: |
Rafael Espindola | 4b02367 | 2006-06-05 22:26:14 +0000 | [diff] [blame] | 128 | Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand()); |
| 129 | if (DAG.getMachineFunction().liveout_empty()) |
| 130 | DAG.getMachineFunction().addLiveOut(ARM::R0); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
Rafael Espindola | 4b02367 | 2006-06-05 22:26:14 +0000 | [diff] [blame] | 133 | |
Rafael Espindola | 85ede37 | 2006-05-30 17:33:19 +0000 | [diff] [blame] | 134 | SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 135 | |
Rafael Espindola | 4b02367 | 2006-06-05 22:26:14 +0000 | [diff] [blame] | 136 | //bug: the copy and branch should be linked with a flag so that the |
| 137 | //scheduller can't move an instruction that destroys R0 in between them |
| 138 | //return DAG.getNode(ISD::BRIND, MVT::Other, Copy, LR, Copy.getValue(1)); |
| 139 | |
Rafael Espindola | 85ede37 | 2006-05-30 17:33:19 +0000 | [diff] [blame] | 140 | return DAG.getNode(ISD::BRIND, MVT::Other, Copy, LR); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 143 | static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG, |
| 144 | unsigned ArgNo) { |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 145 | MachineFunction &MF = DAG.getMachineFunction(); |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 146 | MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType(); |
| 147 | assert (ObjectVT == MVT::i32); |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 148 | SDOperand Root = Op.getOperand(0); |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 149 | SSARegMap *RegMap = MF.getSSARegMap(); |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 150 | |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 151 | unsigned num_regs = 4; |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 152 | static const unsigned REGS[] = { |
| 153 | ARM::R0, ARM::R1, ARM::R2, ARM::R3 |
| 154 | }; |
| 155 | |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 156 | if(ArgNo < num_regs) { |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 157 | unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass); |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 158 | MF.addLiveIn(REGS[ArgNo], VReg); |
| 159 | return DAG.getCopyFromReg(Root, VReg, MVT::i32); |
| 160 | } else { |
| 161 | // If the argument is actually used, emit a load from the right stack |
| 162 | // slot. |
| 163 | if (!Op.Val->hasNUsesOfValue(0, ArgNo)) { |
Rafael Espindola | aefe142 | 2006-07-10 01:41:35 +0000 | [diff] [blame] | 164 | unsigned ArgOffset = (ArgNo - num_regs) * 4; |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 165 | |
| 166 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 167 | unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8; |
| 168 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 169 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 170 | return DAG.getLoad(ObjectVT, Root, FIN, |
| 171 | DAG.getSrcValue(NULL)); |
| 172 | } else { |
| 173 | // Don't emit a dead load. |
| 174 | return DAG.getNode(ISD::UNDEF, ObjectVT); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
| 180 | std::vector<SDOperand> ArgValues; |
| 181 | SDOperand Root = Op.getOperand(0); |
| 182 | |
| 183 | for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) { |
| 184 | SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo); |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 185 | |
| 186 | ArgValues.push_back(ArgVal); |
| 187 | } |
| 188 | |
| 189 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 190 | assert(!isVarArg); |
| 191 | |
| 192 | ArgValues.push_back(Root); |
| 193 | |
| 194 | // Return the new list of results. |
| 195 | std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(), |
| 196 | Op.Val->value_end()); |
| 197 | return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues); |
Rafael Espindola | dc124a2 | 2006-05-18 21:45:49 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 200 | SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 201 | switch (Op.getOpcode()) { |
| 202 | default: |
| 203 | assert(0 && "Should not custom lower this!"); |
Rafael Espindola | 1c8f053 | 2006-05-15 22:34:39 +0000 | [diff] [blame] | 204 | abort(); |
Rafael Espindola | dc124a2 | 2006-05-18 21:45:49 +0000 | [diff] [blame] | 205 | case ISD::FORMAL_ARGUMENTS: |
| 206 | return LowerFORMAL_ARGUMENTS(Op, DAG); |
Rafael Espindola | c3c1a86 | 2006-05-25 11:00:18 +0000 | [diff] [blame] | 207 | case ISD::CALL: |
| 208 | return LowerCALL(Op, DAG); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 209 | case ISD::RET: |
| 210 | return LowerRET(Op, DAG); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // Instruction Selector Implementation |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | //===--------------------------------------------------------------------===// |
| 219 | /// ARMDAGToDAGISel - ARM specific code to select ARM machine |
| 220 | /// instructions for SelectionDAG operations. |
| 221 | /// |
| 222 | namespace { |
| 223 | class ARMDAGToDAGISel : public SelectionDAGISel { |
| 224 | ARMTargetLowering Lowering; |
| 225 | |
| 226 | public: |
| 227 | ARMDAGToDAGISel(TargetMachine &TM) |
| 228 | : SelectionDAGISel(Lowering), Lowering(TM) { |
| 229 | } |
| 230 | |
| 231 | void Select(SDOperand &Result, SDOperand Op); |
| 232 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
Rafael Espindola | a4e6435 | 2006-07-11 11:36:48 +0000 | [diff] [blame] | 233 | bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 234 | |
| 235 | // Include the pieces autogenerated from the target description. |
| 236 | #include "ARMGenDAGISel.inc" |
| 237 | }; |
| 238 | |
| 239 | void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 240 | DEBUG(BB->dump()); |
| 241 | |
| 242 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
Evan Cheng | 6a3d5a6 | 2006-05-25 00:24:28 +0000 | [diff] [blame] | 243 | assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!"); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 244 | CodeGenMap.clear(); |
Evan Cheng | afe358e | 2006-05-24 20:46:25 +0000 | [diff] [blame] | 245 | HandleMap.clear(); |
| 246 | ReplaceMap.clear(); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 247 | DAG.RemoveDeadNodes(); |
| 248 | |
| 249 | ScheduleAndEmitDAG(DAG); |
| 250 | } |
| 251 | |
Rafael Espindola | a4e6435 | 2006-07-11 11:36:48 +0000 | [diff] [blame] | 252 | //register plus/minus 12 bit offset |
| 253 | bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset, |
| 254 | SDOperand &Base) { |
| 255 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
Rafael Espindola | aefe142 | 2006-07-10 01:41:35 +0000 | [diff] [blame] | 256 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) { |
| 257 | Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 258 | } |
| 259 | else |
| 260 | Base = N; |
| 261 | return true; //any address fits in a register |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 264 | void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) { |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 265 | SDNode *N = Op.Val; |
| 266 | |
| 267 | switch (N->getOpcode()) { |
| 268 | default: |
| 269 | SelectCode(Result, Op); |
| 270 | break; |
Rafael Espindola | 337c4ad6 | 2006-06-12 12:28:08 +0000 | [diff] [blame] | 271 | } |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | } // end anonymous namespace |
| 275 | |
| 276 | /// createARMISelDag - This pass converts a legalized DAG into a |
| 277 | /// ARM-specific DAG, ready for instruction scheduling. |
| 278 | /// |
| 279 | FunctionPass *llvm::createARMISelDag(TargetMachine &TM) { |
| 280 | return new ARMDAGToDAGISel(TM); |
| 281 | } |