1 //===-- AVRISelLowering.cpp - AVR DAG Lowering Implementation -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the interfaces that AVR uses to lower LLVM code into a 10 // selection DAG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AVRISelLowering.h" 15 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/CodeGen/CallingConvLower.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineInstrBuilder.h" 20 #include "llvm/CodeGen/MachineRegisterInfo.h" 21 #include "llvm/CodeGen/SelectionDAG.h" 22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/Support/ErrorHandling.h" 25 26 #include "AVR.h" 27 #include "AVRMachineFunctionInfo.h" 28 #include "AVRSubtarget.h" 29 #include "AVRTargetMachine.h" 30 #include "MCTargetDesc/AVRMCTargetDesc.h" 31 32 namespace llvm { 33 34 AVRTargetLowering::AVRTargetLowering(const AVRTargetMachine &TM, 35 const AVRSubtarget &STI) 36 : TargetLowering(TM), Subtarget(STI) { 37 // Set up the register classes. 38 addRegisterClass(MVT::i8, &AVR::GPR8RegClass); 39 addRegisterClass(MVT::i16, &AVR::DREGSRegClass); 40 41 // Compute derived properties from the register classes. 42 computeRegisterProperties(Subtarget.getRegisterInfo()); 43 44 setBooleanContents(ZeroOrOneBooleanContent); 45 setBooleanVectorContents(ZeroOrOneBooleanContent); 46 setSchedulingPreference(Sched::RegPressure); 47 setStackPointerRegisterToSaveRestore(AVR::SP); 48 setSupportsUnalignedAtomics(true); 49 50 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom); 51 setOperationAction(ISD::BlockAddress, MVT::i16, Custom); 52 53 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 54 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 55 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand); 56 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand); 57 58 for (MVT VT : MVT::integer_valuetypes()) { 59 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) { 60 setLoadExtAction(N, VT, MVT::i1, Promote); 61 setLoadExtAction(N, VT, MVT::i8, Expand); 62 } 63 } 64 65 setTruncStoreAction(MVT::i16, MVT::i8, Expand); 66 67 for (MVT VT : MVT::integer_valuetypes()) { 68 setOperationAction(ISD::ADDC, VT, Legal); 69 setOperationAction(ISD::SUBC, VT, Legal); 70 setOperationAction(ISD::ADDE, VT, Legal); 71 setOperationAction(ISD::SUBE, VT, Legal); 72 } 73 74 // sub (x, imm) gets canonicalized to add (x, -imm), so for illegal types 75 // revert into a sub since we don't have an add with immediate instruction. 76 setOperationAction(ISD::ADD, MVT::i32, Custom); 77 setOperationAction(ISD::ADD, MVT::i64, Custom); 78 79 // our shift instructions are only able to shift 1 bit at a time, so handle 80 // this in a custom way. 81 setOperationAction(ISD::SRA, MVT::i8, Custom); 82 setOperationAction(ISD::SHL, MVT::i8, Custom); 83 setOperationAction(ISD::SRL, MVT::i8, Custom); 84 setOperationAction(ISD::SRA, MVT::i16, Custom); 85 setOperationAction(ISD::SHL, MVT::i16, Custom); 86 setOperationAction(ISD::SRL, MVT::i16, Custom); 87 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand); 88 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand); 89 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand); 90 91 setOperationAction(ISD::ROTL, MVT::i8, Custom); 92 setOperationAction(ISD::ROTL, MVT::i16, Expand); 93 setOperationAction(ISD::ROTR, MVT::i8, Custom); 94 setOperationAction(ISD::ROTR, MVT::i16, Expand); 95 96 setOperationAction(ISD::BR_CC, MVT::i8, Custom); 97 setOperationAction(ISD::BR_CC, MVT::i16, Custom); 98 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 99 setOperationAction(ISD::BR_CC, MVT::i64, Custom); 100 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 101 102 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom); 103 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom); 104 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 105 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 106 setOperationAction(ISD::SETCC, MVT::i8, Custom); 107 setOperationAction(ISD::SETCC, MVT::i16, Custom); 108 setOperationAction(ISD::SETCC, MVT::i32, Custom); 109 setOperationAction(ISD::SETCC, MVT::i64, Custom); 110 setOperationAction(ISD::SELECT, MVT::i8, Expand); 111 setOperationAction(ISD::SELECT, MVT::i16, Expand); 112 113 setOperationAction(ISD::BSWAP, MVT::i16, Expand); 114 115 // Add support for postincrement and predecrement load/stores. 116 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal); 117 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal); 118 setIndexedLoadAction(ISD::PRE_DEC, MVT::i8, Legal); 119 setIndexedLoadAction(ISD::PRE_DEC, MVT::i16, Legal); 120 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal); 121 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal); 122 setIndexedStoreAction(ISD::PRE_DEC, MVT::i8, Legal); 123 setIndexedStoreAction(ISD::PRE_DEC, MVT::i16, Legal); 124 125 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 126 127 setOperationAction(ISD::VASTART, MVT::Other, Custom); 128 setOperationAction(ISD::VAEND, MVT::Other, Expand); 129 setOperationAction(ISD::VAARG, MVT::Other, Expand); 130 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 131 132 // Atomic operations which must be lowered to rtlib calls 133 for (MVT VT : MVT::integer_valuetypes()) { 134 setOperationAction(ISD::ATOMIC_SWAP, VT, Expand); 135 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Expand); 136 setOperationAction(ISD::ATOMIC_LOAD_NAND, VT, Expand); 137 setOperationAction(ISD::ATOMIC_LOAD_MAX, VT, Expand); 138 setOperationAction(ISD::ATOMIC_LOAD_MIN, VT, Expand); 139 setOperationAction(ISD::ATOMIC_LOAD_UMAX, VT, Expand); 140 setOperationAction(ISD::ATOMIC_LOAD_UMIN, VT, Expand); 141 } 142 143 // Division/remainder 144 setOperationAction(ISD::UDIV, MVT::i8, Expand); 145 setOperationAction(ISD::UDIV, MVT::i16, Expand); 146 setOperationAction(ISD::UREM, MVT::i8, Expand); 147 setOperationAction(ISD::UREM, MVT::i16, Expand); 148 setOperationAction(ISD::SDIV, MVT::i8, Expand); 149 setOperationAction(ISD::SDIV, MVT::i16, Expand); 150 setOperationAction(ISD::SREM, MVT::i8, Expand); 151 setOperationAction(ISD::SREM, MVT::i16, Expand); 152 153 // Make division and modulus custom 154 setOperationAction(ISD::UDIVREM, MVT::i8, Custom); 155 setOperationAction(ISD::UDIVREM, MVT::i16, Custom); 156 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 157 setOperationAction(ISD::SDIVREM, MVT::i8, Custom); 158 setOperationAction(ISD::SDIVREM, MVT::i16, Custom); 159 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 160 161 // Do not use MUL. The AVR instructions are closer to SMUL_LOHI &co. 162 setOperationAction(ISD::MUL, MVT::i8, Expand); 163 setOperationAction(ISD::MUL, MVT::i16, Expand); 164 165 // Expand 16 bit multiplications. 166 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand); 167 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand); 168 169 // Expand multiplications to libcalls when there is 170 // no hardware MUL. 171 if (!Subtarget.supportsMultiplication()) { 172 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand); 173 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand); 174 } 175 176 for (MVT VT : MVT::integer_valuetypes()) { 177 setOperationAction(ISD::MULHS, VT, Expand); 178 setOperationAction(ISD::MULHU, VT, Expand); 179 } 180 181 for (MVT VT : MVT::integer_valuetypes()) { 182 setOperationAction(ISD::CTPOP, VT, Expand); 183 setOperationAction(ISD::CTLZ, VT, Expand); 184 setOperationAction(ISD::CTTZ, VT, Expand); 185 } 186 187 for (MVT VT : MVT::integer_valuetypes()) { 188 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 189 // TODO: The generated code is pretty poor. Investigate using the 190 // same "shift and subtract with carry" trick that we do for 191 // extending 8-bit to 16-bit. This may require infrastructure 192 // improvements in how we treat 16-bit "registers" to be feasible. 193 } 194 195 // Division rtlib functions (not supported), use divmod functions instead 196 setLibcallName(RTLIB::SDIV_I8, nullptr); 197 setLibcallName(RTLIB::SDIV_I16, nullptr); 198 setLibcallName(RTLIB::SDIV_I32, nullptr); 199 setLibcallName(RTLIB::UDIV_I8, nullptr); 200 setLibcallName(RTLIB::UDIV_I16, nullptr); 201 setLibcallName(RTLIB::UDIV_I32, nullptr); 202 203 // Modulus rtlib functions (not supported), use divmod functions instead 204 setLibcallName(RTLIB::SREM_I8, nullptr); 205 setLibcallName(RTLIB::SREM_I16, nullptr); 206 setLibcallName(RTLIB::SREM_I32, nullptr); 207 setLibcallName(RTLIB::UREM_I8, nullptr); 208 setLibcallName(RTLIB::UREM_I16, nullptr); 209 setLibcallName(RTLIB::UREM_I32, nullptr); 210 211 // Division and modulus rtlib functions 212 setLibcallName(RTLIB::SDIVREM_I8, "__divmodqi4"); 213 setLibcallName(RTLIB::SDIVREM_I16, "__divmodhi4"); 214 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 215 setLibcallName(RTLIB::UDIVREM_I8, "__udivmodqi4"); 216 setLibcallName(RTLIB::UDIVREM_I16, "__udivmodhi4"); 217 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 218 219 // Several of the runtime library functions use a special calling conv 220 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::AVR_BUILTIN); 221 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::AVR_BUILTIN); 222 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::AVR_BUILTIN); 223 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::AVR_BUILTIN); 224 225 // Trigonometric rtlib functions 226 setLibcallName(RTLIB::SIN_F32, "sin"); 227 setLibcallName(RTLIB::COS_F32, "cos"); 228 229 setMinFunctionAlignment(Align(2)); 230 setMinimumJumpTableEntries(UINT_MAX); 231 } 232 233 const char *AVRTargetLowering::getTargetNodeName(unsigned Opcode) const { 234 #define NODE(name) \ 235 case AVRISD::name: \ 236 return #name 237 238 switch (Opcode) { 239 default: 240 return nullptr; 241 NODE(RET_FLAG); 242 NODE(RETI_FLAG); 243 NODE(CALL); 244 NODE(WRAPPER); 245 NODE(LSL); 246 NODE(LSR); 247 NODE(ROL); 248 NODE(ROR); 249 NODE(ASR); 250 NODE(LSLLOOP); 251 NODE(LSRLOOP); 252 NODE(ROLLOOP); 253 NODE(RORLOOP); 254 NODE(ASRLOOP); 255 NODE(BRCOND); 256 NODE(CMP); 257 NODE(CMPC); 258 NODE(TST); 259 NODE(SELECT_CC); 260 #undef NODE 261 } 262 } 263 264 EVT AVRTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 265 EVT VT) const { 266 assert(!VT.isVector() && "No AVR SetCC type for vectors!"); 267 return MVT::i8; 268 } 269 270 SDValue AVRTargetLowering::LowerShifts(SDValue Op, SelectionDAG &DAG) const { 271 //:TODO: this function has to be completely rewritten to produce optimal 272 // code, for now it's producing very long but correct code. 273 unsigned Opc8; 274 const SDNode *N = Op.getNode(); 275 EVT VT = Op.getValueType(); 276 SDLoc dl(N); 277 assert(isPowerOf2_32(VT.getSizeInBits()) && 278 "Expected power-of-2 shift amount"); 279 280 // Expand non-constant shifts to loops. 281 if (!isa<ConstantSDNode>(N->getOperand(1))) { 282 switch (Op.getOpcode()) { 283 default: 284 llvm_unreachable("Invalid shift opcode!"); 285 case ISD::SHL: 286 return DAG.getNode(AVRISD::LSLLOOP, dl, VT, N->getOperand(0), 287 N->getOperand(1)); 288 case ISD::SRL: 289 return DAG.getNode(AVRISD::LSRLOOP, dl, VT, N->getOperand(0), 290 N->getOperand(1)); 291 case ISD::ROTL: { 292 SDValue Amt = N->getOperand(1); 293 EVT AmtVT = Amt.getValueType(); 294 Amt = DAG.getNode(ISD::AND, dl, AmtVT, Amt, 295 DAG.getConstant(VT.getSizeInBits() - 1, dl, AmtVT)); 296 return DAG.getNode(AVRISD::ROLLOOP, dl, VT, N->getOperand(0), Amt); 297 } 298 case ISD::ROTR: { 299 SDValue Amt = N->getOperand(1); 300 EVT AmtVT = Amt.getValueType(); 301 Amt = DAG.getNode(ISD::AND, dl, AmtVT, Amt, 302 DAG.getConstant(VT.getSizeInBits() - 1, dl, AmtVT)); 303 return DAG.getNode(AVRISD::RORLOOP, dl, VT, N->getOperand(0), Amt); 304 } 305 case ISD::SRA: 306 return DAG.getNode(AVRISD::ASRLOOP, dl, VT, N->getOperand(0), 307 N->getOperand(1)); 308 } 309 } 310 311 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 312 SDValue Victim = N->getOperand(0); 313 314 switch (Op.getOpcode()) { 315 case ISD::SRA: 316 Opc8 = AVRISD::ASR; 317 break; 318 case ISD::ROTL: 319 Opc8 = AVRISD::ROL; 320 ShiftAmount = ShiftAmount % VT.getSizeInBits(); 321 break; 322 case ISD::ROTR: 323 Opc8 = AVRISD::ROR; 324 ShiftAmount = ShiftAmount % VT.getSizeInBits(); 325 break; 326 case ISD::SRL: 327 Opc8 = AVRISD::LSR; 328 break; 329 case ISD::SHL: 330 Opc8 = AVRISD::LSL; 331 break; 332 default: 333 llvm_unreachable("Invalid shift opcode"); 334 } 335 336 while (ShiftAmount--) { 337 Victim = DAG.getNode(Opc8, dl, VT, Victim); 338 } 339 340 return Victim; 341 } 342 343 SDValue AVRTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 344 unsigned Opcode = Op->getOpcode(); 345 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 346 "Invalid opcode for Div/Rem lowering"); 347 bool IsSigned = (Opcode == ISD::SDIVREM); 348 EVT VT = Op->getValueType(0); 349 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 350 351 RTLIB::Libcall LC; 352 switch (VT.getSimpleVT().SimpleTy) { 353 default: 354 llvm_unreachable("Unexpected request for libcall!"); 355 case MVT::i8: 356 LC = IsSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; 357 break; 358 case MVT::i16: 359 LC = IsSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; 360 break; 361 case MVT::i32: 362 LC = IsSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; 363 break; 364 } 365 366 SDValue InChain = DAG.getEntryNode(); 367 368 TargetLowering::ArgListTy Args; 369 TargetLowering::ArgListEntry Entry; 370 for (SDValue const &Value : Op->op_values()) { 371 Entry.Node = Value; 372 Entry.Ty = Value.getValueType().getTypeForEVT(*DAG.getContext()); 373 Entry.IsSExt = IsSigned; 374 Entry.IsZExt = !IsSigned; 375 Args.push_back(Entry); 376 } 377 378 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 379 getPointerTy(DAG.getDataLayout())); 380 381 Type *RetTy = (Type *)StructType::get(Ty, Ty); 382 383 SDLoc dl(Op); 384 TargetLowering::CallLoweringInfo CLI(DAG); 385 CLI.setDebugLoc(dl) 386 .setChain(InChain) 387 .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args)) 388 .setInRegister() 389 .setSExtResult(IsSigned) 390 .setZExtResult(!IsSigned); 391 392 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 393 return CallInfo.first; 394 } 395 396 SDValue AVRTargetLowering::LowerGlobalAddress(SDValue Op, 397 SelectionDAG &DAG) const { 398 auto DL = DAG.getDataLayout(); 399 400 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 401 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); 402 403 // Create the TargetGlobalAddress node, folding in the constant offset. 404 SDValue Result = 405 DAG.getTargetGlobalAddress(GV, SDLoc(Op), getPointerTy(DL), Offset); 406 return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result); 407 } 408 409 SDValue AVRTargetLowering::LowerBlockAddress(SDValue Op, 410 SelectionDAG &DAG) const { 411 auto DL = DAG.getDataLayout(); 412 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 413 414 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(DL)); 415 416 return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result); 417 } 418 419 /// IntCCToAVRCC - Convert a DAG integer condition code to an AVR CC. 420 static AVRCC::CondCodes intCCToAVRCC(ISD::CondCode CC) { 421 switch (CC) { 422 default: 423 llvm_unreachable("Unknown condition code!"); 424 case ISD::SETEQ: 425 return AVRCC::COND_EQ; 426 case ISD::SETNE: 427 return AVRCC::COND_NE; 428 case ISD::SETGE: 429 return AVRCC::COND_GE; 430 case ISD::SETLT: 431 return AVRCC::COND_LT; 432 case ISD::SETUGE: 433 return AVRCC::COND_SH; 434 case ISD::SETULT: 435 return AVRCC::COND_LO; 436 } 437 } 438 439 /// Returns appropriate AVR CMP/CMPC nodes and corresponding condition code for 440 /// the given operands. 441 SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 442 SDValue &AVRcc, SelectionDAG &DAG, 443 SDLoc DL) const { 444 SDValue Cmp; 445 EVT VT = LHS.getValueType(); 446 bool UseTest = false; 447 448 switch (CC) { 449 default: 450 break; 451 case ISD::SETLE: { 452 // Swap operands and reverse the branching condition. 453 std::swap(LHS, RHS); 454 CC = ISD::SETGE; 455 break; 456 } 457 case ISD::SETGT: { 458 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 459 switch (C->getSExtValue()) { 460 case -1: { 461 // When doing lhs > -1 use a tst instruction on the top part of lhs 462 // and use brpl instead of using a chain of cp/cpc. 463 UseTest = true; 464 AVRcc = DAG.getConstant(AVRCC::COND_PL, DL, MVT::i8); 465 break; 466 } 467 case 0: { 468 // Turn lhs > 0 into 0 < lhs since 0 can be materialized with 469 // __zero_reg__ in lhs. 470 RHS = LHS; 471 LHS = DAG.getConstant(0, DL, VT); 472 CC = ISD::SETLT; 473 break; 474 } 475 default: { 476 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows 477 // us to fold the constant into the cmp instruction. 478 RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT); 479 CC = ISD::SETGE; 480 break; 481 } 482 } 483 break; 484 } 485 // Swap operands and reverse the branching condition. 486 std::swap(LHS, RHS); 487 CC = ISD::SETLT; 488 break; 489 } 490 case ISD::SETLT: { 491 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 492 switch (C->getSExtValue()) { 493 case 1: { 494 // Turn lhs < 1 into 0 >= lhs since 0 can be materialized with 495 // __zero_reg__ in lhs. 496 RHS = LHS; 497 LHS = DAG.getConstant(0, DL, VT); 498 CC = ISD::SETGE; 499 break; 500 } 501 case 0: { 502 // When doing lhs < 0 use a tst instruction on the top part of lhs 503 // and use brmi instead of using a chain of cp/cpc. 504 UseTest = true; 505 AVRcc = DAG.getConstant(AVRCC::COND_MI, DL, MVT::i8); 506 break; 507 } 508 } 509 } 510 break; 511 } 512 case ISD::SETULE: { 513 // Swap operands and reverse the branching condition. 514 std::swap(LHS, RHS); 515 CC = ISD::SETUGE; 516 break; 517 } 518 case ISD::SETUGT: { 519 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to 520 // fold the constant into the cmp instruction. 521 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) { 522 RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT); 523 CC = ISD::SETUGE; 524 break; 525 } 526 // Swap operands and reverse the branching condition. 527 std::swap(LHS, RHS); 528 CC = ISD::SETULT; 529 break; 530 } 531 } 532 533 // Expand 32 and 64 bit comparisons with custom CMP and CMPC nodes instead of 534 // using the default and/or/xor expansion code which is much longer. 535 if (VT == MVT::i32) { 536 SDValue LHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS, 537 DAG.getIntPtrConstant(0, DL)); 538 SDValue LHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS, 539 DAG.getIntPtrConstant(1, DL)); 540 SDValue RHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS, 541 DAG.getIntPtrConstant(0, DL)); 542 SDValue RHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS, 543 DAG.getIntPtrConstant(1, DL)); 544 545 if (UseTest) { 546 // When using tst we only care about the highest part. 547 SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHShi, 548 DAG.getIntPtrConstant(1, DL)); 549 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top); 550 } else { 551 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHSlo, RHSlo); 552 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHShi, RHShi, Cmp); 553 } 554 } else if (VT == MVT::i64) { 555 SDValue LHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS, 556 DAG.getIntPtrConstant(0, DL)); 557 SDValue LHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS, 558 DAG.getIntPtrConstant(1, DL)); 559 560 SDValue LHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0, 561 DAG.getIntPtrConstant(0, DL)); 562 SDValue LHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0, 563 DAG.getIntPtrConstant(1, DL)); 564 SDValue LHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1, 565 DAG.getIntPtrConstant(0, DL)); 566 SDValue LHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1, 567 DAG.getIntPtrConstant(1, DL)); 568 569 SDValue RHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS, 570 DAG.getIntPtrConstant(0, DL)); 571 SDValue RHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS, 572 DAG.getIntPtrConstant(1, DL)); 573 574 SDValue RHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0, 575 DAG.getIntPtrConstant(0, DL)); 576 SDValue RHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0, 577 DAG.getIntPtrConstant(1, DL)); 578 SDValue RHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1, 579 DAG.getIntPtrConstant(0, DL)); 580 SDValue RHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1, 581 DAG.getIntPtrConstant(1, DL)); 582 583 if (UseTest) { 584 // When using tst we only care about the highest part. 585 SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHS3, 586 DAG.getIntPtrConstant(1, DL)); 587 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top); 588 } else { 589 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHS0, RHS0); 590 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS1, RHS1, Cmp); 591 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS2, RHS2, Cmp); 592 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS3, RHS3, Cmp); 593 } 594 } else if (VT == MVT::i8 || VT == MVT::i16) { 595 if (UseTest) { 596 // When using tst we only care about the highest part. 597 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, 598 (VT == MVT::i8) 599 ? LHS 600 : DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, 601 LHS, DAG.getIntPtrConstant(1, DL))); 602 } else { 603 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHS, RHS); 604 } 605 } else { 606 llvm_unreachable("Invalid comparison size"); 607 } 608 609 // When using a test instruction AVRcc is already set. 610 if (!UseTest) { 611 AVRcc = DAG.getConstant(intCCToAVRCC(CC), DL, MVT::i8); 612 } 613 614 return Cmp; 615 } 616 617 SDValue AVRTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 618 SDValue Chain = Op.getOperand(0); 619 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 620 SDValue LHS = Op.getOperand(2); 621 SDValue RHS = Op.getOperand(3); 622 SDValue Dest = Op.getOperand(4); 623 SDLoc dl(Op); 624 625 SDValue TargetCC; 626 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl); 627 628 return DAG.getNode(AVRISD::BRCOND, dl, MVT::Other, Chain, Dest, TargetCC, 629 Cmp); 630 } 631 632 SDValue AVRTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 633 SDValue LHS = Op.getOperand(0); 634 SDValue RHS = Op.getOperand(1); 635 SDValue TrueV = Op.getOperand(2); 636 SDValue FalseV = Op.getOperand(3); 637 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 638 SDLoc dl(Op); 639 640 SDValue TargetCC; 641 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl); 642 643 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 644 SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp}; 645 646 return DAG.getNode(AVRISD::SELECT_CC, dl, VTs, Ops); 647 } 648 649 SDValue AVRTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 650 SDValue LHS = Op.getOperand(0); 651 SDValue RHS = Op.getOperand(1); 652 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 653 SDLoc DL(Op); 654 655 SDValue TargetCC; 656 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, DL); 657 658 SDValue TrueV = DAG.getConstant(1, DL, Op.getValueType()); 659 SDValue FalseV = DAG.getConstant(0, DL, Op.getValueType()); 660 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 661 SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp}; 662 663 return DAG.getNode(AVRISD::SELECT_CC, DL, VTs, Ops); 664 } 665 666 SDValue AVRTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { 667 const MachineFunction &MF = DAG.getMachineFunction(); 668 const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>(); 669 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 670 auto DL = DAG.getDataLayout(); 671 SDLoc dl(Op); 672 673 // Vastart just stores the address of the VarArgsFrameIndex slot into the 674 // memory location argument. 675 SDValue FI = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(), getPointerTy(DL)); 676 677 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1), 678 MachinePointerInfo(SV), 0); 679 } 680 681 SDValue AVRTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 682 switch (Op.getOpcode()) { 683 default: 684 llvm_unreachable("Don't know how to custom lower this!"); 685 case ISD::SHL: 686 case ISD::SRA: 687 case ISD::SRL: 688 case ISD::ROTL: 689 case ISD::ROTR: 690 return LowerShifts(Op, DAG); 691 case ISD::GlobalAddress: 692 return LowerGlobalAddress(Op, DAG); 693 case ISD::BlockAddress: 694 return LowerBlockAddress(Op, DAG); 695 case ISD::BR_CC: 696 return LowerBR_CC(Op, DAG); 697 case ISD::SELECT_CC: 698 return LowerSELECT_CC(Op, DAG); 699 case ISD::SETCC: 700 return LowerSETCC(Op, DAG); 701 case ISD::VASTART: 702 return LowerVASTART(Op, DAG); 703 case ISD::SDIVREM: 704 case ISD::UDIVREM: 705 return LowerDivRem(Op, DAG); 706 } 707 708 return SDValue(); 709 } 710 711 /// Replace a node with an illegal result type 712 /// with a new node built out of custom code. 713 void AVRTargetLowering::ReplaceNodeResults(SDNode *N, 714 SmallVectorImpl<SDValue> &Results, 715 SelectionDAG &DAG) const { 716 SDLoc DL(N); 717 718 switch (N->getOpcode()) { 719 case ISD::ADD: { 720 // Convert add (x, imm) into sub (x, -imm). 721 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 722 SDValue Sub = DAG.getNode( 723 ISD::SUB, DL, N->getValueType(0), N->getOperand(0), 724 DAG.getConstant(-C->getAPIntValue(), DL, C->getValueType(0))); 725 Results.push_back(Sub); 726 } 727 break; 728 } 729 default: { 730 SDValue Res = LowerOperation(SDValue(N, 0), DAG); 731 732 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I) 733 Results.push_back(Res.getValue(I)); 734 735 break; 736 } 737 } 738 } 739 740 /// Return true if the addressing mode represented 741 /// by AM is legal for this target, for a load/store of the specified type. 742 bool AVRTargetLowering::isLegalAddressingMode(const DataLayout &DL, 743 const AddrMode &AM, Type *Ty, 744 unsigned AS, Instruction *I) const { 745 int64_t Offs = AM.BaseOffs; 746 747 // Allow absolute addresses. 748 if (AM.BaseGV && !AM.HasBaseReg && AM.Scale == 0 && Offs == 0) { 749 return true; 750 } 751 752 // Flash memory instructions only allow zero offsets. 753 if (isa<PointerType>(Ty) && AS == AVR::ProgramMemory) { 754 return false; 755 } 756 757 // Allow reg+<6bit> offset. 758 if (Offs < 0) 759 Offs = -Offs; 760 if (AM.BaseGV == 0 && AM.HasBaseReg && AM.Scale == 0 && isUInt<6>(Offs)) { 761 return true; 762 } 763 764 return false; 765 } 766 767 /// Returns true by value, base pointer and 768 /// offset pointer and addressing mode by reference if the node's address 769 /// can be legally represented as pre-indexed load / store address. 770 bool AVRTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 771 SDValue &Offset, 772 ISD::MemIndexedMode &AM, 773 SelectionDAG &DAG) const { 774 EVT VT; 775 const SDNode *Op; 776 SDLoc DL(N); 777 778 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 779 VT = LD->getMemoryVT(); 780 Op = LD->getBasePtr().getNode(); 781 if (LD->getExtensionType() != ISD::NON_EXTLOAD) 782 return false; 783 if (AVR::isProgramMemoryAccess(LD)) { 784 return false; 785 } 786 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 787 VT = ST->getMemoryVT(); 788 Op = ST->getBasePtr().getNode(); 789 if (AVR::isProgramMemoryAccess(ST)) { 790 return false; 791 } 792 } else { 793 return false; 794 } 795 796 if (VT != MVT::i8 && VT != MVT::i16) { 797 return false; 798 } 799 800 if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) { 801 return false; 802 } 803 804 if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) { 805 int RHSC = RHS->getSExtValue(); 806 if (Op->getOpcode() == ISD::SUB) 807 RHSC = -RHSC; 808 809 if ((VT == MVT::i16 && RHSC != -2) || (VT == MVT::i8 && RHSC != -1)) { 810 return false; 811 } 812 813 Base = Op->getOperand(0); 814 Offset = DAG.getConstant(RHSC, DL, MVT::i8); 815 AM = ISD::PRE_DEC; 816 817 return true; 818 } 819 820 return false; 821 } 822 823 /// Returns true by value, base pointer and 824 /// offset pointer and addressing mode by reference if this node can be 825 /// combined with a load / store to form a post-indexed load / store. 826 bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 827 SDValue &Base, 828 SDValue &Offset, 829 ISD::MemIndexedMode &AM, 830 SelectionDAG &DAG) const { 831 EVT VT; 832 SDLoc DL(N); 833 834 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 835 VT = LD->getMemoryVT(); 836 if (LD->getExtensionType() != ISD::NON_EXTLOAD) 837 return false; 838 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 839 VT = ST->getMemoryVT(); 840 if (AVR::isProgramMemoryAccess(ST)) { 841 return false; 842 } 843 } else { 844 return false; 845 } 846 847 if (VT != MVT::i8 && VT != MVT::i16) { 848 return false; 849 } 850 851 if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) { 852 return false; 853 } 854 855 if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) { 856 int RHSC = RHS->getSExtValue(); 857 if (Op->getOpcode() == ISD::SUB) 858 RHSC = -RHSC; 859 if ((VT == MVT::i16 && RHSC != 2) || (VT == MVT::i8 && RHSC != 1)) { 860 return false; 861 } 862 863 Base = Op->getOperand(0); 864 Offset = DAG.getConstant(RHSC, DL, MVT::i8); 865 AM = ISD::POST_INC; 866 867 return true; 868 } 869 870 return false; 871 } 872 873 bool AVRTargetLowering::isOffsetFoldingLegal( 874 const GlobalAddressSDNode *GA) const { 875 return true; 876 } 877 878 //===----------------------------------------------------------------------===// 879 // Formal Arguments Calling Convention Implementation 880 //===----------------------------------------------------------------------===// 881 882 #include "AVRGenCallingConv.inc" 883 884 /// For each argument in a function store the number of pieces it is composed 885 /// of. 886 static void parseFunctionArgs(const SmallVectorImpl<ISD::InputArg> &Ins, 887 SmallVectorImpl<unsigned> &Out) { 888 for (const ISD::InputArg &Arg : Ins) { 889 if(Arg.PartOffset > 0) continue; 890 unsigned Bytes = ((Arg.ArgVT.getSizeInBits()) + 7) / 8; 891 892 Out.push_back((Bytes + 1) / 2); 893 } 894 } 895 896 /// For external symbols there is no function prototype information so we 897 /// have to rely directly on argument sizes. 898 static void parseExternFuncCallArgs(const SmallVectorImpl<ISD::OutputArg> &In, 899 SmallVectorImpl<unsigned> &Out) { 900 for (unsigned i = 0, e = In.size(); i != e;) { 901 unsigned Size = 0; 902 unsigned Offset = 0; 903 while ((i != e) && (In[i].PartOffset == Offset)) { 904 Offset += In[i].VT.getStoreSize(); 905 ++i; 906 ++Size; 907 } 908 Out.push_back(Size); 909 } 910 } 911 912 static StringRef getFunctionName(TargetLowering::CallLoweringInfo &CLI) { 913 SDValue Callee = CLI.Callee; 914 915 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee)) { 916 return G->getSymbol(); 917 } 918 919 if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 920 return G->getGlobal()->getName(); 921 } 922 923 llvm_unreachable("don't know how to get the name for this callee"); 924 } 925 926 /// Analyze incoming and outgoing function arguments. We need custom C++ code 927 /// to handle special constraints in the ABI like reversing the order of the 928 /// pieces of splitted arguments. In addition, all pieces of a certain argument 929 /// have to be passed either using registers or the stack but never mixing both. 930 static void analyzeStandardArguments(TargetLowering::CallLoweringInfo *CLI, 931 const Function *F, const DataLayout *TD, 932 const SmallVectorImpl<ISD::OutputArg> *Outs, 933 const SmallVectorImpl<ISD::InputArg> *Ins, 934 CallingConv::ID CallConv, 935 SmallVectorImpl<CCValAssign> &ArgLocs, 936 CCState &CCInfo, bool IsCall, bool IsVarArg) { 937 static const MCPhysReg RegList8[] = {AVR::R24, AVR::R22, AVR::R20, 938 AVR::R18, AVR::R16, AVR::R14, 939 AVR::R12, AVR::R10, AVR::R8}; 940 static const MCPhysReg RegList16[] = {AVR::R25R24, AVR::R23R22, AVR::R21R20, 941 AVR::R19R18, AVR::R17R16, AVR::R15R14, 942 AVR::R13R12, AVR::R11R10, AVR::R9R8}; 943 if (IsVarArg) { 944 // Variadic functions do not need all the analysis below. 945 if (IsCall) { 946 CCInfo.AnalyzeCallOperands(*Outs, ArgCC_AVR_Vararg); 947 } else { 948 CCInfo.AnalyzeFormalArguments(*Ins, ArgCC_AVR_Vararg); 949 } 950 return; 951 } 952 953 // Fill in the Args array which will contain original argument sizes. 954 SmallVector<unsigned, 8> Args; 955 if (IsCall) { 956 parseExternFuncCallArgs(*Outs, Args); 957 } else { 958 assert(F != nullptr && "function should not be null"); 959 parseFunctionArgs(*Ins, Args); 960 } 961 962 unsigned RegsLeft = array_lengthof(RegList8), ValNo = 0; 963 // Variadic functions always use the stack. 964 bool UsesStack = false; 965 for (unsigned i = 0, pos = 0, e = Args.size(); i != e; ++i) { 966 unsigned Size = Args[i]; 967 968 // If we have a zero-sized argument, don't attempt to lower it. 969 // AVR-GCC does not support zero-sized arguments and so we need not 970 // worry about ABI compatibility. 971 if (Size == 0) continue; 972 973 MVT LocVT = (IsCall) ? (*Outs)[pos].VT : (*Ins)[pos].VT; 974 975 // If we have plenty of regs to pass the whole argument do it. 976 if (!UsesStack && (Size <= RegsLeft)) { 977 const MCPhysReg *RegList = (LocVT == MVT::i16) ? RegList16 : RegList8; 978 979 for (unsigned j = 0; j != Size; ++j) { 980 unsigned Reg = CCInfo.AllocateReg( 981 ArrayRef<MCPhysReg>(RegList, array_lengthof(RegList8))); 982 CCInfo.addLoc( 983 CCValAssign::getReg(ValNo++, LocVT, Reg, LocVT, CCValAssign::Full)); 984 --RegsLeft; 985 } 986 987 // Reverse the order of the pieces to agree with the "big endian" format 988 // required in the calling convention ABI. 989 std::reverse(ArgLocs.begin() + pos, ArgLocs.begin() + pos + Size); 990 } else { 991 // Pass the rest of arguments using the stack. 992 UsesStack = true; 993 for (unsigned j = 0; j != Size; ++j) { 994 unsigned Offset = CCInfo.AllocateStack( 995 TD->getTypeAllocSize(EVT(LocVT).getTypeForEVT(CCInfo.getContext())), 996 TD->getABITypeAlign(EVT(LocVT).getTypeForEVT(CCInfo.getContext()))); 997 CCInfo.addLoc(CCValAssign::getMem(ValNo++, LocVT, Offset, LocVT, 998 CCValAssign::Full)); 999 } 1000 } 1001 pos += Size; 1002 } 1003 } 1004 1005 static void analyzeBuiltinArguments(TargetLowering::CallLoweringInfo &CLI, 1006 const Function *F, const DataLayout *TD, 1007 const SmallVectorImpl<ISD::OutputArg> *Outs, 1008 const SmallVectorImpl<ISD::InputArg> *Ins, 1009 CallingConv::ID CallConv, 1010 SmallVectorImpl<CCValAssign> &ArgLocs, 1011 CCState &CCInfo, bool IsCall, bool IsVarArg) { 1012 StringRef FuncName = getFunctionName(CLI); 1013 1014 if (FuncName.startswith("__udivmod") || FuncName.startswith("__divmod")) { 1015 CCInfo.AnalyzeCallOperands(*Outs, ArgCC_AVR_BUILTIN_DIV); 1016 } else { 1017 analyzeStandardArguments(&CLI, F, TD, Outs, Ins, 1018 CallConv, ArgLocs, CCInfo, 1019 IsCall, IsVarArg); 1020 } 1021 } 1022 1023 static void analyzeArguments(TargetLowering::CallLoweringInfo *CLI, 1024 const Function *F, const DataLayout *TD, 1025 const SmallVectorImpl<ISD::OutputArg> *Outs, 1026 const SmallVectorImpl<ISD::InputArg> *Ins, 1027 CallingConv::ID CallConv, 1028 SmallVectorImpl<CCValAssign> &ArgLocs, 1029 CCState &CCInfo, bool IsCall, bool IsVarArg) { 1030 switch (CallConv) { 1031 case CallingConv::AVR_BUILTIN: { 1032 analyzeBuiltinArguments(*CLI, F, TD, Outs, Ins, 1033 CallConv, ArgLocs, CCInfo, 1034 IsCall, IsVarArg); 1035 return; 1036 } 1037 default: { 1038 analyzeStandardArguments(CLI, F, TD, Outs, Ins, 1039 CallConv, ArgLocs, CCInfo, 1040 IsCall, IsVarArg); 1041 return; 1042 } 1043 } 1044 } 1045 1046 SDValue AVRTargetLowering::LowerFormalArguments( 1047 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 1048 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG, 1049 SmallVectorImpl<SDValue> &InVals) const { 1050 MachineFunction &MF = DAG.getMachineFunction(); 1051 MachineFrameInfo &MFI = MF.getFrameInfo(); 1052 auto DL = DAG.getDataLayout(); 1053 1054 // Assign locations to all of the incoming arguments. 1055 SmallVector<CCValAssign, 16> ArgLocs; 1056 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1057 *DAG.getContext()); 1058 1059 analyzeArguments(nullptr, &MF.getFunction(), &DL, 0, &Ins, CallConv, ArgLocs, CCInfo, 1060 false, isVarArg); 1061 1062 SDValue ArgValue; 1063 for (CCValAssign &VA : ArgLocs) { 1064 1065 // Arguments stored on registers. 1066 if (VA.isRegLoc()) { 1067 EVT RegVT = VA.getLocVT(); 1068 const TargetRegisterClass *RC; 1069 if (RegVT == MVT::i8) { 1070 RC = &AVR::GPR8RegClass; 1071 } else if (RegVT == MVT::i16) { 1072 RC = &AVR::DREGSRegClass; 1073 } else { 1074 llvm_unreachable("Unknown argument type!"); 1075 } 1076 1077 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 1078 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 1079 1080 // :NOTE: Clang should not promote any i8 into i16 but for safety the 1081 // following code will handle zexts or sexts generated by other 1082 // front ends. Otherwise: 1083 // If this is an 8 bit value, it is really passed promoted 1084 // to 16 bits. Insert an assert[sz]ext to capture this, then 1085 // truncate to the right size. 1086 switch (VA.getLocInfo()) { 1087 default: 1088 llvm_unreachable("Unknown loc info!"); 1089 case CCValAssign::Full: 1090 break; 1091 case CCValAssign::BCvt: 1092 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 1093 break; 1094 case CCValAssign::SExt: 1095 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 1096 DAG.getValueType(VA.getValVT())); 1097 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 1098 break; 1099 case CCValAssign::ZExt: 1100 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 1101 DAG.getValueType(VA.getValVT())); 1102 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 1103 break; 1104 } 1105 1106 InVals.push_back(ArgValue); 1107 } else { 1108 // Sanity check. 1109 assert(VA.isMemLoc()); 1110 1111 EVT LocVT = VA.getLocVT(); 1112 1113 // Create the frame index object for this incoming parameter. 1114 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8, 1115 VA.getLocMemOffset(), true); 1116 1117 // Create the SelectionDAG nodes corresponding to a load 1118 // from this parameter. 1119 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DL)); 1120 InVals.push_back(DAG.getLoad(LocVT, dl, Chain, FIN, 1121 MachinePointerInfo::getFixedStack(MF, FI), 1122 0)); 1123 } 1124 } 1125 1126 // If the function takes variable number of arguments, make a frame index for 1127 // the start of the first vararg value... for expansion of llvm.va_start. 1128 if (isVarArg) { 1129 unsigned StackSize = CCInfo.getNextStackOffset(); 1130 AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>(); 1131 1132 AFI->setVarArgsFrameIndex(MFI.CreateFixedObject(2, StackSize, true)); 1133 } 1134 1135 return Chain; 1136 } 1137 1138 //===----------------------------------------------------------------------===// 1139 // Call Calling Convention Implementation 1140 //===----------------------------------------------------------------------===// 1141 1142 SDValue AVRTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1143 SmallVectorImpl<SDValue> &InVals) const { 1144 SelectionDAG &DAG = CLI.DAG; 1145 SDLoc &DL = CLI.DL; 1146 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1147 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1148 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1149 SDValue Chain = CLI.Chain; 1150 SDValue Callee = CLI.Callee; 1151 bool &isTailCall = CLI.IsTailCall; 1152 CallingConv::ID CallConv = CLI.CallConv; 1153 bool isVarArg = CLI.IsVarArg; 1154 1155 MachineFunction &MF = DAG.getMachineFunction(); 1156 1157 // AVR does not yet support tail call optimization. 1158 isTailCall = false; 1159 1160 // Analyze operands of the call, assigning locations to each operand. 1161 SmallVector<CCValAssign, 16> ArgLocs; 1162 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1163 *DAG.getContext()); 1164 1165 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1166 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1167 // node so that legalize doesn't hack it. 1168 const Function *F = nullptr; 1169 if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1170 const GlobalValue *GV = G->getGlobal(); 1171 1172 F = cast<Function>(GV); 1173 Callee = 1174 DAG.getTargetGlobalAddress(GV, DL, getPointerTy(DAG.getDataLayout())); 1175 } else if (const ExternalSymbolSDNode *ES = 1176 dyn_cast<ExternalSymbolSDNode>(Callee)) { 1177 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), 1178 getPointerTy(DAG.getDataLayout())); 1179 } 1180 1181 analyzeArguments(&CLI, F, &DAG.getDataLayout(), &Outs, 0, CallConv, ArgLocs, CCInfo, 1182 true, isVarArg); 1183 1184 // Get a count of how many bytes are to be pushed on the stack. 1185 unsigned NumBytes = CCInfo.getNextStackOffset(); 1186 1187 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL); 1188 1189 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 1190 1191 // First, walk the register assignments, inserting copies. 1192 unsigned AI, AE; 1193 bool HasStackArgs = false; 1194 for (AI = 0, AE = ArgLocs.size(); AI != AE; ++AI) { 1195 CCValAssign &VA = ArgLocs[AI]; 1196 EVT RegVT = VA.getLocVT(); 1197 SDValue Arg = OutVals[AI]; 1198 1199 // Promote the value if needed. With Clang this should not happen. 1200 switch (VA.getLocInfo()) { 1201 default: 1202 llvm_unreachable("Unknown loc info!"); 1203 case CCValAssign::Full: 1204 break; 1205 case CCValAssign::SExt: 1206 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, RegVT, Arg); 1207 break; 1208 case CCValAssign::ZExt: 1209 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, RegVT, Arg); 1210 break; 1211 case CCValAssign::AExt: 1212 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, RegVT, Arg); 1213 break; 1214 case CCValAssign::BCvt: 1215 Arg = DAG.getNode(ISD::BITCAST, DL, RegVT, Arg); 1216 break; 1217 } 1218 1219 // Stop when we encounter a stack argument, we need to process them 1220 // in reverse order in the loop below. 1221 if (VA.isMemLoc()) { 1222 HasStackArgs = true; 1223 break; 1224 } 1225 1226 // Arguments that can be passed on registers must be kept in the RegsToPass 1227 // vector. 1228 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1229 } 1230 1231 // Second, stack arguments have to walked in reverse order by inserting 1232 // chained stores, this ensures their order is not changed by the scheduler 1233 // and that the push instruction sequence generated is correct, otherwise they 1234 // can be freely intermixed. 1235 if (HasStackArgs) { 1236 for (AE = AI, AI = ArgLocs.size(); AI != AE; --AI) { 1237 unsigned Loc = AI - 1; 1238 CCValAssign &VA = ArgLocs[Loc]; 1239 SDValue Arg = OutVals[Loc]; 1240 1241 assert(VA.isMemLoc()); 1242 1243 // SP points to one stack slot further so add one to adjust it. 1244 SDValue PtrOff = DAG.getNode( 1245 ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), 1246 DAG.getRegister(AVR::SP, getPointerTy(DAG.getDataLayout())), 1247 DAG.getIntPtrConstant(VA.getLocMemOffset() + 1, DL)); 1248 1249 Chain = 1250 DAG.getStore(Chain, DL, Arg, PtrOff, 1251 MachinePointerInfo::getStack(MF, VA.getLocMemOffset()), 1252 0); 1253 } 1254 } 1255 1256 // Build a sequence of copy-to-reg nodes chained together with token chain and 1257 // flag operands which copy the outgoing args into registers. The InFlag in 1258 // necessary since all emited instructions must be stuck together. 1259 SDValue InFlag; 1260 for (auto Reg : RegsToPass) { 1261 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, InFlag); 1262 InFlag = Chain.getValue(1); 1263 } 1264 1265 // Returns a chain & a flag for retval copy to use. 1266 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1267 SmallVector<SDValue, 8> Ops; 1268 Ops.push_back(Chain); 1269 Ops.push_back(Callee); 1270 1271 // Add argument registers to the end of the list so that they are known live 1272 // into the call. 1273 for (auto Reg : RegsToPass) { 1274 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType())); 1275 } 1276 1277 // Add a register mask operand representing the call-preserved registers. 1278 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 1279 const uint32_t *Mask = 1280 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv); 1281 assert(Mask && "Missing call preserved mask for calling convention"); 1282 Ops.push_back(DAG.getRegisterMask(Mask)); 1283 1284 if (InFlag.getNode()) { 1285 Ops.push_back(InFlag); 1286 } 1287 1288 Chain = DAG.getNode(AVRISD::CALL, DL, NodeTys, Ops); 1289 InFlag = Chain.getValue(1); 1290 1291 // Create the CALLSEQ_END node. 1292 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true), 1293 DAG.getIntPtrConstant(0, DL, true), InFlag, DL); 1294 1295 if (!Ins.empty()) { 1296 InFlag = Chain.getValue(1); 1297 } 1298 1299 // Handle result values, copying them out of physregs into vregs that we 1300 // return. 1301 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, DL, DAG, 1302 InVals); 1303 } 1304 1305 /// Lower the result values of a call into the 1306 /// appropriate copies out of appropriate physical registers. 1307 /// 1308 SDValue AVRTargetLowering::LowerCallResult( 1309 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 1310 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG, 1311 SmallVectorImpl<SDValue> &InVals) const { 1312 1313 // Assign locations to each value returned by this call. 1314 SmallVector<CCValAssign, 16> RVLocs; 1315 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1316 *DAG.getContext()); 1317 1318 // Handle runtime calling convs. 1319 auto CCFunction = CCAssignFnForReturn(CallConv); 1320 CCInfo.AnalyzeCallResult(Ins, CCFunction); 1321 1322 if (CallConv != CallingConv::AVR_BUILTIN && RVLocs.size() > 1) { 1323 // Reverse splitted return values to get the "big endian" format required 1324 // to agree with the calling convention ABI. 1325 std::reverse(RVLocs.begin(), RVLocs.end()); 1326 } 1327 1328 // Copy all of the result registers out of their specified physreg. 1329 for (CCValAssign const &RVLoc : RVLocs) { 1330 Chain = DAG.getCopyFromReg(Chain, dl, RVLoc.getLocReg(), RVLoc.getValVT(), 1331 InFlag) 1332 .getValue(1); 1333 InFlag = Chain.getValue(2); 1334 InVals.push_back(Chain.getValue(0)); 1335 } 1336 1337 return Chain; 1338 } 1339 1340 //===----------------------------------------------------------------------===// 1341 // Return Value Calling Convention Implementation 1342 //===----------------------------------------------------------------------===// 1343 1344 CCAssignFn *AVRTargetLowering::CCAssignFnForReturn(CallingConv::ID CC) const { 1345 switch (CC) { 1346 case CallingConv::AVR_BUILTIN: 1347 return RetCC_AVR_BUILTIN; 1348 default: 1349 return RetCC_AVR; 1350 } 1351 } 1352 1353 bool 1354 AVRTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 1355 MachineFunction &MF, bool isVarArg, 1356 const SmallVectorImpl<ISD::OutputArg> &Outs, 1357 LLVMContext &Context) const 1358 { 1359 SmallVector<CCValAssign, 16> RVLocs; 1360 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 1361 1362 auto CCFunction = CCAssignFnForReturn(CallConv); 1363 return CCInfo.CheckReturn(Outs, CCFunction); 1364 } 1365 1366 SDValue 1367 AVRTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 1368 bool isVarArg, 1369 const SmallVectorImpl<ISD::OutputArg> &Outs, 1370 const SmallVectorImpl<SDValue> &OutVals, 1371 const SDLoc &dl, SelectionDAG &DAG) const { 1372 // CCValAssign - represent the assignment of the return value to locations. 1373 SmallVector<CCValAssign, 16> RVLocs; 1374 1375 // CCState - Info about the registers and stack slot. 1376 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1377 *DAG.getContext()); 1378 1379 // Analyze return values. 1380 auto CCFunction = CCAssignFnForReturn(CallConv); 1381 CCInfo.AnalyzeReturn(Outs, CCFunction); 1382 1383 // If this is the first return lowered for this function, add the regs to 1384 // the liveout set for the function. 1385 MachineFunction &MF = DAG.getMachineFunction(); 1386 unsigned e = RVLocs.size(); 1387 1388 // Reverse splitted return values to get the "big endian" format required 1389 // to agree with the calling convention ABI. 1390 if (e > 1) { 1391 std::reverse(RVLocs.begin(), RVLocs.end()); 1392 } 1393 1394 SDValue Flag; 1395 SmallVector<SDValue, 4> RetOps(1, Chain); 1396 // Copy the result values into the output registers. 1397 for (unsigned i = 0; i != e; ++i) { 1398 CCValAssign &VA = RVLocs[i]; 1399 assert(VA.isRegLoc() && "Can only return in registers!"); 1400 1401 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag); 1402 1403 // Guarantee that all emitted copies are stuck together with flags. 1404 Flag = Chain.getValue(1); 1405 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1406 } 1407 1408 // Don't emit the ret/reti instruction when the naked attribute is present in 1409 // the function being compiled. 1410 if (MF.getFunction().getAttributes().hasAttribute( 1411 AttributeList::FunctionIndex, Attribute::Naked)) { 1412 return Chain; 1413 } 1414 1415 const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>(); 1416 1417 unsigned RetOpc = 1418 AFI->isInterruptOrSignalHandler() 1419 ? AVRISD::RETI_FLAG 1420 : AVRISD::RET_FLAG; 1421 1422 RetOps[0] = Chain; // Update chain. 1423 1424 if (Flag.getNode()) { 1425 RetOps.push_back(Flag); 1426 } 1427 1428 return DAG.getNode(RetOpc, dl, MVT::Other, RetOps); 1429 } 1430 1431 //===----------------------------------------------------------------------===// 1432 // Custom Inserters 1433 //===----------------------------------------------------------------------===// 1434 1435 MachineBasicBlock *AVRTargetLowering::insertShift(MachineInstr &MI, 1436 MachineBasicBlock *BB) const { 1437 unsigned Opc; 1438 const TargetRegisterClass *RC; 1439 bool HasRepeatedOperand = false; 1440 MachineFunction *F = BB->getParent(); 1441 MachineRegisterInfo &RI = F->getRegInfo(); 1442 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1443 DebugLoc dl = MI.getDebugLoc(); 1444 1445 switch (MI.getOpcode()) { 1446 default: 1447 llvm_unreachable("Invalid shift opcode!"); 1448 case AVR::Lsl8: 1449 Opc = AVR::ADDRdRr; // LSL is an alias of ADD Rd, Rd 1450 RC = &AVR::GPR8RegClass; 1451 HasRepeatedOperand = true; 1452 break; 1453 case AVR::Lsl16: 1454 Opc = AVR::LSLWRd; 1455 RC = &AVR::DREGSRegClass; 1456 break; 1457 case AVR::Asr8: 1458 Opc = AVR::ASRRd; 1459 RC = &AVR::GPR8RegClass; 1460 break; 1461 case AVR::Asr16: 1462 Opc = AVR::ASRWRd; 1463 RC = &AVR::DREGSRegClass; 1464 break; 1465 case AVR::Lsr8: 1466 Opc = AVR::LSRRd; 1467 RC = &AVR::GPR8RegClass; 1468 break; 1469 case AVR::Lsr16: 1470 Opc = AVR::LSRWRd; 1471 RC = &AVR::DREGSRegClass; 1472 break; 1473 case AVR::Rol8: 1474 Opc = AVR::ROLBRd; 1475 RC = &AVR::GPR8RegClass; 1476 break; 1477 case AVR::Rol16: 1478 Opc = AVR::ROLWRd; 1479 RC = &AVR::DREGSRegClass; 1480 break; 1481 case AVR::Ror8: 1482 Opc = AVR::RORBRd; 1483 RC = &AVR::GPR8RegClass; 1484 break; 1485 case AVR::Ror16: 1486 Opc = AVR::RORWRd; 1487 RC = &AVR::DREGSRegClass; 1488 break; 1489 } 1490 1491 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1492 1493 MachineFunction::iterator I; 1494 for (I = BB->getIterator(); I != F->end() && &(*I) != BB; ++I); 1495 if (I != F->end()) ++I; 1496 1497 // Create loop block. 1498 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB); 1499 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB); 1500 1501 F->insert(I, LoopBB); 1502 F->insert(I, RemBB); 1503 1504 // Update machine-CFG edges by transferring all successors of the current 1505 // block to the block containing instructions after shift. 1506 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)), 1507 BB->end()); 1508 RemBB->transferSuccessorsAndUpdatePHIs(BB); 1509 1510 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB. 1511 BB->addSuccessor(LoopBB); 1512 BB->addSuccessor(RemBB); 1513 LoopBB->addSuccessor(RemBB); 1514 LoopBB->addSuccessor(LoopBB); 1515 1516 Register ShiftAmtReg = RI.createVirtualRegister(&AVR::LD8RegClass); 1517 Register ShiftAmtReg2 = RI.createVirtualRegister(&AVR::LD8RegClass); 1518 Register ShiftReg = RI.createVirtualRegister(RC); 1519 Register ShiftReg2 = RI.createVirtualRegister(RC); 1520 Register ShiftAmtSrcReg = MI.getOperand(2).getReg(); 1521 Register SrcReg = MI.getOperand(1).getReg(); 1522 Register DstReg = MI.getOperand(0).getReg(); 1523 1524 // BB: 1525 // cpi N, 0 1526 // breq RemBB 1527 BuildMI(BB, dl, TII.get(AVR::CPIRdK)).addReg(ShiftAmtSrcReg).addImm(0); 1528 BuildMI(BB, dl, TII.get(AVR::BREQk)).addMBB(RemBB); 1529 1530 // LoopBB: 1531 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB] 1532 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB] 1533 // ShiftReg2 = shift ShiftReg 1534 // ShiftAmt2 = ShiftAmt - 1; 1535 BuildMI(LoopBB, dl, TII.get(AVR::PHI), ShiftReg) 1536 .addReg(SrcReg) 1537 .addMBB(BB) 1538 .addReg(ShiftReg2) 1539 .addMBB(LoopBB); 1540 BuildMI(LoopBB, dl, TII.get(AVR::PHI), ShiftAmtReg) 1541 .addReg(ShiftAmtSrcReg) 1542 .addMBB(BB) 1543 .addReg(ShiftAmtReg2) 1544 .addMBB(LoopBB); 1545 1546 auto ShiftMI = BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2).addReg(ShiftReg); 1547 if (HasRepeatedOperand) 1548 ShiftMI.addReg(ShiftReg); 1549 1550 BuildMI(LoopBB, dl, TII.get(AVR::SUBIRdK), ShiftAmtReg2) 1551 .addReg(ShiftAmtReg) 1552 .addImm(1); 1553 BuildMI(LoopBB, dl, TII.get(AVR::BRNEk)).addMBB(LoopBB); 1554 1555 // RemBB: 1556 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB] 1557 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(AVR::PHI), DstReg) 1558 .addReg(SrcReg) 1559 .addMBB(BB) 1560 .addReg(ShiftReg2) 1561 .addMBB(LoopBB); 1562 1563 MI.eraseFromParent(); // The pseudo instruction is gone now. 1564 return RemBB; 1565 } 1566 1567 static bool isCopyMulResult(MachineBasicBlock::iterator const &I) { 1568 if (I->getOpcode() == AVR::COPY) { 1569 Register SrcReg = I->getOperand(1).getReg(); 1570 return (SrcReg == AVR::R0 || SrcReg == AVR::R1); 1571 } 1572 1573 return false; 1574 } 1575 1576 // The mul instructions wreak havock on our zero_reg R1. We need to clear it 1577 // after the result has been evacuated. This is probably not the best way to do 1578 // it, but it works for now. 1579 MachineBasicBlock *AVRTargetLowering::insertMul(MachineInstr &MI, 1580 MachineBasicBlock *BB) const { 1581 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1582 MachineBasicBlock::iterator I(MI); 1583 ++I; // in any case insert *after* the mul instruction 1584 if (isCopyMulResult(I)) 1585 ++I; 1586 if (isCopyMulResult(I)) 1587 ++I; 1588 BuildMI(*BB, I, MI.getDebugLoc(), TII.get(AVR::EORRdRr), AVR::R1) 1589 .addReg(AVR::R1) 1590 .addReg(AVR::R1); 1591 return BB; 1592 } 1593 1594 MachineBasicBlock * 1595 AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 1596 MachineBasicBlock *MBB) const { 1597 int Opc = MI.getOpcode(); 1598 1599 // Pseudo shift instructions with a non constant shift amount are expanded 1600 // into a loop. 1601 switch (Opc) { 1602 case AVR::Lsl8: 1603 case AVR::Lsl16: 1604 case AVR::Lsr8: 1605 case AVR::Lsr16: 1606 case AVR::Rol8: 1607 case AVR::Rol16: 1608 case AVR::Ror8: 1609 case AVR::Ror16: 1610 case AVR::Asr8: 1611 case AVR::Asr16: 1612 return insertShift(MI, MBB); 1613 case AVR::MULRdRr: 1614 case AVR::MULSRdRr: 1615 return insertMul(MI, MBB); 1616 } 1617 1618 assert((Opc == AVR::Select16 || Opc == AVR::Select8) && 1619 "Unexpected instr type to insert"); 1620 1621 const AVRInstrInfo &TII = (const AVRInstrInfo &)*MI.getParent() 1622 ->getParent() 1623 ->getSubtarget() 1624 .getInstrInfo(); 1625 DebugLoc dl = MI.getDebugLoc(); 1626 1627 // To "insert" a SELECT instruction, we insert the diamond 1628 // control-flow pattern. The incoming instruction knows the 1629 // destination vreg to set, the condition code register to branch 1630 // on, the true/false values to select between, and a branch opcode 1631 // to use. 1632 1633 MachineFunction *MF = MBB->getParent(); 1634 const BasicBlock *LLVM_BB = MBB->getBasicBlock(); 1635 MachineBasicBlock *FallThrough = MBB->getFallThrough(); 1636 1637 // If the current basic block falls through to another basic block, 1638 // we must insert an unconditional branch to the fallthrough destination 1639 // if we are to insert basic blocks at the prior fallthrough point. 1640 if (FallThrough != nullptr) { 1641 BuildMI(MBB, dl, TII.get(AVR::RJMPk)).addMBB(FallThrough); 1642 } 1643 1644 MachineBasicBlock *trueMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1645 MachineBasicBlock *falseMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1646 1647 MachineFunction::iterator I; 1648 for (I = MF->begin(); I != MF->end() && &(*I) != MBB; ++I); 1649 if (I != MF->end()) ++I; 1650 MF->insert(I, trueMBB); 1651 MF->insert(I, falseMBB); 1652 1653 // Transfer remaining instructions and all successors of the current 1654 // block to the block which will contain the Phi node for the 1655 // select. 1656 trueMBB->splice(trueMBB->begin(), MBB, 1657 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 1658 trueMBB->transferSuccessorsAndUpdatePHIs(MBB); 1659 1660 AVRCC::CondCodes CC = (AVRCC::CondCodes)MI.getOperand(3).getImm(); 1661 BuildMI(MBB, dl, TII.getBrCond(CC)).addMBB(trueMBB); 1662 BuildMI(MBB, dl, TII.get(AVR::RJMPk)).addMBB(falseMBB); 1663 MBB->addSuccessor(falseMBB); 1664 MBB->addSuccessor(trueMBB); 1665 1666 // Unconditionally flow back to the true block 1667 BuildMI(falseMBB, dl, TII.get(AVR::RJMPk)).addMBB(trueMBB); 1668 falseMBB->addSuccessor(trueMBB); 1669 1670 // Set up the Phi node to determine where we came from 1671 BuildMI(*trueMBB, trueMBB->begin(), dl, TII.get(AVR::PHI), MI.getOperand(0).getReg()) 1672 .addReg(MI.getOperand(1).getReg()) 1673 .addMBB(MBB) 1674 .addReg(MI.getOperand(2).getReg()) 1675 .addMBB(falseMBB) ; 1676 1677 MI.eraseFromParent(); // The pseudo instruction is gone now. 1678 return trueMBB; 1679 } 1680 1681 //===----------------------------------------------------------------------===// 1682 // Inline Asm Support 1683 //===----------------------------------------------------------------------===// 1684 1685 AVRTargetLowering::ConstraintType 1686 AVRTargetLowering::getConstraintType(StringRef Constraint) const { 1687 if (Constraint.size() == 1) { 1688 // See http://www.nongnu.org/avr-libc/user-manual/inline_asm.html 1689 switch (Constraint[0]) { 1690 default: 1691 break; 1692 case 'a': // Simple upper registers 1693 case 'b': // Base pointer registers pairs 1694 case 'd': // Upper register 1695 case 'l': // Lower registers 1696 case 'e': // Pointer register pairs 1697 case 'q': // Stack pointer register 1698 case 'r': // Any register 1699 case 'w': // Special upper register pairs 1700 return C_RegisterClass; 1701 case 't': // Temporary register 1702 case 'x': case 'X': // Pointer register pair X 1703 case 'y': case 'Y': // Pointer register pair Y 1704 case 'z': case 'Z': // Pointer register pair Z 1705 return C_Register; 1706 case 'Q': // A memory address based on Y or Z pointer with displacement. 1707 return C_Memory; 1708 case 'G': // Floating point constant 1709 case 'I': // 6-bit positive integer constant 1710 case 'J': // 6-bit negative integer constant 1711 case 'K': // Integer constant (Range: 2) 1712 case 'L': // Integer constant (Range: 0) 1713 case 'M': // 8-bit integer constant 1714 case 'N': // Integer constant (Range: -1) 1715 case 'O': // Integer constant (Range: 8, 16, 24) 1716 case 'P': // Integer constant (Range: 1) 1717 case 'R': // Integer constant (Range: -6 to 5)x 1718 return C_Immediate; 1719 } 1720 } 1721 1722 return TargetLowering::getConstraintType(Constraint); 1723 } 1724 1725 unsigned 1726 AVRTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const { 1727 // Not sure if this is actually the right thing to do, but we got to do 1728 // *something* [agnat] 1729 switch (ConstraintCode[0]) { 1730 case 'Q': 1731 return InlineAsm::Constraint_Q; 1732 } 1733 return TargetLowering::getInlineAsmMemConstraint(ConstraintCode); 1734 } 1735 1736 AVRTargetLowering::ConstraintWeight 1737 AVRTargetLowering::getSingleConstraintMatchWeight( 1738 AsmOperandInfo &info, const char *constraint) const { 1739 ConstraintWeight weight = CW_Invalid; 1740 Value *CallOperandVal = info.CallOperandVal; 1741 1742 // If we don't have a value, we can't do a match, 1743 // but allow it at the lowest weight. 1744 // (this behaviour has been copied from the ARM backend) 1745 if (!CallOperandVal) { 1746 return CW_Default; 1747 } 1748 1749 // Look at the constraint type. 1750 switch (*constraint) { 1751 default: 1752 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 1753 break; 1754 case 'd': 1755 case 'r': 1756 case 'l': 1757 weight = CW_Register; 1758 break; 1759 case 'a': 1760 case 'b': 1761 case 'e': 1762 case 'q': 1763 case 't': 1764 case 'w': 1765 case 'x': case 'X': 1766 case 'y': case 'Y': 1767 case 'z': case 'Z': 1768 weight = CW_SpecificReg; 1769 break; 1770 case 'G': 1771 if (const ConstantFP *C = dyn_cast<ConstantFP>(CallOperandVal)) { 1772 if (C->isZero()) { 1773 weight = CW_Constant; 1774 } 1775 } 1776 break; 1777 case 'I': 1778 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1779 if (isUInt<6>(C->getZExtValue())) { 1780 weight = CW_Constant; 1781 } 1782 } 1783 break; 1784 case 'J': 1785 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1786 if ((C->getSExtValue() >= -63) && (C->getSExtValue() <= 0)) { 1787 weight = CW_Constant; 1788 } 1789 } 1790 break; 1791 case 'K': 1792 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1793 if (C->getZExtValue() == 2) { 1794 weight = CW_Constant; 1795 } 1796 } 1797 break; 1798 case 'L': 1799 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1800 if (C->getZExtValue() == 0) { 1801 weight = CW_Constant; 1802 } 1803 } 1804 break; 1805 case 'M': 1806 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1807 if (isUInt<8>(C->getZExtValue())) { 1808 weight = CW_Constant; 1809 } 1810 } 1811 break; 1812 case 'N': 1813 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1814 if (C->getSExtValue() == -1) { 1815 weight = CW_Constant; 1816 } 1817 } 1818 break; 1819 case 'O': 1820 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1821 if ((C->getZExtValue() == 8) || (C->getZExtValue() == 16) || 1822 (C->getZExtValue() == 24)) { 1823 weight = CW_Constant; 1824 } 1825 } 1826 break; 1827 case 'P': 1828 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1829 if (C->getZExtValue() == 1) { 1830 weight = CW_Constant; 1831 } 1832 } 1833 break; 1834 case 'R': 1835 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { 1836 if ((C->getSExtValue() >= -6) && (C->getSExtValue() <= 5)) { 1837 weight = CW_Constant; 1838 } 1839 } 1840 break; 1841 case 'Q': 1842 weight = CW_Memory; 1843 break; 1844 } 1845 1846 return weight; 1847 } 1848 1849 std::pair<unsigned, const TargetRegisterClass *> 1850 AVRTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 1851 StringRef Constraint, 1852 MVT VT) const { 1853 // We only support i8 and i16. 1854 // 1855 //:FIXME: remove this assert for now since it gets sometimes executed 1856 // assert((VT == MVT::i16 || VT == MVT::i8) && "Wrong operand type."); 1857 1858 if (Constraint.size() == 1) { 1859 switch (Constraint[0]) { 1860 case 'a': // Simple upper registers r16..r23. 1861 return std::make_pair(0U, &AVR::LD8loRegClass); 1862 case 'b': // Base pointer registers: y, z. 1863 return std::make_pair(0U, &AVR::PTRDISPREGSRegClass); 1864 case 'd': // Upper registers r16..r31. 1865 return std::make_pair(0U, &AVR::LD8RegClass); 1866 case 'l': // Lower registers r0..r15. 1867 return std::make_pair(0U, &AVR::GPR8loRegClass); 1868 case 'e': // Pointer register pairs: x, y, z. 1869 return std::make_pair(0U, &AVR::PTRREGSRegClass); 1870 case 'q': // Stack pointer register: SPH:SPL. 1871 return std::make_pair(0U, &AVR::GPRSPRegClass); 1872 case 'r': // Any register: r0..r31. 1873 if (VT == MVT::i8) 1874 return std::make_pair(0U, &AVR::GPR8RegClass); 1875 1876 assert(VT == MVT::i16 && "inline asm constraint too large"); 1877 return std::make_pair(0U, &AVR::DREGSRegClass); 1878 case 't': // Temporary register: r0. 1879 return std::make_pair(unsigned(AVR::R0), &AVR::GPR8RegClass); 1880 case 'w': // Special upper register pairs: r24, r26, r28, r30. 1881 return std::make_pair(0U, &AVR::IWREGSRegClass); 1882 case 'x': // Pointer register pair X: r27:r26. 1883 case 'X': 1884 return std::make_pair(unsigned(AVR::R27R26), &AVR::PTRREGSRegClass); 1885 case 'y': // Pointer register pair Y: r29:r28. 1886 case 'Y': 1887 return std::make_pair(unsigned(AVR::R29R28), &AVR::PTRREGSRegClass); 1888 case 'z': // Pointer register pair Z: r31:r30. 1889 case 'Z': 1890 return std::make_pair(unsigned(AVR::R31R30), &AVR::PTRREGSRegClass); 1891 default: 1892 break; 1893 } 1894 } 1895 1896 return TargetLowering::getRegForInlineAsmConstraint( 1897 Subtarget.getRegisterInfo(), Constraint, VT); 1898 } 1899 1900 void AVRTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 1901 std::string &Constraint, 1902 std::vector<SDValue> &Ops, 1903 SelectionDAG &DAG) const { 1904 SDValue Result(0, 0); 1905 SDLoc DL(Op); 1906 EVT Ty = Op.getValueType(); 1907 1908 // Currently only support length 1 constraints. 1909 if (Constraint.length() != 1) { 1910 return; 1911 } 1912 1913 char ConstraintLetter = Constraint[0]; 1914 switch (ConstraintLetter) { 1915 default: 1916 break; 1917 // Deal with integers first: 1918 case 'I': 1919 case 'J': 1920 case 'K': 1921 case 'L': 1922 case 'M': 1923 case 'N': 1924 case 'O': 1925 case 'P': 1926 case 'R': { 1927 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 1928 if (!C) { 1929 return; 1930 } 1931 1932 int64_t CVal64 = C->getSExtValue(); 1933 uint64_t CUVal64 = C->getZExtValue(); 1934 switch (ConstraintLetter) { 1935 case 'I': // 0..63 1936 if (!isUInt<6>(CUVal64)) 1937 return; 1938 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1939 break; 1940 case 'J': // -63..0 1941 if (CVal64 < -63 || CVal64 > 0) 1942 return; 1943 Result = DAG.getTargetConstant(CVal64, DL, Ty); 1944 break; 1945 case 'K': // 2 1946 if (CUVal64 != 2) 1947 return; 1948 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1949 break; 1950 case 'L': // 0 1951 if (CUVal64 != 0) 1952 return; 1953 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1954 break; 1955 case 'M': // 0..255 1956 if (!isUInt<8>(CUVal64)) 1957 return; 1958 // i8 type may be printed as a negative number, 1959 // e.g. 254 would be printed as -2, 1960 // so we force it to i16 at least. 1961 if (Ty.getSimpleVT() == MVT::i8) { 1962 Ty = MVT::i16; 1963 } 1964 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1965 break; 1966 case 'N': // -1 1967 if (CVal64 != -1) 1968 return; 1969 Result = DAG.getTargetConstant(CVal64, DL, Ty); 1970 break; 1971 case 'O': // 8, 16, 24 1972 if (CUVal64 != 8 && CUVal64 != 16 && CUVal64 != 24) 1973 return; 1974 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1975 break; 1976 case 'P': // 1 1977 if (CUVal64 != 1) 1978 return; 1979 Result = DAG.getTargetConstant(CUVal64, DL, Ty); 1980 break; 1981 case 'R': // -6..5 1982 if (CVal64 < -6 || CVal64 > 5) 1983 return; 1984 Result = DAG.getTargetConstant(CVal64, DL, Ty); 1985 break; 1986 } 1987 1988 break; 1989 } 1990 case 'G': 1991 const ConstantFPSDNode *FC = dyn_cast<ConstantFPSDNode>(Op); 1992 if (!FC || !FC->isZero()) 1993 return; 1994 // Soften float to i8 0 1995 Result = DAG.getTargetConstant(0, DL, MVT::i8); 1996 break; 1997 } 1998 1999 if (Result.getNode()) { 2000 Ops.push_back(Result); 2001 return; 2002 } 2003 2004 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 2005 } 2006 2007 Register AVRTargetLowering::getRegisterByName(const char *RegName, LLT VT, 2008 const MachineFunction &MF) const { 2009 Register Reg; 2010 2011 if (VT == LLT::scalar(8)) { 2012 Reg = StringSwitch<unsigned>(RegName) 2013 .Case("r0", AVR::R0).Case("r1", AVR::R1).Case("r2", AVR::R2) 2014 .Case("r3", AVR::R3).Case("r4", AVR::R4).Case("r5", AVR::R5) 2015 .Case("r6", AVR::R6).Case("r7", AVR::R7).Case("r8", AVR::R8) 2016 .Case("r9", AVR::R9).Case("r10", AVR::R10).Case("r11", AVR::R11) 2017 .Case("r12", AVR::R12).Case("r13", AVR::R13).Case("r14", AVR::R14) 2018 .Case("r15", AVR::R15).Case("r16", AVR::R16).Case("r17", AVR::R17) 2019 .Case("r18", AVR::R18).Case("r19", AVR::R19).Case("r20", AVR::R20) 2020 .Case("r21", AVR::R21).Case("r22", AVR::R22).Case("r23", AVR::R23) 2021 .Case("r24", AVR::R24).Case("r25", AVR::R25).Case("r26", AVR::R26) 2022 .Case("r27", AVR::R27).Case("r28", AVR::R28).Case("r29", AVR::R29) 2023 .Case("r30", AVR::R30).Case("r31", AVR::R31) 2024 .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30) 2025 .Default(0); 2026 } else { 2027 Reg = StringSwitch<unsigned>(RegName) 2028 .Case("r0", AVR::R1R0).Case("r2", AVR::R3R2) 2029 .Case("r4", AVR::R5R4).Case("r6", AVR::R7R6) 2030 .Case("r8", AVR::R9R8).Case("r10", AVR::R11R10) 2031 .Case("r12", AVR::R13R12).Case("r14", AVR::R15R14) 2032 .Case("r16", AVR::R17R16).Case("r18", AVR::R19R18) 2033 .Case("r20", AVR::R21R20).Case("r22", AVR::R23R22) 2034 .Case("r24", AVR::R25R24).Case("r26", AVR::R27R26) 2035 .Case("r28", AVR::R29R28).Case("r30", AVR::R31R30) 2036 .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30) 2037 .Default(0); 2038 } 2039 2040 if (Reg) 2041 return Reg; 2042 2043 report_fatal_error("Invalid register name global variable"); 2044 } 2045 2046 } // end of namespace llvm 2047