Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 1 | //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 9 | // This file demotes all registers to memory references. It is intended to be |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 10 | // the inverse of PromoteMemoryToRegister. By converting to loads, the only |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 11 | // values live across basic blocks are allocas and loads before phi nodes. |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 12 | // It is intended that this should make CFG hacking much easier. |
| 13 | // To make later hacking easier, the entry block is split into two, such that |
| 14 | // all introduced allocas and nothing else are in the entry block. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/Instructions.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
| 25 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Scalar.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils.h" |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 29 | #include <list> |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "reg2mem" |
| 33 | |
Anton Korobeynikov | 7499a3b | 2007-10-21 23:05:16 +0000 | [diff] [blame] | 34 | STATISTIC(NumRegsDemoted, "Number of registers demoted"); |
| 35 | STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 36 | |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 37 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 38 | struct RegToMem : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | RegToMem() : FunctionPass(ID) { |
| 41 | initializeRegToMemPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 43 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 44 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Andrew Lenharth | 71b09bb | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 45 | AU.addRequiredID(BreakCriticalEdgesID); |
Andrew Lenharth | 5fc3794 | 2005-11-25 16:04:54 +0000 | [diff] [blame] | 46 | AU.addPreservedID(BreakCriticalEdgesID); |
Andrew Lenharth | 71b09bb | 2005-11-22 21:45:19 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 49 | bool valueEscapes(const Instruction *Inst) const { |
| 50 | const BasicBlock *BB = Inst->getParent(); |
| 51 | for (const User *U : Inst->users()) { |
| 52 | const Instruction *UI = cast<Instruction>(U); |
| 53 | if (UI->getParent() != BB || isa<PHINode>(UI)) |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 54 | return true; |
Gabor Greif | c08e5df | 2010-04-14 16:48:56 +0000 | [diff] [blame] | 55 | } |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 59 | bool runOnFunction(Function &F) override; |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 61 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 62 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 63 | char RegToMem::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 64 | INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 65 | false, false) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 66 | INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges) |
| 67 | INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots", |
| 68 | false, false) |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 69 | |
| 70 | bool RegToMem::runOnFunction(Function &F) { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 71 | if (F.isDeclaration() || skipFunction(F)) |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 72 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 74 | // Insert all new allocas into entry block. |
| 75 | BasicBlock *BBEntry = &F.getEntryBlock(); |
Ramkumar Ramachandra | 40c3e03 | 2015-01-13 03:46:47 +0000 | [diff] [blame] | 76 | assert(pred_empty(BBEntry) && |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 77 | "Entry block to function must not have predecessors!"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 79 | // Find first non-alloca instruction and create insertion point. This is |
| 80 | // safe if block is well-formed: it always have terminator, otherwise |
| 81 | // we'll get and assertion. |
| 82 | BasicBlock::iterator I = BBEntry->begin(); |
| 83 | while (isa<AllocaInst>(I)) ++I; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 84 | |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 85 | CastInst *AllocaInsertionPoint = new BitCastInst( |
| 86 | Constant::getNullValue(Type::getInt32Ty(F.getContext())), |
| 87 | Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 89 | // Find the escaped instructions. But don't create stack slots for |
| 90 | // allocas in entry block. |
| 91 | std::list<Instruction*> WorkList; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 92 | for (BasicBlock &ibb : F) |
| 93 | for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie; |
| 94 | ++iib) { |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 95 | if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) && |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 96 | valueEscapes(&*iib)) { |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 97 | WorkList.push_front(&*iib); |
| 98 | } |
| 99 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 101 | // Demote escaped instructions |
| 102 | NumRegsDemoted += WorkList.size(); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 103 | for (Instruction *ilb : WorkList) |
| 104 | DemoteRegToStack(*ilb, false, AllocaInsertionPoint); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 106 | WorkList.clear(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 108 | // Find all phi's |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 109 | for (BasicBlock &ibb : F) |
| 110 | for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie; |
| 111 | ++iib) |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 112 | if (isa<PHINode>(iib)) |
| 113 | WorkList.push_front(&*iib); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 115 | // Demote phi nodes |
| 116 | NumPhisDemoted += WorkList.size(); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 117 | for (Instruction *ilb : WorkList) |
| 118 | DemotePHIToStack(cast<PHINode>(ilb), AllocaInsertionPoint); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 09a79dc | 2009-09-02 06:15:37 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 124 | // createDemoteRegisterToMemory - Provide an entry point to create this pass. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 125 | char &llvm::DemoteRegisterToMemoryID = RegToMem::ID; |
Andrew Lenharth | 4130a4f | 2005-11-10 01:58:38 +0000 | [diff] [blame] | 126 | FunctionPass *llvm::createDemoteRegisterToMemoryPass() { |
| 127 | return new RegToMem(); |
| 128 | } |