Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/FinalizeISel.cpp
35233 views
//===-- llvm/CodeGen/FinalizeISel.cpp ---------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8/// This pass expands Pseudo-instructions produced by ISel, fixes register9/// reservations and may do machine frame information adjustments.10/// The pseudo instructions are used to allow the expansion to contain control11/// flow, such as a conditional move implemented with a conditional branch and a12/// phi, or an atomic operation implemented with a loop.13//14//===----------------------------------------------------------------------===//1516#include "llvm/CodeGen/FinalizeISel.h"17#include "llvm/CodeGen/MachineFrameInfo.h"18#include "llvm/CodeGen/MachineFunction.h"19#include "llvm/CodeGen/MachineFunctionPass.h"20#include "llvm/CodeGen/TargetInstrInfo.h"21#include "llvm/CodeGen/TargetLowering.h"22#include "llvm/CodeGen/TargetSubtargetInfo.h"23#include "llvm/InitializePasses.h"24using namespace llvm;2526#define DEBUG_TYPE "finalize-isel"2728namespace {29class FinalizeISel : public MachineFunctionPass {30public:31static char ID; // Pass identification, replacement for typeid32FinalizeISel() : MachineFunctionPass(ID) {}3334private:35bool runOnMachineFunction(MachineFunction &MF) override;3637void getAnalysisUsage(AnalysisUsage &AU) const override {38MachineFunctionPass::getAnalysisUsage(AU);39}40};41} // end anonymous namespace4243static std::pair<bool, bool> runImpl(MachineFunction &MF) {44bool Changed = false;45bool PreserveCFG = true;46const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();47const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();4849// Iterate through each instruction in the function, looking for pseudos.50for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {51MachineBasicBlock *MBB = &*I;52for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();53MBBI != MBBE; ) {54MachineInstr &MI = *MBBI++;5556// Set AdjustsStack to true if the instruction selector emits a stack57// frame setup instruction or a stack aligning inlineasm.58if (TII->isFrameInstr(MI) || MI.isStackAligningInlineAsm())59MF.getFrameInfo().setAdjustsStack(true);6061// If MI is a pseudo, expand it.62if (MI.usesCustomInsertionHook()) {63Changed = true;64MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);65// The expansion may involve new basic blocks.66if (NewMBB != MBB) {67PreserveCFG = false;68MBB = NewMBB;69I = NewMBB->getIterator();70MBBI = NewMBB->begin();71MBBE = NewMBB->end();72}73}74}75}7677TLI->finalizeLowering(MF);7879return {Changed, PreserveCFG};80}8182char FinalizeISel::ID = 0;83char &llvm::FinalizeISelID = FinalizeISel::ID;84INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,85"Finalize ISel and expand pseudo-instructions", false, false)8687bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {88return runImpl(MF).first;89}9091PreservedAnalyses FinalizeISelPass::run(MachineFunction &MF,92MachineFunctionAnalysisManager &) {93auto [Changed, PreserveCFG] = runImpl(MF);94if (!Changed)95return PreservedAnalyses::all();96auto PA = getMachineFunctionPassPreservedAnalyses();97if (PreserveCFG)98PA.preserveSet<CFGAnalyses>();99return PA;100}101102103