181ad6265SDimitry Andric //===-- SPIRVInstrInfo.cpp - SPIR-V Instruction Information ------*- C++-*-===// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric // 981ad6265SDimitry Andric // This file contains the SPIR-V implementation of the TargetInstrInfo class. 1081ad6265SDimitry Andric // 1181ad6265SDimitry Andric //===----------------------------------------------------------------------===// 1281ad6265SDimitry Andric 1381ad6265SDimitry Andric #include "SPIRVInstrInfo.h" 1481ad6265SDimitry Andric #include "SPIRV.h" 1581ad6265SDimitry Andric #include "llvm/ADT/SmallVector.h" 1681ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 1781ad6265SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 1881ad6265SDimitry Andric #include "llvm/IR/DebugLoc.h" 1981ad6265SDimitry Andric #include "llvm/Support/ErrorHandling.h" 2081ad6265SDimitry Andric 2181ad6265SDimitry Andric #define GET_INSTRINFO_CTOR_DTOR 2281ad6265SDimitry Andric #include "SPIRVGenInstrInfo.inc" 2381ad6265SDimitry Andric 2481ad6265SDimitry Andric using namespace llvm; 2581ad6265SDimitry Andric 2681ad6265SDimitry Andric SPIRVInstrInfo::SPIRVInstrInfo() : SPIRVGenInstrInfo() {} 2781ad6265SDimitry Andric 2881ad6265SDimitry Andric bool SPIRVInstrInfo::isConstantInstr(const MachineInstr &MI) const { 2981ad6265SDimitry Andric switch (MI.getOpcode()) { 3081ad6265SDimitry Andric case SPIRV::OpConstantTrue: 3181ad6265SDimitry Andric case SPIRV::OpConstantFalse: 3281ad6265SDimitry Andric case SPIRV::OpConstantI: 3381ad6265SDimitry Andric case SPIRV::OpConstantF: 3481ad6265SDimitry Andric case SPIRV::OpConstantComposite: 3581ad6265SDimitry Andric case SPIRV::OpConstantSampler: 3681ad6265SDimitry Andric case SPIRV::OpConstantNull: 3781ad6265SDimitry Andric case SPIRV::OpSpecConstantTrue: 3881ad6265SDimitry Andric case SPIRV::OpSpecConstantFalse: 3981ad6265SDimitry Andric case SPIRV::OpSpecConstant: 4081ad6265SDimitry Andric case SPIRV::OpSpecConstantComposite: 4181ad6265SDimitry Andric case SPIRV::OpSpecConstantOp: 4281ad6265SDimitry Andric case SPIRV::OpUndef: 4381ad6265SDimitry Andric return true; 4481ad6265SDimitry Andric default: 4581ad6265SDimitry Andric return false; 4681ad6265SDimitry Andric } 4781ad6265SDimitry Andric } 4881ad6265SDimitry Andric 4981ad6265SDimitry Andric bool SPIRVInstrInfo::isTypeDeclInstr(const MachineInstr &MI) const { 5081ad6265SDimitry Andric auto &MRI = MI.getMF()->getRegInfo(); 5181ad6265SDimitry Andric if (MI.getNumDefs() >= 1 && MI.getOperand(0).isReg()) { 5281ad6265SDimitry Andric auto DefRegClass = MRI.getRegClassOrNull(MI.getOperand(0).getReg()); 5381ad6265SDimitry Andric return DefRegClass && DefRegClass->getID() == SPIRV::TYPERegClass.getID(); 5481ad6265SDimitry Andric } else { 55fcaf7f86SDimitry Andric return MI.getOpcode() == SPIRV::OpTypeForwardPointer; 5681ad6265SDimitry Andric } 5781ad6265SDimitry Andric } 5881ad6265SDimitry Andric 5981ad6265SDimitry Andric bool SPIRVInstrInfo::isDecorationInstr(const MachineInstr &MI) const { 6081ad6265SDimitry Andric switch (MI.getOpcode()) { 6181ad6265SDimitry Andric case SPIRV::OpDecorate: 6281ad6265SDimitry Andric case SPIRV::OpDecorateId: 6381ad6265SDimitry Andric case SPIRV::OpDecorateString: 6481ad6265SDimitry Andric case SPIRV::OpMemberDecorate: 6581ad6265SDimitry Andric case SPIRV::OpMemberDecorateString: 6681ad6265SDimitry Andric return true; 6781ad6265SDimitry Andric default: 6881ad6265SDimitry Andric return false; 6981ad6265SDimitry Andric } 7081ad6265SDimitry Andric } 7181ad6265SDimitry Andric 7281ad6265SDimitry Andric bool SPIRVInstrInfo::isHeaderInstr(const MachineInstr &MI) const { 7381ad6265SDimitry Andric switch (MI.getOpcode()) { 7481ad6265SDimitry Andric case SPIRV::OpCapability: 7581ad6265SDimitry Andric case SPIRV::OpExtension: 7681ad6265SDimitry Andric case SPIRV::OpExtInstImport: 7781ad6265SDimitry Andric case SPIRV::OpMemoryModel: 7881ad6265SDimitry Andric case SPIRV::OpEntryPoint: 7981ad6265SDimitry Andric case SPIRV::OpExecutionMode: 8081ad6265SDimitry Andric case SPIRV::OpExecutionModeId: 8181ad6265SDimitry Andric case SPIRV::OpString: 8281ad6265SDimitry Andric case SPIRV::OpSourceExtension: 8381ad6265SDimitry Andric case SPIRV::OpSource: 8481ad6265SDimitry Andric case SPIRV::OpSourceContinued: 8581ad6265SDimitry Andric case SPIRV::OpName: 8681ad6265SDimitry Andric case SPIRV::OpMemberName: 8781ad6265SDimitry Andric case SPIRV::OpModuleProcessed: 8881ad6265SDimitry Andric return true; 8981ad6265SDimitry Andric default: 9081ad6265SDimitry Andric return isTypeDeclInstr(MI) || isConstantInstr(MI) || isDecorationInstr(MI); 9181ad6265SDimitry Andric } 9281ad6265SDimitry Andric } 9381ad6265SDimitry Andric 94*bdd1243dSDimitry Andric bool SPIRVInstrInfo::canUseFastMathFlags(const MachineInstr &MI) const { 95*bdd1243dSDimitry Andric switch (MI.getOpcode()) { 96*bdd1243dSDimitry Andric case SPIRV::OpFAddS: 97*bdd1243dSDimitry Andric case SPIRV::OpFSubS: 98*bdd1243dSDimitry Andric case SPIRV::OpFMulS: 99*bdd1243dSDimitry Andric case SPIRV::OpFDivS: 100*bdd1243dSDimitry Andric case SPIRV::OpFRemS: 101*bdd1243dSDimitry Andric case SPIRV::OpFAddV: 102*bdd1243dSDimitry Andric case SPIRV::OpFSubV: 103*bdd1243dSDimitry Andric case SPIRV::OpFMulV: 104*bdd1243dSDimitry Andric case SPIRV::OpFDivV: 105*bdd1243dSDimitry Andric case SPIRV::OpFRemV: 106*bdd1243dSDimitry Andric case SPIRV::OpFMod: 107*bdd1243dSDimitry Andric return true; 108*bdd1243dSDimitry Andric default: 109*bdd1243dSDimitry Andric return false; 110*bdd1243dSDimitry Andric } 111*bdd1243dSDimitry Andric } 112*bdd1243dSDimitry Andric 113*bdd1243dSDimitry Andric bool SPIRVInstrInfo::canUseNSW(const MachineInstr &MI) const { 114*bdd1243dSDimitry Andric switch (MI.getOpcode()) { 115*bdd1243dSDimitry Andric case SPIRV::OpIAddS: 116*bdd1243dSDimitry Andric case SPIRV::OpIAddV: 117*bdd1243dSDimitry Andric case SPIRV::OpISubS: 118*bdd1243dSDimitry Andric case SPIRV::OpISubV: 119*bdd1243dSDimitry Andric case SPIRV::OpIMulS: 120*bdd1243dSDimitry Andric case SPIRV::OpIMulV: 121*bdd1243dSDimitry Andric case SPIRV::OpShiftLeftLogicalS: 122*bdd1243dSDimitry Andric case SPIRV::OpShiftLeftLogicalV: 123*bdd1243dSDimitry Andric case SPIRV::OpSNegate: 124*bdd1243dSDimitry Andric return true; 125*bdd1243dSDimitry Andric default: 126*bdd1243dSDimitry Andric return false; 127*bdd1243dSDimitry Andric } 128*bdd1243dSDimitry Andric } 129*bdd1243dSDimitry Andric 130*bdd1243dSDimitry Andric bool SPIRVInstrInfo::canUseNUW(const MachineInstr &MI) const { 131*bdd1243dSDimitry Andric switch (MI.getOpcode()) { 132*bdd1243dSDimitry Andric case SPIRV::OpIAddS: 133*bdd1243dSDimitry Andric case SPIRV::OpIAddV: 134*bdd1243dSDimitry Andric case SPIRV::OpISubS: 135*bdd1243dSDimitry Andric case SPIRV::OpISubV: 136*bdd1243dSDimitry Andric case SPIRV::OpIMulS: 137*bdd1243dSDimitry Andric case SPIRV::OpIMulV: 138*bdd1243dSDimitry Andric return true; 139*bdd1243dSDimitry Andric default: 140*bdd1243dSDimitry Andric return false; 141*bdd1243dSDimitry Andric } 142*bdd1243dSDimitry Andric } 143*bdd1243dSDimitry Andric 14481ad6265SDimitry Andric // Analyze the branching code at the end of MBB, returning 14581ad6265SDimitry Andric // true if it cannot be understood (e.g. it's a switch dispatch or isn't 14681ad6265SDimitry Andric // implemented for a target). Upon success, this returns false and returns 14781ad6265SDimitry Andric // with the following information in various cases: 14881ad6265SDimitry Andric // 14981ad6265SDimitry Andric // 1. If this block ends with no branches (it just falls through to its succ) 15081ad6265SDimitry Andric // just return false, leaving TBB/FBB null. 15181ad6265SDimitry Andric // 2. If this block ends with only an unconditional branch, it sets TBB to be 15281ad6265SDimitry Andric // the destination block. 15381ad6265SDimitry Andric // 3. If this block ends with a conditional branch and it falls through to a 15481ad6265SDimitry Andric // successor block, it sets TBB to be the branch destination block and a 15581ad6265SDimitry Andric // list of operands that evaluate the condition. These operands can be 15681ad6265SDimitry Andric // passed to other TargetInstrInfo methods to create new branches. 15781ad6265SDimitry Andric // 4. If this block ends with a conditional branch followed by an 15881ad6265SDimitry Andric // unconditional branch, it returns the 'true' destination in TBB, the 15981ad6265SDimitry Andric // 'false' destination in FBB, and a list of operands that evaluate the 16081ad6265SDimitry Andric // condition. These operands can be passed to other TargetInstrInfo 16181ad6265SDimitry Andric // methods to create new branches. 16281ad6265SDimitry Andric // 16381ad6265SDimitry Andric // Note that removeBranch and insertBranch must be implemented to support 16481ad6265SDimitry Andric // cases where this method returns success. 16581ad6265SDimitry Andric // 16681ad6265SDimitry Andric // If AllowModify is true, then this routine is allowed to modify the basic 16781ad6265SDimitry Andric // block (e.g. delete instructions after the unconditional branch). 16881ad6265SDimitry Andric // 16981ad6265SDimitry Andric // The CFG information in MBB.Predecessors and MBB.Successors must be valid 17081ad6265SDimitry Andric // before calling this function. 17181ad6265SDimitry Andric bool SPIRVInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 17281ad6265SDimitry Andric MachineBasicBlock *&TBB, 17381ad6265SDimitry Andric MachineBasicBlock *&FBB, 17481ad6265SDimitry Andric SmallVectorImpl<MachineOperand> &Cond, 17581ad6265SDimitry Andric bool AllowModify) const { 17681ad6265SDimitry Andric TBB = nullptr; 17781ad6265SDimitry Andric FBB = nullptr; 17881ad6265SDimitry Andric if (MBB.empty()) 17981ad6265SDimitry Andric return false; 18081ad6265SDimitry Andric auto MI = MBB.getLastNonDebugInstr(); 18181ad6265SDimitry Andric if (!MI.isValid()) 18281ad6265SDimitry Andric return false; 18381ad6265SDimitry Andric if (MI->getOpcode() == SPIRV::OpBranch) { 18481ad6265SDimitry Andric TBB = MI->getOperand(0).getMBB(); 18581ad6265SDimitry Andric return false; 18681ad6265SDimitry Andric } else if (MI->getOpcode() == SPIRV::OpBranchConditional) { 18781ad6265SDimitry Andric Cond.push_back(MI->getOperand(0)); 18881ad6265SDimitry Andric TBB = MI->getOperand(1).getMBB(); 18981ad6265SDimitry Andric if (MI->getNumOperands() == 3) { 19081ad6265SDimitry Andric FBB = MI->getOperand(2).getMBB(); 19181ad6265SDimitry Andric } 19281ad6265SDimitry Andric return false; 19381ad6265SDimitry Andric } else { 19481ad6265SDimitry Andric return true; 19581ad6265SDimitry Andric } 19681ad6265SDimitry Andric } 19781ad6265SDimitry Andric 19881ad6265SDimitry Andric // Remove the branching code at the end of the specific MBB. 19981ad6265SDimitry Andric // This is only invoked in cases where analyzeBranch returns success. It 20081ad6265SDimitry Andric // returns the number of instructions that were removed. 20181ad6265SDimitry Andric // If \p BytesRemoved is non-null, report the change in code size from the 20281ad6265SDimitry Andric // removed instructions. 20381ad6265SDimitry Andric unsigned SPIRVInstrInfo::removeBranch(MachineBasicBlock &MBB, 20481ad6265SDimitry Andric int *BytesRemoved) const { 20581ad6265SDimitry Andric report_fatal_error("Branch removal not supported, as MBB info not propagated" 20681ad6265SDimitry Andric " to OpPhi instructions. Try using -O0 instead."); 20781ad6265SDimitry Andric } 20881ad6265SDimitry Andric 20981ad6265SDimitry Andric // Insert branch code into the end of the specified MachineBasicBlock. The 21081ad6265SDimitry Andric // operands to this method are the same as those returned by analyzeBranch. 21181ad6265SDimitry Andric // This is only invoked in cases where analyzeBranch returns success. It 21281ad6265SDimitry Andric // returns the number of instructions inserted. If \p BytesAdded is non-null, 21381ad6265SDimitry Andric // report the change in code size from the added instructions. 21481ad6265SDimitry Andric // 21581ad6265SDimitry Andric // It is also invoked by tail merging to add unconditional branches in 21681ad6265SDimitry Andric // cases where analyzeBranch doesn't apply because there was no original 21781ad6265SDimitry Andric // branch to analyze. At least this much must be implemented, else tail 21881ad6265SDimitry Andric // merging needs to be disabled. 21981ad6265SDimitry Andric // 22081ad6265SDimitry Andric // The CFG information in MBB.Predecessors and MBB.Successors must be valid 22181ad6265SDimitry Andric // before calling this function. 22281ad6265SDimitry Andric unsigned SPIRVInstrInfo::insertBranch( 22381ad6265SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, 22481ad6265SDimitry Andric ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { 22581ad6265SDimitry Andric report_fatal_error("Branch insertion not supported, as MBB info not " 22681ad6265SDimitry Andric "propagated to OpPhi instructions. Try using " 22781ad6265SDimitry Andric "-O0 instead."); 22881ad6265SDimitry Andric } 22981ad6265SDimitry Andric 23081ad6265SDimitry Andric void SPIRVInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 23181ad6265SDimitry Andric MachineBasicBlock::iterator I, 23281ad6265SDimitry Andric const DebugLoc &DL, MCRegister DestReg, 23381ad6265SDimitry Andric MCRegister SrcReg, bool KillSrc) const { 23481ad6265SDimitry Andric // Actually we don't need this COPY instruction. However if we do nothing with 23581ad6265SDimitry Andric // it, post RA pseudo instrs expansion just removes it and we get the code 23681ad6265SDimitry Andric // with undef registers. Therefore, we need to replace all uses of dst with 23781ad6265SDimitry Andric // the src register. COPY instr itself will be safely removed later. 23881ad6265SDimitry Andric assert(I->isCopy() && "Copy instruction is expected"); 23981ad6265SDimitry Andric auto DstOp = I->getOperand(0); 24081ad6265SDimitry Andric auto SrcOp = I->getOperand(1); 24181ad6265SDimitry Andric assert(DstOp.isReg() && SrcOp.isReg() && 24281ad6265SDimitry Andric "Register operands are expected in COPY"); 24381ad6265SDimitry Andric auto &MRI = I->getMF()->getRegInfo(); 24481ad6265SDimitry Andric MRI.replaceRegWith(DstOp.getReg(), SrcOp.getReg()); 24581ad6265SDimitry Andric } 246fcaf7f86SDimitry Andric 247fcaf7f86SDimitry Andric bool SPIRVInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 248fcaf7f86SDimitry Andric if (MI.getOpcode() == SPIRV::GET_ID || MI.getOpcode() == SPIRV::GET_fID || 249fcaf7f86SDimitry Andric MI.getOpcode() == SPIRV::GET_pID || MI.getOpcode() == SPIRV::GET_vfID || 250fcaf7f86SDimitry Andric MI.getOpcode() == SPIRV::GET_vID) { 251fcaf7f86SDimitry Andric auto &MRI = MI.getMF()->getRegInfo(); 252fcaf7f86SDimitry Andric MRI.replaceRegWith(MI.getOperand(0).getReg(), MI.getOperand(1).getReg()); 253fcaf7f86SDimitry Andric MI.eraseFromParent(); 254fcaf7f86SDimitry Andric return true; 255fcaf7f86SDimitry Andric } 256fcaf7f86SDimitry Andric return false; 257fcaf7f86SDimitry Andric } 258