Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1 | //===-- MBlazeISelLowering.cpp - MBlaze DAG Lowering Implementation -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that MBlaze uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "mblaze-lower" |
| 16 | #include "MBlazeISelLowering.h" |
| 17 | #include "MBlazeMachineFunction.h" |
| 18 | #include "MBlazeTargetMachine.h" |
| 19 | #include "MBlazeTargetObjectFile.h" |
| 20 | #include "MBlazeSubtarget.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 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" |
| 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 32 | #include "llvm/CodeGen/ValueTypes.h" |
| 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/Support/ErrorHandling.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | using namespace llvm; |
| 37 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 38 | static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 39 | CCValAssign::LocInfo &LocInfo, |
| 40 | ISD::ArgFlagsTy &ArgFlags, |
| 41 | CCState &State); |
| 42 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 43 | const char *MBlazeTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 44 | switch (Opcode) { |
| 45 | case MBlazeISD::JmpLink : return "MBlazeISD::JmpLink"; |
| 46 | case MBlazeISD::GPRel : return "MBlazeISD::GPRel"; |
| 47 | case MBlazeISD::Wrap : return "MBlazeISD::Wrap"; |
| 48 | case MBlazeISD::ICmp : return "MBlazeISD::ICmp"; |
| 49 | case MBlazeISD::Ret : return "MBlazeISD::Ret"; |
| 50 | case MBlazeISD::Select_CC : return "MBlazeISD::Select_CC"; |
| 51 | default : return NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | MBlazeTargetLowering::MBlazeTargetLowering(MBlazeTargetMachine &TM) |
| 56 | : TargetLowering(TM, new MBlazeTargetObjectFile()) { |
| 57 | Subtarget = &TM.getSubtarget<MBlazeSubtarget>(); |
| 58 | |
| 59 | // MBlaze does not have i1 type, so use i32 for |
| 60 | // setcc operations results (slt, sgt, ...). |
| 61 | setBooleanContents(ZeroOrOneBooleanContent); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 62 | setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct? |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 63 | |
| 64 | // Set up the register classes |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 65 | addRegisterClass(MVT::i32, &MBlaze::GPRRegClass); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 66 | if (Subtarget->hasFPU()) { |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 67 | addRegisterClass(MVT::f32, &MBlaze::GPRRegClass); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 68 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
| 69 | } |
| 70 | |
| 71 | // Floating point operations which are not supported |
| 72 | setOperationAction(ISD::FREM, MVT::f32, Expand); |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 73 | setOperationAction(ISD::FMA, MVT::f32, Expand); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::UINT_TO_FP, MVT::i8, Expand); |
| 75 | setOperationAction(ISD::UINT_TO_FP, MVT::i16, Expand); |
| 76 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 77 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| 78 | setOperationAction(ISD::FP_ROUND, MVT::f32, Expand); |
| 79 | setOperationAction(ISD::FP_ROUND, MVT::f64, Expand); |
| 80 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 81 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 82 | setOperationAction(ISD::FSIN, MVT::f32, Expand); |
| 83 | setOperationAction(ISD::FCOS, MVT::f32, Expand); |
| 84 | setOperationAction(ISD::FPOWI, MVT::f32, Expand); |
| 85 | setOperationAction(ISD::FPOW, MVT::f32, Expand); |
| 86 | setOperationAction(ISD::FLOG, MVT::f32, Expand); |
| 87 | setOperationAction(ISD::FLOG2, MVT::f32, Expand); |
| 88 | setOperationAction(ISD::FLOG10, MVT::f32, Expand); |
| 89 | setOperationAction(ISD::FEXP, MVT::f32, Expand); |
| 90 | |
| 91 | // Load extented operations for i1 types must be promoted |
| 92 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
| 93 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 94 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 95 | |
Wesley Peck | 4da992a | 2010-10-21 19:48:38 +0000 | [diff] [blame] | 96 | // Sign extended loads must be expanded |
| 97 | setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); |
| 98 | setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); |
| 99 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 100 | // MBlaze has no REM or DIVREM operations. |
| 101 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 102 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 104 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
| 105 | |
| 106 | // If the processor doesn't support multiply then expand it |
| 107 | if (!Subtarget->hasMul()) { |
| 108 | setOperationAction(ISD::MUL, MVT::i32, Expand); |
| 109 | } |
| 110 | |
| 111 | // If the processor doesn't support 64-bit multiply then expand |
| 112 | if (!Subtarget->hasMul() || !Subtarget->hasMul64()) { |
| 113 | setOperationAction(ISD::MULHS, MVT::i32, Expand); |
| 114 | setOperationAction(ISD::MULHS, MVT::i64, Expand); |
| 115 | setOperationAction(ISD::MULHU, MVT::i32, Expand); |
| 116 | setOperationAction(ISD::MULHU, MVT::i64, Expand); |
| 117 | } |
| 118 | |
| 119 | // If the processor doesn't support division then expand |
| 120 | if (!Subtarget->hasDiv()) { |
| 121 | setOperationAction(ISD::UDIV, MVT::i32, Expand); |
| 122 | setOperationAction(ISD::SDIV, MVT::i32, Expand); |
| 123 | } |
| 124 | |
| 125 | // Expand unsupported conversions |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 126 | setOperationAction(ISD::BITCAST, MVT::f32, Expand); |
| 127 | setOperationAction(ISD::BITCAST, MVT::i32, Expand); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 128 | |
| 129 | // Expand SELECT_CC |
| 130 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 131 | |
| 132 | // MBlaze doesn't have MUL_LOHI |
| 133 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
| 134 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 135 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
| 136 | setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); |
| 137 | |
| 138 | // Used by legalize types to correctly generate the setcc result. |
| 139 | // Without this, every float setcc comes with a AND/OR with the result, |
| 140 | // we don't want this, since the fpcmp result goes to a flag register, |
| 141 | // which is used implicitly by brcond and select operations. |
| 142 | AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); |
| 143 | AddPromotedToType(ISD::SELECT, MVT::i1, MVT::i32); |
| 144 | AddPromotedToType(ISD::SELECT_CC, MVT::i1, MVT::i32); |
| 145 | |
| 146 | // MBlaze Custom Operations |
| 147 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 148 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
| 149 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
| 150 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 151 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 152 | // Variable Argument support |
| 153 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 154 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
| 155 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 156 | setOperationAction(ISD::VACOPY, MVT::Other, Expand); |
| 157 | |
| 158 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 159 | // Operations not directly supported by MBlaze. |
| 160 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
| 161 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 162 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 163 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 164 | setOperationAction(ISD::ROTL, MVT::i32, Expand); |
| 165 | setOperationAction(ISD::ROTR, MVT::i32, Expand); |
| 166 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); |
| 167 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); |
| 168 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); |
| 169 | setOperationAction(ISD::CTLZ, MVT::i32, Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 171 | setOperationAction(ISD::CTTZ, MVT::i32, Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 172 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::CTPOP, MVT::i32, Expand); |
| 174 | setOperationAction(ISD::BSWAP, MVT::i32, Expand); |
| 175 | |
| 176 | // We don't have line number support yet. |
| 177 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| 178 | |
| 179 | // Use the default for now |
| 180 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 181 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 182 | |
| 183 | // MBlaze doesn't have extending float->double load/store |
| 184 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 185 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 186 | |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 187 | setMinFunctionAlignment(2); |
| 188 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 189 | setStackPointerRegisterToSaveRestore(MBlaze::R1); |
| 190 | computeRegisterProperties(); |
| 191 | } |
| 192 | |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 193 | EVT MBlazeTargetLowering::getSetCCResultType(EVT VT) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 194 | return MVT::i32; |
| 195 | } |
| 196 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 197 | SDValue MBlazeTargetLowering::LowerOperation(SDValue Op, |
| 198 | SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 199 | switch (Op.getOpcode()) |
| 200 | { |
| 201 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 202 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 203 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
| 204 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
| 205 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 206 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 207 | } |
| 208 | return SDValue(); |
| 209 | } |
| 210 | |
| 211 | //===----------------------------------------------------------------------===// |
| 212 | // Lower helper functions |
| 213 | //===----------------------------------------------------------------------===// |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 214 | MachineBasicBlock* |
| 215 | MBlazeTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 216 | MachineBasicBlock *MBB) |
| 217 | const { |
| 218 | switch (MI->getOpcode()) { |
Craig Topper | bc21981 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 219 | default: llvm_unreachable("Unexpected instr type to insert"); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 220 | |
| 221 | case MBlaze::ShiftRL: |
| 222 | case MBlaze::ShiftRA: |
| 223 | case MBlaze::ShiftL: |
| 224 | return EmitCustomShift(MI, MBB); |
| 225 | |
| 226 | case MBlaze::Select_FCC: |
| 227 | case MBlaze::Select_CC: |
| 228 | return EmitCustomSelect(MI, MBB); |
| 229 | |
| 230 | case MBlaze::CAS32: |
| 231 | case MBlaze::SWP32: |
| 232 | case MBlaze::LAA32: |
| 233 | case MBlaze::LAS32: |
| 234 | case MBlaze::LAD32: |
| 235 | case MBlaze::LAO32: |
| 236 | case MBlaze::LAX32: |
| 237 | case MBlaze::LAN32: |
| 238 | return EmitCustomAtomic(MI, MBB); |
| 239 | |
| 240 | case MBlaze::MEMBARRIER: |
| 241 | // The Microblaze does not need memory barriers. Just delete the pseudo |
| 242 | // instruction and finish. |
| 243 | MI->eraseFromParent(); |
| 244 | return MBB; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | MachineBasicBlock* |
| 249 | MBlazeTargetLowering::EmitCustomShift(MachineInstr *MI, |
| 250 | MachineBasicBlock *MBB) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 251 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 252 | DebugLoc dl = MI->getDebugLoc(); |
| 253 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 254 | // To "insert" a shift left instruction, we actually have to insert a |
| 255 | // simple loop. The incoming instruction knows the destination vreg to |
| 256 | // set, the source vreg to operate over and the shift amount. |
| 257 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 258 | MachineFunction::iterator It = MBB; |
| 259 | ++It; |
| 260 | |
| 261 | // start: |
| 262 | // andi samt, samt, 31 |
| 263 | // beqid samt, finish |
| 264 | // add dst, src, r0 |
| 265 | // loop: |
| 266 | // addik samt, samt, -1 |
| 267 | // sra dst, dst |
| 268 | // bneid samt, loop |
| 269 | // nop |
| 270 | // finish: |
| 271 | MachineFunction *F = MBB->getParent(); |
| 272 | MachineRegisterInfo &R = F->getRegInfo(); |
| 273 | MachineBasicBlock *loop = F->CreateMachineBasicBlock(LLVM_BB); |
| 274 | MachineBasicBlock *finish = F->CreateMachineBasicBlock(LLVM_BB); |
| 275 | F->insert(It, loop); |
| 276 | F->insert(It, finish); |
| 277 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 278 | // Update machine-CFG edges by transferring adding all successors and |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 279 | // remaining instructions from the current block to the new block which |
| 280 | // will contain the Phi node for the select. |
| 281 | finish->splice(finish->begin(), MBB, |
| 282 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 283 | MBB->end()); |
| 284 | finish->transferSuccessorsAndUpdatePHIs(MBB); |
| 285 | |
| 286 | // Add the true and fallthrough blocks as its successors. |
| 287 | MBB->addSuccessor(loop); |
| 288 | MBB->addSuccessor(finish); |
| 289 | |
| 290 | // Next, add the finish block as a successor of the loop block |
| 291 | loop->addSuccessor(finish); |
| 292 | loop->addSuccessor(loop); |
| 293 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 294 | unsigned IAMT = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 295 | BuildMI(MBB, dl, TII->get(MBlaze::ANDI), IAMT) |
| 296 | .addReg(MI->getOperand(2).getReg()) |
| 297 | .addImm(31); |
| 298 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 299 | unsigned IVAL = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 300 | BuildMI(MBB, dl, TII->get(MBlaze::ADDIK), IVAL) |
| 301 | .addReg(MI->getOperand(1).getReg()) |
| 302 | .addImm(0); |
| 303 | |
| 304 | BuildMI(MBB, dl, TII->get(MBlaze::BEQID)) |
| 305 | .addReg(IAMT) |
| 306 | .addMBB(finish); |
| 307 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 308 | unsigned DST = R.createVirtualRegister(&MBlaze::GPRRegClass); |
| 309 | unsigned NDST = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 310 | BuildMI(loop, dl, TII->get(MBlaze::PHI), DST) |
| 311 | .addReg(IVAL).addMBB(MBB) |
| 312 | .addReg(NDST).addMBB(loop); |
| 313 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 314 | unsigned SAMT = R.createVirtualRegister(&MBlaze::GPRRegClass); |
| 315 | unsigned NAMT = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 316 | BuildMI(loop, dl, TII->get(MBlaze::PHI), SAMT) |
| 317 | .addReg(IAMT).addMBB(MBB) |
| 318 | .addReg(NAMT).addMBB(loop); |
| 319 | |
| 320 | if (MI->getOpcode() == MBlaze::ShiftL) |
| 321 | BuildMI(loop, dl, TII->get(MBlaze::ADD), NDST).addReg(DST).addReg(DST); |
| 322 | else if (MI->getOpcode() == MBlaze::ShiftRA) |
| 323 | BuildMI(loop, dl, TII->get(MBlaze::SRA), NDST).addReg(DST); |
| 324 | else if (MI->getOpcode() == MBlaze::ShiftRL) |
| 325 | BuildMI(loop, dl, TII->get(MBlaze::SRL), NDST).addReg(DST); |
| 326 | else |
| 327 | llvm_unreachable("Cannot lower unknown shift instruction"); |
| 328 | |
| 329 | BuildMI(loop, dl, TII->get(MBlaze::ADDIK), NAMT) |
| 330 | .addReg(SAMT) |
| 331 | .addImm(-1); |
| 332 | |
| 333 | BuildMI(loop, dl, TII->get(MBlaze::BNEID)) |
| 334 | .addReg(NAMT) |
| 335 | .addMBB(loop); |
| 336 | |
| 337 | BuildMI(*finish, finish->begin(), dl, |
| 338 | TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
| 339 | .addReg(IVAL).addMBB(MBB) |
| 340 | .addReg(NDST).addMBB(loop); |
| 341 | |
| 342 | // The pseudo instruction is no longer needed so remove it |
| 343 | MI->eraseFromParent(); |
| 344 | return finish; |
| 345 | } |
| 346 | |
| 347 | MachineBasicBlock* |
| 348 | MBlazeTargetLowering::EmitCustomSelect(MachineInstr *MI, |
| 349 | MachineBasicBlock *MBB) const { |
| 350 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 351 | DebugLoc dl = MI->getDebugLoc(); |
| 352 | |
| 353 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 354 | // diamond control-flow pattern. The incoming instruction knows the |
| 355 | // destination vreg to set, the condition code register to branch on, the |
| 356 | // true/false values to select between, and a branch opcode to use. |
| 357 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 358 | MachineFunction::iterator It = MBB; |
| 359 | ++It; |
| 360 | |
| 361 | // thisMBB: |
| 362 | // ... |
| 363 | // TrueVal = ... |
| 364 | // setcc r1, r2, r3 |
| 365 | // bNE r1, r0, copy1MBB |
| 366 | // fallthrough --> copy0MBB |
| 367 | MachineFunction *F = MBB->getParent(); |
| 368 | MachineBasicBlock *flsBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 369 | MachineBasicBlock *dneBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 370 | |
| 371 | unsigned Opc; |
| 372 | switch (MI->getOperand(4).getImm()) { |
| 373 | default: llvm_unreachable("Unknown branch condition"); |
| 374 | case MBlazeCC::EQ: Opc = MBlaze::BEQID; break; |
| 375 | case MBlazeCC::NE: Opc = MBlaze::BNEID; break; |
| 376 | case MBlazeCC::GT: Opc = MBlaze::BGTID; break; |
| 377 | case MBlazeCC::LT: Opc = MBlaze::BLTID; break; |
| 378 | case MBlazeCC::GE: Opc = MBlaze::BGEID; break; |
| 379 | case MBlazeCC::LE: Opc = MBlaze::BLEID; break; |
| 380 | } |
| 381 | |
| 382 | F->insert(It, flsBB); |
| 383 | F->insert(It, dneBB); |
| 384 | |
| 385 | // Transfer the remainder of MBB and its successor edges to dneBB. |
| 386 | dneBB->splice(dneBB->begin(), MBB, |
| 387 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 388 | MBB->end()); |
| 389 | dneBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 390 | |
| 391 | MBB->addSuccessor(flsBB); |
| 392 | MBB->addSuccessor(dneBB); |
| 393 | flsBB->addSuccessor(dneBB); |
| 394 | |
| 395 | BuildMI(MBB, dl, TII->get(Opc)) |
| 396 | .addReg(MI->getOperand(3).getReg()) |
| 397 | .addMBB(dneBB); |
| 398 | |
| 399 | // sinkMBB: |
| 400 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 401 | // ... |
| 402 | //BuildMI(dneBB, dl, TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
| 403 | // .addReg(MI->getOperand(1).getReg()).addMBB(flsBB) |
| 404 | // .addReg(MI->getOperand(2).getReg()).addMBB(BB); |
| 405 | |
| 406 | BuildMI(*dneBB, dneBB->begin(), dl, |
| 407 | TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
| 408 | .addReg(MI->getOperand(2).getReg()).addMBB(flsBB) |
| 409 | .addReg(MI->getOperand(1).getReg()).addMBB(MBB); |
| 410 | |
| 411 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| 412 | return dneBB; |
| 413 | } |
| 414 | |
| 415 | MachineBasicBlock* |
| 416 | MBlazeTargetLowering::EmitCustomAtomic(MachineInstr *MI, |
| 417 | MachineBasicBlock *MBB) const { |
| 418 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 419 | DebugLoc dl = MI->getDebugLoc(); |
| 420 | |
| 421 | // All atomic instructions on the Microblaze are implemented using the |
| 422 | // load-linked / store-conditional style atomic instruction sequences. |
| 423 | // Thus, all operations will look something like the following: |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 424 | // |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 425 | // start: |
| 426 | // lwx RV, RP, 0 |
| 427 | // <do stuff> |
| 428 | // swx RV, RP, 0 |
| 429 | // addic RC, R0, 0 |
| 430 | // bneid RC, start |
| 431 | // |
| 432 | // exit: |
| 433 | // |
| 434 | // To "insert" a shift left instruction, we actually have to insert a |
| 435 | // simple loop. The incoming instruction knows the destination vreg to |
| 436 | // set, the source vreg to operate over and the shift amount. |
| 437 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 438 | MachineFunction::iterator It = MBB; |
| 439 | ++It; |
| 440 | |
| 441 | // start: |
| 442 | // andi samt, samt, 31 |
| 443 | // beqid samt, finish |
| 444 | // add dst, src, r0 |
| 445 | // loop: |
| 446 | // addik samt, samt, -1 |
| 447 | // sra dst, dst |
| 448 | // bneid samt, loop |
| 449 | // nop |
| 450 | // finish: |
| 451 | MachineFunction *F = MBB->getParent(); |
| 452 | MachineRegisterInfo &R = F->getRegInfo(); |
| 453 | |
| 454 | // Create the start and exit basic blocks for the atomic operation |
| 455 | MachineBasicBlock *start = F->CreateMachineBasicBlock(LLVM_BB); |
| 456 | MachineBasicBlock *exit = F->CreateMachineBasicBlock(LLVM_BB); |
| 457 | F->insert(It, start); |
| 458 | F->insert(It, exit); |
| 459 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 460 | // Update machine-CFG edges by transferring adding all successors and |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 461 | // remaining instructions from the current block to the new block which |
| 462 | // will contain the Phi node for the select. |
| 463 | exit->splice(exit->begin(), MBB, llvm::next(MachineBasicBlock::iterator(MI)), |
| 464 | MBB->end()); |
| 465 | exit->transferSuccessorsAndUpdatePHIs(MBB); |
| 466 | |
| 467 | // Add the fallthrough block as its successors. |
| 468 | MBB->addSuccessor(start); |
| 469 | |
| 470 | BuildMI(start, dl, TII->get(MBlaze::LWX), MI->getOperand(0).getReg()) |
| 471 | .addReg(MI->getOperand(1).getReg()) |
| 472 | .addReg(MBlaze::R0); |
| 473 | |
| 474 | MachineBasicBlock *final = start; |
| 475 | unsigned finalReg = 0; |
| 476 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 477 | switch (MI->getOpcode()) { |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 478 | default: llvm_unreachable("Cannot lower unknown atomic instruction!"); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 479 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 480 | case MBlaze::SWP32: |
| 481 | finalReg = MI->getOperand(2).getReg(); |
| 482 | start->addSuccessor(exit); |
| 483 | start->addSuccessor(start); |
| 484 | break; |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 485 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 486 | case MBlaze::LAN32: |
| 487 | case MBlaze::LAX32: |
| 488 | case MBlaze::LAO32: |
| 489 | case MBlaze::LAD32: |
| 490 | case MBlaze::LAS32: |
| 491 | case MBlaze::LAA32: { |
| 492 | unsigned opcode = 0; |
| 493 | switch (MI->getOpcode()) { |
| 494 | default: llvm_unreachable("Cannot lower unknown atomic load!"); |
| 495 | case MBlaze::LAA32: opcode = MBlaze::ADDIK; break; |
| 496 | case MBlaze::LAS32: opcode = MBlaze::RSUBIK; break; |
| 497 | case MBlaze::LAD32: opcode = MBlaze::AND; break; |
| 498 | case MBlaze::LAO32: opcode = MBlaze::OR; break; |
| 499 | case MBlaze::LAX32: opcode = MBlaze::XOR; break; |
| 500 | case MBlaze::LAN32: opcode = MBlaze::AND; break; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 503 | finalReg = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 504 | start->addSuccessor(exit); |
| 505 | start->addSuccessor(start); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 506 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 507 | BuildMI(start, dl, TII->get(opcode), finalReg) |
| 508 | .addReg(MI->getOperand(0).getReg()) |
| 509 | .addReg(MI->getOperand(2).getReg()); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 510 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 511 | if (MI->getOpcode() == MBlaze::LAN32) { |
| 512 | unsigned tmp = finalReg; |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 513 | finalReg = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 514 | BuildMI(start, dl, TII->get(MBlaze::XORI), finalReg) |
| 515 | .addReg(tmp) |
| 516 | .addImm(-1); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 517 | } |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 518 | break; |
| 519 | } |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 520 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 521 | case MBlaze::CAS32: { |
| 522 | finalReg = MI->getOperand(3).getReg(); |
| 523 | final = F->CreateMachineBasicBlock(LLVM_BB); |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 524 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 525 | F->insert(It, final); |
| 526 | start->addSuccessor(exit); |
| 527 | start->addSuccessor(final); |
| 528 | final->addSuccessor(exit); |
| 529 | final->addSuccessor(start); |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 530 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 531 | unsigned CMP = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 532 | BuildMI(start, dl, TII->get(MBlaze::CMP), CMP) |
| 533 | .addReg(MI->getOperand(0).getReg()) |
| 534 | .addReg(MI->getOperand(2).getReg()); |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 535 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 536 | BuildMI(start, dl, TII->get(MBlaze::BNEID)) |
| 537 | .addReg(CMP) |
| 538 | .addMBB(exit); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 539 | |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 540 | final->moveAfter(start); |
| 541 | exit->moveAfter(final); |
| 542 | break; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 545 | |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 546 | unsigned CHK = R.createVirtualRegister(&MBlaze::GPRRegClass); |
Wesley Peck | 6b3bbb1 | 2010-12-22 01:15:01 +0000 | [diff] [blame] | 547 | BuildMI(final, dl, TII->get(MBlaze::SWX)) |
| 548 | .addReg(finalReg) |
| 549 | .addReg(MI->getOperand(1).getReg()) |
| 550 | .addReg(MBlaze::R0); |
| 551 | |
| 552 | BuildMI(final, dl, TII->get(MBlaze::ADDIC), CHK) |
| 553 | .addReg(MBlaze::R0) |
| 554 | .addImm(0); |
| 555 | |
| 556 | BuildMI(final, dl, TII->get(MBlaze::BNEID)) |
| 557 | .addReg(CHK) |
| 558 | .addMBB(start); |
| 559 | |
| 560 | // The pseudo instruction is no longer needed so remove it |
| 561 | MI->eraseFromParent(); |
| 562 | return exit; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | //===----------------------------------------------------------------------===// |
| 566 | // Misc Lower Operation implementation |
| 567 | //===----------------------------------------------------------------------===// |
| 568 | // |
| 569 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 570 | SDValue MBlazeTargetLowering::LowerSELECT_CC(SDValue Op, |
| 571 | SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 572 | SDValue LHS = Op.getOperand(0); |
| 573 | SDValue RHS = Op.getOperand(1); |
| 574 | SDValue TrueVal = Op.getOperand(2); |
| 575 | SDValue FalseVal = Op.getOperand(3); |
| 576 | DebugLoc dl = Op.getDebugLoc(); |
| 577 | unsigned Opc; |
| 578 | |
| 579 | SDValue CompareFlag; |
| 580 | if (LHS.getValueType() == MVT::i32) { |
| 581 | Opc = MBlazeISD::Select_CC; |
| 582 | CompareFlag = DAG.getNode(MBlazeISD::ICmp, dl, MVT::i32, LHS, RHS) |
| 583 | .getValue(1); |
| 584 | } else { |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 585 | llvm_unreachable("Cannot lower select_cc with unknown type"); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 586 | } |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 587 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 588 | return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal, |
| 589 | CompareFlag); |
| 590 | } |
| 591 | |
| 592 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 593 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 594 | // FIXME there isn't actually debug info here |
| 595 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 596 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 597 | SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 598 | |
| 599 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, GA); |
| 600 | } |
| 601 | |
| 602 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 603 | LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 604 | llvm_unreachable("TLS not implemented for MicroBlaze."); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 608 | LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 609 | SDValue ResNode; |
| 610 | SDValue HiPart; |
| 611 | // FIXME there isn't actually debug info here |
| 612 | DebugLoc dl = Op.getDebugLoc(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 613 | |
| 614 | EVT PtrVT = Op.getValueType(); |
| 615 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 616 | |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 617 | SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 0); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 618 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, JTI); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 622 | LowerConstantPool(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 623 | SDValue ResNode; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 624 | ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 625 | const Constant *C = N->getConstVal(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 626 | DebugLoc dl = Op.getDebugLoc(); |
| 627 | |
| 628 | SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(), |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 629 | N->getOffset(), 0); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 630 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, CP); |
| 631 | } |
| 632 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 633 | SDValue MBlazeTargetLowering::LowerVASTART(SDValue Op, |
| 634 | SelectionDAG &DAG) const { |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 635 | MachineFunction &MF = DAG.getMachineFunction(); |
| 636 | MBlazeFunctionInfo *FuncInfo = MF.getInfo<MBlazeFunctionInfo>(); |
| 637 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 638 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 639 | SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 640 | getPointerTy()); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 641 | |
| 642 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 643 | // memory location argument. |
| 644 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 645 | return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1), |
| 646 | MachinePointerInfo(SV), |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 647 | false, false, 0); |
| 648 | } |
| 649 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 650 | //===----------------------------------------------------------------------===// |
| 651 | // Calling Convention Implementation |
| 652 | //===----------------------------------------------------------------------===// |
| 653 | |
| 654 | #include "MBlazeGenCallingConv.inc" |
| 655 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 656 | static bool CC_MBlaze_AssignReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 657 | CCValAssign::LocInfo &LocInfo, |
| 658 | ISD::ArgFlagsTy &ArgFlags, |
| 659 | CCState &State) { |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 660 | static const uint16_t ArgRegs[] = { |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 661 | MBlaze::R5, MBlaze::R6, MBlaze::R7, |
| 662 | MBlaze::R8, MBlaze::R9, MBlaze::R10 |
| 663 | }; |
| 664 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 665 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| 666 | unsigned Reg = State.AllocateReg(ArgRegs, NumArgRegs); |
| 667 | if (!Reg) return false; |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 668 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 669 | unsigned SizeInBytes = ValVT.getSizeInBits() >> 3; |
| 670 | State.AllocateStack(SizeInBytes, SizeInBytes); |
| 671 | State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 672 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 673 | return true; |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 676 | //===----------------------------------------------------------------------===// |
| 677 | // Call Calling Convention Implementation |
| 678 | //===----------------------------------------------------------------------===// |
| 679 | |
| 680 | /// LowerCall - functions arguments are copied from virtual regs to |
| 681 | /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. |
| 682 | /// TODO: isVarArg, isTailCall. |
| 683 | SDValue MBlazeTargetLowering:: |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 684 | LowerCall(TargetLowering::CallLoweringInfo &CLI, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 685 | SmallVectorImpl<SDValue> &InVals) const { |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 686 | SelectionDAG &DAG = CLI.DAG; |
| 687 | DebugLoc &dl = CLI.DL; |
| 688 | SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; |
| 689 | SmallVector<SDValue, 32> &OutVals = CLI.OutVals; |
| 690 | SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; |
| 691 | SDValue Chain = CLI.Chain; |
| 692 | SDValue Callee = CLI.Callee; |
| 693 | bool &isTailCall = CLI.IsTailCall; |
| 694 | CallingConv::ID CallConv = CLI.CallConv; |
| 695 | bool isVarArg = CLI.IsVarArg; |
| 696 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 697 | // MBlaze does not yet support tail call optimization |
| 698 | isTailCall = false; |
| 699 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 700 | // The MBlaze requires stack slots for arguments passed to var arg |
| 701 | // functions even if they are passed in registers. |
| 702 | bool needsRegArgSlots = isVarArg; |
| 703 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 704 | MachineFunction &MF = DAG.getMachineFunction(); |
| 705 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 706 | const TargetFrameLowering &TFI = *MF.getTarget().getFrameLowering(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 707 | |
| 708 | // Analyze operands of the call, assigning locations to each operand. |
| 709 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 710 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 711 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 712 | CCInfo.AnalyzeCallOperands(Outs, CC_MBlaze); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 713 | |
| 714 | // Get a count of how many bytes are to be pushed on the stack. |
| 715 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 716 | |
| 717 | // Variable argument function calls require a minimum of 24-bytes of stack |
| 718 | if (isVarArg && NumBytes < 24) NumBytes = 24; |
| 719 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 720 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
| 721 | |
| 722 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 723 | SmallVector<SDValue, 8> MemOpChains; |
| 724 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 725 | // Walk the register/memloc assignments, inserting copies/loads. |
| 726 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 727 | CCValAssign &VA = ArgLocs[i]; |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 728 | MVT RegVT = VA.getLocVT(); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 729 | SDValue Arg = OutVals[i]; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 730 | |
| 731 | // Promote the value if needed. |
| 732 | switch (VA.getLocInfo()) { |
| 733 | default: llvm_unreachable("Unknown loc info!"); |
| 734 | case CCValAssign::Full: break; |
| 735 | case CCValAssign::SExt: |
| 736 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); |
| 737 | break; |
| 738 | case CCValAssign::ZExt: |
| 739 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); |
| 740 | break; |
| 741 | case CCValAssign::AExt: |
| 742 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); |
| 743 | break; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // Arguments that can be passed on register must be kept at |
| 747 | // RegsToPass vector |
| 748 | if (VA.isRegLoc()) { |
| 749 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 750 | } else { |
| 751 | // Register can't get to this point... |
| 752 | assert(VA.isMemLoc()); |
| 753 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 754 | // Since we are alread passing values on the stack we don't |
| 755 | // need to worry about creating additional slots for the |
| 756 | // values passed via registers. |
| 757 | needsRegArgSlots = false; |
| 758 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 759 | // Create the frame index object for this incoming parameter |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 760 | unsigned ArgSize = VA.getValVT().getSizeInBits()/8; |
| 761 | unsigned StackLoc = VA.getLocMemOffset() + 4; |
| 762 | int FI = MFI->CreateFixedObject(ArgSize, StackLoc, true); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 763 | |
| 764 | SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy()); |
| 765 | |
| 766 | // emit ISD::STORE whichs stores the |
| 767 | // parameter value to a stack Location |
Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 768 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
| 769 | MachinePointerInfo(), |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 770 | false, false, 0)); |
| 771 | } |
| 772 | } |
| 773 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 774 | // If we need to reserve stack space for the arguments passed via registers |
| 775 | // then create a fixed stack object at the beginning of the stack. |
| 776 | if (needsRegArgSlots && TFI.hasReservedCallFrame(MF)) |
| 777 | MFI->CreateFixedObject(28,0,true); |
| 778 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 779 | // Transform all store nodes into one single node because all store |
| 780 | // nodes are independent of each other. |
| 781 | if (!MemOpChains.empty()) |
| 782 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 783 | &MemOpChains[0], MemOpChains.size()); |
| 784 | |
| 785 | // Build a sequence of copy-to-reg nodes chained together with token |
| 786 | // chain and flag operands which copy the outgoing args into registers. |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 787 | // The InFlag in necessary since all emitted instructions must be |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 788 | // stuck together. |
| 789 | SDValue InFlag; |
| 790 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 791 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 792 | RegsToPass[i].second, InFlag); |
| 793 | InFlag = Chain.getValue(1); |
| 794 | } |
| 795 | |
| 796 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 797 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 798 | // node so that legalize doesn't hack it. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 799 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 800 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 801 | getPointerTy(), 0, 0); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 802 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 803 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 804 | getPointerTy(), 0); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 805 | |
| 806 | // MBlazeJmpLink = #chain, #target_address, #opt_in_flags... |
| 807 | // = Chain, Callee, Reg#1, Reg#2, ... |
| 808 | // |
| 809 | // Returns a chain & a flag for retval copy to use. |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 810 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 811 | SmallVector<SDValue, 8> Ops; |
| 812 | Ops.push_back(Chain); |
| 813 | Ops.push_back(Callee); |
| 814 | |
| 815 | // Add argument registers to the end of the list so that they are |
| 816 | // known live into the call. |
| 817 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 818 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 819 | RegsToPass[i].second.getValueType())); |
| 820 | } |
| 821 | |
| 822 | if (InFlag.getNode()) |
| 823 | Ops.push_back(InFlag); |
| 824 | |
| 825 | Chain = DAG.getNode(MBlazeISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size()); |
| 826 | InFlag = Chain.getValue(1); |
| 827 | |
| 828 | // Create the CALLSEQ_END node. |
| 829 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 830 | DAG.getIntPtrConstant(0, true), InFlag); |
| 831 | if (!Ins.empty()) |
| 832 | InFlag = Chain.getValue(1); |
| 833 | |
| 834 | // Handle result values, copying them out of physregs into vregs that we |
| 835 | // return. |
| 836 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 837 | Ins, dl, DAG, InVals); |
| 838 | } |
| 839 | |
| 840 | /// LowerCallResult - Lower the result values of a call into the |
| 841 | /// appropriate copies out of appropriate physical registers. |
| 842 | SDValue MBlazeTargetLowering:: |
| 843 | LowerCallResult(SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, |
| 844 | bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, |
| 845 | DebugLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 846 | SmallVectorImpl<SDValue> &InVals) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 847 | // Assign locations to each value returned by this call. |
| 848 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 849 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 850 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 851 | |
| 852 | CCInfo.AnalyzeCallResult(Ins, RetCC_MBlaze); |
| 853 | |
| 854 | // Copy all of the result registers out of their specified physreg. |
| 855 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 856 | Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), |
| 857 | RVLocs[i].getValVT(), InFlag).getValue(1); |
| 858 | InFlag = Chain.getValue(2); |
| 859 | InVals.push_back(Chain.getValue(0)); |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 860 | } |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 861 | |
| 862 | return Chain; |
| 863 | } |
| 864 | |
| 865 | //===----------------------------------------------------------------------===// |
| 866 | // Formal Arguments Calling Convention Implementation |
| 867 | //===----------------------------------------------------------------------===// |
| 868 | |
| 869 | /// LowerFormalArguments - transform physical registers into |
| 870 | /// virtual registers and generate load operations for |
| 871 | /// arguments places on the stack. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 872 | SDValue MBlazeTargetLowering:: |
| 873 | LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 874 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 875 | DebugLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 876 | SmallVectorImpl<SDValue> &InVals) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 877 | MachineFunction &MF = DAG.getMachineFunction(); |
| 878 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 879 | MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>(); |
| 880 | |
| 881 | unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 882 | MBlazeFI->setVarArgsFrameIndex(0); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 883 | |
| 884 | // Used with vargs to acumulate store chains. |
| 885 | std::vector<SDValue> OutChains; |
| 886 | |
| 887 | // Keep track of the last register used for arguments |
| 888 | unsigned ArgRegEnd = 0; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 889 | |
| 890 | // Assign locations to all of the incoming arguments. |
| 891 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 892 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 893 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 894 | |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 895 | CCInfo.AnalyzeFormalArguments(Ins, CC_MBlaze); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 896 | SDValue StackPtr; |
| 897 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 898 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 899 | CCValAssign &VA = ArgLocs[i]; |
| 900 | |
| 901 | // Arguments stored on registers |
| 902 | if (VA.isRegLoc()) { |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 903 | MVT RegVT = VA.getLocVT(); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 904 | ArgRegEnd = VA.getLocReg(); |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 905 | const TargetRegisterClass *RC; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 906 | |
| 907 | if (RegVT == MVT::i32) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 908 | RC = &MBlaze::GPRRegClass; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 909 | else if (RegVT == MVT::f32) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 910 | RC = &MBlaze::GPRRegClass; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 911 | else |
| 912 | llvm_unreachable("RegVT not supported by LowerFormalArguments"); |
| 913 | |
| 914 | // Transform the arguments stored on |
| 915 | // physical registers into virtual ones |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 916 | unsigned Reg = MF.addLiveIn(ArgRegEnd, RC); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 917 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| 918 | |
| 919 | // If this is an 8 or 16-bit value, it has been passed promoted |
| 920 | // to 32 bits. Insert an assert[sz]ext to capture this, then |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 921 | // truncate to the right size. If if is a floating point value |
| 922 | // then convert to the correct type. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 923 | if (VA.getLocInfo() != CCValAssign::Full) { |
| 924 | unsigned Opcode = 0; |
| 925 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 926 | Opcode = ISD::AssertSext; |
| 927 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 928 | Opcode = ISD::AssertZext; |
| 929 | if (Opcode) |
| 930 | ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue, |
| 931 | DAG.getValueType(VA.getValVT())); |
| 932 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
| 933 | } |
| 934 | |
| 935 | InVals.push_back(ArgValue); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 936 | } else { // VA.isRegLoc() |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 937 | // sanity check |
| 938 | assert(VA.isMemLoc()); |
| 939 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 940 | // The last argument is not a register |
| 941 | ArgRegEnd = 0; |
| 942 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 943 | // The stack pointer offset is relative to the caller stack frame. |
| 944 | // Since the real stack size is unknown here, a negative SPOffset |
| 945 | // is used so there's a way to adjust these offsets when the stack |
| 946 | // size get known (on EliminateFrameIndex). A dummy SPOffset is |
| 947 | // used instead of a direct negative address (which is recorded to |
| 948 | // be used on emitPrologue) to avoid mis-calc of the first stack |
| 949 | // offset on PEI::calculateFrameObjectOffsets. |
| 950 | // Arguments are always 32-bit. |
| 951 | unsigned ArgSize = VA.getLocVT().getSizeInBits()/8; |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 952 | unsigned StackLoc = VA.getLocMemOffset() + 4; |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 953 | int FI = MFI->CreateFixedObject(ArgSize, 0, true); |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 954 | MBlazeFI->recordLoadArgsFI(FI, -StackLoc); |
Wesley Peck | eb13382 | 2010-12-12 20:52:31 +0000 | [diff] [blame] | 955 | MBlazeFI->recordLiveIn(FI); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 956 | |
| 957 | // Create load nodes to retrieve arguments from the stack |
| 958 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 959 | InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, |
| 960 | MachinePointerInfo::getFixedStack(FI), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 961 | false, false, false, 0)); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 965 | // To meet ABI, when VARARGS are passed on registers, the registers |
| 966 | // must have their values written to the caller stack frame. If the last |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 967 | // argument was placed in the stack, there's no need to save any register. |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 968 | if ((isVarArg) && ArgRegEnd) { |
| 969 | if (StackPtr.getNode() == 0) |
| 970 | StackPtr = DAG.getRegister(StackReg, getPointerTy()); |
| 971 | |
| 972 | // The last register argument that must be saved is MBlaze::R10 |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 973 | const TargetRegisterClass *RC = &MBlaze::GPRRegClass; |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 974 | |
Evan Cheng | 8cb2d61 | 2011-07-25 20:18:18 +0000 | [diff] [blame] | 975 | unsigned Begin = getMBlazeRegisterNumbering(MBlaze::R5); |
| 976 | unsigned Start = getMBlazeRegisterNumbering(ArgRegEnd+1); |
| 977 | unsigned End = getMBlazeRegisterNumbering(MBlaze::R10); |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 978 | unsigned StackLoc = Start - Begin + 1; |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 979 | |
| 980 | for (; Start <= End; ++Start, ++StackLoc) { |
Evan Cheng | 617793d | 2011-07-25 22:16:37 +0000 | [diff] [blame] | 981 | unsigned Reg = getMBlazeRegisterFromNumbering(Start); |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 982 | unsigned LiveReg = MF.addLiveIn(Reg, RC); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 983 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, LiveReg, MVT::i32); |
| 984 | |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 985 | int FI = MFI->CreateFixedObject(4, 0, true); |
Wesley Peck | 8397be0 | 2010-12-09 03:42:04 +0000 | [diff] [blame] | 986 | MBlazeFI->recordStoreVarArgsFI(FI, -(StackLoc*4)); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 987 | SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy()); |
Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 988 | OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff, |
| 989 | MachinePointerInfo(), |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 990 | false, false, 0)); |
| 991 | |
| 992 | // Record the frame index of the first variable argument |
| 993 | // which is a value necessary to VASTART. |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 994 | if (!MBlazeFI->getVarArgsFrameIndex()) |
| 995 | MBlazeFI->setVarArgsFrameIndex(FI); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 999 | // All stores are grouped in one node to allow the matching between |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 1000 | // the size of Ins and InVals. This only happens when on varg functions |
| 1001 | if (!OutChains.empty()) { |
| 1002 | OutChains.push_back(Chain); |
| 1003 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 1004 | &OutChains[0], OutChains.size()); |
| 1005 | } |
| 1006 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1007 | return Chain; |
| 1008 | } |
| 1009 | |
| 1010 | //===----------------------------------------------------------------------===// |
| 1011 | // Return Value Calling Convention Implementation |
| 1012 | //===----------------------------------------------------------------------===// |
| 1013 | |
| 1014 | SDValue MBlazeTargetLowering:: |
| 1015 | LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 1016 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1017 | const SmallVectorImpl<SDValue> &OutVals, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1018 | DebugLoc dl, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1019 | // CCValAssign - represent the assignment of |
| 1020 | // the return value to a location |
| 1021 | SmallVector<CCValAssign, 16> RVLocs; |
| 1022 | |
| 1023 | // CCState - Info about the registers and stack slot. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1024 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 1025 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Analize return values. |
| 1028 | CCInfo.AnalyzeReturn(Outs, RetCC_MBlaze); |
| 1029 | |
| 1030 | // If this is the first return lowered for this function, add |
| 1031 | // the regs to the liveout set for the function. |
| 1032 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
| 1033 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 1034 | if (RVLocs[i].isRegLoc()) |
| 1035 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
| 1036 | } |
| 1037 | |
| 1038 | SDValue Flag; |
| 1039 | |
| 1040 | // Copy the result values into the output registers. |
| 1041 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 1042 | CCValAssign &VA = RVLocs[i]; |
| 1043 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 1044 | |
| 1045 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1046 | OutVals[i], Flag); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1047 | |
| 1048 | // guarantee that all emitted copies are |
| 1049 | // stuck together, avoiding something bad |
| 1050 | Flag = Chain.getValue(1); |
| 1051 | } |
| 1052 | |
Wesley Peck | dc9d87a | 2010-12-15 20:27:28 +0000 | [diff] [blame] | 1053 | // If this function is using the interrupt_handler calling convention |
| 1054 | // then use "rtid r14, 0" otherwise use "rtsd r15, 8" |
Craig Topper | c89c744 | 2012-03-27 07:21:54 +0000 | [diff] [blame] | 1055 | unsigned Ret = (CallConv == CallingConv::MBLAZE_INTR) ? MBlazeISD::IRet |
| 1056 | : MBlazeISD::Ret; |
| 1057 | unsigned Reg = (CallConv == CallingConv::MBLAZE_INTR) ? MBlaze::R14 |
| 1058 | : MBlaze::R15; |
Wesley Peck | dc9d87a | 2010-12-15 20:27:28 +0000 | [diff] [blame] | 1059 | SDValue DReg = DAG.getRegister(Reg, MVT::i32); |
| 1060 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1061 | if (Flag.getNode()) |
Wesley Peck | dc9d87a | 2010-12-15 20:27:28 +0000 | [diff] [blame] | 1062 | return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg, Flag); |
| 1063 | |
| 1064 | return DAG.getNode(Ret, dl, MVT::Other, Chain, DReg); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | //===----------------------------------------------------------------------===// |
| 1068 | // MBlaze Inline Assembly Support |
| 1069 | //===----------------------------------------------------------------------===// |
| 1070 | |
| 1071 | /// getConstraintType - Given a constraint letter, return the type of |
| 1072 | /// constraint it is for this target. |
| 1073 | MBlazeTargetLowering::ConstraintType MBlazeTargetLowering:: |
| 1074 | getConstraintType(const std::string &Constraint) const |
| 1075 | { |
| 1076 | // MBlaze specific constrainy |
| 1077 | // |
| 1078 | // 'd' : An address register. Equivalent to r. |
| 1079 | // 'y' : Equivalent to r; retained for |
| 1080 | // backwards compatibility. |
| 1081 | // 'f' : Floating Point registers. |
| 1082 | if (Constraint.size() == 1) { |
| 1083 | switch (Constraint[0]) { |
| 1084 | default : break; |
| 1085 | case 'd': |
| 1086 | case 'y': |
| 1087 | case 'f': |
| 1088 | return C_RegisterClass; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | return TargetLowering::getConstraintType(Constraint); |
| 1092 | } |
| 1093 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 1094 | /// Examine constraint type and operand type and determine a weight value. |
| 1095 | /// This object must already have been set up with the operand type |
| 1096 | /// and the current alternative constraint selected. |
| 1097 | TargetLowering::ConstraintWeight |
| 1098 | MBlazeTargetLowering::getSingleConstraintMatchWeight( |
| 1099 | AsmOperandInfo &info, const char *constraint) const { |
| 1100 | ConstraintWeight weight = CW_Invalid; |
| 1101 | Value *CallOperandVal = info.CallOperandVal; |
| 1102 | // If we don't have a value, we can't do a match, |
| 1103 | // but allow it at the lowest weight. |
| 1104 | if (CallOperandVal == NULL) |
| 1105 | return CW_Default; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1106 | Type *type = CallOperandVal->getType(); |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 1107 | // Look at the constraint type. |
| 1108 | switch (*constraint) { |
| 1109 | default: |
| 1110 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
Chandler Carruth | a4a2a03 | 2011-04-25 07:11:23 +0000 | [diff] [blame] | 1111 | break; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1112 | case 'd': |
| 1113 | case 'y': |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 1114 | if (type->isIntegerTy()) |
| 1115 | weight = CW_Register; |
| 1116 | break; |
| 1117 | case 'f': |
| 1118 | if (type->isFloatTy()) |
| 1119 | weight = CW_Register; |
| 1120 | break; |
| 1121 | } |
| 1122 | return weight; |
| 1123 | } |
| 1124 | |
Eric Christopher | 193f7e2 | 2011-06-29 19:12:24 +0000 | [diff] [blame] | 1125 | /// Given a register class constraint, like 'r', if this corresponds directly |
| 1126 | /// to an LLVM register class, return a register of 0 and the register class |
| 1127 | /// pointer. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1128 | std::pair<unsigned, const TargetRegisterClass*> MBlazeTargetLowering:: |
| 1129 | getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const { |
| 1130 | if (Constraint.size() == 1) { |
| 1131 | switch (Constraint[0]) { |
| 1132 | case 'r': |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1133 | return std::make_pair(0U, &MBlaze::GPRRegClass); |
Eric Christopher | 193f7e2 | 2011-06-29 19:12:24 +0000 | [diff] [blame] | 1134 | // TODO: These can't possibly be right, but match what was in |
| 1135 | // getRegClassForInlineAsmConstraint. |
| 1136 | case 'd': |
| 1137 | case 'y': |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1138 | case 'f': |
| 1139 | if (VT == MVT::f32) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1140 | return std::make_pair(0U, &MBlaze::GPRRegClass); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 1144 | } |
| 1145 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1146 | bool MBlazeTargetLowering:: |
| 1147 | isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 1148 | // The MBlaze target isn't yet aware of offsets. |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | bool MBlazeTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 1153 | return VT != MVT::f32; |
| 1154 | } |