aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp')
-rw-r--r--llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp77
1 files changed, 42 insertions, 35 deletions
diff --git a/llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp b/llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp
index c3842f026670..6027d24636d6 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVPostLegalizer.cpp
@@ -19,11 +19,13 @@
#include "SPIRVUtils.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/CodeGen/MachinePostDominators.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/IntrinsicsSPIRV.h"
#include "llvm/Target/TargetIntrinsicInfo.h"
+#include <stack>
#define DEBUG_TYPE "spirv-postlegalizer"
@@ -53,14 +55,6 @@ extern void processInstr(MachineInstr &MI, MachineIRBuilder &MIB,
MachineRegisterInfo &MRI, SPIRVGlobalRegistry *GR);
} // namespace llvm
-static bool isMetaInstrGET(unsigned Opcode) {
- return Opcode == SPIRV::GET_ID || Opcode == SPIRV::GET_ID64 ||
- Opcode == SPIRV::GET_fID || Opcode == SPIRV::GET_fID64 ||
- Opcode == SPIRV::GET_pID32 || Opcode == SPIRV::GET_pID64 ||
- Opcode == SPIRV::GET_vID || Opcode == SPIRV::GET_vfID ||
- Opcode == SPIRV::GET_vpID32 || Opcode == SPIRV::GET_vpID64;
-}
-
static bool mayBeInserted(unsigned Opcode) {
switch (Opcode) {
case TargetOpcode::G_SMAX:
@@ -102,10 +96,7 @@ static void processNewInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR,
if (!ResType) {
// There was no "assign type" actions, let's fix this now
ResType = ScalarType;
- MRI.setRegClass(ResVReg, &SPIRV::IDRegClass);
- MRI.setType(ResVReg,
- LLT::scalar(GR->getScalarOrVectorBitWidth(ResType)));
- GR->assignSPIRVTypeToVReg(ResType, ResVReg, *GR->CurMF);
+ setRegClassType(ResVReg, ResType, GR, &MRI, *GR->CurMF, true);
}
}
} else if (mayBeInserted(Opcode) && I.getNumDefs() == 1 &&
@@ -114,34 +105,20 @@ static void processNewInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR,
// registers, we must decorate them as if they were introduced in a
// non-automatic way
Register ResVReg = I.getOperand(0).getReg();
- SPIRVType *ResVType = GR->getSPIRVTypeForVReg(ResVReg);
// Check if the register defined by the instruction is newly generated
// or already processed
- if (!ResVType) {
- // Set type of the defined register
- ResVType = GR->getSPIRVTypeForVReg(I.getOperand(1).getReg());
- // Check if we have type defined for operands of the new instruction
- if (!ResVType)
- continue;
- // Set type & class
- MRI.setRegClass(ResVReg, &SPIRV::IDRegClass);
- MRI.setType(ResVReg,
- LLT::scalar(GR->getScalarOrVectorBitWidth(ResVType)));
- GR->assignSPIRVTypeToVReg(ResVType, ResVReg, *GR->CurMF);
- }
+ if (MRI.getRegClassOrNull(ResVReg))
+ continue;
+ assert(GR->getSPIRVTypeForVReg(ResVReg) == nullptr);
+ // Check if we have type defined for operands of the new instruction
+ SPIRVType *ResVType = GR->getSPIRVTypeForVReg(I.getOperand(1).getReg());
+ if (!ResVType)
+ continue;
+ // Set type & class
+ setRegClassType(ResVReg, ResVType, GR, &MRI, *GR->CurMF, true);
// If this is a simple operation that is to be reduced by TableGen
// definition we must apply some of pre-legalizer rules here
if (isTypeFoldingSupported(Opcode)) {
- // Check if the instruction newly generated or already processed
- MachineInstr *NextMI = I.getNextNode();
- if (NextMI && isMetaInstrGET(NextMI->getOpcode()))
- continue;
- // Restore usual instructions pattern for the newly inserted
- // instruction
- MRI.setRegClass(ResVReg, MRI.getType(ResVReg).isVector()
- ? &SPIRV::IDRegClass
- : &SPIRV::ANYIDRegClass);
- MRI.setType(ResVReg, LLT::scalar(32));
insertAssignInstr(ResVReg, nullptr, ResVType, GR, MIB, MRI);
processInstr(I, MIB, MRI, GR);
}
@@ -150,6 +127,36 @@ static void processNewInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR,
}
}
+// Do a preorder traversal of the CFG starting from the BB |Start|.
+// point. Calls |op| on each basic block encountered during the traversal.
+void visit(MachineFunction &MF, MachineBasicBlock &Start,
+ std::function<void(MachineBasicBlock *)> op) {
+ std::stack<MachineBasicBlock *> ToVisit;
+ SmallPtrSet<MachineBasicBlock *, 8> Seen;
+
+ ToVisit.push(&Start);
+ Seen.insert(ToVisit.top());
+ while (ToVisit.size() != 0) {
+ MachineBasicBlock *MBB = ToVisit.top();
+ ToVisit.pop();
+
+ op(MBB);
+
+ for (auto Succ : MBB->successors()) {
+ if (Seen.contains(Succ))
+ continue;
+ ToVisit.push(Succ);
+ Seen.insert(Succ);
+ }
+ }
+}
+
+// Do a preorder traversal of the CFG starting from the given function's entry
+// point. Calls |op| on each basic block encountered during the traversal.
+void visit(MachineFunction &MF, std::function<void(MachineBasicBlock *)> op) {
+ visit(MF, *MF.begin(), op);
+}
+
bool SPIRVPostLegalizer::runOnMachineFunction(MachineFunction &MF) {
// Initialize the type registry.
const SPIRVSubtarget &ST = MF.getSubtarget<SPIRVSubtarget>();