Home | History | Annotate | Download | only in ARM
      1 //===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
      2 //removed one -===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is distributed under the University of Illinois Open Source
      7 // License. See LICENSE.TXT for details.
      8 //
      9 //===------------------------------------------------------------------------------------------===//
     10 
     11 #include "ARM.h"
     12 #include "ARMInstrInfo.h"
     13 #include "ARMMachineFunctionInfo.h"
     14 #include "llvm/ADT/Statistic.h"
     15 #include "llvm/CodeGen/MachineFunctionPass.h"
     16 using namespace llvm;
     17 
     18 #define DEBUG_TYPE "double barriers"
     19 
     20 STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
     21 
     22 namespace {
     23 class ARMOptimizeBarriersPass : public MachineFunctionPass {
     24 public:
     25   static char ID;
     26   ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
     27 
     28   bool runOnMachineFunction(MachineFunction &Fn) override;
     29 
     30   MachineFunctionProperties getRequiredProperties() const override {
     31     return MachineFunctionProperties().set(
     32         MachineFunctionProperties::Property::AllVRegsAllocated);
     33   }
     34 
     35   const char *getPassName() const override {
     36     return "optimise barriers pass";
     37   }
     38 };
     39 char ARMOptimizeBarriersPass::ID = 0;
     40 }
     41 
     42 // Returns whether the instruction can safely move past a DMB instruction
     43 // The current implementation allows this iif MI does not have any possible
     44 // memory access
     45 static bool CanMovePastDMB(const MachineInstr *MI) {
     46   return !(MI->mayLoad() ||
     47           MI->mayStore() ||
     48           MI->hasUnmodeledSideEffects() ||
     49           MI->isCall() ||
     50           MI->isReturn());
     51 }
     52 
     53 bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
     54   if (skipFunction(*MF.getFunction()))
     55     return false;
     56 
     57   // Vector to store the DMBs we will remove after the first iteration
     58   std::vector<MachineInstr *> ToRemove;
     59   // DMBType is the Imm value of the first operand. It determines whether it's a
     60   // DMB ish, dmb sy, dmb osh, etc
     61   int64_t DMBType = -1;
     62 
     63   // Find a dmb. If we can move it until the next dmb, tag the second one for
     64   // removal
     65   for (auto &MBB : MF) {
     66     // Will be true when we have seen a DMB, and not seen any instruction since
     67     // that cannot move past a DMB
     68     bool IsRemovableNextDMB = false;
     69     for (auto &MI : MBB) {
     70       if (MI.getOpcode() == ARM::DMB) {
     71         if (IsRemovableNextDMB) {
     72           // If the Imm of this DMB is the same as that of the last DMB, we can
     73           // tag this second DMB for removal
     74           if (MI.getOperand(0).getImm() == DMBType) {
     75             ToRemove.push_back(&MI);
     76           } else {
     77             // If it has a different DMBType, we cannot remove it, but will scan
     78             // for the next DMB, recording this DMB's type as last seen DMB type
     79             DMBType = MI.getOperand(0).getImm();
     80           }
     81         } else {
     82           // After we see a DMB, a next one is removable
     83           IsRemovableNextDMB = true;
     84           DMBType = MI.getOperand(0).getImm();
     85         }
     86       } else if (!CanMovePastDMB(&MI)) {
     87         // If we find an instruction unable to pass past a DMB, a next DMB is
     88         // not removable
     89         IsRemovableNextDMB = false;
     90       }
     91     }
     92   }
     93   // Remove the tagged DMB
     94   for (auto MI : ToRemove) {
     95     MI->eraseFromParent();
     96     ++NumDMBsRemoved;
     97   }
     98 
     99   return NumDMBsRemoved > 0;
    100 }
    101 
    102 /// createARMOptimizeBarriersPass - Returns an instance of the remove double
    103 /// barriers
    104 /// pass.
    105 FunctionPass *llvm::createARMOptimizeBarriersPass() {
    106   return new ARMOptimizeBarriersPass();
    107 }
    108