LLVM API Documentation

ARMOptimizeBarriersPass.cpp
Go to the documentation of this file.
00001 //===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
00002 //removed one -===//
00003 //
00004 //                     The LLVM Compiler Infrastructure
00005 //
00006 // This file is distributed under the University of Illinois Open Source
00007 // License. See LICENSE.TXT for details.
00008 //
00009 //===------------------------------------------------------------------------------------------===//
00010 
00011 #include "ARM.h"
00012 #include "ARMMachineFunctionInfo.h"
00013 #include "ARMInstrInfo.h"
00014 #include "llvm/ADT/Statistic.h"
00015 #include "llvm/CodeGen/MachineFunctionPass.h"
00016 using namespace llvm;
00017 
00018 #define DEBUG_TYPE "double barriers"
00019 
00020 STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
00021 
00022 namespace {
00023 class ARMOptimizeBarriersPass : public MachineFunctionPass {
00024 public:
00025   static char ID;
00026   ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
00027 
00028   bool runOnMachineFunction(MachineFunction &Fn) override;
00029 
00030   const char *getPassName() const override {
00031     return "optimise barriers pass";
00032   }
00033 
00034 private:
00035 };
00036 char ARMOptimizeBarriersPass::ID = 0;
00037 }
00038 
00039 // Returns whether the instruction can safely move past a DMB instruction
00040 // The current implementation allows this iif MI does not have any possible
00041 // memory access
00042 static bool CanMovePastDMB(const MachineInstr *MI) {
00043   return !(MI->mayLoad() ||
00044           MI->mayStore() ||
00045           MI->hasUnmodeledSideEffects() ||
00046           MI->isCall() ||
00047           MI->isReturn());
00048 }
00049 
00050 bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
00051   // Vector to store the DMBs we will remove after the first iteration
00052   std::vector<MachineInstr *> ToRemove;
00053   // DMBType is the Imm value of the first operand. It determines whether it's a
00054   // DMB ish, dmb sy, dmb osh, etc
00055   int64_t DMBType = -1;
00056 
00057   // Find a dmb. If we can move it until the next dmb, tag the second one for
00058   // removal
00059   for (auto &MBB : MF) {
00060     // Will be true when we have seen a DMB, and not seen any instruction since
00061     // that cannot move past a DMB
00062     bool IsRemovableNextDMB = false;
00063     for (auto &MI : MBB) {
00064       if (MI.getOpcode() == ARM::DMB) {
00065         if (IsRemovableNextDMB) {
00066           // If the Imm of this DMB is the same as that of the last DMB, we can
00067           // tag this second DMB for removal
00068           if (MI.getOperand(0).getImm() == DMBType) {
00069             ToRemove.push_back(&MI);
00070           } else {
00071             // If it has a different DMBType, we cannot remove it, but will scan
00072             // for the next DMB, recording this DMB's type as last seen DMB type
00073             DMBType = MI.getOperand(0).getImm();
00074           }
00075         } else {
00076           // After we see a DMB, a next one is removable
00077           IsRemovableNextDMB = true;
00078           DMBType = MI.getOperand(0).getImm();
00079         }
00080       } else if (!CanMovePastDMB(&MI)) {
00081         // If we find an instruction unable to pass past a DMB, a next DMB is
00082         // not removable
00083         IsRemovableNextDMB = false;
00084       }
00085     }
00086   }
00087   // Remove the tagged DMB
00088   for (auto MI : ToRemove) {
00089     MI->eraseFromParent();
00090     ++NumDMBsRemoved;
00091   }
00092 
00093   return NumDMBsRemoved > 0;
00094 }
00095 
00096 /// createARMOptimizeBarriersPass - Returns an instance of the remove double
00097 /// barriers
00098 /// pass.
00099 FunctionPass *llvm::createARMOptimizeBarriersPass() {
00100   return new ARMOptimizeBarriersPass();
00101 }