Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
35271 views
//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//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//===----------------------------------------------------------------------===//78#include "llvm/ADT/DenseMap.h"9#include "llvm/Analysis/CFG.h"10#include "llvm/IR/DataLayout.h"11#include "llvm/IR/Function.h"12#include "llvm/IR/Instructions.h"13#include "llvm/Transforms/Utils/BasicBlockUtils.h"14#include "llvm/Transforms/Utils/Local.h"15using namespace llvm;1617/// DemoteRegToStack - This function takes a virtual register computed by an18/// Instruction and replaces it with a slot in the stack frame, allocated via19/// alloca. This allows the CFG to be changed around without fear of20/// invalidating the SSA information for the value. It returns the pointer to21/// the alloca inserted to create a stack slot for I.22AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,23std::optional<BasicBlock::iterator> AllocaPoint) {24if (I.use_empty()) {25I.eraseFromParent();26return nullptr;27}2829Function *F = I.getParent()->getParent();30const DataLayout &DL = F->getDataLayout();3132// Create a stack slot to hold the value.33AllocaInst *Slot;34if (AllocaPoint) {35Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,36I.getName()+".reg2mem", *AllocaPoint);37} else {38Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,39I.getName() + ".reg2mem", F->getEntryBlock().begin());40}4142// We cannot demote invoke instructions to the stack if their normal edge43// is critical. Therefore, split the critical edge and create a basic block44// into which the store can be inserted.45if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {46if (!II->getNormalDest()->getSinglePredecessor()) {47unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());48assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");49BasicBlock *BB = SplitCriticalEdge(II, SuccNum);50assert(BB && "Unable to split critical edge.");51(void)BB;52}53} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {54for (unsigned i = 0; i < CBI->getNumSuccessors(); i++) {55auto *Succ = CBI->getSuccessor(i);56if (!Succ->getSinglePredecessor()) {57assert(isCriticalEdge(II, i) && "Expected a critical edge!");58[[maybe_unused]] BasicBlock *BB = SplitCriticalEdge(II, i);59assert(BB && "Unable to split critical edge.");60}61}62}6364// Change all of the users of the instruction to read from the stack slot.65while (!I.use_empty()) {66Instruction *U = cast<Instruction>(I.user_back());67if (PHINode *PN = dyn_cast<PHINode>(U)) {68// If this is a PHI node, we can't insert a load of the value before the69// use. Instead insert the load in the predecessor block corresponding70// to the incoming value.71//72// Note that if there are multiple edges from a basic block to this PHI73// node that we cannot have multiple loads. The problem is that the74// resulting PHI node will have multiple values (from each load) coming in75// from the same block, which is illegal SSA form. For this reason, we76// keep track of and reuse loads we insert.77DenseMap<BasicBlock*, Value*> Loads;78for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)79if (PN->getIncomingValue(i) == &I) {80Value *&V = Loads[PN->getIncomingBlock(i)];81if (!V) {82// Insert the load into the predecessor block83V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",84VolatileLoads,85PN->getIncomingBlock(i)->getTerminator()->getIterator());86Loads[PN->getIncomingBlock(i)] = V;87}88PN->setIncomingValue(i, V);89}9091} else {92// If this is a normal instruction, just insert a load.93Value *V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",94VolatileLoads, U->getIterator());95U->replaceUsesOfWith(&I, V);96}97}9899// Insert stores of the computed value into the stack slot. We have to be100// careful if I is an invoke instruction, because we can't insert the store101// AFTER the terminator instruction.102BasicBlock::iterator InsertPt;103if (!I.isTerminator()) {104InsertPt = ++I.getIterator();105// Don't insert before PHI nodes or landingpad instrs.106for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)107if (isa<CatchSwitchInst>(InsertPt))108break;109if (isa<CatchSwitchInst>(InsertPt)) {110for (BasicBlock *Handler : successors(&*InsertPt))111new StoreInst(&I, Slot, Handler->getFirstInsertionPt());112return Slot;113}114} else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {115InsertPt = II->getNormalDest()->getFirstInsertionPt();116} else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {117for (BasicBlock *Succ : successors(CBI))118new StoreInst(CBI, Slot, Succ->getFirstInsertionPt());119return Slot;120} else {121llvm_unreachable("Unsupported terminator for Reg2Mem");122}123124new StoreInst(&I, Slot, InsertPt);125return Slot;126}127128/// DemotePHIToStack - This function takes a virtual register computed by a PHI129/// node and replaces it with a slot in the stack frame allocated via alloca.130/// The PHI node is deleted. It returns the pointer to the alloca inserted.131AllocaInst *llvm::DemotePHIToStack(PHINode *P, std::optional<BasicBlock::iterator> AllocaPoint) {132if (P->use_empty()) {133P->eraseFromParent();134return nullptr;135}136137const DataLayout &DL = P->getDataLayout();138139// Create a stack slot to hold the value.140AllocaInst *Slot;141if (AllocaPoint) {142Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,143P->getName()+".reg2mem", *AllocaPoint);144} else {145Function *F = P->getParent()->getParent();146Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,147P->getName() + ".reg2mem",148F->getEntryBlock().begin());149}150151// Iterate over each operand inserting a store in each predecessor.152for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {153if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {154assert(II->getParent() != P->getIncomingBlock(i) &&155"Invoke edge not supported yet"); (void)II;156}157new StoreInst(P->getIncomingValue(i), Slot,158P->getIncomingBlock(i)->getTerminator()->getIterator());159}160161// Insert a load in place of the PHI and replace all uses.162BasicBlock::iterator InsertPt = P->getIterator();163// Don't insert before PHI nodes or landingpad instrs.164for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)165if (isa<CatchSwitchInst>(InsertPt))166break;167if (isa<CatchSwitchInst>(InsertPt)) {168// We need a separate load before each actual use of the PHI169SmallVector<Instruction *, 4> Users;170for (User *U : P->users()) {171Instruction *User = cast<Instruction>(U);172Users.push_back(User);173}174for (Instruction *User : Users) {175Value *V =176new LoadInst(P->getType(), Slot, P->getName() + ".reload", User->getIterator());177User->replaceUsesOfWith(P, V);178}179} else {180Value *V =181new LoadInst(P->getType(), Slot, P->getName() + ".reload", InsertPt);182P->replaceAllUsesWith(V);183}184// Delete PHI.185P->eraseFromParent();186return Slot;187}188189190