26#include <llvm/Config/llvm-config.h>
28#include "llvm/ADT/SmallVector.h"
30#include "llvm/IR/BasicBlock.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DebugInfoMetadata.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/IRBuilder.h"
35#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/MDBuilder.h"
38#include "llvm/IR/Metadata.h"
40#include "llvm/IR/LegacyPassManager.h"
42#include "llvm/Support/Debug.h"
44#include "llvm/Analysis/TargetLibraryInfo.h"
46#include "llvm-c/Core.h"
47#include "llvm-c/DataTypes.h"
49#include "llvm-c/ExternC.h"
50#include "llvm-c/Types.h"
60#define DEBUG_TYPE "jl-inst-simplify"
63bool jlInstSimplify(llvm::Function &F, TargetLibraryInfo &TLI,
64 llvm::AAResults &AA, llvm::LoopInfo &LI) {
69 if (
auto FI = dyn_cast<FreezeInst>(&I)) {
70 if (FI->hasOneUse()) {
71 bool allBranch =
true;
72 for (
auto user : FI->users()) {
73 if (!isa<BranchInst>(user)) {
79 FI->replaceAllUsesWith(FI->getOperand(0));
89 ICmpInst::Predicate pred;
90 if (
auto cmp = dyn_cast<ICmpInst>(&I)) {
91 pred = cmp->getPredicate();
93 }
else if (
auto CI = dyn_cast<CallBase>(&I)) {
95#if LLVM_VERSION_MAJOR >= 14
96 size_t numargs = CI->arg_size();
98 size_t numargs = CI->getNumArgOperands();
100 if (numargs == 2 && isa<PointerType>(I.getOperand(0)->getType()) &&
101 isa<PointerType>(I.getOperand(0)->getType())) {
103 pred = ICmpInst::Predicate::ICMP_EQ;
110 TLI, AA, LI, I.getOperand(0), I.getOperand(1),
false)) {
113 auto repval = ICmpInst::isTrueWhenEqual(pred)
114 ? ConstantInt::get(I.getType(), 1 - val)
115 : ConstantInt::get(I.getType(), val);
116 I.replaceAllUsesWith(repval);
126class JLInstSimplify final :
public FunctionPass {
129 JLInstSimplify() : FunctionPass(ID) {}
131 void getAnalysisUsage(AnalysisUsage &AU)
const override {
132 AU.addRequired<TargetLibraryInfoWrapperPass>();
133 AU.addRequired<AAResultsWrapperPass>();
134 AU.addRequired<LoopInfoWrapperPass>();
137 bool runOnFunction(Function &F)
override {
138 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
139 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
140 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
141 return jlInstSimplify(F, TLI, AA, LI);
153char JLInstSimplify::ID = 0;
155static RegisterPass<JLInstSimplify>
X(
"jl-inst-simplify",
156 "JL instruction simplification");
160 llvm::FunctionAnalysisManager &FAM) {
161 bool changed =
false;
162 changed = jlInstSimplify(F, FAM.getResult<TargetLibraryAnalysis>(F),
163 FAM.getResult<AAManager>(F),
164 FAM.getResult<LoopAnalysis>(F));
165 return changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
167llvm::AnalysisKey JLInstSimplifyNewPM::Key;
FunctionPass * createJLInstSimplifyPass()
void LLVMAddJLInstSimplifyPass(LLVMPassManagerRef PM)
static RegisterPass< JLInstSimplify > X("jl-inst-simplify", "JL instruction simplification")
llvm::Optional< bool > arePointersGuaranteedNoAlias(TargetLibraryInfo &TLI, llvm::AAResults &AA, llvm::LoopInfo &LI, llvm::Value *op0, llvm::Value *op1, bool offsetAllowed)
static llvm::StringRef getFuncNameFromCall(const llvm::CallBase *op)
Result run(llvm::Function &M, llvm::FunctionAnalysisManager &MAM)
llvm::PreservedAnalyses Result