Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Scalar/InferAlignment.cpp
35266 views
//===- InferAlignment.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Infer alignment for load, stores and other memory operations based on9// trailing zero known bits information.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/Scalar/InferAlignment.h"14#include "llvm/Analysis/AssumptionCache.h"15#include "llvm/Analysis/ValueTracking.h"16#include "llvm/IR/Instructions.h"17#include "llvm/InitializePasses.h"18#include "llvm/Support/KnownBits.h"19#include "llvm/Transforms/Scalar.h"20#include "llvm/Transforms/Utils/Local.h"2122using namespace llvm;2324static bool tryToImproveAlign(25const DataLayout &DL, Instruction *I,26function_ref<Align(Value *PtrOp, Align OldAlign, Align PrefAlign)> Fn) {27if (auto *LI = dyn_cast<LoadInst>(I)) {28Value *PtrOp = LI->getPointerOperand();29Align OldAlign = LI->getAlign();30Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(LI->getType()));31if (NewAlign > OldAlign) {32LI->setAlignment(NewAlign);33return true;34}35} else if (auto *SI = dyn_cast<StoreInst>(I)) {36Value *PtrOp = SI->getPointerOperand();37Value *ValOp = SI->getValueOperand();38Align OldAlign = SI->getAlign();39Align NewAlign = Fn(PtrOp, OldAlign, DL.getPrefTypeAlign(ValOp->getType()));40if (NewAlign > OldAlign) {41SI->setAlignment(NewAlign);42return true;43}44}45// TODO: Also handle memory intrinsics.46return false;47}4849bool inferAlignment(Function &F, AssumptionCache &AC, DominatorTree &DT) {50const DataLayout &DL = F.getDataLayout();51bool Changed = false;5253// Enforce preferred type alignment if possible. We do this as a separate54// pass first, because it may improve the alignments we infer below.55for (BasicBlock &BB : F) {56for (Instruction &I : BB) {57Changed |= tryToImproveAlign(58DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {59if (PrefAlign > OldAlign)60return std::max(OldAlign,61tryEnforceAlignment(PtrOp, PrefAlign, DL));62return OldAlign;63});64}65}6667// Compute alignment from known bits.68for (BasicBlock &BB : F) {69for (Instruction &I : BB) {70Changed |= tryToImproveAlign(71DL, &I, [&](Value *PtrOp, Align OldAlign, Align PrefAlign) {72KnownBits Known = computeKnownBits(PtrOp, DL, 0, &AC, &I, &DT);73unsigned TrailZ = std::min(Known.countMinTrailingZeros(),74+Value::MaxAlignmentExponent);75return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));76});77}78}7980return Changed;81}8283PreservedAnalyses InferAlignmentPass::run(Function &F,84FunctionAnalysisManager &AM) {85AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);86DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);87inferAlignment(F, AC, DT);88// Changes to alignment shouldn't invalidated analyses.89return PreservedAnalyses::all();90}919293