Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 1 | //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===// |
| 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 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that Mips uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "mips-lower" |
| 16 | |
| 17 | #include "MipsISelLowering.h" |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 18 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "MipsTargetMachine.h" |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 20 | #include "MipsSubtarget.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
| 25 | #include "llvm/CallingConv.h" |
| 26 | #include "llvm/CodeGen/CallingConvLower.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/ValueTypes.h" |
| 33 | #include "llvm/Support/Debug.h" |
| 34 | #include <queue> |
| 35 | #include <set> |
| 36 | |
| 37 | using namespace llvm; |
| 38 | |
| 39 | const char *MipsTargetLowering:: |
| 40 | getTargetNodeName(unsigned Opcode) const |
| 41 | { |
| 42 | switch (Opcode) |
| 43 | { |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 44 | case MipsISD::JmpLink : return "MipsISD::JmpLink"; |
| 45 | case MipsISD::Hi : return "MipsISD::Hi"; |
| 46 | case MipsISD::Lo : return "MipsISD::Lo"; |
| 47 | case MipsISD::GPRel : return "MipsISD::GPRel"; |
| 48 | case MipsISD::Ret : return "MipsISD::Ret"; |
| 49 | case MipsISD::SelectCC : return "MipsISD::SelectCC"; |
| 50 | case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC"; |
| 51 | case MipsISD::FPBrcond : return "MipsISD::FPBrcond"; |
| 52 | case MipsISD::FPCmp : return "MipsISD::FPCmp"; |
| 53 | default : return NULL; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | MipsTargetLowering:: |
| 58 | MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM) |
| 59 | { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 60 | Subtarget = &TM.getSubtarget<MipsSubtarget>(); |
| 61 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 62 | // Mips does not have i1 type, so use i32 for |
| 63 | // setcc operations results (slt, sgt, ...). |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 64 | setSetCCResultContents(ZeroOrOneSetCCResult); |
| 65 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 66 | // JumpTable targets must use GOT when using PIC_ |
| 67 | setUsesGlobalOffsetTable(true); |
| 68 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 69 | // Set up the register classes |
| 70 | addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass); |
| 71 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 72 | // When dealing with single precision only, use libcalls |
| 73 | if (!Subtarget->isSingleFloat()) { |
| 74 | addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass); |
| 75 | if (!Subtarget->isFP64bit()) |
| 76 | addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass); |
| 77 | } else |
| 78 | addRegisterClass(MVT::f32, Mips::FGR32RegisterClass); |
| 79 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 80 | // Load extented operations for i1 types must be promoted |
| 81 | setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote); |
| 82 | setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 83 | setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 84 | |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 85 | // Mips Custom Operations |
| 86 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 87 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
| 88 | setOperationAction(ISD::RET, MVT::Other, Custom); |
| 89 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 90 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 91 | setOperationAction(ISD::SELECT, MVT::f32, Custom); |
| 92 | setOperationAction(ISD::SELECT, MVT::i32, Custom); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 93 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::SETCC, MVT::f32, Custom); |
| 95 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 96 | |
| 97 | // Operations not directly supported by Mips. |
Bruno Cardoso Lopes | 85e9212 | 2008-07-07 19:11:24 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 99 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 100 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
Bruno Cardoso Lopes | 85e9212 | 2008-07-07 19:11:24 +0000 | [diff] [blame] | 101 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 102 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 104 | setOperationAction(ISD::CTPOP, MVT::i32, Expand); |
| 105 | setOperationAction(ISD::CTTZ, MVT::i32, Expand); |
| 106 | setOperationAction(ISD::CTLZ, MVT::i32, Expand); |
| 107 | setOperationAction(ISD::ROTL, MVT::i32, Expand); |
| 108 | setOperationAction(ISD::ROTR, MVT::i32, Expand); |
| 109 | setOperationAction(ISD::BSWAP, MVT::i32, Expand); |
| 110 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); |
| 111 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); |
| 112 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); |
| 113 | |
| 114 | // We don't have line number support yet. |
| 115 | setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); |
| 116 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
| 117 | setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand); |
| 118 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| 119 | |
| 120 | // Use the default for now |
| 121 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 122 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 123 | setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand); |
Bruno Cardoso Lopes | 85e9212 | 2008-07-07 19:11:24 +0000 | [diff] [blame] | 124 | |
| 125 | if (Subtarget->isSingleFloat()) |
| 126 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 127 | |
Bruno Cardoso Lopes | 7728f7e | 2008-07-09 05:32:22 +0000 | [diff] [blame] | 128 | if (!Subtarget->hasSEInReg()) { |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 129 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 130 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); |
| 131 | } |
| 132 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 133 | setStackPointerRegisterToSaveRestore(Mips::SP); |
| 134 | computeRegisterProperties(); |
| 135 | } |
| 136 | |
| 137 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 138 | MVT MipsTargetLowering::getSetCCResultType(const SDValue &) const { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 139 | return MVT::i32; |
| 140 | } |
| 141 | |
| 142 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 143 | SDValue MipsTargetLowering:: |
| 144 | LowerOperation(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 145 | { |
| 146 | switch (Op.getOpcode()) |
| 147 | { |
| 148 | case ISD::CALL: return LowerCALL(Op, DAG); |
| 149 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
| 150 | case ISD::RET: return LowerRET(Op, DAG); |
| 151 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 152 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 153 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 154 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 155 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 156 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 157 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
| 158 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 159 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 160 | return SDValue(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | // Lower helper functions |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | // AddLiveIn - This helper function adds the specified physical register to the |
| 168 | // MachineFunction as a live in value. It also creates a corresponding |
| 169 | // virtual register for it. |
| 170 | static unsigned |
| 171 | AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC) |
| 172 | { |
| 173 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 174 | unsigned VReg = MF.getRegInfo().createVirtualRegister(RC); |
| 175 | MF.getRegInfo().addLiveIn(PReg, VReg); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 176 | return VReg; |
| 177 | } |
| 178 | |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 179 | // A address must be loaded from a small section if its size is less than the |
| 180 | // small section size threshold. Data in this section must be addressed using |
| 181 | // gp_rel operator. |
| 182 | bool MipsTargetLowering::IsInSmallSection(unsigned Size) { |
| 183 | return (Size > 0 && (Size <= Subtarget->getSSectionThreshold())); |
| 184 | } |
| 185 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 186 | // Discover if this global address can be placed into small data/bss section. |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 187 | bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV) |
| 188 | { |
| 189 | const TargetData *TD = getTargetData(); |
Bruno Cardoso Lopes | feb95cc | 2008-07-22 15:34:27 +0000 | [diff] [blame] | 190 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); |
| 191 | |
| 192 | if (!GVA) |
| 193 | return false; |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 194 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 195 | const Type *Ty = GV->getType()->getElementType(); |
| 196 | unsigned Size = TD->getABITypeSize(Ty); |
| 197 | |
| 198 | // if this is a internal constant string, there is a special |
| 199 | // section for it, but not in small data/bss. |
| 200 | if (GVA->hasInitializer() && GV->hasInternalLinkage()) { |
| 201 | Constant *C = GVA->getInitializer(); |
| 202 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 203 | if (CVA && CVA->isCString()) |
| 204 | return false; |
| 205 | } |
| 206 | |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 207 | return IsInSmallSection(Size); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 210 | // Get fp branch code (not opcode) from condition code. |
| 211 | static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) { |
| 212 | if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT) |
| 213 | return Mips::BRANCH_T; |
| 214 | |
| 215 | if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) |
| 216 | return Mips::BRANCH_F; |
| 217 | |
| 218 | return Mips::BRANCH_INVALID; |
| 219 | } |
| 220 | |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 221 | static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) { |
| 222 | switch(BC) { |
| 223 | default: |
| 224 | assert(0 && "Unknown branch code"); |
| 225 | case Mips::BRANCH_T : return Mips::BC1T; |
| 226 | case Mips::BRANCH_F : return Mips::BC1F; |
| 227 | case Mips::BRANCH_TL : return Mips::BC1TL; |
| 228 | case Mips::BRANCH_FL : return Mips::BC1FL; |
| 229 | } |
| 230 | } |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 231 | |
| 232 | static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) { |
| 233 | switch (CC) { |
| 234 | default: assert(0 && "Unknown fp condition code!"); |
| 235 | case ISD::SETEQ: |
| 236 | case ISD::SETOEQ: return Mips::FCOND_EQ; |
| 237 | case ISD::SETUNE: return Mips::FCOND_OGL; |
| 238 | case ISD::SETLT: |
| 239 | case ISD::SETOLT: return Mips::FCOND_OLT; |
| 240 | case ISD::SETGT: |
| 241 | case ISD::SETOGT: return Mips::FCOND_OGT; |
| 242 | case ISD::SETLE: |
| 243 | case ISD::SETOLE: return Mips::FCOND_OLE; |
| 244 | case ISD::SETGE: |
| 245 | case ISD::SETOGE: return Mips::FCOND_OGE; |
| 246 | case ISD::SETULT: return Mips::FCOND_ULT; |
| 247 | case ISD::SETULE: return Mips::FCOND_ULE; |
| 248 | case ISD::SETUGT: return Mips::FCOND_UGT; |
| 249 | case ISD::SETUGE: return Mips::FCOND_UGE; |
| 250 | case ISD::SETUO: return Mips::FCOND_UN; |
| 251 | case ISD::SETO: return Mips::FCOND_OR; |
| 252 | case ISD::SETNE: |
| 253 | case ISD::SETONE: return Mips::FCOND_NEQ; |
| 254 | case ISD::SETUEQ: return Mips::FCOND_UEQ; |
| 255 | } |
| 256 | } |
| 257 | |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 258 | MachineBasicBlock * |
| 259 | MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
| 260 | MachineBasicBlock *BB) |
| 261 | { |
| 262 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 263 | bool isFPCmp = false; |
| 264 | |
| 265 | switch (MI->getOpcode()) { |
| 266 | default: assert(false && "Unexpected instr type to insert"); |
| 267 | case Mips::Select_FCC: |
| 268 | case Mips::Select_FCC_SO32: |
| 269 | case Mips::Select_FCC_AS32: |
| 270 | case Mips::Select_FCC_D32: |
| 271 | isFPCmp = true; // FALL THROUGH |
| 272 | case Mips::Select_CC: |
| 273 | case Mips::Select_CC_SO32: |
| 274 | case Mips::Select_CC_AS32: |
| 275 | case Mips::Select_CC_D32: { |
| 276 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 277 | // diamond control-flow pattern. The incoming instruction knows the |
| 278 | // destination vreg to set, the condition code register to branch on, the |
| 279 | // true/false values to select between, and a branch opcode to use. |
| 280 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 281 | MachineFunction::iterator It = BB; |
| 282 | ++It; |
| 283 | |
| 284 | // thisMBB: |
| 285 | // ... |
| 286 | // TrueVal = ... |
| 287 | // setcc r1, r2, r3 |
| 288 | // bNE r1, r0, copy1MBB |
| 289 | // fallthrough --> copy0MBB |
| 290 | MachineBasicBlock *thisMBB = BB; |
| 291 | MachineFunction *F = BB->getParent(); |
| 292 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 293 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 294 | |
| 295 | // Emit the right instruction according to the type of the operands compared |
| 296 | if (isFPCmp) { |
| 297 | // Find the condiction code present in the setcc operation. |
| 298 | Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm(); |
| 299 | // Get the branch opcode from the branch code. |
| 300 | unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC)); |
| 301 | BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB); |
| 302 | } else |
| 303 | BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg()) |
| 304 | .addReg(Mips::ZERO).addMBB(sinkMBB); |
| 305 | |
| 306 | F->insert(It, copy0MBB); |
| 307 | F->insert(It, sinkMBB); |
| 308 | // Update machine-CFG edges by first adding all successors of the current |
| 309 | // block to the new block which will contain the Phi node for the select. |
| 310 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
| 311 | e = BB->succ_end(); i != e; ++i) |
| 312 | sinkMBB->addSuccessor(*i); |
| 313 | // Next, remove all successors of the current block, and add the true |
| 314 | // and fallthrough blocks as its successors. |
| 315 | while(!BB->succ_empty()) |
| 316 | BB->removeSuccessor(BB->succ_begin()); |
| 317 | BB->addSuccessor(copy0MBB); |
| 318 | BB->addSuccessor(sinkMBB); |
| 319 | |
| 320 | // copy0MBB: |
| 321 | // %FalseValue = ... |
| 322 | // # fallthrough to sinkMBB |
| 323 | BB = copy0MBB; |
| 324 | |
| 325 | // Update machine-CFG edges |
| 326 | BB->addSuccessor(sinkMBB); |
| 327 | |
| 328 | // sinkMBB: |
| 329 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 330 | // ... |
| 331 | BB = sinkMBB; |
| 332 | BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg()) |
| 333 | .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) |
| 334 | .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB); |
| 335 | |
| 336 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
| 337 | return BB; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 342 | //===----------------------------------------------------------------------===// |
| 343 | // Misc Lower Operation implementation |
| 344 | //===----------------------------------------------------------------------===// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 345 | SDValue MipsTargetLowering:: |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 346 | LowerBRCOND(SDValue Op, SelectionDAG &DAG) |
| 347 | { |
| 348 | // The first operand is the chain, the second is the condition, the third is |
| 349 | // the block to branch to if the condition is true. |
| 350 | SDValue Chain = Op.getOperand(0); |
| 351 | SDValue Dest = Op.getOperand(2); |
| 352 | SDValue CondRes; |
| 353 | |
| 354 | if (Op.getOperand(1).getOpcode() == ISD::AND) |
| 355 | CondRes = Op.getOperand(1).getOperand(0); |
| 356 | else if (Op.getOperand(1).getOpcode() == MipsISD::FPCmp) |
| 357 | CondRes = Op.getOperand(1); |
| 358 | else |
| 359 | assert(0 && "Incoming condition flag unknown"); |
| 360 | |
| 361 | SDValue CCNode = CondRes.getOperand(2); |
| 362 | Mips::CondCode CC = (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getValue(); |
| 363 | SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32); |
| 364 | |
| 365 | return DAG.getNode(MipsISD::FPBrcond, Op.getValueType(), Chain, BrCode, |
| 366 | Dest, CondRes); |
| 367 | } |
| 368 | |
| 369 | SDValue MipsTargetLowering:: |
| 370 | LowerSETCC(SDValue Op, SelectionDAG &DAG) |
| 371 | { |
| 372 | // The operands to this are the left and right operands to compare (ops #0, |
| 373 | // and #1) and the condition code to compare them with (op #2) as a |
| 374 | // CondCodeSDNode. |
| 375 | SDValue LHS = Op.getOperand(0); |
| 376 | SDValue RHS = Op.getOperand(1); |
| 377 | |
| 378 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| 379 | |
| 380 | return DAG.getNode(MipsISD::FPCmp, Op.getValueType(), LHS, RHS, |
| 381 | DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32)); |
| 382 | } |
| 383 | |
| 384 | SDValue MipsTargetLowering:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 385 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 386 | { |
| 387 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 388 | SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32); |
Bruno Cardoso Lopes | 7ff6fa2 | 2007-08-18 02:16:30 +0000 | [diff] [blame] | 389 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 390 | if (!Subtarget->hasABICall()) { |
| 391 | if (isa<Function>(GV)) return GA; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 392 | const MVT *VTs = DAG.getNodeValueTypes(MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 393 | SDValue Ops[] = { GA }; |
Bruno Cardoso Lopes | 7ff6fa2 | 2007-08-18 02:16:30 +0000 | [diff] [blame] | 394 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 395 | if (IsGlobalInSmallSection(GV)) { // %gp_rel relocation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 396 | SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1); |
| 397 | SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 398 | return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); |
| 399 | } |
| 400 | // %hi/%lo relocation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 401 | SDValue HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1); |
| 402 | SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 403 | return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 404 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 405 | } else { // Abicall relocations, TODO: make this cleaner. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 406 | SDValue ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 407 | // On functions and global targets not internal linked only |
| 408 | // a load from got/GP is necessary for PIC to work. |
| 409 | if (!GV->hasInternalLinkage() || isa<Function>(GV)) |
| 410 | return ResNode; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 411 | SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 412 | return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo); |
| 413 | } |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 414 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 415 | assert(0 && "Dont know how to handle GlobalAddress"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 416 | return SDValue(0,0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 419 | SDValue MipsTargetLowering:: |
| 420 | LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 421 | { |
| 422 | assert(0 && "TLS not implemented for MIPS."); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 423 | return SDValue(); // Not reached |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 426 | SDValue MipsTargetLowering:: |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 427 | LowerSELECT(SDValue Op, SelectionDAG &DAG) |
| 428 | { |
| 429 | SDValue Cond = Op.getOperand(0); |
| 430 | SDValue True = Op.getOperand(1); |
| 431 | SDValue False = Op.getOperand(2); |
| 432 | |
| 433 | // this can be a fp select but with a setcc comming from a |
| 434 | // integer compare. |
| 435 | if (Cond.getOpcode() == ISD::SETCC) |
| 436 | if (Cond.getOperand(0).getValueType().isInteger()) |
| 437 | return DAG.getNode(MipsISD::SelectCC, True.getValueType(), |
| 438 | Cond, True, False); |
| 439 | |
| 440 | // Otherwise we're dealing with floating point compare. |
| 441 | SDValue CondRes; |
| 442 | if (Cond.getOpcode() == ISD::AND) |
| 443 | CondRes = Cond.getOperand(0); |
| 444 | else if (Cond.getOpcode() == MipsISD::FPCmp) |
| 445 | CondRes = Cond; |
| 446 | else |
| 447 | assert(0 && "Incoming condition flag unknown"); |
| 448 | |
| 449 | SDValue CCNode = CondRes.getOperand(2); |
| 450 | return DAG.getNode(MipsISD::FPSelectCC, True.getValueType(), |
| 451 | CondRes, True, False, CCNode); |
| 452 | } |
| 453 | |
| 454 | SDValue MipsTargetLowering:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 455 | LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 456 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 457 | SDValue LHS = Op.getOperand(0); |
| 458 | SDValue RHS = Op.getOperand(1); |
| 459 | SDValue True = Op.getOperand(2); |
| 460 | SDValue False = Op.getOperand(3); |
| 461 | SDValue CC = Op.getOperand(4); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 462 | |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame^] | 463 | SDValue SetCCRes = DAG.getNode(ISD::SETCC, LHS.getValueType(), LHS, RHS, CC); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 464 | return DAG.getNode(MipsISD::SelectCC, True.getValueType(), |
| 465 | SetCCRes, True, False); |
| 466 | } |
| 467 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 468 | SDValue MipsTargetLowering:: |
| 469 | LowerJumpTable(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 470 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 471 | SDValue ResNode; |
| 472 | SDValue HiPart; |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 473 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 474 | MVT PtrVT = Op.getValueType(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 475 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 476 | SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 477 | |
| 478 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 479 | const MVT *VTs = DAG.getNodeValueTypes(MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 480 | SDValue Ops[] = { JTI }; |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 481 | HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1); |
| 482 | } else // Emit Load from Global Pointer |
| 483 | HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0); |
| 484 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 485 | SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 486 | ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo); |
| 487 | |
| 488 | return ResNode; |
| 489 | } |
| 490 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 491 | SDValue MipsTargetLowering:: |
| 492 | LowerConstantPool(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 493 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 494 | SDValue ResNode; |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 495 | ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); |
| 496 | Constant *C = N->getConstVal(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 497 | SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment()); |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 498 | |
| 499 | // gp_rel relocation |
Bruno Cardoso Lopes | f33bc43 | 2008-07-28 19:26:25 +0000 | [diff] [blame] | 500 | // FIXME: we should reference the constant pool using small data sections, |
| 501 | // but the asm printer currently doens't support this feature without |
| 502 | // hacking it. This feature should come soon so we can uncomment the |
| 503 | // stuff below. |
| 504 | //if (!Subtarget->hasABICall() && |
| 505 | // IsInSmallSection(getTargetData()->getABITypeSize(C->getType()))) { |
| 506 | // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP); |
| 507 | // SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32); |
| 508 | // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); |
| 509 | //} else { // %hi/%lo relocation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 510 | SDValue HiPart = DAG.getNode(MipsISD::Hi, MVT::i32, CP); |
| 511 | SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, CP); |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 512 | ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo); |
Bruno Cardoso Lopes | f33bc43 | 2008-07-28 19:26:25 +0000 | [diff] [blame] | 513 | //} |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 514 | |
| 515 | return ResNode; |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 518 | //===----------------------------------------------------------------------===// |
| 519 | // Calling Convention Implementation |
| 520 | // |
| 521 | // The lower operations present on calling convention works on this order: |
| 522 | // LowerCALL (virt regs --> phys regs, virt regs --> stack) |
| 523 | // LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs) |
| 524 | // LowerRET (virt regs --> phys regs) |
| 525 | // LowerCALL (phys regs --> virt regs) |
| 526 | // |
| 527 | //===----------------------------------------------------------------------===// |
| 528 | |
| 529 | #include "MipsGenCallingConv.inc" |
| 530 | |
| 531 | //===----------------------------------------------------------------------===// |
| 532 | // CALL Calling Convention Implementation |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | |
| 535 | /// Mips custom CALL implementation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 536 | SDValue MipsTargetLowering:: |
| 537 | LowerCALL(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 538 | { |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 539 | unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 540 | |
| 541 | // By now, only CallingConv::C implemented |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 542 | switch (CallingConv) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 543 | default: |
| 544 | assert(0 && "Unsupported calling convention"); |
| 545 | case CallingConv::Fast: |
| 546 | case CallingConv::C: |
| 547 | return LowerCCCCallTo(Op, DAG, CallingConv); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /// LowerCCCCallTo - functions arguments are copied from virtual |
| 552 | /// regs to (physical regs)/(stack frame), CALLSEQ_START and |
| 553 | /// CALLSEQ_END are emitted. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 554 | /// TODO: isVarArg, isTailCall. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 555 | SDValue MipsTargetLowering:: |
| 556 | LowerCCCCallTo(SDValue Op, SelectionDAG &DAG, unsigned CC) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 557 | { |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 558 | MachineFunction &MF = DAG.getMachineFunction(); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 559 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 560 | SDValue Chain = Op.getOperand(0); |
| 561 | SDValue Callee = Op.getOperand(4); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 562 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 563 | |
| 564 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 565 | |
| 566 | // Analyze operands of the call, assigning locations to each operand. |
| 567 | SmallVector<CCValAssign, 16> ArgLocs; |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 568 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
| 569 | |
Bruno Cardoso Lopes | b27cb55 | 2008-07-15 02:03:36 +0000 | [diff] [blame] | 570 | // To meet O32 ABI, Mips must always allocate 16 bytes on |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 571 | // the stack (even if less than 4 are used as arguments) |
Bruno Cardoso Lopes | b27cb55 | 2008-07-15 02:03:36 +0000 | [diff] [blame] | 572 | if (Subtarget->isABI_O32()) { |
| 573 | int VTsize = MVT(MVT::i32).getSizeInBits()/8; |
| 574 | MFI->CreateFixedObject(VTsize, (VTsize*3)); |
| 575 | } |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 576 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 577 | CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips); |
| 578 | |
| 579 | // Get a count of how many bytes are to be pushed on the stack. |
| 580 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 581 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, |
| 582 | getPointerTy())); |
| 583 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 584 | // With EABI is it possible to have 16 args on registers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 585 | SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass; |
| 586 | SmallVector<SDValue, 8> MemOpChains; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 587 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 588 | // First/LastArgStackLoc contains the first/last |
| 589 | // "at stack" argument location. |
| 590 | int LastArgStackLoc = 0; |
| 591 | unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 592 | |
| 593 | // Walk the register/memloc assignments, inserting copies/loads. |
| 594 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 595 | CCValAssign &VA = ArgLocs[i]; |
| 596 | |
| 597 | // Arguments start after the 5 first operands of ISD::CALL |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 598 | SDValue Arg = Op.getOperand(5+2*VA.getValNo()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 599 | |
| 600 | // Promote the value if needed. |
| 601 | switch (VA.getLocInfo()) { |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 602 | default: assert(0 && "Unknown loc info!"); |
| 603 | case CCValAssign::Full: break; |
| 604 | case CCValAssign::SExt: |
| 605 | Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); |
| 606 | break; |
| 607 | case CCValAssign::ZExt: |
| 608 | Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); |
| 609 | break; |
| 610 | case CCValAssign::AExt: |
| 611 | Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); |
| 612 | break; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 615 | // Arguments that can be passed on register must be kept at |
| 616 | // RegsToPass vector |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 617 | if (VA.isRegLoc()) { |
| 618 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 619 | continue; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 620 | } |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 621 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 622 | // Register cant get to this point... |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 623 | assert(VA.isMemLoc()); |
| 624 | |
| 625 | // Create the frame index object for this incoming parameter |
| 626 | // This guarantees that when allocating Local Area the firsts |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 627 | // 16 bytes which are alwayes reserved won't be overwritten |
| 628 | // if O32 ABI is used. For EABI the first address is zero. |
| 629 | LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset()); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 630 | int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 631 | LastArgStackLoc); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 632 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 633 | SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy()); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 634 | |
| 635 | // emit ISD::STORE whichs stores the |
| 636 | // parameter value to a stack Location |
| 637 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 640 | // Transform all store nodes into one single node because all store |
| 641 | // nodes are independent of each other. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 642 | if (!MemOpChains.empty()) |
| 643 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 644 | &MemOpChains[0], MemOpChains.size()); |
| 645 | |
| 646 | // Build a sequence of copy-to-reg nodes chained together with token |
| 647 | // chain and flag operands which copy the outgoing args into registers. |
| 648 | // The InFlag in necessary since all emited instructions must be |
| 649 | // stuck together. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 650 | SDValue InFlag; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 651 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 652 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, |
| 653 | RegsToPass[i].second, InFlag); |
| 654 | InFlag = Chain.getValue(1); |
| 655 | } |
| 656 | |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 657 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 658 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 659 | // node so that legalize doesn't hack it. |
| 660 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 661 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 662 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 663 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 664 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 665 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 666 | // MipsJmpLink = #chain, #target_address, #opt_in_flags... |
| 667 | // = Chain, Callee, Reg#1, Reg#2, ... |
| 668 | // |
| 669 | // Returns a chain & a flag for retval copy to use. |
| 670 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 671 | SmallVector<SDValue, 8> Ops; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 672 | Ops.push_back(Chain); |
| 673 | Ops.push_back(Callee); |
| 674 | |
| 675 | // Add argument registers to the end of the list so that they are |
| 676 | // known live into the call. |
| 677 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 678 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 679 | RegsToPass[i].second.getValueType())); |
| 680 | |
| 681 | if (InFlag.Val) |
| 682 | Ops.push_back(InFlag); |
| 683 | |
| 684 | Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size()); |
| 685 | InFlag = Chain.getValue(1); |
| 686 | |
Bruno Cardoso Lopes | d2947ee | 2008-06-04 01:45:25 +0000 | [diff] [blame] | 687 | // Create the CALLSEQ_END node. |
Bruno Cardoso Lopes | bdbb750 | 2008-06-01 03:49:39 +0000 | [diff] [blame] | 688 | Chain = DAG.getCALLSEQ_END(Chain, |
| 689 | DAG.getConstant(NumBytes, getPointerTy()), |
| 690 | DAG.getConstant(0, getPointerTy()), |
| 691 | InFlag); |
| 692 | InFlag = Chain.getValue(1); |
| 693 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 694 | // Create a stack location to hold GP when PIC is used. This stack |
| 695 | // location is used on function prologue to save GP and also after all |
| 696 | // emited CALL's to restore GP. |
| 697 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 698 | // Function can have an arbitrary number of calls, so |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 699 | // hold the LastArgStackLoc with the biggest offset. |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 700 | int FI; |
| 701 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 702 | if (LastArgStackLoc >= MipsFI->getGPStackOffset()) { |
| 703 | LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 704 | // Create the frame index only once. SPOffset here can be anything |
| 705 | // (this will be fixed on processFunctionBeforeFrameFinalized) |
| 706 | if (MipsFI->getGPStackOffset() == -1) { |
| 707 | FI = MFI->CreateFixedObject(4, 0); |
| 708 | MipsFI->setGPFI(FI); |
| 709 | } |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 710 | MipsFI->setGPStackOffset(LastArgStackLoc); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 713 | // Reload GP value. |
| 714 | FI = MipsFI->getGPFI(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 715 | SDValue FIN = DAG.getFrameIndex(FI,getPointerTy()); |
| 716 | SDValue GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 717 | Chain = GPLoad.getValue(1); |
| 718 | Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32), |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 719 | GPLoad, SDValue(0,0)); |
Bruno Cardoso Lopes | bdbb750 | 2008-06-01 03:49:39 +0000 | [diff] [blame] | 720 | InFlag = Chain.getValue(1); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 723 | // Handle result values, copying them out of physregs into vregs that we |
| 724 | // return. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 725 | return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// LowerCallResult - Lower the result values of an ISD::CALL into the |
| 729 | /// appropriate copies out of appropriate physical registers. This assumes that |
| 730 | /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call |
| 731 | /// being lowered. Returns a SDNode with the same number of values as the |
| 732 | /// ISD::CALL. |
| 733 | SDNode *MipsTargetLowering:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 734 | LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall, |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 735 | unsigned CallingConv, SelectionDAG &DAG) { |
| 736 | |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 737 | bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0; |
| 738 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 739 | // Assign locations to each value returned by this call. |
| 740 | SmallVector<CCValAssign, 16> RVLocs; |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 741 | CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs); |
| 742 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 743 | CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 744 | SmallVector<SDValue, 8> ResultVals; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 745 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 746 | // Copy all of the result registers out of their specified physreg. |
| 747 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 748 | Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(), |
| 749 | RVLocs[i].getValVT(), InFlag).getValue(1); |
| 750 | InFlag = Chain.getValue(2); |
| 751 | ResultVals.push_back(Chain.getValue(0)); |
| 752 | } |
| 753 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 754 | ResultVals.push_back(Chain); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 755 | |
| 756 | // Merge everything together with a MERGE_VALUES node. |
Duncan Sands | f951620 | 2008-06-30 10:19:09 +0000 | [diff] [blame] | 757 | return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0], |
| 758 | ResultVals.size()).Val; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | //===----------------------------------------------------------------------===// |
| 762 | // FORMAL_ARGUMENTS Calling Convention Implementation |
| 763 | //===----------------------------------------------------------------------===// |
| 764 | |
| 765 | /// Mips custom FORMAL_ARGUMENTS implementation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 766 | SDValue MipsTargetLowering:: |
| 767 | LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 768 | { |
| 769 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 770 | switch(CC) |
| 771 | { |
| 772 | default: |
| 773 | assert(0 && "Unsupported calling convention"); |
| 774 | case CallingConv::C: |
| 775 | return LowerCCCArguments(Op, DAG); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /// LowerCCCArguments - transform physical registers into |
| 780 | /// virtual registers and generate load operations for |
| 781 | /// arguments places on the stack. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 782 | /// TODO: isVarArg |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 783 | SDValue MipsTargetLowering:: |
| 784 | LowerCCCArguments(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 785 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 786 | SDValue Root = Op.getOperand(0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 787 | MachineFunction &MF = DAG.getMachineFunction(); |
| 788 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 789 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 790 | |
| 791 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 792 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
| 793 | |
| 794 | unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 795 | |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 796 | // GP must be live into PIC and non-PIC call target. |
| 797 | AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 798 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 799 | // Assign locations to all of the incoming arguments. |
| 800 | SmallVector<CCValAssign, 16> ArgLocs; |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 801 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
| 802 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 803 | CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 804 | SmallVector<SDValue, 16> ArgValues; |
| 805 | SDValue StackPtr; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 806 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 807 | unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16); |
| 808 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 809 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 810 | |
| 811 | CCValAssign &VA = ArgLocs[i]; |
| 812 | |
| 813 | // Arguments stored on registers |
| 814 | if (VA.isRegLoc()) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 815 | MVT RegVT = VA.getLocVT(); |
Bill Wendling | 06b8c19 | 2008-07-09 05:55:53 +0000 | [diff] [blame] | 816 | TargetRegisterClass *RC = 0; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 817 | |
| 818 | if (RegVT == MVT::i32) |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 819 | RC = Mips::CPURegsRegisterClass; |
| 820 | else if (RegVT == MVT::f32) { |
| 821 | if (Subtarget->isSingleFloat()) |
| 822 | RC = Mips::FGR32RegisterClass; |
| 823 | else |
| 824 | RC = Mips::AFGR32RegisterClass; |
| 825 | } else if (RegVT == MVT::f64) { |
| 826 | if (!Subtarget->isSingleFloat()) |
| 827 | RC = Mips::AFGR64RegisterClass; |
| 828 | } else |
| 829 | assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 830 | |
| 831 | // Transform the arguments stored on |
| 832 | // physical registers into virtual ones |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 833 | unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 834 | SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 835 | |
| 836 | // If this is an 8 or 16-bit value, it is really passed promoted |
| 837 | // to 32 bits. Insert an assert[sz]ext to capture this, then |
| 838 | // truncate to the right size. |
| 839 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 840 | ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, |
| 841 | DAG.getValueType(VA.getValVT())); |
| 842 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 843 | ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, |
| 844 | DAG.getValueType(VA.getValVT())); |
| 845 | |
| 846 | if (VA.getLocInfo() != CCValAssign::Full) |
| 847 | ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); |
| 848 | |
| 849 | ArgValues.push_back(ArgValue); |
| 850 | |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 851 | // To meet ABI, when VARARGS are passed on registers, the registers |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 852 | // must have their values written to the caller stack frame. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 853 | if ((isVarArg) && (Subtarget->isABI_O32())) { |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 854 | if (StackPtr.Val == 0) |
| 855 | StackPtr = DAG.getRegister(StackReg, getPointerTy()); |
| 856 | |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 857 | // The stack pointer offset is relative to the caller stack frame. |
| 858 | // Since the real stack size is unknown here, a negative SPOffset |
| 859 | // is used so there's a way to adjust these offsets when the stack |
| 860 | // size get known (on EliminateFrameIndex). A dummy SPOffset is |
| 861 | // used instead of a direct negative address (which is recorded to |
| 862 | // be used on emitPrologue) to avoid mis-calc of the first stack |
| 863 | // offset on PEI::calculateFrameObjectOffsets. |
| 864 | // Arguments are always 32-bit. |
| 865 | int FI = MFI->CreateFixedObject(4, 0); |
| 866 | MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4))); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 867 | SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy()); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 868 | |
| 869 | // emit ISD::STORE whichs stores the |
| 870 | // parameter value to a stack Location |
| 871 | ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0)); |
| 872 | } |
| 873 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 874 | } else { // VA.isRegLoc() |
| 875 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 876 | // sanity check |
| 877 | assert(VA.isMemLoc()); |
| 878 | |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 879 | // The stack pointer offset is relative to the caller stack frame. |
| 880 | // Since the real stack size is unknown here, a negative SPOffset |
| 881 | // is used so there's a way to adjust these offsets when the stack |
| 882 | // size get known (on EliminateFrameIndex). A dummy SPOffset is |
| 883 | // used instead of a direct negative address (which is recorded to |
| 884 | // be used on emitPrologue) to avoid mis-calc of the first stack |
| 885 | // offset on PEI::calculateFrameObjectOffsets. |
| 886 | // Arguments are always 32-bit. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 887 | unsigned ArgSize = VA.getLocVT().getSizeInBits()/8; |
| 888 | int FI = MFI->CreateFixedObject(ArgSize, 0); |
| 889 | MipsFI->recordLoadArgsFI(FI, -(ArgSize+ |
| 890 | (FirstStackArgLoc + VA.getLocMemOffset()))); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 891 | |
| 892 | // Create load nodes to retrieve arguments from the stack |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 893 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 894 | ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0)); |
| 895 | } |
| 896 | } |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 897 | |
| 898 | // The mips ABIs for returning structs by value requires that we copy |
| 899 | // the sret argument into $v0 for the return. Save the argument into |
| 900 | // a virtual register so that we can access it from the return points. |
| 901 | if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 902 | unsigned Reg = MipsFI->getSRetReturnReg(); |
| 903 | if (!Reg) { |
| 904 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32)); |
| 905 | MipsFI->setSRetReturnReg(Reg); |
| 906 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 907 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 908 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root); |
| 909 | } |
| 910 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 911 | ArgValues.push_back(Root); |
| 912 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 913 | // Return the new list of results. |
Duncan Sands | f951620 | 2008-06-30 10:19:09 +0000 | [diff] [blame] | 914 | return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0], |
| 915 | ArgValues.size()).getValue(Op.ResNo); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | //===----------------------------------------------------------------------===// |
| 919 | // Return Value Calling Convention Implementation |
| 920 | //===----------------------------------------------------------------------===// |
| 921 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 922 | SDValue MipsTargetLowering:: |
| 923 | LowerRET(SDValue Op, SelectionDAG &DAG) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 924 | { |
| 925 | // CCValAssign - represent the assignment of |
| 926 | // the return value to a location |
| 927 | SmallVector<CCValAssign, 16> RVLocs; |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 928 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
| 929 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 930 | |
| 931 | // CCState - Info about the registers and stack slot. |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 932 | CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 933 | |
| 934 | // Analize return values of ISD::RET |
| 935 | CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips); |
| 936 | |
| 937 | // If this is the first return lowered for this function, add |
| 938 | // the regs to the liveout set for the function. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 939 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 940 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 941 | if (RVLocs[i].isRegLoc()) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 942 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | // The chain is always operand #0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 946 | SDValue Chain = Op.getOperand(0); |
| 947 | SDValue Flag; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 948 | |
| 949 | // Copy the result values into the output registers. |
| 950 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 951 | CCValAssign &VA = RVLocs[i]; |
| 952 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 953 | |
| 954 | // ISD::RET => ret chain, (regnum1,val1), ... |
| 955 | // So i*2+1 index only the regnums |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 956 | Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 957 | |
| 958 | // guarantee that all emitted copies are |
| 959 | // stuck together, avoiding something bad |
| 960 | Flag = Chain.getValue(1); |
| 961 | } |
| 962 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 963 | // The mips ABIs for returning structs by value requires that we copy |
| 964 | // the sret argument into $v0 for the return. We saved the argument into |
| 965 | // a virtual register in the entry block, so now we copy the value out |
| 966 | // and into $v0. |
| 967 | if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 968 | MachineFunction &MF = DAG.getMachineFunction(); |
| 969 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 970 | unsigned Reg = MipsFI->getSRetReturnReg(); |
| 971 | |
| 972 | if (!Reg) |
| 973 | assert(0 && "sret virtual register not created in the entry block"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 974 | SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy()); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 975 | |
| 976 | Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag); |
| 977 | Flag = Chain.getValue(1); |
| 978 | } |
| 979 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 980 | // Return on Mips is always a "jr $ra" |
| 981 | if (Flag.Val) |
| 982 | return DAG.getNode(MipsISD::Ret, MVT::Other, |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 983 | Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 984 | else // Return Void |
| 985 | return DAG.getNode(MipsISD::Ret, MVT::Other, |
Bruno Cardoso Lopes | 8262df3 | 2007-10-09 03:15:11 +0000 | [diff] [blame] | 986 | Chain, DAG.getRegister(Mips::RA, MVT::i32)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 987 | } |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 988 | |
| 989 | //===----------------------------------------------------------------------===// |
| 990 | // Mips Inline Assembly Support |
| 991 | //===----------------------------------------------------------------------===// |
| 992 | |
| 993 | /// getConstraintType - Given a constraint letter, return the type of |
| 994 | /// constraint it is for this target. |
| 995 | MipsTargetLowering::ConstraintType MipsTargetLowering:: |
| 996 | getConstraintType(const std::string &Constraint) const |
| 997 | { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 998 | // Mips specific constrainy |
| 999 | // GCC config/mips/constraints.md |
| 1000 | // |
| 1001 | // 'd' : An address register. Equivalent to r |
| 1002 | // unless generating MIPS16 code. |
| 1003 | // 'y' : Equivalent to r; retained for |
| 1004 | // backwards compatibility. |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 1005 | // 'f' : Floating Point registers. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1006 | if (Constraint.size() == 1) { |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1007 | switch (Constraint[0]) { |
| 1008 | default : break; |
| 1009 | case 'd': |
| 1010 | case 'y': |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1011 | case 'f': |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1012 | return C_RegisterClass; |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | return TargetLowering::getConstraintType(Constraint); |
| 1017 | } |
| 1018 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1019 | /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"), |
| 1020 | /// return a list of registers that can be used to satisfy the constraint. |
| 1021 | /// This should only be used for C_RegisterClass constraints. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1022 | std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering:: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1023 | getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1024 | { |
| 1025 | if (Constraint.size() == 1) { |
| 1026 | switch (Constraint[0]) { |
| 1027 | case 'r': |
| 1028 | return std::make_pair(0U, Mips::CPURegsRegisterClass); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1029 | case 'f': |
Duncan Sands | 1512642 | 2008-07-08 09:33:14 +0000 | [diff] [blame] | 1030 | if (VT == MVT::f32) { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1031 | if (Subtarget->isSingleFloat()) |
| 1032 | return std::make_pair(0U, Mips::FGR32RegisterClass); |
| 1033 | else |
| 1034 | return std::make_pair(0U, Mips::AFGR32RegisterClass); |
Duncan Sands | 1512642 | 2008-07-08 09:33:14 +0000 | [diff] [blame] | 1035 | } |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1036 | if (VT == MVT::f64) |
| 1037 | if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit())) |
| 1038 | return std::make_pair(0U, Mips::AFGR64RegisterClass); |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 1042 | } |
| 1043 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1044 | /// Given a register class constraint, like 'r', if this corresponds directly |
| 1045 | /// to an LLVM register class, return a register of 0 and the register class |
| 1046 | /// pointer. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1047 | std::vector<unsigned> MipsTargetLowering:: |
| 1048 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1049 | MVT VT) const |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1050 | { |
| 1051 | if (Constraint.size() != 1) |
| 1052 | return std::vector<unsigned>(); |
| 1053 | |
| 1054 | switch (Constraint[0]) { |
| 1055 | default : break; |
| 1056 | case 'r': |
| 1057 | // GCC Mips Constraint Letters |
| 1058 | case 'd': |
| 1059 | case 'y': |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1060 | return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3, |
| 1061 | Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1, |
| 1062 | Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7, |
| 1063 | Mips::T8, 0); |
| 1064 | |
| 1065 | case 'f': |
Duncan Sands | 1512642 | 2008-07-08 09:33:14 +0000 | [diff] [blame] | 1066 | if (VT == MVT::f32) { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1067 | if (Subtarget->isSingleFloat()) |
| 1068 | return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5, |
| 1069 | Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11, |
| 1070 | Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24, |
| 1071 | Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29, |
| 1072 | Mips::F30, Mips::F31, 0); |
| 1073 | else |
| 1074 | return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8, |
| 1075 | Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26, |
| 1076 | Mips::F28, Mips::F30, 0); |
Duncan Sands | 1512642 | 2008-07-08 09:33:14 +0000 | [diff] [blame] | 1077 | } |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (VT == MVT::f64) |
| 1080 | if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit())) |
| 1081 | return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4, |
| 1082 | Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13, |
| 1083 | Mips::D14, Mips::D15, 0); |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 1084 | } |
| 1085 | return std::vector<unsigned>(); |
| 1086 | } |