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,
70 if (
auto FI = dyn_cast<FreezeInst>(&I)) {
71 if (FI->hasOneUse()) {
72 bool allBranch =
true;
73 for (
auto user : FI->users()) {
80 FI->replaceAllUsesWith(FI->getOperand(0));
90 ICmpInst::Predicate pred;
91 if (
auto cmp = dyn_cast<ICmpInst>(&I)) {
92 pred = cmp->getPredicate();
94 }
else if (
auto CI = dyn_cast<CallBase>(&I)) {
96#if LLVM_VERSION_MAJOR >= 14
97 size_t numargs = CI->arg_size();
99 size_t numargs = CI->getNumArgOperands();
101 if (numargs == 2 && isa<PointerType>(I.getOperand(0)->getType()) &&
102 isa<PointerType>(I.getOperand(0)->getType())) {
104 pred = ICmpInst::Predicate::ICMP_EQ;
111 TLI, AA, DT, LI, I.getOperand(0), I.getOperand(1),
false)) {
114 auto repval = ICmpInst::isTrueWhenEqual(pred)
115 ? ConstantInt::get(I.getType(), 1 - val)
116 : ConstantInt::get(I.getType(), val);
117 I.replaceAllUsesWith(repval);
127class JLInstSimplify final :
public FunctionPass {
130 JLInstSimplify() : FunctionPass(ID) {}
132 void getAnalysisUsage(AnalysisUsage &AU)
const override {
133 AU.addRequired<TargetLibraryInfoWrapperPass>();
134 AU.addRequired<AAResultsWrapperPass>();
135 AU.addRequired<LoopInfoWrapperPass>();
136 AU.addRequired<DominatorTreeWrapperPass>();
139 bool runOnFunction(Function &F)
override {
140 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
141 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
142 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
143 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
144 return jlInstSimplify(F, TLI, AA, LI, DT);
156char JLInstSimplify::ID = 0;
158static RegisterPass<JLInstSimplify>
X(
"jl-inst-simplify",
159 "JL instruction simplification");
163 llvm::FunctionAnalysisManager &FAM) {
164 bool changed =
false;
165 changed = jlInstSimplify(
166 F, FAM.getResult<TargetLibraryAnalysis>(F), FAM.getResult<AAManager>(F),
167 FAM.getResult<LoopAnalysis>(F), FAM.getResult<DominatorTreeAnalysis>(F));
168 return changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
170llvm::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::DominatorTree &DT, llvm::LoopInfo &LI, llvm::Value *op0, llvm::Value *op1, bool offsetAllowed)
static bool isAnyBranch(const llvm::Value *V)
static llvm::StringRef getFuncNameFromCall(const llvm::CallBase *op)
Result run(llvm::Function &M, llvm::FunctionAnalysisManager &MAM)
llvm::PreservedAnalyses Result