| 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/GlobalVariable.h" |
| 16 | #include "llvm/IR/Module.h" |
| 17 | #include "llvm/Pass.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class SystemZAlignGlobals : public ModulePass { |
| 24 | public: |
| 25 | static char ID; |
| 26 | |
| 27 | explicit SystemZAlignGlobals() : ModulePass(ID) {} |
| 28 | |
| 29 | StringRef getPassName() const override { return "SystemZ Align Globals"; } |
| 30 | |
| 31 | bool runOnModule(Module &M) override; |
| 32 | }; |
| 33 | |
| 34 | } // end anonymous namespace |
| 35 | |
| 36 | char SystemZAlignGlobals::ID = 0; |
| 37 | |
| 38 | INITIALIZE_PASS(SystemZAlignGlobals, "systemz-align-globals", |
| 39 | "Try aligning globals to 2 bytes", false, false) |
| 40 | |
| 41 | ModulePass *llvm::createSystemZAlignGlobalsPass() { |
| 42 | return new SystemZAlignGlobals(); |
| 43 | } |
| 44 | |
| 45 | bool SystemZAlignGlobals::runOnModule(Module &M) { |
| 46 | bool Changed = false; |
| 47 | |
| 48 | for (GlobalVariable &GV : M.globals()) { |
| 49 | MaybeAlign GVAlign = GV.getAlign(); |
| 50 | if (!GVAlign || GVAlign.value() > 1) |
| 51 | continue; |
| 52 | |
| 53 | if (!GV.canIncreaseAlignment()) |
| 54 | continue; |
| 55 | |
| 56 | GV.setAlignment(Align(2)); |
| 57 | Changed = true; |
| 58 | } |
| 59 | |
| 60 | return Changed; |
| 61 | } |
| 62 |