blob: 768e5226e87ae32f800719eb50fdec82aac6dc29 [file] [edit]
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Try to increase the alignment of globals to at least 2, to avoid the need for
// GOT indirection to load an unaligned address.
//
//===----------------------------------------------------------------------===//
#include "SystemZ.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
using namespace llvm;
namespace {
class SystemZAlignGlobalsLegacy : public ModulePass {
public:
static char ID;
explicit SystemZAlignGlobalsLegacy() : ModulePass(ID) {}
StringRef getPassName() const override { return "SystemZ Align Globals"; }
bool runOnModule(Module &M) override;
};
} // end anonymous namespace
char SystemZAlignGlobalsLegacy::ID = 0;
INITIALIZE_PASS(SystemZAlignGlobalsLegacy, "systemz-align-globals",
"Try aligning globals to 2 bytes", false, false)
ModulePass *llvm::createSystemZAlignGlobalsLegacyPass() {
return new SystemZAlignGlobalsLegacy();
}
static bool alignGlobalsInModule(Module &M) {
bool Changed = false;
for (GlobalVariable &GV : M.globals()) {
MaybeAlign GVAlign = GV.getAlign();
if (!GVAlign || GVAlign.value() > 1)
continue;
if (!GV.canIncreaseAlignment())
continue;
GV.setAlignment(Align(2));
Changed = true;
}
return Changed;
}
bool SystemZAlignGlobalsLegacy::runOnModule(Module &M) {
return alignGlobalsInModule(M);
}
PreservedAnalyses SystemZAlignGlobalsPass::run(Module &M,
ModuleAnalysisManager &MAM) {
return alignGlobalsInModule(M) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}