1 //===-- R600ISelLowering.cpp - R600 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 /// \file 10 /// Custom DAG lowering for R600 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "R600ISelLowering.h" 15 #include "AMDGPUFrameLowering.h" 16 #include "AMDGPUSubtarget.h" 17 #include "R600Defines.h" 18 #include "R600FrameLowering.h" 19 #include "R600InstrInfo.h" 20 #include "R600MachineFunctionInfo.h" 21 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 22 #include "Utils/AMDGPUBaseInfo.h" 23 #include "llvm/ADT/APFloat.h" 24 #include "llvm/ADT/APInt.h" 25 #include "llvm/ADT/ArrayRef.h" 26 #include "llvm/ADT/DenseMap.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/CodeGen/CallingConvLower.h" 29 #include "llvm/CodeGen/DAGCombine.h" 30 #include "llvm/CodeGen/ISDOpcodes.h" 31 #include "llvm/CodeGen/MachineBasicBlock.h" 32 #include "llvm/CodeGen/MachineFunction.h" 33 #include "llvm/CodeGen/MachineInstr.h" 34 #include "llvm/CodeGen/MachineInstrBuilder.h" 35 #include "llvm/CodeGen/MachineMemOperand.h" 36 #include "llvm/CodeGen/MachineRegisterInfo.h" 37 #include "llvm/CodeGen/SelectionDAG.h" 38 #include "llvm/IR/Constants.h" 39 #include "llvm/IR/DerivedTypes.h" 40 #include "llvm/IR/IntrinsicsR600.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/Compiler.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/MachineValueType.h" 45 #include "llvm/Support/MathExtras.h" 46 #include <cassert> 47 #include <cstdint> 48 #include <iterator> 49 #include <utility> 50 #include <vector> 51 52 using namespace llvm; 53 54 #include "R600GenCallingConv.inc" 55 56 R600TargetLowering::R600TargetLowering(const TargetMachine &TM, 57 const R600Subtarget &STI) 58 : AMDGPUTargetLowering(TM, STI), Subtarget(&STI), Gen(STI.getGeneration()) { 59 addRegisterClass(MVT::f32, &R600::R600_Reg32RegClass); 60 addRegisterClass(MVT::i32, &R600::R600_Reg32RegClass); 61 addRegisterClass(MVT::v2f32, &R600::R600_Reg64RegClass); 62 addRegisterClass(MVT::v2i32, &R600::R600_Reg64RegClass); 63 addRegisterClass(MVT::v4f32, &R600::R600_Reg128RegClass); 64 addRegisterClass(MVT::v4i32, &R600::R600_Reg128RegClass); 65 66 setBooleanContents(ZeroOrNegativeOneBooleanContent); 67 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 68 69 computeRegisterProperties(Subtarget->getRegisterInfo()); 70 71 // Legalize loads and stores to the private address space. 72 setOperationAction(ISD::LOAD, MVT::i32, Custom); 73 setOperationAction(ISD::LOAD, MVT::v2i32, Custom); 74 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 75 76 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address 77 // spaces, so it is custom lowered to handle those where it isn't. 78 for (MVT VT : MVT::integer_valuetypes()) { 79 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 80 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom); 81 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom); 82 83 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 84 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom); 85 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom); 86 87 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 88 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom); 89 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom); 90 } 91 92 // Workaround for LegalizeDAG asserting on expansion of i1 vector loads. 93 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, MVT::v2i1, Expand); 94 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, MVT::v2i1, Expand); 95 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i1, Expand); 96 97 setLoadExtAction(ISD::EXTLOAD, MVT::v4i32, MVT::v4i1, Expand); 98 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i1, Expand); 99 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i1, Expand); 100 101 setOperationAction(ISD::STORE, MVT::i8, Custom); 102 setOperationAction(ISD::STORE, MVT::i32, Custom); 103 setOperationAction(ISD::STORE, MVT::v2i32, Custom); 104 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 105 106 setTruncStoreAction(MVT::i32, MVT::i8, Custom); 107 setTruncStoreAction(MVT::i32, MVT::i16, Custom); 108 // We need to include these since trunc STORES to PRIVATE need 109 // special handling to accommodate RMW 110 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom); 111 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Custom); 112 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Custom); 113 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Custom); 114 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Custom); 115 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom); 116 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom); 117 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Custom); 118 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Custom); 119 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Custom); 120 121 // Workaround for LegalizeDAG asserting on expansion of i1 vector stores. 122 setTruncStoreAction(MVT::v2i32, MVT::v2i1, Expand); 123 setTruncStoreAction(MVT::v4i32, MVT::v4i1, Expand); 124 125 // Set condition code actions 126 setCondCodeAction(ISD::SETO, MVT::f32, Expand); 127 setCondCodeAction(ISD::SETUO, MVT::f32, Expand); 128 setCondCodeAction(ISD::SETLT, MVT::f32, Expand); 129 setCondCodeAction(ISD::SETLE, MVT::f32, Expand); 130 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand); 131 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); 132 setCondCodeAction(ISD::SETONE, MVT::f32, Expand); 133 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); 134 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand); 135 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); 136 setCondCodeAction(ISD::SETULT, MVT::f32, Expand); 137 setCondCodeAction(ISD::SETULE, MVT::f32, Expand); 138 139 setCondCodeAction(ISD::SETLE, MVT::i32, Expand); 140 setCondCodeAction(ISD::SETLT, MVT::i32, Expand); 141 setCondCodeAction(ISD::SETULE, MVT::i32, Expand); 142 setCondCodeAction(ISD::SETULT, MVT::i32, Expand); 143 144 setOperationAction(ISD::FCOS, MVT::f32, Custom); 145 setOperationAction(ISD::FSIN, MVT::f32, Custom); 146 147 setOperationAction(ISD::SETCC, MVT::v4i32, Expand); 148 setOperationAction(ISD::SETCC, MVT::v2i32, Expand); 149 150 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 151 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 152 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 153 154 setOperationAction(ISD::FSUB, MVT::f32, Expand); 155 156 setOperationAction(ISD::FCEIL, MVT::f64, Custom); 157 setOperationAction(ISD::FTRUNC, MVT::f64, Custom); 158 setOperationAction(ISD::FRINT, MVT::f64, Custom); 159 setOperationAction(ISD::FFLOOR, MVT::f64, Custom); 160 161 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 162 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 163 164 setOperationAction(ISD::SETCC, MVT::i32, Expand); 165 setOperationAction(ISD::SETCC, MVT::f32, Expand); 166 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom); 167 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Custom); 168 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 169 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 170 171 setOperationAction(ISD::SELECT, MVT::i32, Expand); 172 setOperationAction(ISD::SELECT, MVT::f32, Expand); 173 setOperationAction(ISD::SELECT, MVT::v2i32, Expand); 174 setOperationAction(ISD::SELECT, MVT::v4i32, Expand); 175 176 // ADD, SUB overflow. 177 // TODO: turn these into Legal? 178 if (Subtarget->hasCARRY()) 179 setOperationAction(ISD::UADDO, MVT::i32, Custom); 180 181 if (Subtarget->hasBORROW()) 182 setOperationAction(ISD::USUBO, MVT::i32, Custom); 183 184 // Expand sign extension of vectors 185 if (!Subtarget->hasBFE()) 186 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 187 188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand); 189 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand); 190 191 if (!Subtarget->hasBFE()) 192 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 193 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand); 194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand); 195 196 if (!Subtarget->hasBFE()) 197 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 198 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand); 199 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand); 200 201 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); 202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand); 203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand); 204 205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand); 206 207 setOperationAction(ISD::FrameIndex, MVT::i32, Custom); 208 209 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom); 210 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom); 211 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom); 212 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 213 214 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom); 215 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom); 216 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); 217 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 218 219 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32 220 // to be Legal/Custom in order to avoid library calls. 221 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 222 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 223 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 224 225 if (!Subtarget->hasFMA()) { 226 setOperationAction(ISD::FMA, MVT::f32, Expand); 227 setOperationAction(ISD::FMA, MVT::f64, Expand); 228 } 229 230 // FIXME: May need no denormals check 231 setOperationAction(ISD::FMAD, MVT::f32, Legal); 232 233 if (!Subtarget->hasBFI()) { 234 // fcopysign can be done in a single instruction with BFI. 235 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 236 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 237 } 238 239 if (!Subtarget->hasBCNT(32)) 240 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 241 242 if (!Subtarget->hasBCNT(64)) 243 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 244 245 if (Subtarget->hasFFBH()) 246 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom); 247 248 if (Subtarget->hasFFBL()) 249 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom); 250 251 // FIXME: This was moved from AMDGPUTargetLowering, I'm not sure if we 252 // need it for R600. 253 if (Subtarget->hasBFE()) 254 setHasExtractBitsInsn(true); 255 256 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 257 258 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 }; 259 for (MVT VT : ScalarIntVTs) { 260 setOperationAction(ISD::ADDC, VT, Expand); 261 setOperationAction(ISD::SUBC, VT, Expand); 262 setOperationAction(ISD::ADDE, VT, Expand); 263 setOperationAction(ISD::SUBE, VT, Expand); 264 } 265 266 // LLVM will expand these to atomic_cmp_swap(0) 267 // and atomic_swap, respectively. 268 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand); 269 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand); 270 271 // We need to custom lower some of the intrinsics 272 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 273 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 274 275 setSchedulingPreference(Sched::Source); 276 277 setTargetDAGCombine(ISD::FP_ROUND); 278 setTargetDAGCombine(ISD::FP_TO_SINT); 279 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 280 setTargetDAGCombine(ISD::SELECT_CC); 281 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 282 setTargetDAGCombine(ISD::LOAD); 283 } 284 285 static inline bool isEOP(MachineBasicBlock::iterator I) { 286 if (std::next(I) == I->getParent()->end()) 287 return false; 288 return std::next(I)->getOpcode() == R600::RETURN; 289 } 290 291 MachineBasicBlock * 292 R600TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 293 MachineBasicBlock *BB) const { 294 MachineFunction *MF = BB->getParent(); 295 MachineRegisterInfo &MRI = MF->getRegInfo(); 296 MachineBasicBlock::iterator I = MI; 297 const R600InstrInfo *TII = Subtarget->getInstrInfo(); 298 299 switch (MI.getOpcode()) { 300 default: 301 // Replace LDS_*_RET instruction that don't have any uses with the 302 // equivalent LDS_*_NORET instruction. 303 if (TII->isLDSRetInstr(MI.getOpcode())) { 304 int DstIdx = TII->getOperandIdx(MI.getOpcode(), R600::OpName::dst); 305 assert(DstIdx != -1); 306 MachineInstrBuilder NewMI; 307 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add 308 // LDS_1A2D support and remove this special case. 309 if (!MRI.use_empty(MI.getOperand(DstIdx).getReg()) || 310 MI.getOpcode() == R600::LDS_CMPST_RET) 311 return BB; 312 313 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I), 314 TII->get(R600::getLDSNoRetOp(MI.getOpcode()))); 315 for (unsigned i = 1, e = MI.getNumOperands(); i < e; ++i) { 316 NewMI.add(MI.getOperand(i)); 317 } 318 } else { 319 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 320 } 321 break; 322 323 case R600::FABS_R600: { 324 MachineInstr *NewMI = TII->buildDefaultInstruction( 325 *BB, I, R600::MOV, MI.getOperand(0).getReg(), 326 MI.getOperand(1).getReg()); 327 TII->addFlag(*NewMI, 0, MO_FLAG_ABS); 328 break; 329 } 330 331 case R600::FNEG_R600: { 332 MachineInstr *NewMI = TII->buildDefaultInstruction( 333 *BB, I, R600::MOV, MI.getOperand(0).getReg(), 334 MI.getOperand(1).getReg()); 335 TII->addFlag(*NewMI, 0, MO_FLAG_NEG); 336 break; 337 } 338 339 case R600::MASK_WRITE: { 340 Register maskedRegister = MI.getOperand(0).getReg(); 341 assert(Register::isVirtualRegister(maskedRegister)); 342 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister); 343 TII->addFlag(*defInstr, 0, MO_FLAG_MASK); 344 break; 345 } 346 347 case R600::MOV_IMM_F32: 348 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), MI.getOperand(1) 349 .getFPImm() 350 ->getValueAPF() 351 .bitcastToAPInt() 352 .getZExtValue()); 353 break; 354 355 case R600::MOV_IMM_I32: 356 TII->buildMovImm(*BB, I, MI.getOperand(0).getReg(), 357 MI.getOperand(1).getImm()); 358 break; 359 360 case R600::MOV_IMM_GLOBAL_ADDR: { 361 //TODO: Perhaps combine this instruction with the next if possible 362 auto MIB = TII->buildDefaultInstruction( 363 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_LITERAL_X); 364 int Idx = TII->getOperandIdx(*MIB, R600::OpName::literal); 365 //TODO: Ugh this is rather ugly 366 MIB->getOperand(Idx) = MI.getOperand(1); 367 break; 368 } 369 370 case R600::CONST_COPY: { 371 MachineInstr *NewMI = TII->buildDefaultInstruction( 372 *BB, MI, R600::MOV, MI.getOperand(0).getReg(), R600::ALU_CONST); 373 TII->setImmOperand(*NewMI, R600::OpName::src0_sel, 374 MI.getOperand(1).getImm()); 375 break; 376 } 377 378 case R600::RAT_WRITE_CACHELESS_32_eg: 379 case R600::RAT_WRITE_CACHELESS_64_eg: 380 case R600::RAT_WRITE_CACHELESS_128_eg: 381 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode())) 382 .add(MI.getOperand(0)) 383 .add(MI.getOperand(1)) 384 .addImm(isEOP(I)); // Set End of program bit 385 break; 386 387 case R600::RAT_STORE_TYPED_eg: 388 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode())) 389 .add(MI.getOperand(0)) 390 .add(MI.getOperand(1)) 391 .add(MI.getOperand(2)) 392 .addImm(isEOP(I)); // Set End of program bit 393 break; 394 395 case R600::BRANCH: 396 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP)) 397 .add(MI.getOperand(0)); 398 break; 399 400 case R600::BRANCH_COND_f32: { 401 MachineInstr *NewMI = 402 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X), 403 R600::PREDICATE_BIT) 404 .add(MI.getOperand(1)) 405 .addImm(R600::PRED_SETNE) 406 .addImm(0); // Flags 407 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH); 408 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND)) 409 .add(MI.getOperand(0)) 410 .addReg(R600::PREDICATE_BIT, RegState::Kill); 411 break; 412 } 413 414 case R600::BRANCH_COND_i32: { 415 MachineInstr *NewMI = 416 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::PRED_X), 417 R600::PREDICATE_BIT) 418 .add(MI.getOperand(1)) 419 .addImm(R600::PRED_SETNE_INT) 420 .addImm(0); // Flags 421 TII->addFlag(*NewMI, 0, MO_FLAG_PUSH); 422 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(R600::JUMP_COND)) 423 .add(MI.getOperand(0)) 424 .addReg(R600::PREDICATE_BIT, RegState::Kill); 425 break; 426 } 427 428 case R600::EG_ExportSwz: 429 case R600::R600_ExportSwz: { 430 // Instruction is left unmodified if its not the last one of its type 431 bool isLastInstructionOfItsType = true; 432 unsigned InstExportType = MI.getOperand(1).getImm(); 433 for (MachineBasicBlock::iterator NextExportInst = std::next(I), 434 EndBlock = BB->end(); NextExportInst != EndBlock; 435 NextExportInst = std::next(NextExportInst)) { 436 if (NextExportInst->getOpcode() == R600::EG_ExportSwz || 437 NextExportInst->getOpcode() == R600::R600_ExportSwz) { 438 unsigned CurrentInstExportType = NextExportInst->getOperand(1) 439 .getImm(); 440 if (CurrentInstExportType == InstExportType) { 441 isLastInstructionOfItsType = false; 442 break; 443 } 444 } 445 } 446 bool EOP = isEOP(I); 447 if (!EOP && !isLastInstructionOfItsType) 448 return BB; 449 unsigned CfInst = (MI.getOpcode() == R600::EG_ExportSwz) ? 84 : 40; 450 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI.getOpcode())) 451 .add(MI.getOperand(0)) 452 .add(MI.getOperand(1)) 453 .add(MI.getOperand(2)) 454 .add(MI.getOperand(3)) 455 .add(MI.getOperand(4)) 456 .add(MI.getOperand(5)) 457 .add(MI.getOperand(6)) 458 .addImm(CfInst) 459 .addImm(EOP); 460 break; 461 } 462 case R600::RETURN: { 463 return BB; 464 } 465 } 466 467 MI.eraseFromParent(); 468 return BB; 469 } 470 471 //===----------------------------------------------------------------------===// 472 // Custom DAG Lowering Operations 473 //===----------------------------------------------------------------------===// 474 475 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 476 MachineFunction &MF = DAG.getMachineFunction(); 477 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); 478 switch (Op.getOpcode()) { 479 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 480 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 481 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 482 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG); 483 case ISD::SRA_PARTS: 484 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG); 485 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY); 486 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW); 487 case ISD::FCOS: 488 case ISD::FSIN: return LowerTrig(Op, DAG); 489 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 490 case ISD::STORE: return LowerSTORE(Op, DAG); 491 case ISD::LOAD: { 492 SDValue Result = LowerLOAD(Op, DAG); 493 assert((!Result.getNode() || 494 Result.getNode()->getNumValues() == 2) && 495 "Load should return a value and a chain"); 496 return Result; 497 } 498 499 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 500 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG); 501 case ISD::FrameIndex: return lowerFrameIndex(Op, DAG); 502 case ISD::INTRINSIC_VOID: { 503 SDValue Chain = Op.getOperand(0); 504 unsigned IntrinsicID = 505 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 506 switch (IntrinsicID) { 507 case Intrinsic::r600_store_swizzle: { 508 SDLoc DL(Op); 509 const SDValue Args[8] = { 510 Chain, 511 Op.getOperand(2), // Export Value 512 Op.getOperand(3), // ArrayBase 513 Op.getOperand(4), // Type 514 DAG.getConstant(0, DL, MVT::i32), // SWZ_X 515 DAG.getConstant(1, DL, MVT::i32), // SWZ_Y 516 DAG.getConstant(2, DL, MVT::i32), // SWZ_Z 517 DAG.getConstant(3, DL, MVT::i32) // SWZ_W 518 }; 519 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, Op.getValueType(), Args); 520 } 521 522 // default for switch(IntrinsicID) 523 default: break; 524 } 525 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode()) 526 break; 527 } 528 case ISD::INTRINSIC_WO_CHAIN: { 529 unsigned IntrinsicID = 530 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 531 EVT VT = Op.getValueType(); 532 SDLoc DL(Op); 533 switch (IntrinsicID) { 534 case Intrinsic::r600_tex: 535 case Intrinsic::r600_texc: { 536 unsigned TextureOp; 537 switch (IntrinsicID) { 538 case Intrinsic::r600_tex: 539 TextureOp = 0; 540 break; 541 case Intrinsic::r600_texc: 542 TextureOp = 1; 543 break; 544 default: 545 llvm_unreachable("unhandled texture operation"); 546 } 547 548 SDValue TexArgs[19] = { 549 DAG.getConstant(TextureOp, DL, MVT::i32), 550 Op.getOperand(1), 551 DAG.getConstant(0, DL, MVT::i32), 552 DAG.getConstant(1, DL, MVT::i32), 553 DAG.getConstant(2, DL, MVT::i32), 554 DAG.getConstant(3, DL, MVT::i32), 555 Op.getOperand(2), 556 Op.getOperand(3), 557 Op.getOperand(4), 558 DAG.getConstant(0, DL, MVT::i32), 559 DAG.getConstant(1, DL, MVT::i32), 560 DAG.getConstant(2, DL, MVT::i32), 561 DAG.getConstant(3, DL, MVT::i32), 562 Op.getOperand(5), 563 Op.getOperand(6), 564 Op.getOperand(7), 565 Op.getOperand(8), 566 Op.getOperand(9), 567 Op.getOperand(10) 568 }; 569 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs); 570 } 571 case Intrinsic::r600_dot4: { 572 SDValue Args[8] = { 573 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1), 574 DAG.getConstant(0, DL, MVT::i32)), 575 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2), 576 DAG.getConstant(0, DL, MVT::i32)), 577 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1), 578 DAG.getConstant(1, DL, MVT::i32)), 579 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2), 580 DAG.getConstant(1, DL, MVT::i32)), 581 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1), 582 DAG.getConstant(2, DL, MVT::i32)), 583 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2), 584 DAG.getConstant(2, DL, MVT::i32)), 585 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1), 586 DAG.getConstant(3, DL, MVT::i32)), 587 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2), 588 DAG.getConstant(3, DL, MVT::i32)) 589 }; 590 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args); 591 } 592 593 case Intrinsic::r600_implicitarg_ptr: { 594 MVT PtrVT = getPointerTy(DAG.getDataLayout(), AMDGPUAS::PARAM_I_ADDRESS); 595 uint32_t ByteOffset = getImplicitParameterOffset(MF, FIRST_IMPLICIT); 596 return DAG.getConstant(ByteOffset, DL, PtrVT); 597 } 598 case Intrinsic::r600_read_ngroups_x: 599 return LowerImplicitParameter(DAG, VT, DL, 0); 600 case Intrinsic::r600_read_ngroups_y: 601 return LowerImplicitParameter(DAG, VT, DL, 1); 602 case Intrinsic::r600_read_ngroups_z: 603 return LowerImplicitParameter(DAG, VT, DL, 2); 604 case Intrinsic::r600_read_global_size_x: 605 return LowerImplicitParameter(DAG, VT, DL, 3); 606 case Intrinsic::r600_read_global_size_y: 607 return LowerImplicitParameter(DAG, VT, DL, 4); 608 case Intrinsic::r600_read_global_size_z: 609 return LowerImplicitParameter(DAG, VT, DL, 5); 610 case Intrinsic::r600_read_local_size_x: 611 return LowerImplicitParameter(DAG, VT, DL, 6); 612 case Intrinsic::r600_read_local_size_y: 613 return LowerImplicitParameter(DAG, VT, DL, 7); 614 case Intrinsic::r600_read_local_size_z: 615 return LowerImplicitParameter(DAG, VT, DL, 8); 616 617 case Intrinsic::r600_read_tgid_x: 618 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 619 R600::T1_X, VT); 620 case Intrinsic::r600_read_tgid_y: 621 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 622 R600::T1_Y, VT); 623 case Intrinsic::r600_read_tgid_z: 624 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 625 R600::T1_Z, VT); 626 case Intrinsic::r600_read_tidig_x: 627 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 628 R600::T0_X, VT); 629 case Intrinsic::r600_read_tidig_y: 630 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 631 R600::T0_Y, VT); 632 case Intrinsic::r600_read_tidig_z: 633 return CreateLiveInRegisterRaw(DAG, &R600::R600_TReg32RegClass, 634 R600::T0_Z, VT); 635 636 case Intrinsic::r600_recipsqrt_ieee: 637 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 638 639 case Intrinsic::r600_recipsqrt_clamped: 640 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 641 default: 642 return Op; 643 } 644 645 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode()) 646 break; 647 } 648 } // end switch(Op.getOpcode()) 649 return SDValue(); 650 } 651 652 void R600TargetLowering::ReplaceNodeResults(SDNode *N, 653 SmallVectorImpl<SDValue> &Results, 654 SelectionDAG &DAG) const { 655 switch (N->getOpcode()) { 656 default: 657 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG); 658 return; 659 case ISD::FP_TO_UINT: 660 if (N->getValueType(0) == MVT::i1) { 661 Results.push_back(lowerFP_TO_UINT(N->getOperand(0), DAG)); 662 return; 663 } 664 // Since we don't care about out of bounds values we can use FP_TO_SINT for 665 // uints too. The DAGLegalizer code for uint considers some extra cases 666 // which are not necessary here. 667 LLVM_FALLTHROUGH; 668 case ISD::FP_TO_SINT: { 669 if (N->getValueType(0) == MVT::i1) { 670 Results.push_back(lowerFP_TO_SINT(N->getOperand(0), DAG)); 671 return; 672 } 673 674 SDValue Result; 675 if (expandFP_TO_SINT(N, Result, DAG)) 676 Results.push_back(Result); 677 return; 678 } 679 case ISD::SDIVREM: { 680 SDValue Op = SDValue(N, 1); 681 SDValue RES = LowerSDIVREM(Op, DAG); 682 Results.push_back(RES); 683 Results.push_back(RES.getValue(1)); 684 break; 685 } 686 case ISD::UDIVREM: { 687 SDValue Op = SDValue(N, 0); 688 LowerUDIVREM64(Op, DAG, Results); 689 break; 690 } 691 } 692 } 693 694 SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG, 695 SDValue Vector) const { 696 SDLoc DL(Vector); 697 EVT VecVT = Vector.getValueType(); 698 EVT EltVT = VecVT.getVectorElementType(); 699 SmallVector<SDValue, 8> Args; 700 701 for (unsigned i = 0, e = VecVT.getVectorNumElements(); i != e; ++i) { 702 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector, 703 DAG.getVectorIdxConstant(i, DL))); 704 } 705 706 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args); 707 } 708 709 SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, 710 SelectionDAG &DAG) const { 711 SDLoc DL(Op); 712 SDValue Vector = Op.getOperand(0); 713 SDValue Index = Op.getOperand(1); 714 715 if (isa<ConstantSDNode>(Index) || 716 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR) 717 return Op; 718 719 Vector = vectorToVerticalVector(DAG, Vector); 720 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(), 721 Vector, Index); 722 } 723 724 SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, 725 SelectionDAG &DAG) const { 726 SDLoc DL(Op); 727 SDValue Vector = Op.getOperand(0); 728 SDValue Value = Op.getOperand(1); 729 SDValue Index = Op.getOperand(2); 730 731 if (isa<ConstantSDNode>(Index) || 732 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR) 733 return Op; 734 735 Vector = vectorToVerticalVector(DAG, Vector); 736 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(), 737 Vector, Value, Index); 738 return vectorToVerticalVector(DAG, Insert); 739 } 740 741 SDValue R600TargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 742 SDValue Op, 743 SelectionDAG &DAG) const { 744 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 745 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) 746 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 747 748 const DataLayout &DL = DAG.getDataLayout(); 749 const GlobalValue *GV = GSD->getGlobal(); 750 MVT ConstPtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 751 752 SDValue GA = DAG.getTargetGlobalAddress(GV, SDLoc(GSD), ConstPtrVT); 753 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, SDLoc(GSD), ConstPtrVT, GA); 754 } 755 756 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 757 // On hw >= R700, COS/SIN input must be between -1. and 1. 758 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5) 759 EVT VT = Op.getValueType(); 760 SDValue Arg = Op.getOperand(0); 761 SDLoc DL(Op); 762 763 // TODO: Should this propagate fast-math-flags? 764 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 765 DAG.getNode(ISD::FADD, DL, VT, 766 DAG.getNode(ISD::FMUL, DL, VT, Arg, 767 DAG.getConstantFP(0.15915494309, DL, MVT::f32)), 768 DAG.getConstantFP(0.5, DL, MVT::f32))); 769 unsigned TrigNode; 770 switch (Op.getOpcode()) { 771 case ISD::FCOS: 772 TrigNode = AMDGPUISD::COS_HW; 773 break; 774 case ISD::FSIN: 775 TrigNode = AMDGPUISD::SIN_HW; 776 break; 777 default: 778 llvm_unreachable("Wrong trig opcode"); 779 } 780 SDValue TrigVal = DAG.getNode(TrigNode, DL, VT, 781 DAG.getNode(ISD::FADD, DL, VT, FractPart, 782 DAG.getConstantFP(-0.5, DL, MVT::f32))); 783 if (Gen >= AMDGPUSubtarget::R700) 784 return TrigVal; 785 // On R600 hw, COS/SIN input must be between -Pi and Pi. 786 return DAG.getNode(ISD::FMUL, DL, VT, TrigVal, 787 DAG.getConstantFP(numbers::pif, DL, MVT::f32)); 788 } 789 790 SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const { 791 SDLoc DL(Op); 792 EVT VT = Op.getValueType(); 793 794 SDValue Lo = Op.getOperand(0); 795 SDValue Hi = Op.getOperand(1); 796 SDValue Shift = Op.getOperand(2); 797 SDValue Zero = DAG.getConstant(0, DL, VT); 798 SDValue One = DAG.getConstant(1, DL, VT); 799 800 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT); 801 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT); 802 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width); 803 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift); 804 805 // The dance around Width1 is necessary for 0 special case. 806 // Without it the CompShift might be 32, producing incorrect results in 807 // Overflow. So we do the shift in two steps, the alternative is to 808 // add a conditional to filter the special case. 809 810 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift); 811 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One); 812 813 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift); 814 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow); 815 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift); 816 817 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift); 818 SDValue LoBig = Zero; 819 820 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT); 821 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT); 822 823 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi); 824 } 825 826 SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const { 827 SDLoc DL(Op); 828 EVT VT = Op.getValueType(); 829 830 SDValue Lo = Op.getOperand(0); 831 SDValue Hi = Op.getOperand(1); 832 SDValue Shift = Op.getOperand(2); 833 SDValue Zero = DAG.getConstant(0, DL, VT); 834 SDValue One = DAG.getConstant(1, DL, VT); 835 836 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS; 837 838 SDValue Width = DAG.getConstant(VT.getSizeInBits(), DL, VT); 839 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT); 840 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width); 841 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift); 842 843 // The dance around Width1 is necessary for 0 special case. 844 // Without it the CompShift might be 32, producing incorrect results in 845 // Overflow. So we do the shift in two steps, the alternative is to 846 // add a conditional to filter the special case. 847 848 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift); 849 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One); 850 851 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift); 852 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift); 853 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow); 854 855 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift); 856 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero; 857 858 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT); 859 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT); 860 861 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi); 862 } 863 864 SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG, 865 unsigned mainop, unsigned ovf) const { 866 SDLoc DL(Op); 867 EVT VT = Op.getValueType(); 868 869 SDValue Lo = Op.getOperand(0); 870 SDValue Hi = Op.getOperand(1); 871 872 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi); 873 // Extend sign. 874 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF, 875 DAG.getValueType(MVT::i1)); 876 877 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi); 878 879 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF); 880 } 881 882 SDValue R600TargetLowering::lowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const { 883 SDLoc DL(Op); 884 return DAG.getNode( 885 ISD::SETCC, 886 DL, 887 MVT::i1, 888 Op, DAG.getConstantFP(1.0f, DL, MVT::f32), 889 DAG.getCondCode(ISD::SETEQ)); 890 } 891 892 SDValue R600TargetLowering::lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const { 893 SDLoc DL(Op); 894 return DAG.getNode( 895 ISD::SETCC, 896 DL, 897 MVT::i1, 898 Op, DAG.getConstantFP(-1.0f, DL, MVT::f32), 899 DAG.getCondCode(ISD::SETEQ)); 900 } 901 902 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT, 903 const SDLoc &DL, 904 unsigned DwordOffset) const { 905 unsigned ByteOffset = DwordOffset * 4; 906 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()), 907 AMDGPUAS::PARAM_I_ADDRESS); 908 909 // We shouldn't be using an offset wider than 16-bits for implicit parameters. 910 assert(isInt<16>(ByteOffset)); 911 912 return DAG.getLoad(VT, DL, DAG.getEntryNode(), 913 DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR 914 MachinePointerInfo(ConstantPointerNull::get(PtrType))); 915 } 916 917 bool R600TargetLowering::isZero(SDValue Op) const { 918 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) { 919 return Cst->isNullValue(); 920 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){ 921 return CstFP->isZero(); 922 } else { 923 return false; 924 } 925 } 926 927 bool R600TargetLowering::isHWTrueValue(SDValue Op) const { 928 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) { 929 return CFP->isExactlyValue(1.0); 930 } 931 return isAllOnesConstant(Op); 932 } 933 934 bool R600TargetLowering::isHWFalseValue(SDValue Op) const { 935 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) { 936 return CFP->getValueAPF().isZero(); 937 } 938 return isNullConstant(Op); 939 } 940 941 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 942 SDLoc DL(Op); 943 EVT VT = Op.getValueType(); 944 945 SDValue LHS = Op.getOperand(0); 946 SDValue RHS = Op.getOperand(1); 947 SDValue True = Op.getOperand(2); 948 SDValue False = Op.getOperand(3); 949 SDValue CC = Op.getOperand(4); 950 SDValue Temp; 951 952 if (VT == MVT::f32) { 953 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr); 954 SDValue MinMax = combineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI); 955 if (MinMax) 956 return MinMax; 957 } 958 959 // LHS and RHS are guaranteed to be the same value type 960 EVT CompareVT = LHS.getValueType(); 961 962 // Check if we can lower this to a native operation. 963 964 // Try to lower to a SET* instruction: 965 // 966 // SET* can match the following patterns: 967 // 968 // select_cc f32, f32, -1, 0, cc_supported 969 // select_cc f32, f32, 1.0f, 0.0f, cc_supported 970 // select_cc i32, i32, -1, 0, cc_supported 971 // 972 973 // Move hardware True/False values to the correct operand. 974 if (isHWTrueValue(False) && isHWFalseValue(True)) { 975 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 976 ISD::CondCode InverseCC = ISD::getSetCCInverse(CCOpcode, CompareVT); 977 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) { 978 std::swap(False, True); 979 CC = DAG.getCondCode(InverseCC); 980 } else { 981 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC); 982 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) { 983 std::swap(False, True); 984 std::swap(LHS, RHS); 985 CC = DAG.getCondCode(SwapInvCC); 986 } 987 } 988 } 989 990 if (isHWTrueValue(True) && isHWFalseValue(False) && 991 (CompareVT == VT || VT == MVT::i32)) { 992 // This can be matched by a SET* instruction. 993 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC); 994 } 995 996 // Try to lower to a CND* instruction: 997 // 998 // CND* can match the following patterns: 999 // 1000 // select_cc f32, 0.0, f32, f32, cc_supported 1001 // select_cc f32, 0.0, i32, i32, cc_supported 1002 // select_cc i32, 0, f32, f32, cc_supported 1003 // select_cc i32, 0, i32, i32, cc_supported 1004 // 1005 1006 // Try to move the zero value to the RHS 1007 if (isZero(LHS)) { 1008 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 1009 // Try swapping the operands 1010 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode); 1011 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) { 1012 std::swap(LHS, RHS); 1013 CC = DAG.getCondCode(CCSwapped); 1014 } else { 1015 // Try inverting the conditon and then swapping the operands 1016 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT); 1017 CCSwapped = ISD::getSetCCSwappedOperands(CCInv); 1018 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) { 1019 std::swap(True, False); 1020 std::swap(LHS, RHS); 1021 CC = DAG.getCondCode(CCSwapped); 1022 } 1023 } 1024 } 1025 if (isZero(RHS)) { 1026 SDValue Cond = LHS; 1027 SDValue Zero = RHS; 1028 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get(); 1029 if (CompareVT != VT) { 1030 // Bitcast True / False to the correct types. This will end up being 1031 // a nop, but it allows us to define only a single pattern in the 1032 // .TD files for each CND* instruction rather than having to have 1033 // one pattern for integer True/False and one for fp True/False 1034 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True); 1035 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False); 1036 } 1037 1038 switch (CCOpcode) { 1039 case ISD::SETONE: 1040 case ISD::SETUNE: 1041 case ISD::SETNE: 1042 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT); 1043 Temp = True; 1044 True = False; 1045 False = Temp; 1046 break; 1047 default: 1048 break; 1049 } 1050 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, 1051 Cond, Zero, 1052 True, False, 1053 DAG.getCondCode(CCOpcode)); 1054 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode); 1055 } 1056 1057 // If we make it this for it means we have no native instructions to handle 1058 // this SELECT_CC, so we must lower it. 1059 SDValue HWTrue, HWFalse; 1060 1061 if (CompareVT == MVT::f32) { 1062 HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT); 1063 HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT); 1064 } else if (CompareVT == MVT::i32) { 1065 HWTrue = DAG.getConstant(-1, DL, CompareVT); 1066 HWFalse = DAG.getConstant(0, DL, CompareVT); 1067 } 1068 else { 1069 llvm_unreachable("Unhandled value type in LowerSELECT_CC"); 1070 } 1071 1072 // Lower this unsupported SELECT_CC into a combination of two supported 1073 // SELECT_CC operations. 1074 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC); 1075 1076 return DAG.getNode(ISD::SELECT_CC, DL, VT, 1077 Cond, HWFalse, 1078 True, False, 1079 DAG.getCondCode(ISD::SETNE)); 1080 } 1081 1082 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to 1083 /// convert these pointers to a register index. Each register holds 1084 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the 1085 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used 1086 /// for indirect addressing. 1087 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr, 1088 unsigned StackWidth, 1089 SelectionDAG &DAG) const { 1090 unsigned SRLPad; 1091 switch(StackWidth) { 1092 case 1: 1093 SRLPad = 2; 1094 break; 1095 case 2: 1096 SRLPad = 3; 1097 break; 1098 case 4: 1099 SRLPad = 4; 1100 break; 1101 default: llvm_unreachable("Invalid stack width"); 1102 } 1103 1104 SDLoc DL(Ptr); 1105 return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr, 1106 DAG.getConstant(SRLPad, DL, MVT::i32)); 1107 } 1108 1109 void R600TargetLowering::getStackAddress(unsigned StackWidth, 1110 unsigned ElemIdx, 1111 unsigned &Channel, 1112 unsigned &PtrIncr) const { 1113 switch (StackWidth) { 1114 default: 1115 case 1: 1116 Channel = 0; 1117 if (ElemIdx > 0) { 1118 PtrIncr = 1; 1119 } else { 1120 PtrIncr = 0; 1121 } 1122 break; 1123 case 2: 1124 Channel = ElemIdx % 2; 1125 if (ElemIdx == 2) { 1126 PtrIncr = 1; 1127 } else { 1128 PtrIncr = 0; 1129 } 1130 break; 1131 case 4: 1132 Channel = ElemIdx; 1133 PtrIncr = 0; 1134 break; 1135 } 1136 } 1137 1138 SDValue R600TargetLowering::lowerPrivateTruncStore(StoreSDNode *Store, 1139 SelectionDAG &DAG) const { 1140 SDLoc DL(Store); 1141 //TODO: Who creates the i8 stores? 1142 assert(Store->isTruncatingStore() 1143 || Store->getValue().getValueType() == MVT::i8); 1144 assert(Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS); 1145 1146 SDValue Mask; 1147 if (Store->getMemoryVT() == MVT::i8) { 1148 assert(Store->getAlignment() >= 1); 1149 Mask = DAG.getConstant(0xff, DL, MVT::i32); 1150 } else if (Store->getMemoryVT() == MVT::i16) { 1151 assert(Store->getAlignment() >= 2); 1152 Mask = DAG.getConstant(0xffff, DL, MVT::i32); 1153 } else { 1154 llvm_unreachable("Unsupported private trunc store"); 1155 } 1156 1157 SDValue OldChain = Store->getChain(); 1158 bool VectorTrunc = (OldChain.getOpcode() == AMDGPUISD::DUMMY_CHAIN); 1159 // Skip dummy 1160 SDValue Chain = VectorTrunc ? OldChain->getOperand(0) : OldChain; 1161 SDValue BasePtr = Store->getBasePtr(); 1162 SDValue Offset = Store->getOffset(); 1163 EVT MemVT = Store->getMemoryVT(); 1164 1165 SDValue LoadPtr = BasePtr; 1166 if (!Offset.isUndef()) { 1167 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset); 1168 } 1169 1170 // Get dword location 1171 // TODO: this should be eliminated by the future SHR ptr, 2 1172 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr, 1173 DAG.getConstant(0xfffffffc, DL, MVT::i32)); 1174 1175 // Load dword 1176 // TODO: can we be smarter about machine pointer info? 1177 MachinePointerInfo PtrInfo(AMDGPUAS::PRIVATE_ADDRESS); 1178 SDValue Dst = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo); 1179 1180 Chain = Dst.getValue(1); 1181 1182 // Get offset in dword 1183 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr, 1184 DAG.getConstant(0x3, DL, MVT::i32)); 1185 1186 // Convert byte offset to bit shift 1187 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx, 1188 DAG.getConstant(3, DL, MVT::i32)); 1189 1190 // TODO: Contrary to the name of the functiom, 1191 // it also handles sub i32 non-truncating stores (like i1) 1192 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32, 1193 Store->getValue()); 1194 1195 // Mask the value to the right type 1196 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT); 1197 1198 // Shift the value in place 1199 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32, 1200 MaskedValue, ShiftAmt); 1201 1202 // Shift the mask in place 1203 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, Mask, ShiftAmt); 1204 1205 // Invert the mask. NOTE: if we had native ROL instructions we could 1206 // use inverted mask 1207 DstMask = DAG.getNOT(DL, DstMask, MVT::i32); 1208 1209 // Cleanup the target bits 1210 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask); 1211 1212 // Add the new bits 1213 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue); 1214 1215 // Store dword 1216 // TODO: Can we be smarter about MachinePointerInfo? 1217 SDValue NewStore = DAG.getStore(Chain, DL, Value, Ptr, PtrInfo); 1218 1219 // If we are part of expanded vector, make our neighbors depend on this store 1220 if (VectorTrunc) { 1221 // Make all other vector elements depend on this store 1222 Chain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, NewStore); 1223 DAG.ReplaceAllUsesOfValueWith(OldChain, Chain); 1224 } 1225 return NewStore; 1226 } 1227 1228 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 1229 StoreSDNode *StoreNode = cast<StoreSDNode>(Op); 1230 unsigned AS = StoreNode->getAddressSpace(); 1231 1232 SDValue Chain = StoreNode->getChain(); 1233 SDValue Ptr = StoreNode->getBasePtr(); 1234 SDValue Value = StoreNode->getValue(); 1235 1236 EVT VT = Value.getValueType(); 1237 EVT MemVT = StoreNode->getMemoryVT(); 1238 EVT PtrVT = Ptr.getValueType(); 1239 1240 SDLoc DL(Op); 1241 1242 const bool TruncatingStore = StoreNode->isTruncatingStore(); 1243 1244 // Neither LOCAL nor PRIVATE can do vectors at the moment 1245 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS || 1246 TruncatingStore) && 1247 VT.isVector()) { 1248 if ((AS == AMDGPUAS::PRIVATE_ADDRESS) && TruncatingStore) { 1249 // Add an extra level of chain to isolate this vector 1250 SDValue NewChain = DAG.getNode(AMDGPUISD::DUMMY_CHAIN, DL, MVT::Other, Chain); 1251 // TODO: can the chain be replaced without creating a new store? 1252 SDValue NewStore = DAG.getTruncStore( 1253 NewChain, DL, Value, Ptr, StoreNode->getPointerInfo(), 1254 MemVT, StoreNode->getAlignment(), 1255 StoreNode->getMemOperand()->getFlags(), StoreNode->getAAInfo()); 1256 StoreNode = cast<StoreSDNode>(NewStore); 1257 } 1258 1259 return scalarizeVectorStore(StoreNode, DAG); 1260 } 1261 1262 unsigned Align = StoreNode->getAlignment(); 1263 if (Align < MemVT.getStoreSize() && 1264 !allowsMisalignedMemoryAccesses( 1265 MemVT, AS, Align, StoreNode->getMemOperand()->getFlags(), nullptr)) { 1266 return expandUnalignedStore(StoreNode, DAG); 1267 } 1268 1269 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, PtrVT, Ptr, 1270 DAG.getConstant(2, DL, PtrVT)); 1271 1272 if (AS == AMDGPUAS::GLOBAL_ADDRESS) { 1273 // It is beneficial to create MSKOR here instead of combiner to avoid 1274 // artificial dependencies introduced by RMW 1275 if (TruncatingStore) { 1276 assert(VT.bitsLE(MVT::i32)); 1277 SDValue MaskConstant; 1278 if (MemVT == MVT::i8) { 1279 MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32); 1280 } else { 1281 assert(MemVT == MVT::i16); 1282 assert(StoreNode->getAlignment() >= 2); 1283 MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32); 1284 } 1285 1286 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, PtrVT, Ptr, 1287 DAG.getConstant(0x00000003, DL, PtrVT)); 1288 SDValue BitShift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex, 1289 DAG.getConstant(3, DL, VT)); 1290 1291 // Put the mask in correct place 1292 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, BitShift); 1293 1294 // Put the value bits in correct place 1295 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant); 1296 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, BitShift); 1297 1298 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32 1299 // vector instead. 1300 SDValue Src[4] = { 1301 ShiftedValue, 1302 DAG.getConstant(0, DL, MVT::i32), 1303 DAG.getConstant(0, DL, MVT::i32), 1304 Mask 1305 }; 1306 SDValue Input = DAG.getBuildVector(MVT::v4i32, DL, Src); 1307 SDValue Args[3] = { Chain, Input, DWordAddr }; 1308 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL, 1309 Op->getVTList(), Args, MemVT, 1310 StoreNode->getMemOperand()); 1311 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR && VT.bitsGE(MVT::i32)) { 1312 // Convert pointer from byte address to dword address. 1313 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr); 1314 1315 if (StoreNode->isIndexed()) { 1316 llvm_unreachable("Indexed stores not supported yet"); 1317 } else { 1318 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand()); 1319 } 1320 return Chain; 1321 } 1322 } 1323 1324 // GLOBAL_ADDRESS has been handled above, LOCAL_ADDRESS allows all sizes 1325 if (AS != AMDGPUAS::PRIVATE_ADDRESS) 1326 return SDValue(); 1327 1328 if (MemVT.bitsLT(MVT::i32)) 1329 return lowerPrivateTruncStore(StoreNode, DAG); 1330 1331 // Standard i32+ store, tag it with DWORDADDR to note that the address 1332 // has been shifted 1333 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) { 1334 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, PtrVT, DWordAddr); 1335 return DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand()); 1336 } 1337 1338 // Tagged i32+ stores will be matched by patterns 1339 return SDValue(); 1340 } 1341 1342 // return (512 + (kc_bank << 12) 1343 static int 1344 ConstantAddressBlock(unsigned AddressSpace) { 1345 switch (AddressSpace) { 1346 case AMDGPUAS::CONSTANT_BUFFER_0: 1347 return 512; 1348 case AMDGPUAS::CONSTANT_BUFFER_1: 1349 return 512 + 4096; 1350 case AMDGPUAS::CONSTANT_BUFFER_2: 1351 return 512 + 4096 * 2; 1352 case AMDGPUAS::CONSTANT_BUFFER_3: 1353 return 512 + 4096 * 3; 1354 case AMDGPUAS::CONSTANT_BUFFER_4: 1355 return 512 + 4096 * 4; 1356 case AMDGPUAS::CONSTANT_BUFFER_5: 1357 return 512 + 4096 * 5; 1358 case AMDGPUAS::CONSTANT_BUFFER_6: 1359 return 512 + 4096 * 6; 1360 case AMDGPUAS::CONSTANT_BUFFER_7: 1361 return 512 + 4096 * 7; 1362 case AMDGPUAS::CONSTANT_BUFFER_8: 1363 return 512 + 4096 * 8; 1364 case AMDGPUAS::CONSTANT_BUFFER_9: 1365 return 512 + 4096 * 9; 1366 case AMDGPUAS::CONSTANT_BUFFER_10: 1367 return 512 + 4096 * 10; 1368 case AMDGPUAS::CONSTANT_BUFFER_11: 1369 return 512 + 4096 * 11; 1370 case AMDGPUAS::CONSTANT_BUFFER_12: 1371 return 512 + 4096 * 12; 1372 case AMDGPUAS::CONSTANT_BUFFER_13: 1373 return 512 + 4096 * 13; 1374 case AMDGPUAS::CONSTANT_BUFFER_14: 1375 return 512 + 4096 * 14; 1376 case AMDGPUAS::CONSTANT_BUFFER_15: 1377 return 512 + 4096 * 15; 1378 default: 1379 return -1; 1380 } 1381 } 1382 1383 SDValue R600TargetLowering::lowerPrivateExtLoad(SDValue Op, 1384 SelectionDAG &DAG) const { 1385 SDLoc DL(Op); 1386 LoadSDNode *Load = cast<LoadSDNode>(Op); 1387 ISD::LoadExtType ExtType = Load->getExtensionType(); 1388 EVT MemVT = Load->getMemoryVT(); 1389 assert(Load->getAlignment() >= MemVT.getStoreSize()); 1390 1391 SDValue BasePtr = Load->getBasePtr(); 1392 SDValue Chain = Load->getChain(); 1393 SDValue Offset = Load->getOffset(); 1394 1395 SDValue LoadPtr = BasePtr; 1396 if (!Offset.isUndef()) { 1397 LoadPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, Offset); 1398 } 1399 1400 // Get dword location 1401 // NOTE: this should be eliminated by the future SHR ptr, 2 1402 SDValue Ptr = DAG.getNode(ISD::AND, DL, MVT::i32, LoadPtr, 1403 DAG.getConstant(0xfffffffc, DL, MVT::i32)); 1404 1405 // Load dword 1406 // TODO: can we be smarter about machine pointer info? 1407 MachinePointerInfo PtrInfo(AMDGPUAS::PRIVATE_ADDRESS); 1408 SDValue Read = DAG.getLoad(MVT::i32, DL, Chain, Ptr, PtrInfo); 1409 1410 // Get offset within the register. 1411 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, 1412 LoadPtr, DAG.getConstant(0x3, DL, MVT::i32)); 1413 1414 // Bit offset of target byte (byteIdx * 8). 1415 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx, 1416 DAG.getConstant(3, DL, MVT::i32)); 1417 1418 // Shift to the right. 1419 SDValue Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Read, ShiftAmt); 1420 1421 // Eliminate the upper bits by setting them to ... 1422 EVT MemEltVT = MemVT.getScalarType(); 1423 1424 if (ExtType == ISD::SEXTLOAD) { // ... ones. 1425 SDValue MemEltVTNode = DAG.getValueType(MemEltVT); 1426 Ret = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode); 1427 } else { // ... or zeros. 1428 Ret = DAG.getZeroExtendInReg(Ret, DL, MemEltVT); 1429 } 1430 1431 SDValue Ops[] = { 1432 Ret, 1433 Read.getValue(1) // This should be our output chain 1434 }; 1435 1436 return DAG.getMergeValues(Ops, DL); 1437 } 1438 1439 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 1440 LoadSDNode *LoadNode = cast<LoadSDNode>(Op); 1441 unsigned AS = LoadNode->getAddressSpace(); 1442 EVT MemVT = LoadNode->getMemoryVT(); 1443 ISD::LoadExtType ExtType = LoadNode->getExtensionType(); 1444 1445 if (AS == AMDGPUAS::PRIVATE_ADDRESS && 1446 ExtType != ISD::NON_EXTLOAD && MemVT.bitsLT(MVT::i32)) { 1447 return lowerPrivateExtLoad(Op, DAG); 1448 } 1449 1450 SDLoc DL(Op); 1451 EVT VT = Op.getValueType(); 1452 SDValue Chain = LoadNode->getChain(); 1453 SDValue Ptr = LoadNode->getBasePtr(); 1454 1455 if ((LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 1456 LoadNode->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) && 1457 VT.isVector()) { 1458 SDValue Ops[2]; 1459 std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(LoadNode, DAG); 1460 return DAG.getMergeValues(Ops, DL); 1461 } 1462 1463 // This is still used for explicit load from addrspace(8) 1464 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace()); 1465 if (ConstantBlock > -1 && 1466 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) || 1467 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) { 1468 SDValue Result; 1469 if (isa<Constant>(LoadNode->getMemOperand()->getValue()) || 1470 isa<ConstantSDNode>(Ptr)) { 1471 return constBufferLoad(LoadNode, LoadNode->getAddressSpace(), DAG); 1472 } else { 1473 //TODO: Does this even work? 1474 // non-constant ptr can't be folded, keeps it as a v4f32 load 1475 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32, 1476 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, 1477 DAG.getConstant(4, DL, MVT::i32)), 1478 DAG.getConstant(LoadNode->getAddressSpace() - 1479 AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32) 1480 ); 1481 } 1482 1483 if (!VT.isVector()) { 1484 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result, 1485 DAG.getConstant(0, DL, MVT::i32)); 1486 } 1487 1488 SDValue MergedValues[2] = { 1489 Result, 1490 Chain 1491 }; 1492 return DAG.getMergeValues(MergedValues, DL); 1493 } 1494 1495 // For most operations returning SDValue() will result in the node being 1496 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we 1497 // need to manually expand loads that may be legal in some address spaces and 1498 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for 1499 // compute shaders, since the data is sign extended when it is uploaded to the 1500 // buffer. However SEXT loads from other address spaces are not supported, so 1501 // we need to expand them here. 1502 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) { 1503 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8)); 1504 SDValue NewLoad = DAG.getExtLoad( 1505 ISD::EXTLOAD, DL, VT, Chain, Ptr, LoadNode->getPointerInfo(), MemVT, 1506 LoadNode->getAlignment(), LoadNode->getMemOperand()->getFlags()); 1507 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad, 1508 DAG.getValueType(MemVT)); 1509 1510 SDValue MergedValues[2] = { Res, Chain }; 1511 return DAG.getMergeValues(MergedValues, DL); 1512 } 1513 1514 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) { 1515 return SDValue(); 1516 } 1517 1518 // DWORDADDR ISD marks already shifted address 1519 if (Ptr.getOpcode() != AMDGPUISD::DWORDADDR) { 1520 assert(VT == MVT::i32); 1521 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(2, DL, MVT::i32)); 1522 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, MVT::i32, Ptr); 1523 return DAG.getLoad(MVT::i32, DL, Chain, Ptr, LoadNode->getMemOperand()); 1524 } 1525 return SDValue(); 1526 } 1527 1528 SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 1529 SDValue Chain = Op.getOperand(0); 1530 SDValue Cond = Op.getOperand(1); 1531 SDValue Jump = Op.getOperand(2); 1532 1533 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(), 1534 Chain, Jump, Cond); 1535 } 1536 1537 SDValue R600TargetLowering::lowerFrameIndex(SDValue Op, 1538 SelectionDAG &DAG) const { 1539 MachineFunction &MF = DAG.getMachineFunction(); 1540 const R600FrameLowering *TFL = Subtarget->getFrameLowering(); 1541 1542 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op); 1543 1544 unsigned FrameIndex = FIN->getIndex(); 1545 unsigned IgnoredFrameReg; 1546 unsigned Offset = 1547 TFL->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg); 1548 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op), 1549 Op.getValueType()); 1550 } 1551 1552 CCAssignFn *R600TargetLowering::CCAssignFnForCall(CallingConv::ID CC, 1553 bool IsVarArg) const { 1554 switch (CC) { 1555 case CallingConv::AMDGPU_KERNEL: 1556 case CallingConv::SPIR_KERNEL: 1557 case CallingConv::C: 1558 case CallingConv::Fast: 1559 case CallingConv::Cold: 1560 llvm_unreachable("kernels should not be handled here"); 1561 case CallingConv::AMDGPU_VS: 1562 case CallingConv::AMDGPU_GS: 1563 case CallingConv::AMDGPU_PS: 1564 case CallingConv::AMDGPU_CS: 1565 case CallingConv::AMDGPU_HS: 1566 case CallingConv::AMDGPU_ES: 1567 case CallingConv::AMDGPU_LS: 1568 return CC_R600; 1569 default: 1570 report_fatal_error("Unsupported calling convention."); 1571 } 1572 } 1573 1574 /// XXX Only kernel functions are supported, so we can assume for now that 1575 /// every function is a kernel function, but in the future we should use 1576 /// separate calling conventions for kernel and non-kernel functions. 1577 SDValue R600TargetLowering::LowerFormalArguments( 1578 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 1579 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 1580 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 1581 SmallVector<CCValAssign, 16> ArgLocs; 1582 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1583 *DAG.getContext()); 1584 MachineFunction &MF = DAG.getMachineFunction(); 1585 SmallVector<ISD::InputArg, 8> LocalIns; 1586 1587 if (AMDGPU::isShader(CallConv)) { 1588 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg)); 1589 } else { 1590 analyzeFormalArgumentsCompute(CCInfo, Ins); 1591 } 1592 1593 for (unsigned i = 0, e = Ins.size(); i < e; ++i) { 1594 CCValAssign &VA = ArgLocs[i]; 1595 const ISD::InputArg &In = Ins[i]; 1596 EVT VT = In.VT; 1597 EVT MemVT = VA.getLocVT(); 1598 if (!VT.isVector() && MemVT.isVector()) { 1599 // Get load source type if scalarized. 1600 MemVT = MemVT.getVectorElementType(); 1601 } 1602 1603 if (AMDGPU::isShader(CallConv)) { 1604 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &R600::R600_Reg128RegClass); 1605 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT); 1606 InVals.push_back(Register); 1607 continue; 1608 } 1609 1610 // i64 isn't a legal type, so the register type used ends up as i32, which 1611 // isn't expected here. It attempts to create this sextload, but it ends up 1612 // being invalid. Somehow this seems to work with i64 arguments, but breaks 1613 // for <1 x i64>. 1614 1615 // The first 36 bytes of the input buffer contains information about 1616 // thread group and global sizes. 1617 ISD::LoadExtType Ext = ISD::NON_EXTLOAD; 1618 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) { 1619 // FIXME: This should really check the extload type, but the handling of 1620 // extload vector parameters seems to be broken. 1621 1622 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD; 1623 Ext = ISD::SEXTLOAD; 1624 } 1625 1626 // Compute the offset from the value. 1627 // XXX - I think PartOffset should give you this, but it seems to give the 1628 // size of the register which isn't useful. 1629 1630 unsigned PartOffset = VA.getLocMemOffset(); 1631 unsigned Alignment = MinAlign(VT.getStoreSize(), PartOffset); 1632 1633 MachinePointerInfo PtrInfo(AMDGPUAS::PARAM_I_ADDRESS); 1634 SDValue Arg = DAG.getLoad( 1635 ISD::UNINDEXED, Ext, VT, DL, Chain, 1636 DAG.getConstant(PartOffset, DL, MVT::i32), DAG.getUNDEF(MVT::i32), 1637 PtrInfo, 1638 MemVT, Alignment, MachineMemOperand::MONonTemporal | 1639 MachineMemOperand::MODereferenceable | 1640 MachineMemOperand::MOInvariant); 1641 1642 InVals.push_back(Arg); 1643 } 1644 return Chain; 1645 } 1646 1647 EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 1648 EVT VT) const { 1649 if (!VT.isVector()) 1650 return MVT::i32; 1651 return VT.changeVectorElementTypeToInteger(); 1652 } 1653 1654 bool R600TargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT, 1655 const SelectionDAG &DAG) const { 1656 // Local and Private addresses do not handle vectors. Limit to i32 1657 if ((AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::PRIVATE_ADDRESS)) { 1658 return (MemVT.getSizeInBits() <= 32); 1659 } 1660 return true; 1661 } 1662 1663 bool R600TargetLowering::allowsMisalignedMemoryAccesses( 1664 EVT VT, unsigned AddrSpace, unsigned Align, MachineMemOperand::Flags Flags, 1665 bool *IsFast) const { 1666 if (IsFast) 1667 *IsFast = false; 1668 1669 if (!VT.isSimple() || VT == MVT::Other) 1670 return false; 1671 1672 if (VT.bitsLT(MVT::i32)) 1673 return false; 1674 1675 // TODO: This is a rough estimate. 1676 if (IsFast) 1677 *IsFast = true; 1678 1679 return VT.bitsGT(MVT::i32) && Align % 4 == 0; 1680 } 1681 1682 static SDValue CompactSwizzlableVector( 1683 SelectionDAG &DAG, SDValue VectorEntry, 1684 DenseMap<unsigned, unsigned> &RemapSwizzle) { 1685 assert(RemapSwizzle.empty()); 1686 1687 SDLoc DL(VectorEntry); 1688 EVT EltTy = VectorEntry.getValueType().getVectorElementType(); 1689 1690 SDValue NewBldVec[4]; 1691 for (unsigned i = 0; i < 4; i++) 1692 NewBldVec[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltTy, VectorEntry, 1693 DAG.getIntPtrConstant(i, DL)); 1694 1695 for (unsigned i = 0; i < 4; i++) { 1696 if (NewBldVec[i].isUndef()) 1697 // We mask write here to teach later passes that the ith element of this 1698 // vector is undef. Thus we can use it to reduce 128 bits reg usage, 1699 // break false dependencies and additionnaly make assembly easier to read. 1700 RemapSwizzle[i] = 7; // SEL_MASK_WRITE 1701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) { 1702 if (C->isZero()) { 1703 RemapSwizzle[i] = 4; // SEL_0 1704 NewBldVec[i] = DAG.getUNDEF(MVT::f32); 1705 } else if (C->isExactlyValue(1.0)) { 1706 RemapSwizzle[i] = 5; // SEL_1 1707 NewBldVec[i] = DAG.getUNDEF(MVT::f32); 1708 } 1709 } 1710 1711 if (NewBldVec[i].isUndef()) 1712 continue; 1713 1714 for (unsigned j = 0; j < i; j++) { 1715 if (NewBldVec[i] == NewBldVec[j]) { 1716 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType()); 1717 RemapSwizzle[i] = j; 1718 break; 1719 } 1720 } 1721 } 1722 1723 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry), 1724 NewBldVec); 1725 } 1726 1727 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry, 1728 DenseMap<unsigned, unsigned> &RemapSwizzle) { 1729 assert(RemapSwizzle.empty()); 1730 1731 SDLoc DL(VectorEntry); 1732 EVT EltTy = VectorEntry.getValueType().getVectorElementType(); 1733 1734 SDValue NewBldVec[4]; 1735 bool isUnmovable[4] = {false, false, false, false}; 1736 for (unsigned i = 0; i < 4; i++) 1737 NewBldVec[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltTy, VectorEntry, 1738 DAG.getIntPtrConstant(i, DL)); 1739 1740 for (unsigned i = 0; i < 4; i++) { 1741 RemapSwizzle[i] = i; 1742 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 1743 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1)) 1744 ->getZExtValue(); 1745 if (i == Idx) 1746 isUnmovable[Idx] = true; 1747 } 1748 } 1749 1750 for (unsigned i = 0; i < 4; i++) { 1751 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 1752 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1)) 1753 ->getZExtValue(); 1754 if (isUnmovable[Idx]) 1755 continue; 1756 // Swap i and Idx 1757 std::swap(NewBldVec[Idx], NewBldVec[i]); 1758 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]); 1759 break; 1760 } 1761 } 1762 1763 return DAG.getBuildVector(VectorEntry.getValueType(), SDLoc(VectorEntry), 1764 NewBldVec); 1765 } 1766 1767 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector, SDValue Swz[4], 1768 SelectionDAG &DAG, 1769 const SDLoc &DL) const { 1770 // Old -> New swizzle values 1771 DenseMap<unsigned, unsigned> SwizzleRemap; 1772 1773 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap); 1774 for (unsigned i = 0; i < 4; i++) { 1775 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue(); 1776 if (SwizzleRemap.find(Idx) != SwizzleRemap.end()) 1777 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32); 1778 } 1779 1780 SwizzleRemap.clear(); 1781 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap); 1782 for (unsigned i = 0; i < 4; i++) { 1783 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue(); 1784 if (SwizzleRemap.find(Idx) != SwizzleRemap.end()) 1785 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32); 1786 } 1787 1788 return BuildVector; 1789 } 1790 1791 SDValue R600TargetLowering::constBufferLoad(LoadSDNode *LoadNode, int Block, 1792 SelectionDAG &DAG) const { 1793 SDLoc DL(LoadNode); 1794 EVT VT = LoadNode->getValueType(0); 1795 SDValue Chain = LoadNode->getChain(); 1796 SDValue Ptr = LoadNode->getBasePtr(); 1797 assert (isa<ConstantSDNode>(Ptr)); 1798 1799 //TODO: Support smaller loads 1800 if (LoadNode->getMemoryVT().getScalarType() != MVT::i32 || !ISD::isNON_EXTLoad(LoadNode)) 1801 return SDValue(); 1802 1803 if (LoadNode->getAlignment() < 4) 1804 return SDValue(); 1805 1806 int ConstantBlock = ConstantAddressBlock(Block); 1807 1808 SDValue Slots[4]; 1809 for (unsigned i = 0; i < 4; i++) { 1810 // We want Const position encoded with the following formula : 1811 // (((512 + (kc_bank << 12) + const_index) << 2) + chan) 1812 // const_index is Ptr computed by llvm using an alignment of 16. 1813 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and 1814 // then div by 4 at the ISel step 1815 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr, 1816 DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32)); 1817 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr); 1818 } 1819 EVT NewVT = MVT::v4i32; 1820 unsigned NumElements = 4; 1821 if (VT.isVector()) { 1822 NewVT = VT; 1823 NumElements = VT.getVectorNumElements(); 1824 } 1825 SDValue Result = DAG.getBuildVector(NewVT, DL, makeArrayRef(Slots, NumElements)); 1826 if (!VT.isVector()) { 1827 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result, 1828 DAG.getConstant(0, DL, MVT::i32)); 1829 } 1830 SDValue MergedValues[2] = { 1831 Result, 1832 Chain 1833 }; 1834 return DAG.getMergeValues(MergedValues, DL); 1835 } 1836 1837 //===----------------------------------------------------------------------===// 1838 // Custom DAG Optimizations 1839 //===----------------------------------------------------------------------===// 1840 1841 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N, 1842 DAGCombinerInfo &DCI) const { 1843 SelectionDAG &DAG = DCI.DAG; 1844 SDLoc DL(N); 1845 1846 switch (N->getOpcode()) { 1847 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a) 1848 case ISD::FP_ROUND: { 1849 SDValue Arg = N->getOperand(0); 1850 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) { 1851 return DAG.getNode(ISD::UINT_TO_FP, DL, N->getValueType(0), 1852 Arg.getOperand(0)); 1853 } 1854 break; 1855 } 1856 1857 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) -> 1858 // (i32 select_cc f32, f32, -1, 0 cc) 1859 // 1860 // Mesa's GLSL frontend generates the above pattern a lot and we can lower 1861 // this to one of the SET*_DX10 instructions. 1862 case ISD::FP_TO_SINT: { 1863 SDValue FNeg = N->getOperand(0); 1864 if (FNeg.getOpcode() != ISD::FNEG) { 1865 return SDValue(); 1866 } 1867 SDValue SelectCC = FNeg.getOperand(0); 1868 if (SelectCC.getOpcode() != ISD::SELECT_CC || 1869 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS 1870 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True 1871 !isHWTrueValue(SelectCC.getOperand(2)) || 1872 !isHWFalseValue(SelectCC.getOperand(3))) { 1873 return SDValue(); 1874 } 1875 1876 return DAG.getNode(ISD::SELECT_CC, DL, N->getValueType(0), 1877 SelectCC.getOperand(0), // LHS 1878 SelectCC.getOperand(1), // RHS 1879 DAG.getConstant(-1, DL, MVT::i32), // True 1880 DAG.getConstant(0, DL, MVT::i32), // False 1881 SelectCC.getOperand(4)); // CC 1882 } 1883 1884 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx 1885 // => build_vector elt0, ... , NewEltIdx, ... , eltN 1886 case ISD::INSERT_VECTOR_ELT: { 1887 SDValue InVec = N->getOperand(0); 1888 SDValue InVal = N->getOperand(1); 1889 SDValue EltNo = N->getOperand(2); 1890 1891 // If the inserted element is an UNDEF, just use the input vector. 1892 if (InVal.isUndef()) 1893 return InVec; 1894 1895 EVT VT = InVec.getValueType(); 1896 1897 // If we can't generate a legal BUILD_VECTOR, exit 1898 if (!isOperationLegal(ISD::BUILD_VECTOR, VT)) 1899 return SDValue(); 1900 1901 // Check that we know which element is being inserted 1902 if (!isa<ConstantSDNode>(EltNo)) 1903 return SDValue(); 1904 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); 1905 1906 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially 1907 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the 1908 // vector elements. 1909 SmallVector<SDValue, 8> Ops; 1910 if (InVec.getOpcode() == ISD::BUILD_VECTOR) { 1911 Ops.append(InVec.getNode()->op_begin(), 1912 InVec.getNode()->op_end()); 1913 } else if (InVec.isUndef()) { 1914 unsigned NElts = VT.getVectorNumElements(); 1915 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); 1916 } else { 1917 return SDValue(); 1918 } 1919 1920 // Insert the element 1921 if (Elt < Ops.size()) { 1922 // All the operands of BUILD_VECTOR must have the same type; 1923 // we enforce that here. 1924 EVT OpVT = Ops[0].getValueType(); 1925 if (InVal.getValueType() != OpVT) 1926 InVal = OpVT.bitsGT(InVal.getValueType()) ? 1927 DAG.getNode(ISD::ANY_EXTEND, DL, OpVT, InVal) : 1928 DAG.getNode(ISD::TRUNCATE, DL, OpVT, InVal); 1929 Ops[Elt] = InVal; 1930 } 1931 1932 // Return the new vector 1933 return DAG.getBuildVector(VT, DL, Ops); 1934 } 1935 1936 // Extract_vec (Build_vector) generated by custom lowering 1937 // also needs to be customly combined 1938 case ISD::EXTRACT_VECTOR_ELT: { 1939 SDValue Arg = N->getOperand(0); 1940 if (Arg.getOpcode() == ISD::BUILD_VECTOR) { 1941 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 1942 unsigned Element = Const->getZExtValue(); 1943 return Arg->getOperand(Element); 1944 } 1945 } 1946 if (Arg.getOpcode() == ISD::BITCAST && 1947 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && 1948 (Arg.getOperand(0).getValueType().getVectorNumElements() == 1949 Arg.getValueType().getVectorNumElements())) { 1950 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 1951 unsigned Element = Const->getZExtValue(); 1952 return DAG.getNode(ISD::BITCAST, DL, N->getVTList(), 1953 Arg->getOperand(0).getOperand(Element)); 1954 } 1955 } 1956 break; 1957 } 1958 1959 case ISD::SELECT_CC: { 1960 // Try common optimizations 1961 if (SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI)) 1962 return Ret; 1963 1964 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq -> 1965 // selectcc x, y, a, b, inv(cc) 1966 // 1967 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne -> 1968 // selectcc x, y, a, b, cc 1969 SDValue LHS = N->getOperand(0); 1970 if (LHS.getOpcode() != ISD::SELECT_CC) { 1971 return SDValue(); 1972 } 1973 1974 SDValue RHS = N->getOperand(1); 1975 SDValue True = N->getOperand(2); 1976 SDValue False = N->getOperand(3); 1977 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 1978 1979 if (LHS.getOperand(2).getNode() != True.getNode() || 1980 LHS.getOperand(3).getNode() != False.getNode() || 1981 RHS.getNode() != False.getNode()) { 1982 return SDValue(); 1983 } 1984 1985 switch (NCC) { 1986 default: return SDValue(); 1987 case ISD::SETNE: return LHS; 1988 case ISD::SETEQ: { 1989 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get(); 1990 LHSCC = ISD::getSetCCInverse(LHSCC, LHS.getOperand(0).getValueType()); 1991 if (DCI.isBeforeLegalizeOps() || 1992 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType())) 1993 return DAG.getSelectCC(DL, 1994 LHS.getOperand(0), 1995 LHS.getOperand(1), 1996 LHS.getOperand(2), 1997 LHS.getOperand(3), 1998 LHSCC); 1999 break; 2000 } 2001 } 2002 return SDValue(); 2003 } 2004 2005 case AMDGPUISD::R600_EXPORT: { 2006 SDValue Arg = N->getOperand(1); 2007 if (Arg.getOpcode() != ISD::BUILD_VECTOR) 2008 break; 2009 2010 SDValue NewArgs[8] = { 2011 N->getOperand(0), // Chain 2012 SDValue(), 2013 N->getOperand(2), // ArrayBase 2014 N->getOperand(3), // Type 2015 N->getOperand(4), // SWZ_X 2016 N->getOperand(5), // SWZ_Y 2017 N->getOperand(6), // SWZ_Z 2018 N->getOperand(7) // SWZ_W 2019 }; 2020 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL); 2021 return DAG.getNode(AMDGPUISD::R600_EXPORT, DL, N->getVTList(), NewArgs); 2022 } 2023 case AMDGPUISD::TEXTURE_FETCH: { 2024 SDValue Arg = N->getOperand(1); 2025 if (Arg.getOpcode() != ISD::BUILD_VECTOR) 2026 break; 2027 2028 SDValue NewArgs[19] = { 2029 N->getOperand(0), 2030 N->getOperand(1), 2031 N->getOperand(2), 2032 N->getOperand(3), 2033 N->getOperand(4), 2034 N->getOperand(5), 2035 N->getOperand(6), 2036 N->getOperand(7), 2037 N->getOperand(8), 2038 N->getOperand(9), 2039 N->getOperand(10), 2040 N->getOperand(11), 2041 N->getOperand(12), 2042 N->getOperand(13), 2043 N->getOperand(14), 2044 N->getOperand(15), 2045 N->getOperand(16), 2046 N->getOperand(17), 2047 N->getOperand(18), 2048 }; 2049 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL); 2050 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs); 2051 } 2052 2053 case ISD::LOAD: { 2054 LoadSDNode *LoadNode = cast<LoadSDNode>(N); 2055 SDValue Ptr = LoadNode->getBasePtr(); 2056 if (LoadNode->getAddressSpace() == AMDGPUAS::PARAM_I_ADDRESS && 2057 isa<ConstantSDNode>(Ptr)) 2058 return constBufferLoad(LoadNode, AMDGPUAS::CONSTANT_BUFFER_0, DAG); 2059 break; 2060 } 2061 2062 default: break; 2063 } 2064 2065 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 2066 } 2067 2068 bool R600TargetLowering::FoldOperand(SDNode *ParentNode, unsigned SrcIdx, 2069 SDValue &Src, SDValue &Neg, SDValue &Abs, 2070 SDValue &Sel, SDValue &Imm, 2071 SelectionDAG &DAG) const { 2072 const R600InstrInfo *TII = Subtarget->getInstrInfo(); 2073 if (!Src.isMachineOpcode()) 2074 return false; 2075 2076 switch (Src.getMachineOpcode()) { 2077 case R600::FNEG_R600: 2078 if (!Neg.getNode()) 2079 return false; 2080 Src = Src.getOperand(0); 2081 Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32); 2082 return true; 2083 case R600::FABS_R600: 2084 if (!Abs.getNode()) 2085 return false; 2086 Src = Src.getOperand(0); 2087 Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32); 2088 return true; 2089 case R600::CONST_COPY: { 2090 unsigned Opcode = ParentNode->getMachineOpcode(); 2091 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1; 2092 2093 if (!Sel.getNode()) 2094 return false; 2095 2096 SDValue CstOffset = Src.getOperand(0); 2097 if (ParentNode->getValueType(0).isVector()) 2098 return false; 2099 2100 // Gather constants values 2101 int SrcIndices[] = { 2102 TII->getOperandIdx(Opcode, R600::OpName::src0), 2103 TII->getOperandIdx(Opcode, R600::OpName::src1), 2104 TII->getOperandIdx(Opcode, R600::OpName::src2), 2105 TII->getOperandIdx(Opcode, R600::OpName::src0_X), 2106 TII->getOperandIdx(Opcode, R600::OpName::src0_Y), 2107 TII->getOperandIdx(Opcode, R600::OpName::src0_Z), 2108 TII->getOperandIdx(Opcode, R600::OpName::src0_W), 2109 TII->getOperandIdx(Opcode, R600::OpName::src1_X), 2110 TII->getOperandIdx(Opcode, R600::OpName::src1_Y), 2111 TII->getOperandIdx(Opcode, R600::OpName::src1_Z), 2112 TII->getOperandIdx(Opcode, R600::OpName::src1_W) 2113 }; 2114 std::vector<unsigned> Consts; 2115 for (int OtherSrcIdx : SrcIndices) { 2116 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx); 2117 if (OtherSrcIdx < 0 || OtherSelIdx < 0) 2118 continue; 2119 if (HasDst) { 2120 OtherSrcIdx--; 2121 OtherSelIdx--; 2122 } 2123 if (RegisterSDNode *Reg = 2124 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) { 2125 if (Reg->getReg() == R600::ALU_CONST) { 2126 ConstantSDNode *Cst 2127 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx)); 2128 Consts.push_back(Cst->getZExtValue()); 2129 } 2130 } 2131 } 2132 2133 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset); 2134 Consts.push_back(Cst->getZExtValue()); 2135 if (!TII->fitsConstReadLimitations(Consts)) { 2136 return false; 2137 } 2138 2139 Sel = CstOffset; 2140 Src = DAG.getRegister(R600::ALU_CONST, MVT::f32); 2141 return true; 2142 } 2143 case R600::MOV_IMM_GLOBAL_ADDR: 2144 // Check if the Imm slot is used. Taken from below. 2145 if (cast<ConstantSDNode>(Imm)->getZExtValue()) 2146 return false; 2147 Imm = Src.getOperand(0); 2148 Src = DAG.getRegister(R600::ALU_LITERAL_X, MVT::i32); 2149 return true; 2150 case R600::MOV_IMM_I32: 2151 case R600::MOV_IMM_F32: { 2152 unsigned ImmReg = R600::ALU_LITERAL_X; 2153 uint64_t ImmValue = 0; 2154 2155 if (Src.getMachineOpcode() == R600::MOV_IMM_F32) { 2156 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0)); 2157 float FloatValue = FPC->getValueAPF().convertToFloat(); 2158 if (FloatValue == 0.0) { 2159 ImmReg = R600::ZERO; 2160 } else if (FloatValue == 0.5) { 2161 ImmReg = R600::HALF; 2162 } else if (FloatValue == 1.0) { 2163 ImmReg = R600::ONE; 2164 } else { 2165 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue(); 2166 } 2167 } else { 2168 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0)); 2169 uint64_t Value = C->getZExtValue(); 2170 if (Value == 0) { 2171 ImmReg = R600::ZERO; 2172 } else if (Value == 1) { 2173 ImmReg = R600::ONE_INT; 2174 } else { 2175 ImmValue = Value; 2176 } 2177 } 2178 2179 // Check that we aren't already using an immediate. 2180 // XXX: It's possible for an instruction to have more than one 2181 // immediate operand, but this is not supported yet. 2182 if (ImmReg == R600::ALU_LITERAL_X) { 2183 if (!Imm.getNode()) 2184 return false; 2185 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm); 2186 assert(C); 2187 if (C->getZExtValue()) 2188 return false; 2189 Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32); 2190 } 2191 Src = DAG.getRegister(ImmReg, MVT::i32); 2192 return true; 2193 } 2194 default: 2195 return false; 2196 } 2197 } 2198 2199 /// Fold the instructions after selecting them 2200 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node, 2201 SelectionDAG &DAG) const { 2202 const R600InstrInfo *TII = Subtarget->getInstrInfo(); 2203 if (!Node->isMachineOpcode()) 2204 return Node; 2205 2206 unsigned Opcode = Node->getMachineOpcode(); 2207 SDValue FakeOp; 2208 2209 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end()); 2210 2211 if (Opcode == R600::DOT_4) { 2212 int OperandIdx[] = { 2213 TII->getOperandIdx(Opcode, R600::OpName::src0_X), 2214 TII->getOperandIdx(Opcode, R600::OpName::src0_Y), 2215 TII->getOperandIdx(Opcode, R600::OpName::src0_Z), 2216 TII->getOperandIdx(Opcode, R600::OpName::src0_W), 2217 TII->getOperandIdx(Opcode, R600::OpName::src1_X), 2218 TII->getOperandIdx(Opcode, R600::OpName::src1_Y), 2219 TII->getOperandIdx(Opcode, R600::OpName::src1_Z), 2220 TII->getOperandIdx(Opcode, R600::OpName::src1_W) 2221 }; 2222 int NegIdx[] = { 2223 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_X), 2224 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Y), 2225 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_Z), 2226 TII->getOperandIdx(Opcode, R600::OpName::src0_neg_W), 2227 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_X), 2228 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Y), 2229 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_Z), 2230 TII->getOperandIdx(Opcode, R600::OpName::src1_neg_W) 2231 }; 2232 int AbsIdx[] = { 2233 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_X), 2234 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Y), 2235 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_Z), 2236 TII->getOperandIdx(Opcode, R600::OpName::src0_abs_W), 2237 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_X), 2238 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Y), 2239 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_Z), 2240 TII->getOperandIdx(Opcode, R600::OpName::src1_abs_W) 2241 }; 2242 for (unsigned i = 0; i < 8; i++) { 2243 if (OperandIdx[i] < 0) 2244 return Node; 2245 SDValue &Src = Ops[OperandIdx[i] - 1]; 2246 SDValue &Neg = Ops[NegIdx[i] - 1]; 2247 SDValue &Abs = Ops[AbsIdx[i] - 1]; 2248 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1; 2249 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]); 2250 if (HasDst) 2251 SelIdx--; 2252 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp; 2253 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG)) 2254 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops); 2255 } 2256 } else if (Opcode == R600::REG_SEQUENCE) { 2257 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) { 2258 SDValue &Src = Ops[i]; 2259 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG)) 2260 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops); 2261 } 2262 } else { 2263 if (!TII->hasInstrModifiers(Opcode)) 2264 return Node; 2265 int OperandIdx[] = { 2266 TII->getOperandIdx(Opcode, R600::OpName::src0), 2267 TII->getOperandIdx(Opcode, R600::OpName::src1), 2268 TII->getOperandIdx(Opcode, R600::OpName::src2) 2269 }; 2270 int NegIdx[] = { 2271 TII->getOperandIdx(Opcode, R600::OpName::src0_neg), 2272 TII->getOperandIdx(Opcode, R600::OpName::src1_neg), 2273 TII->getOperandIdx(Opcode, R600::OpName::src2_neg) 2274 }; 2275 int AbsIdx[] = { 2276 TII->getOperandIdx(Opcode, R600::OpName::src0_abs), 2277 TII->getOperandIdx(Opcode, R600::OpName::src1_abs), 2278 -1 2279 }; 2280 for (unsigned i = 0; i < 3; i++) { 2281 if (OperandIdx[i] < 0) 2282 return Node; 2283 SDValue &Src = Ops[OperandIdx[i] - 1]; 2284 SDValue &Neg = Ops[NegIdx[i] - 1]; 2285 SDValue FakeAbs; 2286 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs; 2287 bool HasDst = TII->getOperandIdx(Opcode, R600::OpName::dst) > -1; 2288 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]); 2289 int ImmIdx = TII->getOperandIdx(Opcode, R600::OpName::literal); 2290 if (HasDst) { 2291 SelIdx--; 2292 ImmIdx--; 2293 } 2294 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp; 2295 SDValue &Imm = Ops[ImmIdx]; 2296 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG)) 2297 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops); 2298 } 2299 } 2300 2301 return Node; 2302 } 2303