22#define DEBUG_TYPE "spirv-regularizer"
32 StringRef getPassName()
const override {
return "SPIR-V Regularizer"; }
43char SPIRVRegularizer::ID = 0;
56void SPIRVRegularizer::runLowerConstExpr(
Function &
F) {
58 std::list<Instruction *> WorkList;
60 WorkList.push_back(&
II);
62 auto FBegin =
F.begin();
63 while (!WorkList.empty()) {
64 Instruction *II = WorkList.front();
66 auto LowerOp = [&II, &FBegin, &F](Value *V) -> Value * {
69 auto *CE = cast<ConstantExpr>(V);
70 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] " << *CE);
71 auto ReplInst = CE->getAsInstruction();
72 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
73 ReplInst->insertBefore(InsPoint->getIterator());
74 LLVM_DEBUG(dbgs() <<
" -> " << *ReplInst <<
'\n');
75 std::vector<Instruction *> Users;
77 for (auto U : CE->users()) {
78 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] Use: " << *U <<
'\n');
79 auto InstUser = dyn_cast<Instruction>(U);
81 if (InstUser && InstUser->getParent()->getParent() == &F)
82 Users.push_back(InstUser);
84 for (auto &User : Users) {
85 if (ReplInst->getParent() == User->getParent() &&
86 User->comesBefore(ReplInst))
87 ReplInst->moveBefore(User->getIterator());
88 User->replaceUsesOfWith(CE, ReplInst);
94 auto LowerConstantVec = [&
II, &LowerOp, &WorkList,
96 unsigned NumOfOp) ->
Value * {
97 if (std::all_of(Vec->op_begin(), Vec->op_end(), [](Value *V) {
98 return isa<ConstantExpr>(V) || isa<Function>(V);
102 std::list<Value *> OpList;
104 std::back_inserter(OpList),
105 [LowerOp](
Value *V) { return LowerOp(V); });
106 Value *Repl =
nullptr;
110 PhiII ? &PhiII->getIncomingBlock(NumOfOp)->back() :
II;
111 std::list<Instruction *> ReplList;
112 for (
auto V : OpList) {
114 ReplList.push_back(Inst);
120 WorkList.splice(WorkList.begin(), ReplList);
125 for (
unsigned OI = 0, OE =
II->getNumOperands(); OI != OE; ++OI) {
126 auto *
Op =
II->getOperand(OI);
128 Value *ReplInst = LowerConstantVec(Vec, OI);
130 II->replaceUsesOfWith(
Op, ReplInst);
138 Value *ReplInst =
nullptr;
140 ReplInst = LowerConstantVec(Vec, OI);
142 ReplInst = LowerOp(CE);
147 II->setOperand(OI, RepMDVal);
154bool SPIRVRegularizer::runOnFunction(
Function &
F) {
155 runLowerConstExpr(
F);
160 return new SPIRVRegularizer();
Expand Atomic instructions
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Represent the analysis usage information of a pass.
Constant Vector Declarations.
FixedVectorType * getType() const
Specialize the getType() method to always return a FixedVectorType, which reduces the amount of casti...
This is an important base class in LLVM.
FunctionPass class - This class is used to implement most global optimizations.
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
StringRef - Represent a constant reference to a string, i.e.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
FunctionPass * createSPIRVRegularizerPass()
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.