1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the ARM target.
12 //===----------------------------------------------------------------------===//
15 #include "ARMTargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Support/Debug.h"
33 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
39 class ARMTargetLowering : public TargetLowering {
41 ARMTargetLowering(TargetMachine &TM);
42 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
44 virtual std::pair<SDOperand, SDOperand>
45 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
47 bool isTailCall, SDOperand Callee, ArgListTy &Args,
54 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
55 : TargetLowering(TM) {
56 setOperationAction(ISD::RET, MVT::Other, Custom);
59 std::pair<SDOperand, SDOperand>
60 ARMTargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
61 bool isVarArg, unsigned CC,
62 bool isTailCall, SDOperand Callee,
63 ArgListTy &Args, SelectionDAG &DAG) {
64 assert(0 && "Not implemented");
68 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
70 switch(Op.getNumOperands()) {
72 assert(0 && "Do not know how to return this many arguments!");
75 return SDOperand(); // ret void is legal
77 Copy = DAG.getCopyToReg(Op.getOperand(0), ARM::R0, Op.getOperand(1), SDOperand());
81 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
84 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
85 MachineFunction &MF = DAG.getMachineFunction();
86 SSARegMap *RegMap = MF.getSSARegMap();
87 std::vector<SDOperand> ArgValues;
88 SDOperand Root = Op.getOperand(0);
91 unsigned num_regs = 4;
93 static const unsigned REGS[] = {
94 ARM::R0, ARM::R1, ARM::R2, ARM::R3
97 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
100 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
101 assert (ObjectVT == MVT::i32);
103 assert(reg_idx < num_regs);
104 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
105 MF.addLiveIn(REGS[reg_idx], VReg);
106 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
109 ArgValues.push_back(ArgVal);
112 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
115 ArgValues.push_back(Root);
117 // Return the new list of results.
118 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
119 Op.Val->value_end());
120 return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues);
123 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
124 switch (Op.getOpcode()) {
126 assert(0 && "Should not custom lower this!");
128 case ISD::FORMAL_ARGUMENTS:
129 return LowerFORMAL_ARGUMENTS(Op, DAG);
131 return LowerRET(Op, DAG);
135 //===----------------------------------------------------------------------===//
136 // Instruction Selector Implementation
137 //===----------------------------------------------------------------------===//
139 //===--------------------------------------------------------------------===//
140 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
141 /// instructions for SelectionDAG operations.
144 class ARMDAGToDAGISel : public SelectionDAGISel {
145 ARMTargetLowering Lowering;
148 ARMDAGToDAGISel(TargetMachine &TM)
149 : SelectionDAGISel(Lowering), Lowering(TM) {
152 void Select(SDOperand &Result, SDOperand Op);
153 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
155 // Include the pieces autogenerated from the target description.
156 #include "ARMGenDAGISel.inc"
159 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
162 DAG.setRoot(SelectRoot(DAG.getRoot()));
164 DAG.RemoveDeadNodes();
166 ScheduleAndEmitDAG(DAG);
169 void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
170 SelectCode(Result, Op);
173 } // end anonymous namespace
175 /// createARMISelDag - This pass converts a legalized DAG into a
176 /// ARM-specific DAG, ready for instruction scheduling.
178 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
179 return new ARMDAGToDAGISel(TM);