LLVM API Documentation

ExpandISelPseudos.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Expand Pseudo-instructions produced by ISel. These are usually to allow
00011 // the expansion to contain control flow, such as a conditional move
00012 // implemented with a conditional branch and a phi, or an atomic operation
00013 // implemented with a loop.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/CodeGen/Passes.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineFunctionPass.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Target/TargetLowering.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetSubtargetInfo.h"
00024 using namespace llvm;
00025 
00026 #define DEBUG_TYPE "expand-isel-pseudos"
00027 
00028 namespace {
00029   class ExpandISelPseudos : public MachineFunctionPass {
00030   public:
00031     static char ID; // Pass identification, replacement for typeid
00032     ExpandISelPseudos() : MachineFunctionPass(ID) {}
00033 
00034   private:
00035     bool runOnMachineFunction(MachineFunction &MF) override;
00036 
00037     void getAnalysisUsage(AnalysisUsage &AU) const override {
00038       MachineFunctionPass::getAnalysisUsage(AU);
00039     }
00040   };
00041 } // end anonymous namespace
00042 
00043 char ExpandISelPseudos::ID = 0;
00044 char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID;
00045 INITIALIZE_PASS(ExpandISelPseudos, "expand-isel-pseudos",
00046                 "Expand ISel Pseudo-instructions", false, false)
00047 
00048 bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
00049   bool Changed = false;
00050   const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
00051 
00052   // Iterate through each instruction in the function, looking for pseudos.
00053   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
00054     MachineBasicBlock *MBB = I;
00055     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
00056          MBBI != MBBE; ) {
00057       MachineInstr *MI = MBBI++;
00058 
00059       // If MI is a pseudo, expand it.
00060       if (MI->usesCustomInsertionHook()) {
00061         Changed = true;
00062         MachineBasicBlock *NewMBB =
00063           TLI->EmitInstrWithCustomInserter(MI, MBB);
00064         // The expansion may involve new basic blocks.
00065         if (NewMBB != MBB) {
00066           MBB = NewMBB;
00067           I = NewMBB;
00068           MBBI = NewMBB->begin();
00069           MBBE = NewMBB->end();
00070         }
00071       }
00072     }
00073   }
00074 
00075   return Changed;
00076 }