| 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" |
| 16 | #include "llvm/IR/GlobalVariable.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/IR/PassManager.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class SystemZAlignGlobalsLegacy : public ModulePass { |
| 26 | public: |
| 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 | |
| 38 | char SystemZAlignGlobalsLegacy::ID = 0; |
| 39 | |
| 40 | INITIALIZE_PASS(SystemZAlignGlobalsLegacy, "systemz-align-globals", |
| 41 | "Try aligning globals to 2 bytes", false, false) |
| 42 | |
| 43 | ModulePass *llvm::createSystemZAlignGlobalsLegacyPass() { |
| 44 | return new SystemZAlignGlobalsLegacy(); |
| 45 | } |
| 46 | |
| 47 | static bool alignGlobalsInModule(Module &M) { |
| 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 | |
| 65 | bool SystemZAlignGlobalsLegacy::runOnModule(Module &M) { |
| 66 | return alignGlobalsInModule(M); |
| 67 | } |
| 68 | |
| 69 | PreservedAnalyses SystemZAlignGlobalsPass::run(Module &M, |
| 70 | ModuleAnalysisManager &MAM) { |
| 71 | return alignGlobalsInModule(M) ? PreservedAnalyses::none() |
| 72 | : PreservedAnalyses::all(); |
| 73 | } |
| 74 |