24#define DEBUG_TYPE "spirv-regularizer"
36 StringRef getPassName()
const override {
return "SPIR-V Regularizer"; }
38 void getAnalysisUsage(AnalysisUsage &AU)
const override {
39 FunctionPass::getAnalysisUsage(AU);
44char SPIRVRegularizerLegacy::ID = 0;
57static
void runLowerConstExpr(
Function &
F) {
59 std::list<Instruction *> WorkList;
61 WorkList.push_back(&
II);
63 auto FBegin =
F.begin();
64 while (!WorkList.empty()) {
65 Instruction *II = WorkList.front();
67 auto LowerOp = [&II, &FBegin, &F](Value *V) -> Value * {
70 auto *CE = cast<ConstantExpr>(V);
71 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] " << *CE);
72 auto ReplInst = CE->getAsInstruction();
73 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
74 ReplInst->insertBefore(InsPoint->getIterator());
75 LLVM_DEBUG(dbgs() <<
" -> " << *ReplInst <<
'\n');
76 std::vector<Instruction *> Users;
78 for (auto U : CE->users()) {
79 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] Use: " << *U <<
'\n');
80 auto InstUser = dyn_cast<Instruction>(U);
82 if (InstUser && InstUser->getParent()->getParent() == &F)
83 Users.push_back(InstUser);
85 for (auto &User : Users) {
86 if (ReplInst->getParent() == User->getParent() &&
87 User->comesBefore(ReplInst))
88 ReplInst->moveBefore(User->getIterator());
89 User->replaceUsesOfWith(CE, ReplInst);
95 auto LowerConstantVec = [&
II, &LowerOp, &WorkList,
97 unsigned NumOfOp) ->
Value * {
98 if (std::all_of(Vec->op_begin(), Vec->op_end(), [](Value *V) {
99 return isa<ConstantExpr>(V) || isa<Function>(V);
103 std::list<Value *> OpList;
105 std::back_inserter(OpList),
106 [LowerOp](
Value *V) { return LowerOp(V); });
107 Value *Repl =
nullptr;
111 PhiII ? &PhiII->getIncomingBlock(NumOfOp)->back() :
II;
112 std::list<Instruction *> ReplList;
113 for (
auto V : OpList) {
115 ReplList.push_back(Inst);
121 WorkList.splice(WorkList.begin(), ReplList);
126 for (
unsigned OI = 0, OE =
II->getNumOperands(); OI != OE; ++OI) {
127 auto *
Op =
II->getOperand(OI);
129 Value *ReplInst = LowerConstantVec(Vec, OI);
131 II->replaceUsesOfWith(
Op, ReplInst);
139 Value *ReplInst =
nullptr;
141 ReplInst = LowerConstantVec(Vec, OI);
143 ReplInst = LowerOp(CE);
148 II->setOperand(OI, RepMDVal);
165 bool IsI1 = Cmp->getOperand(0)->getType()->isIntegerTy(1);
169 auto Pred = Cmp->getPredicate();
175 Value *
P = Cmp->getOperand(0);
176 Value *Q = Cmp->getOperand(1);
179 Value *Result =
nullptr;
184 Result = Builder.CreateAnd(
P, Builder.CreateNot(Q));
189 Result = Builder.CreateAnd(Q, Builder.CreateNot(
P));
194 Result = Builder.CreateOr(Q, Builder.CreateNot(
P));
199 Result = Builder.CreateOr(
P, Builder.CreateNot(Q));
205 Result->takeName(Cmp);
206 Cmp->replaceAllUsesWith(Result);
207 Cmp->eraseFromParent();
213 runLowerConstExpr(
F);
223 return new SPIRVRegularizerLegacy();
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)
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
This header defines various interfaces for pass management in LLVM.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static void runLowerI1Comparisons(Function &F)
static bool runImpl(Function &F)
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
@ ICMP_ULE
unsigned less or equal
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.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
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.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
FunctionPass * createSPIRVRegularizerPass()
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.