LLVM 24.0.0git
SystemZAlignGlobals.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Try to increase the alignment of globals to at least 2, to avoid the need for
10// GOT indirection to load an unaligned address.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZ.h"
15#include "llvm/IR/Analysis.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/PassManager.h"
19#include "llvm/Pass.h"
20
21using namespace llvm;
22
23namespace {
24
25class SystemZAlignGlobalsLegacy : public ModulePass {
26public:
27 static char ID;
28
29 explicit SystemZAlignGlobalsLegacy() : ModulePass(ID) {}
30
31 StringRef getPassName() const override { return "SystemZ Align Globals"; }
32
33 bool runOnModule(Module &M) override;
34};
35
36} // end anonymous namespace
37
38char SystemZAlignGlobalsLegacy::ID = 0;
39
40INITIALIZE_PASS(SystemZAlignGlobalsLegacy, "systemz-align-globals",
41 "Try aligning globals to 2 bytes", false, false)
42
44 return new SystemZAlignGlobalsLegacy();
45}
46
48 bool Changed = false;
49
50 for (GlobalVariable &GV : M.globals()) {
51 MaybeAlign GVAlign = GV.getAlign();
52 if (!GVAlign || GVAlign.value() > 1)
53 continue;
54
55 if (!GV.canIncreaseAlignment())
56 continue;
57
58 GV.setAlignment(Align(2));
59 Changed = true;
60 }
61
62 return Changed;
63}
64
65bool SystemZAlignGlobalsLegacy::runOnModule(Module &M) {
66 return alignGlobalsInModule(M);
67}
68
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool alignGlobalsInModule(Module &M)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createSystemZAlignGlobalsLegacyPass()
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:111