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 { 55*fcaf7f86SDimitry 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 9481ad6265SDimitry Andric // Analyze the branching code at the end of MBB, returning 9581ad6265SDimitry Andric // true if it cannot be understood (e.g. it's a switch dispatch or isn't 9681ad6265SDimitry Andric // implemented for a target). Upon success, this returns false and returns 9781ad6265SDimitry Andric // with the following information in various cases: 9881ad6265SDimitry Andric // 9981ad6265SDimitry Andric // 1. If this block ends with no branches (it just falls through to its succ) 10081ad6265SDimitry Andric // just return false, leaving TBB/FBB null. 10181ad6265SDimitry Andric // 2. If this block ends with only an unconditional branch, it sets TBB to be 10281ad6265SDimitry Andric // the destination block. 10381ad6265SDimitry Andric // 3. If this block ends with a conditional branch and it falls through to a 10481ad6265SDimitry Andric // successor block, it sets TBB to be the branch destination block and a 10581ad6265SDimitry Andric // list of operands that evaluate the condition. These operands can be 10681ad6265SDimitry Andric // passed to other TargetInstrInfo methods to create new branches. 10781ad6265SDimitry Andric // 4. If this block ends with a conditional branch followed by an 10881ad6265SDimitry Andric // unconditional branch, it returns the 'true' destination in TBB, the 10981ad6265SDimitry Andric // 'false' destination in FBB, and a list of operands that evaluate the 11081ad6265SDimitry Andric // condition. These operands can be passed to other TargetInstrInfo 11181ad6265SDimitry Andric // methods to create new branches. 11281ad6265SDimitry Andric // 11381ad6265SDimitry Andric // Note that removeBranch and insertBranch must be implemented to support 11481ad6265SDimitry Andric // cases where this method returns success. 11581ad6265SDimitry Andric // 11681ad6265SDimitry Andric // If AllowModify is true, then this routine is allowed to modify the basic 11781ad6265SDimitry Andric // block (e.g. delete instructions after the unconditional branch). 11881ad6265SDimitry Andric // 11981ad6265SDimitry Andric // The CFG information in MBB.Predecessors and MBB.Successors must be valid 12081ad6265SDimitry Andric // before calling this function. 12181ad6265SDimitry Andric bool SPIRVInstrInfo::analyzeBranch(MachineBasicBlock &MBB, 12281ad6265SDimitry Andric MachineBasicBlock *&TBB, 12381ad6265SDimitry Andric MachineBasicBlock *&FBB, 12481ad6265SDimitry Andric SmallVectorImpl<MachineOperand> &Cond, 12581ad6265SDimitry Andric bool AllowModify) const { 12681ad6265SDimitry Andric TBB = nullptr; 12781ad6265SDimitry Andric FBB = nullptr; 12881ad6265SDimitry Andric if (MBB.empty()) 12981ad6265SDimitry Andric return false; 13081ad6265SDimitry Andric auto MI = MBB.getLastNonDebugInstr(); 13181ad6265SDimitry Andric if (!MI.isValid()) 13281ad6265SDimitry Andric return false; 13381ad6265SDimitry Andric if (MI->getOpcode() == SPIRV::OpBranch) { 13481ad6265SDimitry Andric TBB = MI->getOperand(0).getMBB(); 13581ad6265SDimitry Andric return false; 13681ad6265SDimitry Andric } else if (MI->getOpcode() == SPIRV::OpBranchConditional) { 13781ad6265SDimitry Andric Cond.push_back(MI->getOperand(0)); 13881ad6265SDimitry Andric TBB = MI->getOperand(1).getMBB(); 13981ad6265SDimitry Andric if (MI->getNumOperands() == 3) { 14081ad6265SDimitry Andric FBB = MI->getOperand(2).getMBB(); 14181ad6265SDimitry Andric } 14281ad6265SDimitry Andric return false; 14381ad6265SDimitry Andric } else { 14481ad6265SDimitry Andric return true; 14581ad6265SDimitry Andric } 14681ad6265SDimitry Andric } 14781ad6265SDimitry Andric 14881ad6265SDimitry Andric // Remove the branching code at the end of the specific MBB. 14981ad6265SDimitry Andric // This is only invoked in cases where analyzeBranch returns success. It 15081ad6265SDimitry Andric // returns the number of instructions that were removed. 15181ad6265SDimitry Andric // If \p BytesRemoved is non-null, report the change in code size from the 15281ad6265SDimitry Andric // removed instructions. 15381ad6265SDimitry Andric unsigned SPIRVInstrInfo::removeBranch(MachineBasicBlock &MBB, 15481ad6265SDimitry Andric int *BytesRemoved) const { 15581ad6265SDimitry Andric report_fatal_error("Branch removal not supported, as MBB info not propagated" 15681ad6265SDimitry Andric " to OpPhi instructions. Try using -O0 instead."); 15781ad6265SDimitry Andric } 15881ad6265SDimitry Andric 15981ad6265SDimitry Andric // Insert branch code into the end of the specified MachineBasicBlock. The 16081ad6265SDimitry Andric // operands to this method are the same as those returned by analyzeBranch. 16181ad6265SDimitry Andric // This is only invoked in cases where analyzeBranch returns success. It 16281ad6265SDimitry Andric // returns the number of instructions inserted. If \p BytesAdded is non-null, 16381ad6265SDimitry Andric // report the change in code size from the added instructions. 16481ad6265SDimitry Andric // 16581ad6265SDimitry Andric // It is also invoked by tail merging to add unconditional branches in 16681ad6265SDimitry Andric // cases where analyzeBranch doesn't apply because there was no original 16781ad6265SDimitry Andric // branch to analyze. At least this much must be implemented, else tail 16881ad6265SDimitry Andric // merging needs to be disabled. 16981ad6265SDimitry Andric // 17081ad6265SDimitry Andric // The CFG information in MBB.Predecessors and MBB.Successors must be valid 17181ad6265SDimitry Andric // before calling this function. 17281ad6265SDimitry Andric unsigned SPIRVInstrInfo::insertBranch( 17381ad6265SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, 17481ad6265SDimitry Andric ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { 17581ad6265SDimitry Andric report_fatal_error("Branch insertion not supported, as MBB info not " 17681ad6265SDimitry Andric "propagated to OpPhi instructions. Try using " 17781ad6265SDimitry Andric "-O0 instead."); 17881ad6265SDimitry Andric } 17981ad6265SDimitry Andric 18081ad6265SDimitry Andric void SPIRVInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 18181ad6265SDimitry Andric MachineBasicBlock::iterator I, 18281ad6265SDimitry Andric const DebugLoc &DL, MCRegister DestReg, 18381ad6265SDimitry Andric MCRegister SrcReg, bool KillSrc) const { 18481ad6265SDimitry Andric // Actually we don't need this COPY instruction. However if we do nothing with 18581ad6265SDimitry Andric // it, post RA pseudo instrs expansion just removes it and we get the code 18681ad6265SDimitry Andric // with undef registers. Therefore, we need to replace all uses of dst with 18781ad6265SDimitry Andric // the src register. COPY instr itself will be safely removed later. 18881ad6265SDimitry Andric assert(I->isCopy() && "Copy instruction is expected"); 18981ad6265SDimitry Andric auto DstOp = I->getOperand(0); 19081ad6265SDimitry Andric auto SrcOp = I->getOperand(1); 19181ad6265SDimitry Andric assert(DstOp.isReg() && SrcOp.isReg() && 19281ad6265SDimitry Andric "Register operands are expected in COPY"); 19381ad6265SDimitry Andric auto &MRI = I->getMF()->getRegInfo(); 19481ad6265SDimitry Andric MRI.replaceRegWith(DstOp.getReg(), SrcOp.getReg()); 19581ad6265SDimitry Andric } 196*fcaf7f86SDimitry Andric 197*fcaf7f86SDimitry Andric bool SPIRVInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 198*fcaf7f86SDimitry Andric if (MI.getOpcode() == SPIRV::GET_ID || MI.getOpcode() == SPIRV::GET_fID || 199*fcaf7f86SDimitry Andric MI.getOpcode() == SPIRV::GET_pID || MI.getOpcode() == SPIRV::GET_vfID || 200*fcaf7f86SDimitry Andric MI.getOpcode() == SPIRV::GET_vID) { 201*fcaf7f86SDimitry Andric auto &MRI = MI.getMF()->getRegInfo(); 202*fcaf7f86SDimitry Andric MRI.replaceRegWith(MI.getOperand(0).getReg(), MI.getOperand(1).getReg()); 203*fcaf7f86SDimitry Andric MI.eraseFromParent(); 204*fcaf7f86SDimitry Andric return true; 205*fcaf7f86SDimitry Andric } 206*fcaf7f86SDimitry Andric return false; 207*fcaf7f86SDimitry Andric } 208