5#include "llvm/IR/IntrinsicsWebAssembly.h"
14struct WebAssemblyReduceToAnyAllTrue final :
FunctionPass {
17 WebAssemblyTargetMachine &TM;
18 const Module *CachedModule =
nullptr;
19 bool ModuleHasInterestingIntrinsics =
false;
21 WebAssemblyReduceToAnyAllTrue(WebAssemblyTargetMachine &TM)
22 : FunctionPass(ID), TM(TM) {}
24 StringRef getPassName()
const override {
25 return "WebAssembly convert reduce to any_true/all_true";
28 bool hasInterestingIntrinsics(
const Module &M) {
29 if (CachedModule == &M)
30 return ModuleHasInterestingIntrinsics;
33 ModuleHasInterestingIntrinsics =
false;
35 for (
const Function &Fn :
M.functions()) {
36 switch (Fn.getIntrinsicID()) {
37 case Intrinsic::vector_reduce_or:
38 case Intrinsic::vector_reduce_and:
39 ModuleHasInterestingIntrinsics =
true;
50 if (!TM.getSubtarget<WebAssemblySubtarget>(
F).hasSIMD128())
53 if (!hasInterestingIntrinsics(*
F.getParent()))
59 for (
auto It = BB.begin(),
E = BB.end(); It !=
E;) {
62 if (!Cmp ||
Cmp->getPredicate() != ICmpInst::ICMP_NE)
65 Value *Reduce =
nullptr;
70 if (!
II || !
II->hasOneUse())
74 Value *Vec =
II->getArgOperand(0);
80 return B.CreateCall(Fn, {Arg});
85 switch (
II->getIntrinsicID()) {
86 case Intrinsic::vector_reduce_or: {
88 Value *
Any = makeIntrinsic(Intrinsic::wasm_anytrue, Vec);
89 New =
B.CreateICmpNE(Any, ConstantInt::get(
Any->getType(), 0));
93 case Intrinsic::vector_reduce_and: {
101 if (Pred != ICmpInst::ICMP_NE)
104 Value *
All = makeIntrinsic(Intrinsic::wasm_alltrue,
LHS);
105 New =
B.CreateICmpNE(
All, ConstantInt::get(
All->getType(), 0));
113 Cmp->replaceAllUsesWith(New);
114 Cmp->eraseFromParent();
117 II->eraseFromParent();
128char WebAssemblyReduceToAnyAllTrue::ID = 0;
132 return new WebAssemblyReduceToAnyAllTrue(TM);
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
uint64_t IntrinsicInst * II
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file declares the WebAssembly-specific subclass of TargetMachine.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
FunctionPass class - This class is used to implement most global optimizations.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
bool match(Val *V, const Pattern &P)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
FunctionPass * createWebAssemblyReduceToAnyAllTrue(WebAssemblyTargetMachine &TM)
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >