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 | |
| 38 | const char *MBlazeTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 39 | switch (Opcode) { |
| 40 | case MBlazeISD::JmpLink : return "MBlazeISD::JmpLink"; |
| 41 | case MBlazeISD::GPRel : return "MBlazeISD::GPRel"; |
| 42 | case MBlazeISD::Wrap : return "MBlazeISD::Wrap"; |
| 43 | case MBlazeISD::ICmp : return "MBlazeISD::ICmp"; |
| 44 | case MBlazeISD::Ret : return "MBlazeISD::Ret"; |
| 45 | case MBlazeISD::Select_CC : return "MBlazeISD::Select_CC"; |
| 46 | default : return NULL; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | MBlazeTargetLowering::MBlazeTargetLowering(MBlazeTargetMachine &TM) |
| 51 | : TargetLowering(TM, new MBlazeTargetObjectFile()) { |
| 52 | Subtarget = &TM.getSubtarget<MBlazeSubtarget>(); |
| 53 | |
| 54 | // MBlaze does not have i1 type, so use i32 for |
| 55 | // setcc operations results (slt, sgt, ...). |
| 56 | setBooleanContents(ZeroOrOneBooleanContent); |
| 57 | |
| 58 | // Set up the register classes |
| 59 | addRegisterClass(MVT::i32, MBlaze::CPURegsRegisterClass); |
| 60 | if (Subtarget->hasFPU()) { |
| 61 | addRegisterClass(MVT::f32, MBlaze::FGR32RegisterClass); |
| 62 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
| 63 | } |
| 64 | |
| 65 | // Floating point operations which are not supported |
| 66 | setOperationAction(ISD::FREM, MVT::f32, Expand); |
| 67 | setOperationAction(ISD::UINT_TO_FP, MVT::i8, Expand); |
| 68 | setOperationAction(ISD::UINT_TO_FP, MVT::i16, Expand); |
| 69 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 70 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| 71 | setOperationAction(ISD::FP_ROUND, MVT::f32, Expand); |
| 72 | setOperationAction(ISD::FP_ROUND, MVT::f64, Expand); |
| 73 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 74 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 75 | setOperationAction(ISD::FSIN, MVT::f32, Expand); |
| 76 | setOperationAction(ISD::FCOS, MVT::f32, Expand); |
| 77 | setOperationAction(ISD::FPOWI, MVT::f32, Expand); |
| 78 | setOperationAction(ISD::FPOW, MVT::f32, Expand); |
| 79 | setOperationAction(ISD::FLOG, MVT::f32, Expand); |
| 80 | setOperationAction(ISD::FLOG2, MVT::f32, Expand); |
| 81 | setOperationAction(ISD::FLOG10, MVT::f32, Expand); |
| 82 | setOperationAction(ISD::FEXP, MVT::f32, Expand); |
| 83 | |
| 84 | // Load extented operations for i1 types must be promoted |
| 85 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
| 86 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 87 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 88 | |
| 89 | // MBlaze has no REM or DIVREM operations. |
| 90 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 91 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 92 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 93 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
| 94 | |
| 95 | // If the processor doesn't support multiply then expand it |
| 96 | if (!Subtarget->hasMul()) { |
| 97 | setOperationAction(ISD::MUL, MVT::i32, Expand); |
| 98 | } |
| 99 | |
| 100 | // If the processor doesn't support 64-bit multiply then expand |
| 101 | if (!Subtarget->hasMul() || !Subtarget->hasMul64()) { |
| 102 | setOperationAction(ISD::MULHS, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::MULHS, MVT::i64, Expand); |
| 104 | setOperationAction(ISD::MULHU, MVT::i32, Expand); |
| 105 | setOperationAction(ISD::MULHU, MVT::i64, Expand); |
| 106 | } |
| 107 | |
| 108 | // If the processor doesn't support division then expand |
| 109 | if (!Subtarget->hasDiv()) { |
| 110 | setOperationAction(ISD::UDIV, MVT::i32, Expand); |
| 111 | setOperationAction(ISD::SDIV, MVT::i32, Expand); |
| 112 | } |
| 113 | |
| 114 | // Expand unsupported conversions |
| 115 | setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand); |
| 116 | setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand); |
| 117 | |
| 118 | // Expand SELECT_CC |
| 119 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 120 | |
| 121 | // MBlaze doesn't have MUL_LOHI |
| 122 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
| 123 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 124 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
| 125 | setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); |
| 126 | |
| 127 | // Used by legalize types to correctly generate the setcc result. |
| 128 | // Without this, every float setcc comes with a AND/OR with the result, |
| 129 | // we don't want this, since the fpcmp result goes to a flag register, |
| 130 | // which is used implicitly by brcond and select operations. |
| 131 | AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); |
| 132 | AddPromotedToType(ISD::SELECT, MVT::i1, MVT::i32); |
| 133 | AddPromotedToType(ISD::SELECT_CC, MVT::i1, MVT::i32); |
| 134 | |
| 135 | // MBlaze Custom Operations |
| 136 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 137 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
| 138 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
| 139 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 140 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 141 | // Variable Argument support |
| 142 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 143 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
| 144 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 145 | setOperationAction(ISD::VACOPY, MVT::Other, Expand); |
| 146 | |
| 147 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 148 | // Operations not directly supported by MBlaze. |
| 149 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
| 150 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 151 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 152 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 153 | setOperationAction(ISD::ROTL, MVT::i32, Expand); |
| 154 | setOperationAction(ISD::ROTR, MVT::i32, Expand); |
| 155 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); |
| 156 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); |
| 157 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); |
| 158 | setOperationAction(ISD::CTLZ, MVT::i32, Expand); |
| 159 | setOperationAction(ISD::CTTZ, MVT::i32, Expand); |
| 160 | setOperationAction(ISD::CTPOP, MVT::i32, Expand); |
| 161 | setOperationAction(ISD::BSWAP, MVT::i32, Expand); |
| 162 | |
| 163 | // We don't have line number support yet. |
| 164 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| 165 | |
| 166 | // Use the default for now |
| 167 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 168 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 169 | setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand); |
| 170 | |
| 171 | // MBlaze doesn't have extending float->double load/store |
| 172 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 173 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 174 | |
| 175 | setStackPointerRegisterToSaveRestore(MBlaze::R1); |
| 176 | computeRegisterProperties(); |
| 177 | } |
| 178 | |
| 179 | MVT::SimpleValueType MBlazeTargetLowering::getSetCCResultType(EVT VT) const { |
| 180 | return MVT::i32; |
| 181 | } |
| 182 | |
| 183 | /// getFunctionAlignment - Return the Log2 alignment of this function. |
| 184 | unsigned MBlazeTargetLowering::getFunctionAlignment(const Function *) const { |
| 185 | return 2; |
| 186 | } |
| 187 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 188 | SDValue MBlazeTargetLowering::LowerOperation(SDValue Op, |
| 189 | SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 190 | switch (Op.getOpcode()) |
| 191 | { |
| 192 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 193 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 194 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
| 195 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
| 196 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 197 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 198 | } |
| 199 | return SDValue(); |
| 200 | } |
| 201 | |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | // Lower helper functions |
| 204 | //===----------------------------------------------------------------------===// |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 205 | MachineBasicBlock* |
| 206 | MBlazeTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
| 207 | MachineBasicBlock *BB) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 208 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 209 | DebugLoc dl = MI->getDebugLoc(); |
| 210 | |
| 211 | switch (MI->getOpcode()) { |
| 212 | default: assert(false && "Unexpected instr type to insert"); |
| 213 | case MBlaze::ShiftRL: |
| 214 | case MBlaze::ShiftRA: |
| 215 | case MBlaze::ShiftL: { |
| 216 | // To "insert" a shift left instruction, we actually have to insert a |
| 217 | // simple loop. The incoming instruction knows the destination vreg to |
| 218 | // set, the source vreg to operate over and the shift amount. |
| 219 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 220 | MachineFunction::iterator It = BB; |
| 221 | ++It; |
| 222 | |
| 223 | // start: |
| 224 | // andi samt, samt, 31 |
| 225 | // beqid samt, finish |
| 226 | // add dst, src, r0 |
| 227 | // loop: |
| 228 | // addik samt, samt, -1 |
| 229 | // sra dst, dst |
| 230 | // bneid samt, loop |
| 231 | // nop |
| 232 | // finish: |
| 233 | MachineFunction *F = BB->getParent(); |
| 234 | MachineRegisterInfo &R = F->getRegInfo(); |
| 235 | MachineBasicBlock *loop = F->CreateMachineBasicBlock(LLVM_BB); |
| 236 | MachineBasicBlock *finish = F->CreateMachineBasicBlock(LLVM_BB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 237 | F->insert(It, loop); |
| 238 | F->insert(It, finish); |
| 239 | |
| 240 | // Update machine-CFG edges by transfering adding all successors and |
| 241 | // remaining instructions from the current block to the new block which |
| 242 | // will contain the Phi node for the select. |
| 243 | finish->splice(finish->begin(), BB, |
| 244 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 245 | BB->end()); |
| 246 | finish->transferSuccessorsAndUpdatePHIs(BB); |
| 247 | |
| 248 | // Add the true and fallthrough blocks as its successors. |
| 249 | BB->addSuccessor(loop); |
| 250 | BB->addSuccessor(finish); |
| 251 | |
| 252 | // Next, add the finish block as a successor of the loop block |
| 253 | loop->addSuccessor(finish); |
| 254 | loop->addSuccessor(loop); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 255 | |
| 256 | unsigned IAMT = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 257 | BuildMI(BB, dl, TII->get(MBlaze::ANDI), IAMT) |
| 258 | .addReg(MI->getOperand(2).getReg()) |
| 259 | .addImm(31); |
| 260 | |
| 261 | unsigned IVAL = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 262 | BuildMI(BB, dl, TII->get(MBlaze::ADDI), IVAL) |
| 263 | .addReg(MI->getOperand(1).getReg()) |
| 264 | .addImm(0); |
| 265 | |
| 266 | BuildMI(BB, dl, TII->get(MBlaze::BEQID)) |
| 267 | .addReg(IAMT) |
| 268 | .addMBB(finish); |
| 269 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 270 | unsigned DST = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 271 | unsigned NDST = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 272 | BuildMI(loop, dl, TII->get(MBlaze::PHI), DST) |
| 273 | .addReg(IVAL).addMBB(BB) |
| 274 | .addReg(NDST).addMBB(loop); |
| 275 | |
| 276 | unsigned SAMT = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 277 | unsigned NAMT = R.createVirtualRegister(MBlaze::CPURegsRegisterClass); |
| 278 | BuildMI(loop, dl, TII->get(MBlaze::PHI), SAMT) |
| 279 | .addReg(IAMT).addMBB(BB) |
| 280 | .addReg(NAMT).addMBB(loop); |
| 281 | |
| 282 | if (MI->getOpcode() == MBlaze::ShiftL) |
| 283 | BuildMI(loop, dl, TII->get(MBlaze::ADD), NDST).addReg(DST).addReg(DST); |
| 284 | else if (MI->getOpcode() == MBlaze::ShiftRA) |
| 285 | BuildMI(loop, dl, TII->get(MBlaze::SRA), NDST).addReg(DST); |
| 286 | else if (MI->getOpcode() == MBlaze::ShiftRL) |
| 287 | BuildMI(loop, dl, TII->get(MBlaze::SRL), NDST).addReg(DST); |
| 288 | else |
| 289 | llvm_unreachable( "Cannot lower unknown shift instruction" ); |
| 290 | |
| 291 | BuildMI(loop, dl, TII->get(MBlaze::ADDI), NAMT) |
| 292 | .addReg(SAMT) |
| 293 | .addImm(-1); |
| 294 | |
| 295 | BuildMI(loop, dl, TII->get(MBlaze::BNEID)) |
| 296 | .addReg(NAMT) |
| 297 | .addMBB(loop); |
| 298 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 299 | BuildMI(*finish, finish->begin(), dl, |
| 300 | TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 301 | .addReg(IVAL).addMBB(BB) |
| 302 | .addReg(NDST).addMBB(loop); |
| 303 | |
| 304 | // The pseudo instruction is no longer needed so remove it |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 305 | MI->eraseFromParent(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 306 | return finish; |
| 307 | } |
| 308 | |
| 309 | case MBlaze::Select_FCC: |
| 310 | case MBlaze::Select_CC: { |
| 311 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 312 | // diamond control-flow pattern. The incoming instruction knows the |
| 313 | // destination vreg to set, the condition code register to branch on, the |
| 314 | // true/false values to select between, and a branch opcode to use. |
| 315 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 316 | MachineFunction::iterator It = BB; |
| 317 | ++It; |
| 318 | |
| 319 | // thisMBB: |
| 320 | // ... |
| 321 | // TrueVal = ... |
| 322 | // setcc r1, r2, r3 |
| 323 | // bNE r1, r0, copy1MBB |
| 324 | // fallthrough --> copy0MBB |
| 325 | MachineFunction *F = BB->getParent(); |
| 326 | MachineBasicBlock *flsBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 327 | MachineBasicBlock *dneBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 328 | |
| 329 | unsigned Opc; |
| 330 | switch (MI->getOperand(4).getImm()) { |
| 331 | default: llvm_unreachable( "Unknown branch condition" ); |
| 332 | case MBlazeCC::EQ: Opc = MBlaze::BNEID; break; |
| 333 | case MBlazeCC::NE: Opc = MBlaze::BEQID; break; |
| 334 | case MBlazeCC::GT: Opc = MBlaze::BLEID; break; |
| 335 | case MBlazeCC::LT: Opc = MBlaze::BGEID; break; |
| 336 | case MBlazeCC::GE: Opc = MBlaze::BLTID; break; |
| 337 | case MBlazeCC::LE: Opc = MBlaze::BGTID; break; |
| 338 | } |
| 339 | |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 340 | F->insert(It, flsBB); |
| 341 | F->insert(It, dneBB); |
| 342 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 343 | // Transfer the remainder of BB and its successor edges to dneBB. |
| 344 | dneBB->splice(dneBB->begin(), BB, |
| 345 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 346 | BB->end()); |
| 347 | dneBB->transferSuccessorsAndUpdatePHIs(BB); |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 348 | |
Dan Gohman | 258c58c | 2010-07-06 15:49:48 +0000 | [diff] [blame] | 349 | BB->addSuccessor(flsBB); |
| 350 | BB->addSuccessor(dneBB); |
| 351 | flsBB->addSuccessor(dneBB); |
| 352 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 353 | BuildMI(BB, dl, TII->get(Opc)) |
| 354 | .addReg(MI->getOperand(3).getReg()) |
| 355 | .addMBB(dneBB); |
| 356 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 357 | // sinkMBB: |
| 358 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 359 | // ... |
| 360 | //BuildMI(dneBB, dl, TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
| 361 | // .addReg(MI->getOperand(1).getReg()).addMBB(flsBB) |
| 362 | // .addReg(MI->getOperand(2).getReg()).addMBB(BB); |
| 363 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 364 | BuildMI(*dneBB, dneBB->begin(), dl, |
| 365 | TII->get(MBlaze::PHI), MI->getOperand(0).getReg()) |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 366 | .addReg(MI->getOperand(2).getReg()).addMBB(flsBB) |
| 367 | .addReg(MI->getOperand(1).getReg()).addMBB(BB); |
| 368 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 369 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 370 | return dneBB; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | //===----------------------------------------------------------------------===// |
| 376 | // Misc Lower Operation implementation |
| 377 | //===----------------------------------------------------------------------===// |
| 378 | // |
| 379 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 380 | SDValue MBlazeTargetLowering::LowerSELECT_CC(SDValue Op, |
| 381 | SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 382 | SDValue LHS = Op.getOperand(0); |
| 383 | SDValue RHS = Op.getOperand(1); |
| 384 | SDValue TrueVal = Op.getOperand(2); |
| 385 | SDValue FalseVal = Op.getOperand(3); |
| 386 | DebugLoc dl = Op.getDebugLoc(); |
| 387 | unsigned Opc; |
| 388 | |
| 389 | SDValue CompareFlag; |
| 390 | if (LHS.getValueType() == MVT::i32) { |
| 391 | Opc = MBlazeISD::Select_CC; |
| 392 | CompareFlag = DAG.getNode(MBlazeISD::ICmp, dl, MVT::i32, LHS, RHS) |
| 393 | .getValue(1); |
| 394 | } else { |
| 395 | llvm_unreachable( "Cannot lower select_cc with unknown type" ); |
| 396 | } |
| 397 | |
| 398 | return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal, |
| 399 | CompareFlag); |
| 400 | } |
| 401 | |
| 402 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 403 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 404 | // FIXME there isn't actually debug info here |
| 405 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 406 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 407 | SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 408 | |
| 409 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, GA); |
| 410 | } |
| 411 | |
| 412 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 413 | LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 414 | llvm_unreachable("TLS not implemented for MicroBlaze."); |
| 415 | return SDValue(); // Not reached |
| 416 | } |
| 417 | |
| 418 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 419 | LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 420 | SDValue ResNode; |
| 421 | SDValue HiPart; |
| 422 | // FIXME there isn't actually debug info here |
| 423 | DebugLoc dl = Op.getDebugLoc(); |
| 424 | bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_; |
| 425 | unsigned char OpFlag = IsPIC ? MBlazeII::MO_GOT : MBlazeII::MO_ABS_HILO; |
| 426 | |
| 427 | EVT PtrVT = Op.getValueType(); |
| 428 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 429 | |
| 430 | SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag); |
| 431 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, JTI); |
| 432 | //return JTI; |
| 433 | } |
| 434 | |
| 435 | SDValue MBlazeTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 436 | LowerConstantPool(SDValue Op, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 437 | SDValue ResNode; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 438 | ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 439 | const Constant *C = N->getConstVal(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 440 | DebugLoc dl = Op.getDebugLoc(); |
| 441 | |
| 442 | SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(), |
| 443 | N->getOffset(), MBlazeII::MO_ABS_HILO); |
| 444 | return DAG.getNode(MBlazeISD::Wrap, dl, MVT::i32, CP); |
| 445 | } |
| 446 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 447 | SDValue MBlazeTargetLowering::LowerVASTART(SDValue Op, |
| 448 | SelectionDAG &DAG) const { |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 449 | MachineFunction &MF = DAG.getMachineFunction(); |
| 450 | MBlazeFunctionInfo *FuncInfo = MF.getInfo<MBlazeFunctionInfo>(); |
| 451 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 452 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 453 | SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 454 | getPointerTy()); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 455 | |
| 456 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 457 | // memory location argument. |
| 458 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| 459 | return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1), SV, 0, |
| 460 | false, false, 0); |
| 461 | } |
| 462 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 463 | //===----------------------------------------------------------------------===// |
| 464 | // Calling Convention Implementation |
| 465 | //===----------------------------------------------------------------------===// |
| 466 | |
| 467 | #include "MBlazeGenCallingConv.inc" |
| 468 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 469 | static bool CC_MBlaze2(unsigned ValNo, EVT ValVT, |
| 470 | EVT LocVT, CCValAssign::LocInfo LocInfo, |
| 471 | ISD::ArgFlagsTy ArgFlags, CCState &State) { |
| 472 | static const unsigned RegsSize=6; |
| 473 | static const unsigned IntRegs[] = { |
| 474 | MBlaze::R5, MBlaze::R6, MBlaze::R7, |
| 475 | MBlaze::R8, MBlaze::R9, MBlaze::R10 |
| 476 | }; |
| 477 | |
| 478 | static const unsigned FltRegs[] = { |
| 479 | MBlaze::F5, MBlaze::F6, MBlaze::F7, |
| 480 | MBlaze::F8, MBlaze::F9, MBlaze::F10 |
| 481 | }; |
| 482 | |
| 483 | unsigned Reg=0; |
| 484 | |
| 485 | // Promote i8 and i16 |
| 486 | if (LocVT == MVT::i8 || LocVT == MVT::i16) { |
| 487 | LocVT = MVT::i32; |
| 488 | if (ArgFlags.isSExt()) |
| 489 | LocInfo = CCValAssign::SExt; |
| 490 | else if (ArgFlags.isZExt()) |
| 491 | LocInfo = CCValAssign::ZExt; |
| 492 | else |
| 493 | LocInfo = CCValAssign::AExt; |
| 494 | } |
| 495 | |
| 496 | if (ValVT == MVT::i32) { |
| 497 | Reg = State.AllocateReg(IntRegs, RegsSize); |
| 498 | LocVT = MVT::i32; |
| 499 | } else if (ValVT == MVT::f32) { |
| 500 | Reg = State.AllocateReg(FltRegs, RegsSize); |
| 501 | LocVT = MVT::f32; |
| 502 | } |
| 503 | |
| 504 | if (!Reg) { |
| 505 | unsigned SizeInBytes = ValVT.getSizeInBits() >> 3; |
| 506 | unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes); |
| 507 | State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); |
| 508 | } else { |
| 509 | unsigned SizeInBytes = ValVT.getSizeInBits() >> 3; |
| 510 | State.AllocateStack(SizeInBytes, SizeInBytes); |
| 511 | State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); |
| 512 | } |
| 513 | |
| 514 | return false; // CC must always match |
| 515 | } |
| 516 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 517 | //===----------------------------------------------------------------------===// |
| 518 | // Call Calling Convention Implementation |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | |
| 521 | /// LowerCall - functions arguments are copied from virtual regs to |
| 522 | /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. |
| 523 | /// TODO: isVarArg, isTailCall. |
| 524 | SDValue MBlazeTargetLowering:: |
| 525 | LowerCall(SDValue Chain, SDValue Callee, CallingConv::ID CallConv, |
| 526 | bool isVarArg, bool &isTailCall, |
| 527 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame^] | 528 | const SmallVectorImpl<SDValue> &OutVals, |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 529 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 530 | DebugLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 531 | SmallVectorImpl<SDValue> &InVals) const { |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 532 | // MBlaze does not yet support tail call optimization |
| 533 | isTailCall = false; |
| 534 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 535 | MachineFunction &MF = DAG.getMachineFunction(); |
| 536 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 537 | |
| 538 | // Analyze operands of the call, assigning locations to each operand. |
| 539 | SmallVector<CCValAssign, 16> ArgLocs; |
| 540 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs, |
| 541 | *DAG.getContext()); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 542 | CCInfo.AnalyzeCallOperands(Outs, CC_MBlaze2); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 543 | |
| 544 | // Get a count of how many bytes are to be pushed on the stack. |
| 545 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| 546 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
| 547 | |
| 548 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 549 | SmallVector<SDValue, 8> MemOpChains; |
| 550 | |
| 551 | // First/LastArgStackLoc contains the first/last |
| 552 | // "at stack" argument location. |
| 553 | int LastArgStackLoc = 0; |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 554 | unsigned FirstStackArgLoc = 0; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 555 | |
| 556 | // Walk the register/memloc assignments, inserting copies/loads. |
| 557 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 558 | CCValAssign &VA = ArgLocs[i]; |
| 559 | EVT RegVT = VA.getLocVT(); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame^] | 560 | SDValue Arg = OutVals[i]; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 561 | |
| 562 | // Promote the value if needed. |
| 563 | switch (VA.getLocInfo()) { |
| 564 | default: llvm_unreachable("Unknown loc info!"); |
| 565 | case CCValAssign::Full: break; |
| 566 | case CCValAssign::SExt: |
| 567 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); |
| 568 | break; |
| 569 | case CCValAssign::ZExt: |
| 570 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); |
| 571 | break; |
| 572 | case CCValAssign::AExt: |
| 573 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); |
| 574 | break; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Arguments that can be passed on register must be kept at |
| 578 | // RegsToPass vector |
| 579 | if (VA.isRegLoc()) { |
| 580 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 581 | } else { |
| 582 | // Register can't get to this point... |
| 583 | assert(VA.isMemLoc()); |
| 584 | |
| 585 | // Create the frame index object for this incoming parameter |
| 586 | LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset()); |
| 587 | int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 588 | LastArgStackLoc, true); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 589 | |
| 590 | SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy()); |
| 591 | |
| 592 | // emit ISD::STORE whichs stores the |
| 593 | // parameter value to a stack Location |
| 594 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0, |
| 595 | false, false, 0)); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // Transform all store nodes into one single node because all store |
| 600 | // nodes are independent of each other. |
| 601 | if (!MemOpChains.empty()) |
| 602 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 603 | &MemOpChains[0], MemOpChains.size()); |
| 604 | |
| 605 | // Build a sequence of copy-to-reg nodes chained together with token |
| 606 | // chain and flag operands which copy the outgoing args into registers. |
| 607 | // The InFlag in necessary since all emited instructions must be |
| 608 | // stuck together. |
| 609 | SDValue InFlag; |
| 610 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 611 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 612 | RegsToPass[i].second, InFlag); |
| 613 | InFlag = Chain.getValue(1); |
| 614 | } |
| 615 | |
| 616 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 617 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 618 | // node so that legalize doesn't hack it. |
Wesley Peck | 173c5c4 | 2010-02-24 20:16:27 +0000 | [diff] [blame] | 619 | unsigned char OpFlag = MBlazeII::MO_NO_FLAG; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 620 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 621 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 622 | getPointerTy(), 0, OpFlag); |
| 623 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 624 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), |
| 625 | getPointerTy(), OpFlag); |
| 626 | |
| 627 | // MBlazeJmpLink = #chain, #target_address, #opt_in_flags... |
| 628 | // = Chain, Callee, Reg#1, Reg#2, ... |
| 629 | // |
| 630 | // Returns a chain & a flag for retval copy to use. |
| 631 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 632 | SmallVector<SDValue, 8> Ops; |
| 633 | Ops.push_back(Chain); |
| 634 | Ops.push_back(Callee); |
| 635 | |
| 636 | // Add argument registers to the end of the list so that they are |
| 637 | // known live into the call. |
| 638 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 639 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 640 | RegsToPass[i].second.getValueType())); |
| 641 | } |
| 642 | |
| 643 | if (InFlag.getNode()) |
| 644 | Ops.push_back(InFlag); |
| 645 | |
| 646 | Chain = DAG.getNode(MBlazeISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size()); |
| 647 | InFlag = Chain.getValue(1); |
| 648 | |
| 649 | // Create the CALLSEQ_END node. |
| 650 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 651 | DAG.getIntPtrConstant(0, true), InFlag); |
| 652 | if (!Ins.empty()) |
| 653 | InFlag = Chain.getValue(1); |
| 654 | |
| 655 | // Handle result values, copying them out of physregs into vregs that we |
| 656 | // return. |
| 657 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 658 | Ins, dl, DAG, InVals); |
| 659 | } |
| 660 | |
| 661 | /// LowerCallResult - Lower the result values of a call into the |
| 662 | /// appropriate copies out of appropriate physical registers. |
| 663 | SDValue MBlazeTargetLowering:: |
| 664 | LowerCallResult(SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, |
| 665 | bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, |
| 666 | DebugLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 667 | SmallVectorImpl<SDValue> &InVals) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 668 | // Assign locations to each value returned by this call. |
| 669 | SmallVector<CCValAssign, 16> RVLocs; |
| 670 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 671 | RVLocs, *DAG.getContext()); |
| 672 | |
| 673 | CCInfo.AnalyzeCallResult(Ins, RetCC_MBlaze); |
| 674 | |
| 675 | // Copy all of the result registers out of their specified physreg. |
| 676 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 677 | Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), |
| 678 | RVLocs[i].getValVT(), InFlag).getValue(1); |
| 679 | InFlag = Chain.getValue(2); |
| 680 | InVals.push_back(Chain.getValue(0)); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 681 | } |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 682 | |
| 683 | return Chain; |
| 684 | } |
| 685 | |
| 686 | //===----------------------------------------------------------------------===// |
| 687 | // Formal Arguments Calling Convention Implementation |
| 688 | //===----------------------------------------------------------------------===// |
| 689 | |
| 690 | /// LowerFormalArguments - transform physical registers into |
| 691 | /// virtual registers and generate load operations for |
| 692 | /// arguments places on the stack. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 693 | SDValue MBlazeTargetLowering:: |
| 694 | LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 695 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 696 | DebugLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 697 | SmallVectorImpl<SDValue> &InVals) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 698 | MachineFunction &MF = DAG.getMachineFunction(); |
| 699 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 700 | MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>(); |
| 701 | |
| 702 | unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 703 | MBlazeFI->setVarArgsFrameIndex(0); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 704 | |
| 705 | // Used with vargs to acumulate store chains. |
| 706 | std::vector<SDValue> OutChains; |
| 707 | |
| 708 | // Keep track of the last register used for arguments |
| 709 | unsigned ArgRegEnd = 0; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 710 | |
| 711 | // Assign locations to all of the incoming arguments. |
| 712 | SmallVector<CCValAssign, 16> ArgLocs; |
| 713 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 714 | ArgLocs, *DAG.getContext()); |
| 715 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 716 | CCInfo.AnalyzeFormalArguments(Ins, CC_MBlaze2); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 717 | SDValue StackPtr; |
| 718 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 719 | unsigned FirstStackArgLoc = 0; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 720 | |
| 721 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 722 | CCValAssign &VA = ArgLocs[i]; |
| 723 | |
| 724 | // Arguments stored on registers |
| 725 | if (VA.isRegLoc()) { |
| 726 | EVT RegVT = VA.getLocVT(); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 727 | ArgRegEnd = VA.getLocReg(); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 728 | TargetRegisterClass *RC = 0; |
| 729 | |
| 730 | if (RegVT == MVT::i32) |
| 731 | RC = MBlaze::CPURegsRegisterClass; |
| 732 | else if (RegVT == MVT::f32) |
| 733 | RC = MBlaze::FGR32RegisterClass; |
| 734 | else |
| 735 | llvm_unreachable("RegVT not supported by LowerFormalArguments"); |
| 736 | |
| 737 | // Transform the arguments stored on |
| 738 | // physical registers into virtual ones |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 739 | unsigned Reg = MF.addLiveIn(ArgRegEnd, RC); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 740 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| 741 | |
| 742 | // If this is an 8 or 16-bit value, it has been passed promoted |
| 743 | // to 32 bits. Insert an assert[sz]ext to capture this, then |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 744 | // truncate to the right size. If if is a floating point value |
| 745 | // then convert to the correct type. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 746 | if (VA.getLocInfo() != CCValAssign::Full) { |
| 747 | unsigned Opcode = 0; |
| 748 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 749 | Opcode = ISD::AssertSext; |
| 750 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 751 | Opcode = ISD::AssertZext; |
| 752 | if (Opcode) |
| 753 | ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue, |
| 754 | DAG.getValueType(VA.getValVT())); |
| 755 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
| 756 | } |
| 757 | |
| 758 | InVals.push_back(ArgValue); |
| 759 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 760 | } else { // VA.isRegLoc() |
| 761 | |
| 762 | // sanity check |
| 763 | assert(VA.isMemLoc()); |
| 764 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 765 | // The last argument is not a register |
| 766 | ArgRegEnd = 0; |
| 767 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 768 | // The stack pointer offset is relative to the caller stack frame. |
| 769 | // Since the real stack size is unknown here, a negative SPOffset |
| 770 | // is used so there's a way to adjust these offsets when the stack |
| 771 | // size get known (on EliminateFrameIndex). A dummy SPOffset is |
| 772 | // used instead of a direct negative address (which is recorded to |
| 773 | // be used on emitPrologue) to avoid mis-calc of the first stack |
| 774 | // offset on PEI::calculateFrameObjectOffsets. |
| 775 | // Arguments are always 32-bit. |
| 776 | unsigned ArgSize = VA.getLocVT().getSizeInBits()/8; |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 777 | int FI = MFI->CreateFixedObject(ArgSize, 0, true); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 778 | MBlazeFI->recordLoadArgsFI(FI, -(ArgSize+ |
| 779 | (FirstStackArgLoc + VA.getLocMemOffset()))); |
| 780 | |
| 781 | // Create load nodes to retrieve arguments from the stack |
| 782 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 783 | InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0, |
| 784 | false, false, 0)); |
| 785 | } |
| 786 | } |
| 787 | |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 788 | // To meet ABI, when VARARGS are passed on registers, the registers |
| 789 | // must have their values written to the caller stack frame. If the last |
| 790 | // argument was placed in the stack, there's no need to save any register. |
| 791 | if ((isVarArg) && ArgRegEnd) { |
| 792 | if (StackPtr.getNode() == 0) |
| 793 | StackPtr = DAG.getRegister(StackReg, getPointerTy()); |
| 794 | |
| 795 | // The last register argument that must be saved is MBlaze::R10 |
| 796 | TargetRegisterClass *RC = MBlaze::CPURegsRegisterClass; |
| 797 | |
| 798 | unsigned Begin = MBlazeRegisterInfo::getRegisterNumbering(MBlaze::R5); |
| 799 | unsigned Start = MBlazeRegisterInfo::getRegisterNumbering(ArgRegEnd+1); |
| 800 | unsigned End = MBlazeRegisterInfo::getRegisterNumbering(MBlaze::R10); |
| 801 | unsigned StackLoc = ArgLocs.size()-1 + (Start - Begin); |
| 802 | |
| 803 | for (; Start <= End; ++Start, ++StackLoc) { |
| 804 | unsigned Reg = MBlazeRegisterInfo::getRegisterFromNumbering(Start); |
| 805 | unsigned LiveReg = MF.addLiveIn(Reg, RC); |
| 806 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, LiveReg, MVT::i32); |
| 807 | |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 808 | int FI = MFI->CreateFixedObject(4, 0, true); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 809 | MBlazeFI->recordStoreVarArgsFI(FI, -(4+(StackLoc*4))); |
| 810 | SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy()); |
| 811 | OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff, NULL, 0, |
| 812 | false, false, 0)); |
| 813 | |
| 814 | // Record the frame index of the first variable argument |
| 815 | // which is a value necessary to VASTART. |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 816 | if (!MBlazeFI->getVarArgsFrameIndex()) |
| 817 | MBlazeFI->setVarArgsFrameIndex(FI); |
Wesley Peck | c2bf2bb | 2010-03-06 23:23:12 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | // All stores are grouped in one node to allow the matching between |
| 822 | // the size of Ins and InVals. This only happens when on varg functions |
| 823 | if (!OutChains.empty()) { |
| 824 | OutChains.push_back(Chain); |
| 825 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 826 | &OutChains[0], OutChains.size()); |
| 827 | } |
| 828 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 829 | return Chain; |
| 830 | } |
| 831 | |
| 832 | //===----------------------------------------------------------------------===// |
| 833 | // Return Value Calling Convention Implementation |
| 834 | //===----------------------------------------------------------------------===// |
| 835 | |
| 836 | SDValue MBlazeTargetLowering:: |
| 837 | LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 838 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame^] | 839 | const SmallVectorImpl<SDValue> &OutVals, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 840 | DebugLoc dl, SelectionDAG &DAG) const { |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 841 | // CCValAssign - represent the assignment of |
| 842 | // the return value to a location |
| 843 | SmallVector<CCValAssign, 16> RVLocs; |
| 844 | |
| 845 | // CCState - Info about the registers and stack slot. |
| 846 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 847 | RVLocs, *DAG.getContext()); |
| 848 | |
| 849 | // Analize return values. |
| 850 | CCInfo.AnalyzeReturn(Outs, RetCC_MBlaze); |
| 851 | |
| 852 | // If this is the first return lowered for this function, add |
| 853 | // the regs to the liveout set for the function. |
| 854 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
| 855 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 856 | if (RVLocs[i].isRegLoc()) |
| 857 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
| 858 | } |
| 859 | |
| 860 | SDValue Flag; |
| 861 | |
| 862 | // Copy the result values into the output registers. |
| 863 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 864 | CCValAssign &VA = RVLocs[i]; |
| 865 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 866 | |
| 867 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame^] | 868 | OutVals[i], Flag); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 869 | |
| 870 | // guarantee that all emitted copies are |
| 871 | // stuck together, avoiding something bad |
| 872 | Flag = Chain.getValue(1); |
| 873 | } |
| 874 | |
| 875 | // Return on MBlaze is always a "rtsd R15, 8" |
| 876 | if (Flag.getNode()) |
| 877 | return DAG.getNode(MBlazeISD::Ret, dl, MVT::Other, |
| 878 | Chain, DAG.getRegister(MBlaze::R15, MVT::i32), Flag); |
| 879 | else // Return Void |
| 880 | return DAG.getNode(MBlazeISD::Ret, dl, MVT::Other, |
| 881 | Chain, DAG.getRegister(MBlaze::R15, MVT::i32)); |
| 882 | } |
| 883 | |
| 884 | //===----------------------------------------------------------------------===// |
| 885 | // MBlaze Inline Assembly Support |
| 886 | //===----------------------------------------------------------------------===// |
| 887 | |
| 888 | /// getConstraintType - Given a constraint letter, return the type of |
| 889 | /// constraint it is for this target. |
| 890 | MBlazeTargetLowering::ConstraintType MBlazeTargetLowering:: |
| 891 | getConstraintType(const std::string &Constraint) const |
| 892 | { |
| 893 | // MBlaze specific constrainy |
| 894 | // |
| 895 | // 'd' : An address register. Equivalent to r. |
| 896 | // 'y' : Equivalent to r; retained for |
| 897 | // backwards compatibility. |
| 898 | // 'f' : Floating Point registers. |
| 899 | if (Constraint.size() == 1) { |
| 900 | switch (Constraint[0]) { |
| 901 | default : break; |
| 902 | case 'd': |
| 903 | case 'y': |
| 904 | case 'f': |
| 905 | return C_RegisterClass; |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | return TargetLowering::getConstraintType(Constraint); |
| 910 | } |
| 911 | |
| 912 | /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"), |
| 913 | /// return a list of registers that can be used to satisfy the constraint. |
| 914 | /// This should only be used for C_RegisterClass constraints. |
| 915 | std::pair<unsigned, const TargetRegisterClass*> MBlazeTargetLowering:: |
| 916 | getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const { |
| 917 | if (Constraint.size() == 1) { |
| 918 | switch (Constraint[0]) { |
| 919 | case 'r': |
| 920 | return std::make_pair(0U, MBlaze::CPURegsRegisterClass); |
| 921 | case 'f': |
| 922 | if (VT == MVT::f32) |
| 923 | return std::make_pair(0U, MBlaze::FGR32RegisterClass); |
| 924 | } |
| 925 | } |
| 926 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 927 | } |
| 928 | |
| 929 | /// Given a register class constraint, like 'r', if this corresponds directly |
| 930 | /// to an LLVM register class, return a register of 0 and the register class |
| 931 | /// pointer. |
| 932 | std::vector<unsigned> MBlazeTargetLowering:: |
| 933 | getRegClassForInlineAsmConstraint(const std::string &Constraint, EVT VT) const { |
| 934 | if (Constraint.size() != 1) |
| 935 | return std::vector<unsigned>(); |
| 936 | |
| 937 | switch (Constraint[0]) { |
| 938 | default : break; |
| 939 | case 'r': |
| 940 | // GCC MBlaze Constraint Letters |
| 941 | case 'd': |
| 942 | case 'y': |
| 943 | return make_vector<unsigned>( |
| 944 | MBlaze::R3, MBlaze::R4, MBlaze::R5, MBlaze::R6, |
| 945 | MBlaze::R7, MBlaze::R9, MBlaze::R10, MBlaze::R11, |
| 946 | MBlaze::R12, MBlaze::R19, MBlaze::R20, MBlaze::R21, |
| 947 | MBlaze::R22, MBlaze::R23, MBlaze::R24, MBlaze::R25, |
| 948 | MBlaze::R26, MBlaze::R27, MBlaze::R28, MBlaze::R29, |
| 949 | MBlaze::R30, MBlaze::R31, 0); |
| 950 | |
| 951 | case 'f': |
| 952 | return make_vector<unsigned>( |
| 953 | MBlaze::F3, MBlaze::F4, MBlaze::F5, MBlaze::F6, |
| 954 | MBlaze::F7, MBlaze::F9, MBlaze::F10, MBlaze::F11, |
| 955 | MBlaze::F12, MBlaze::F19, MBlaze::F20, MBlaze::F21, |
| 956 | MBlaze::F22, MBlaze::F23, MBlaze::F24, MBlaze::F25, |
| 957 | MBlaze::F26, MBlaze::F27, MBlaze::F28, MBlaze::F29, |
| 958 | MBlaze::F30, MBlaze::F31, 0); |
| 959 | } |
| 960 | return std::vector<unsigned>(); |
| 961 | } |
| 962 | |
| 963 | bool MBlazeTargetLowering:: |
| 964 | isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 965 | // The MBlaze target isn't yet aware of offsets. |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | bool MBlazeTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 970 | return VT != MVT::f32; |
| 971 | } |