Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the Cfg class. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 12 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "IceCfg.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 16 | |
| 17 | #include "IceAssembler.h" |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 18 | #include "IceBitVector.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 19 | #include "IceCfgNode.h" |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 20 | #include "IceClFlags.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceDefs.h" |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 22 | #include "IceELFObjectWriter.h" |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 23 | #include "IceGlobalInits.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | #include "IceInst.h" |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame^] | 25 | #include "IceInstrumentation.h" |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 26 | #include "IceInstVarIter.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 27 | #include "IceLiveness.h" |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 28 | #include "IceLoopAnalyzer.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 29 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 30 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 31 | |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 32 | #include <memory> |
| 33 | #include <utility> |
| 34 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 35 | namespace Ice { |
| 36 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 37 | Cfg::Cfg(GlobalContext *Ctx, uint32_t SequenceNumber) |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 38 | : Ctx(Ctx), SequenceNumber(SequenceNumber), VMask(getFlags().getVerbose()), |
| 39 | FunctionName(), NextInstNumber(Inst::NumberInitial), Live(nullptr) { |
John Porto | 44b3ce8 | 2016-02-26 13:10:55 -0800 | [diff] [blame] | 40 | Allocator.reset(new ArenaAllocator()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 41 | NodeStrings.reset(new StringPool); |
| 42 | VarStrings.reset(new StringPool); |
John Porto | 44b3ce8 | 2016-02-26 13:10:55 -0800 | [diff] [blame] | 43 | CfgLocalAllocatorScope _(this); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 44 | Target = TargetLowering::createLowering(getFlags().getTargetArch(), this); |
John Porto | 44b3ce8 | 2016-02-26 13:10:55 -0800 | [diff] [blame] | 45 | VMetadata.reset(new VariablesMetadata(this)); |
| 46 | TargetAssembler = Target->createAssembler(); |
| 47 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 48 | if (getFlags().getRandomizeAndPoolImmediatesOption() == RPI_Randomize) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 49 | // If -randomize-pool-immediates=randomize, create a random number |
| 50 | // generator to generate a cookie for constant blinding. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 51 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), RPE_ConstantBlinding, |
| 52 | this->SequenceNumber); |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 53 | ConstantBlindingCookie = |
Qining Lu | 360e319 | 2015-08-20 17:13:07 -0700 | [diff] [blame] | 54 | (uint32_t)RNG.next((uint64_t)std::numeric_limits<uint32_t>::max() + 1); |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 55 | } |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 56 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 57 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 58 | Cfg::~Cfg() { |
| 59 | assert(CfgAllocatorTraits::current() == nullptr); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 60 | if (getFlags().getDumpStrings()) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 61 | OstreamLocker _(Ctx); |
| 62 | Ostream &Str = Ctx->getStrDump(); |
| 63 | getNodeStrings()->dump(Str); |
| 64 | getVarStrings()->dump(Str); |
| 65 | } |
| 66 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 67 | |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 68 | /// Create a string like "foo(i=123:b=9)" indicating the function name, number |
| 69 | /// of high-level instructions, and number of basic blocks. This string is only |
| 70 | /// used for dumping and other diagnostics, and the idea is that given a set of |
| 71 | /// functions to debug a problem on, it's easy to find the smallest or simplest |
| 72 | /// function to attack. Note that the counts may change somewhat depending on |
| 73 | /// what point it is called during the translation passes. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 74 | std::string Cfg::getFunctionNameAndSize() const { |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 75 | if (!BuildDefs::dump()) |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 76 | return getFunctionName().toString(); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 77 | SizeT NodeCount = 0; |
| 78 | SizeT InstCount = 0; |
| 79 | for (CfgNode *Node : getNodes()) { |
| 80 | ++NodeCount; |
| 81 | // Note: deleted instructions are *not* ignored. |
| 82 | InstCount += Node->getPhis().size(); |
| 83 | for (Inst &I : Node->getInsts()) { |
| 84 | if (!llvm::isa<InstTarget>(&I)) |
| 85 | ++InstCount; |
| 86 | } |
| 87 | } |
| 88 | return getFunctionName() + "(i=" + std::to_string(InstCount) + ":b=" + |
| 89 | std::to_string(NodeCount) + ")"; |
| 90 | } |
| 91 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 92 | void Cfg::setError(const std::string &Message) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 93 | HasError = true; |
| 94 | ErrorMessage = Message; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 97 | CfgNode *Cfg::makeNode() { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 98 | SizeT LabelIndex = Nodes.size(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 99 | auto *Node = CfgNode::create(this, LabelIndex); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 100 | Nodes.push_back(Node); |
| 101 | return Node; |
| 102 | } |
| 103 | |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 104 | void Cfg::swapNodes(NodeList &NewNodes) { |
Jim Stichnoth | e7dbc0b | 2015-09-15 10:09:24 -0700 | [diff] [blame] | 105 | assert(Nodes.size() == NewNodes.size()); |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 106 | Nodes.swap(NewNodes); |
| 107 | for (SizeT I = 0, NumNodes = getNumNodes(); I < NumNodes; ++I) |
| 108 | Nodes[I]->resetIndex(I); |
| 109 | } |
| 110 | |
John Porto | a83bfde | 2015-09-18 08:43:02 -0700 | [diff] [blame] | 111 | template <> Variable *Cfg::makeVariable<Variable>(Type Ty) { |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 112 | SizeT Index = Variables.size(); |
| 113 | Variable *Var = Target->shouldSplitToVariable64On32(Ty) |
| 114 | ? Variable64On32::create(this, Ty, Index) |
| 115 | : Variable::create(this, Ty, Index); |
| 116 | Variables.push_back(Var); |
| 117 | return Var; |
| 118 | } |
| 119 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 120 | void Cfg::addArg(Variable *Arg) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 121 | Arg->setIsArg(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 122 | Args.push_back(Arg); |
| 123 | } |
| 124 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 125 | void Cfg::addImplicitArg(Variable *Arg) { |
| 126 | Arg->setIsImplicitArg(); |
| 127 | ImplicitArgs.push_back(Arg); |
| 128 | } |
| 129 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 130 | // Returns whether the stack frame layout has been computed yet. This is used |
| 131 | // for dumping the stack frame location of Variables. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 132 | bool Cfg::hasComputedFrame() const { return getTarget()->hasComputedFrame(); } |
| 133 | |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 134 | namespace { |
| 135 | constexpr char BlockNameGlobalPrefix[] = ".L$profiler$block_name$"; |
| 136 | constexpr char BlockStatsGlobalPrefix[] = ".L$profiler$block_info$"; |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 137 | } // end of anonymous namespace |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 138 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 139 | void Cfg::createNodeNameDeclaration(const std::string &NodeAsmName) { |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 140 | auto *Var = VariableDeclaration::create(GlobalInits.get()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 141 | Var->setName(Ctx, BlockNameGlobalPrefix + NodeAsmName); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 142 | Var->setIsConstant(true); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 143 | Var->addInitializer(VariableDeclaration::DataInitializer::create( |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 144 | GlobalInits.get(), NodeAsmName.data(), NodeAsmName.size() + 1)); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 145 | const SizeT Int64ByteSize = typeWidthInBytes(IceType_i64); |
| 146 | Var->setAlignment(Int64ByteSize); // Wasteful, 32-bit could use 4 bytes. |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 147 | GlobalInits->push_back(Var); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 148 | } |
| 149 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 150 | void Cfg::createBlockProfilingInfoDeclaration( |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 151 | const std::string &NodeAsmName, VariableDeclaration *NodeNameDeclaration) { |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 152 | auto *Var = VariableDeclaration::create(GlobalInits.get()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 153 | Var->setName(Ctx, BlockStatsGlobalPrefix + NodeAsmName); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 154 | const SizeT Int64ByteSize = typeWidthInBytes(IceType_i64); |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 155 | Var->addInitializer(VariableDeclaration::ZeroInitializer::create( |
| 156 | GlobalInits.get(), Int64ByteSize)); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 157 | |
| 158 | const RelocOffsetT NodeNameDeclarationOffset = 0; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 159 | Var->addInitializer(VariableDeclaration::RelocInitializer::create( |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 160 | GlobalInits.get(), NodeNameDeclaration, |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 161 | {RelocOffset::create(Ctx, NodeNameDeclarationOffset)})); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 162 | Var->setAlignment(Int64ByteSize); |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 163 | GlobalInits->push_back(Var); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 164 | } |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 165 | |
| 166 | void Cfg::profileBlocks() { |
| 167 | if (GlobalInits == nullptr) |
| 168 | GlobalInits.reset(new VariableDeclarationList()); |
| 169 | |
| 170 | for (CfgNode *Node : Nodes) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 171 | const std::string NodeAsmName = Node->getAsmName(); |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 172 | createNodeNameDeclaration(NodeAsmName); |
| 173 | createBlockProfilingInfoDeclaration(NodeAsmName, GlobalInits->back()); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 174 | Node->profileExecutionCount(GlobalInits->back()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | bool Cfg::isProfileGlobal(const VariableDeclaration &Var) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 179 | if (!Var.getName().hasStdString()) |
| 180 | return false; |
| 181 | return Var.getName().toString().find(BlockStatsGlobalPrefix) == 0; |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void Cfg::addCallToProfileSummary() { |
| 185 | // The call(s) to __Sz_profile_summary are added by the profiler in functions |
| 186 | // that cause the program to exit. This function is defined in |
| 187 | // runtime/szrt_profiler.c. |
| 188 | Constant *ProfileSummarySym = |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 189 | Ctx->getConstantExternSym(Ctx->getGlobalString("__Sz_profile_summary")); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 190 | constexpr SizeT NumArgs = 0; |
| 191 | constexpr Variable *Void = nullptr; |
| 192 | constexpr bool HasTailCall = false; |
| 193 | auto *Call = |
| 194 | InstCall::create(this, NumArgs, Void, ProfileSummarySym, HasTailCall); |
| 195 | getEntryNode()->getInsts().push_front(Call); |
| 196 | } |
| 197 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 198 | void Cfg::translate() { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 199 | if (hasError()) |
| 200 | return; |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 201 | // Cache the possibly-overridden optimization level once translation begins. |
| 202 | // It would be nicer to do this in the constructor, but we need to wait until |
| 203 | // after setFunctionName() has a chance to be called. |
| 204 | OptimizationLevel = |
| 205 | getFlags().matchForceO2(getFunctionName(), getSequenceNumber()) |
| 206 | ? Opt_2 |
| 207 | : getFlags().getOptLevel(); |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 208 | if (BuildDefs::timers()) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 209 | if (getFlags().matchTimingFocus(getFunctionName(), getSequenceNumber())) { |
| 210 | setFocusedTiming(); |
| 211 | getContext()->resetTimer(GlobalContext::TSK_Default); |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 212 | } |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 213 | } |
| 214 | if (BuildDefs::dump()) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 215 | if (isVerbose(IceV_Status) && |
| 216 | getFlags().matchTestStatus(getFunctionName(), getSequenceNumber())) { |
Jim Stichnoth | 3dd1bb8 | 2016-02-27 09:05:50 -0800 | [diff] [blame] | 217 | getContext()->getStrDump() << ">>>Translating " |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 218 | << getFunctionNameAndSize() |
| 219 | << " seq=" << getSequenceNumber() << "\n"; |
Jim Stichnoth | 3dd1bb8 | 2016-02-27 09:05:50 -0800 | [diff] [blame] | 220 | } |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 221 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 222 | TimerMarker T_func(getContext(), getFunctionName().toStringOrEmpty()); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 223 | TimerMarker T(TimerStack::TT_translate, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 224 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 225 | dump("Initial CFG"); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 226 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 227 | if (getFlags().getEnableBlockProfile()) { |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 228 | profileBlocks(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 229 | // TODO(jpp): this is fragile, at best. Figure out a better way of |
| 230 | // detecting exit functions. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 231 | if (getFunctionName().toStringOrEmpty() == "exit") { |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 232 | addCallToProfileSummary(); |
| 233 | } |
| 234 | dump("Profiled CFG"); |
| 235 | } |
| 236 | |
Jim Stichnoth | be87b2e | 2015-09-18 15:43:59 -0700 | [diff] [blame] | 237 | // Create the Hi and Lo variables where a split was needed |
| 238 | for (Variable *Var : Variables) |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 239 | if (auto *Var64On32 = llvm::dyn_cast<Variable64On32>(Var)) |
Jim Stichnoth | be87b2e | 2015-09-18 15:43:59 -0700 | [diff] [blame] | 240 | Var64On32->initHiLo(this); |
| 241 | |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame^] | 242 | // Instrument the Cfg, e.g. with AddressSanitizer |
| 243 | if (!BuildDefs::minimal() && getFlags().getSanitizeAddresses()) { |
| 244 | getContext()->instrumentFunc(this); |
| 245 | dump("Instrumented CFG"); |
| 246 | } |
| 247 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 248 | // The set of translation passes and their order are determined by the |
| 249 | // target. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 250 | getTarget()->translate(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 251 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 252 | dump("Final output"); |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 253 | if (getFocusedTiming()) { |
Jim Stichnoth | 2b000fd | 2016-04-06 06:37:15 -0700 | [diff] [blame] | 254 | getContext()->dumpLocalTimers(getFunctionName().toString()); |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 255 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 258 | void Cfg::fixPhiNodes() { |
| 259 | for (auto *Node : Nodes) { |
| 260 | // Fix all the phi edges since WASM can't tell how to make them correctly at |
| 261 | // the beginning. |
| 262 | assert(Node); |
| 263 | const auto &InEdges = Node->getInEdges(); |
| 264 | for (auto &Instr : Node->getPhis()) { |
| 265 | auto *Phi = llvm::cast<InstPhi>(&Instr); |
| 266 | assert(Phi); |
| 267 | for (SizeT i = 0; i < InEdges.size(); ++i) { |
| 268 | Phi->setLabel(i, InEdges[i]); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 274 | void Cfg::computeInOutEdges() { |
| 275 | // Compute the out-edges. |
Eric Holk | 085bdae | 2016-04-18 15:08:19 -0700 | [diff] [blame] | 276 | for (CfgNode *Node : Nodes) { |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 277 | Node->computeSuccessors(); |
Eric Holk | 085bdae | 2016-04-18 15:08:19 -0700 | [diff] [blame] | 278 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 279 | |
| 280 | // Prune any unreachable nodes before computing in-edges. |
| 281 | SizeT NumNodes = getNumNodes(); |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 282 | BitVector Reachable(NumNodes); |
| 283 | BitVector Pending(NumNodes); |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 284 | Pending.set(getEntryNode()->getIndex()); |
| 285 | while (true) { |
| 286 | int Index = Pending.find_first(); |
| 287 | if (Index == -1) |
| 288 | break; |
| 289 | Pending.reset(Index); |
| 290 | Reachable.set(Index); |
| 291 | CfgNode *Node = Nodes[Index]; |
| 292 | assert(Node->getIndex() == (SizeT)Index); |
| 293 | for (CfgNode *Succ : Node->getOutEdges()) { |
| 294 | SizeT SuccIndex = Succ->getIndex(); |
| 295 | if (!Reachable.test(SuccIndex)) |
| 296 | Pending.set(SuccIndex); |
| 297 | } |
| 298 | } |
| 299 | SizeT Dest = 0; |
| 300 | for (SizeT Source = 0; Source < NumNodes; ++Source) { |
| 301 | if (Reachable.test(Source)) { |
| 302 | Nodes[Dest] = Nodes[Source]; |
| 303 | Nodes[Dest]->resetIndex(Dest); |
| 304 | // Compute the in-edges. |
| 305 | Nodes[Dest]->computePredecessors(); |
| 306 | ++Dest; |
| 307 | } |
| 308 | } |
| 309 | Nodes.resize(Dest); |
Jim Stichnoth | 1aca230 | 2015-09-16 11:25:22 -0700 | [diff] [blame] | 310 | |
| 311 | TimerMarker T(TimerStack::TT_phiValidation, this); |
| 312 | for (CfgNode *Node : Nodes) |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame] | 313 | Node->enforcePhiConsistency(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 316 | void Cfg::renumberInstructions() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 317 | TimerMarker T(TimerStack::TT_renumberInstructions, this); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 318 | NextInstNumber = Inst::NumberInitial; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 319 | for (CfgNode *Node : Nodes) |
| 320 | Node->renumberInstructions(); |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 321 | // Make sure the entry node is the first node and therefore got the lowest |
| 322 | // instruction numbers, to facilitate live range computation of function |
| 323 | // arguments. We want to model function arguments as being live on entry to |
| 324 | // the function, otherwise an argument whose only use is in the first |
| 325 | // instruction will be assigned a trivial live range and the register |
| 326 | // allocator will not recognize its live range as overlapping another |
| 327 | // variable's live range. |
| 328 | assert(Nodes.empty() || (*Nodes.begin() == getEntryNode())); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 331 | // placePhiLoads() must be called before placePhiStores(). |
| 332 | void Cfg::placePhiLoads() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 333 | TimerMarker T(TimerStack::TT_placePhiLoads, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 334 | for (CfgNode *Node : Nodes) |
| 335 | Node->placePhiLoads(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // placePhiStores() must be called after placePhiLoads(). |
| 339 | void Cfg::placePhiStores() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 340 | TimerMarker T(TimerStack::TT_placePhiStores, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 341 | for (CfgNode *Node : Nodes) |
| 342 | Node->placePhiStores(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void Cfg::deletePhis() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 346 | TimerMarker T(TimerStack::TT_deletePhis, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 347 | for (CfgNode *Node : Nodes) |
| 348 | Node->deletePhis(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 351 | void Cfg::advancedPhiLowering() { |
| 352 | TimerMarker T(TimerStack::TT_advancedPhiLowering, this); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 353 | // Clear all previously computed live ranges (but not live-in/live-out bit |
| 354 | // vectors or last-use markers), because the follow-on register allocation is |
| 355 | // only concerned with live ranges across the newly created blocks. |
| 356 | for (Variable *Var : Variables) { |
| 357 | Var->getLiveRange().reset(); |
| 358 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 359 | // This splits edges and appends new nodes to the end of the node list. This |
| 360 | // can invalidate iterators, so don't use an iterator. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 361 | SizeT NumNodes = getNumNodes(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 362 | SizeT NumVars = getNumVariables(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 363 | for (SizeT I = 0; I < NumNodes; ++I) |
| 364 | Nodes[I]->advancedPhiLowering(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 365 | |
| 366 | TimerMarker TT(TimerStack::TT_lowerPhiAssignments, this); |
| 367 | if (true) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 368 | // The following code does an in-place update of liveness and live ranges |
| 369 | // as a result of adding the new phi edge split nodes. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 370 | getLiveness()->initPhiEdgeSplits(Nodes.begin() + NumNodes, |
| 371 | Variables.begin() + NumVars); |
| 372 | TimerMarker TTT(TimerStack::TT_liveness, this); |
| 373 | // Iterate over the newly added nodes to add their liveness info. |
| 374 | for (auto I = Nodes.begin() + NumNodes, E = Nodes.end(); I != E; ++I) { |
| 375 | InstNumberT FirstInstNum = getNextInstNumber(); |
| 376 | (*I)->renumberInstructions(); |
| 377 | InstNumberT LastInstNum = getNextInstNumber() - 1; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 378 | (*I)->liveness(getLiveness()); |
| 379 | (*I)->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
| 380 | } |
| 381 | } else { |
| 382 | // The following code does a brute-force recalculation of live ranges as a |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 383 | // result of adding the new phi edge split nodes. The liveness calculation |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 384 | // is particularly expensive because the new nodes are not yet in a proper |
| 385 | // topological order and so convergence is slower. |
| 386 | // |
| 387 | // This code is kept here for reference and can be temporarily enabled in |
| 388 | // case the incremental code is under suspicion. |
| 389 | renumberInstructions(); |
| 390 | liveness(Liveness_Intervals); |
| 391 | getVMetadata()->init(VMK_All); |
| 392 | } |
| 393 | Target->regAlloc(RAK_Phi); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 396 | // Find a reasonable placement for nodes that have not yet been placed, while |
| 397 | // maintaining the same relative ordering among already placed nodes. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 398 | void Cfg::reorderNodes() { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 399 | // TODO(ascull): it would be nice if the switch tests were always followed by |
| 400 | // the default case to allow for fall through. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 401 | using PlacedList = CfgList<CfgNode *>; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 402 | PlacedList Placed; // Nodes with relative placement locked down |
| 403 | PlacedList Unreachable; // Unreachable nodes |
| 404 | PlacedList::iterator NoPlace = Placed.end(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 405 | // Keep track of where each node has been tentatively placed so that we can |
| 406 | // manage insertions into the middle. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 407 | CfgVector<PlacedList::iterator> PlaceIndex(Nodes.size(), NoPlace); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 408 | for (CfgNode *Node : Nodes) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 409 | // The "do ... while(0);" construct is to factor out the --PlaceIndex and |
| 410 | // assert() statements before moving to the next node. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 411 | do { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 412 | if (Node != getEntryNode() && Node->getInEdges().empty()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 413 | // The node has essentially been deleted since it is not a successor of |
| 414 | // any other node. |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 415 | Unreachable.push_back(Node); |
| 416 | PlaceIndex[Node->getIndex()] = Unreachable.end(); |
| 417 | Node->setNeedsPlacement(false); |
| 418 | continue; |
| 419 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 420 | if (!Node->needsPlacement()) { |
| 421 | // Add to the end of the Placed list. |
| 422 | Placed.push_back(Node); |
| 423 | PlaceIndex[Node->getIndex()] = Placed.end(); |
| 424 | continue; |
| 425 | } |
| 426 | Node->setNeedsPlacement(false); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 427 | // Assume for now that the unplaced node is from edge-splitting and |
| 428 | // therefore has 1 in-edge and 1 out-edge (actually, possibly more than 1 |
| 429 | // in-edge if the predecessor node was contracted). If this changes in |
| 430 | // the future, rethink the strategy. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 431 | assert(Node->getInEdges().size() >= 1); |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 432 | assert(Node->hasSingleOutEdge()); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 433 | |
| 434 | // If it's a (non-critical) edge where the successor has a single |
| 435 | // in-edge, then place it before the successor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 436 | CfgNode *Succ = Node->getOutEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 437 | if (Succ->getInEdges().size() == 1 && |
| 438 | PlaceIndex[Succ->getIndex()] != NoPlace) { |
| 439 | Placed.insert(PlaceIndex[Succ->getIndex()], Node); |
| 440 | PlaceIndex[Node->getIndex()] = PlaceIndex[Succ->getIndex()]; |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | // Otherwise, place it after the (first) predecessor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 445 | CfgNode *Pred = Node->getInEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 446 | auto PredPosition = PlaceIndex[Pred->getIndex()]; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 447 | // It shouldn't be the case that PredPosition==NoPlace, but if that |
| 448 | // somehow turns out to be true, we just insert Node before |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 449 | // PredPosition=NoPlace=Placed.end() . |
| 450 | if (PredPosition != NoPlace) |
| 451 | ++PredPosition; |
| 452 | Placed.insert(PredPosition, Node); |
| 453 | PlaceIndex[Node->getIndex()] = PredPosition; |
| 454 | } while (0); |
| 455 | |
| 456 | --PlaceIndex[Node->getIndex()]; |
| 457 | assert(*PlaceIndex[Node->getIndex()] == Node); |
| 458 | } |
| 459 | |
| 460 | // Reorder Nodes according to the built-up lists. |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 461 | NodeList Reordered; |
| 462 | Reordered.reserve(Placed.size() + Unreachable.size()); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 463 | for (CfgNode *Node : Placed) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 464 | Reordered.push_back(Node); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 465 | for (CfgNode *Node : Unreachable) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 466 | Reordered.push_back(Node); |
| 467 | assert(getNumNodes() == Reordered.size()); |
| 468 | swapNodes(Reordered); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 471 | namespace { |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 472 | void getRandomPostOrder(CfgNode *Node, BitVector &ToVisit, |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 473 | Ice::NodeList &PostOrder, |
| 474 | Ice::RandomNumberGenerator *RNG) { |
| 475 | assert(ToVisit[Node->getIndex()]); |
| 476 | ToVisit[Node->getIndex()] = false; |
| 477 | NodeList Outs = Node->getOutEdges(); |
| 478 | Ice::RandomShuffle(Outs.begin(), Outs.end(), |
| 479 | [RNG](int N) { return RNG->next(N); }); |
| 480 | for (CfgNode *Next : Outs) { |
| 481 | if (ToVisit[Next->getIndex()]) |
| 482 | getRandomPostOrder(Next, ToVisit, PostOrder, RNG); |
| 483 | } |
| 484 | PostOrder.push_back(Node); |
| 485 | } |
| 486 | } // end of anonymous namespace |
| 487 | |
| 488 | void Cfg::shuffleNodes() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 489 | if (!getFlags().getReorderBasicBlocks()) |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 490 | return; |
| 491 | |
| 492 | NodeList ReversedReachable; |
| 493 | NodeList Unreachable; |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 494 | BitVector ToVisit(Nodes.size(), true); |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 495 | // Create Random number generator for function reordering |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 496 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 497 | RPE_BasicBlockReordering, SequenceNumber); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 498 | // Traverse from entry node. |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 499 | getRandomPostOrder(getEntryNode(), ToVisit, ReversedReachable, &RNG); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 500 | // Collect the unreachable nodes. |
| 501 | for (CfgNode *Node : Nodes) |
| 502 | if (ToVisit[Node->getIndex()]) |
| 503 | Unreachable.push_back(Node); |
| 504 | // Copy the layout list to the Nodes. |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 505 | NodeList Shuffled; |
| 506 | Shuffled.reserve(ReversedReachable.size() + Unreachable.size()); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 507 | for (CfgNode *Node : reverse_range(ReversedReachable)) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 508 | Shuffled.push_back(Node); |
| 509 | for (CfgNode *Node : Unreachable) |
| 510 | Shuffled.push_back(Node); |
| 511 | assert(Nodes.size() == Shuffled.size()); |
| 512 | swapNodes(Shuffled); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 513 | |
| 514 | dump("After basic block shuffling"); |
| 515 | } |
| 516 | |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 517 | void Cfg::localCSE() { |
| 518 | // Performs basic-block local common-subexpression elimination |
| 519 | // If we have |
| 520 | // t1 = op b c |
| 521 | // t2 = op b c |
| 522 | // This pass will replace future references to t2 in a basic block by t1 |
| 523 | // Points to note: |
| 524 | // 1. Does not assume SSA, but not tested on non-SSA input yet as it is run |
| 525 | // at the beginning. |
| 526 | // 2. Leaves removal of instructions to DCE. |
| 527 | // 3. Only enabled on arithmetic instructions. pnacl-clang (-O2) is expected |
| 528 | // to take care of cases not arising from GEP simplification. |
| 529 | // 4. By default, two passes are made over each basic block. Control this |
| 530 | // with -lcse-max-iters=N |
| 531 | |
| 532 | TimerMarker T(TimerStack::TT_localCse, this); |
| 533 | struct VariableHash { |
| 534 | size_t operator()(const Variable *Var) const { return Var->hashValue(); } |
| 535 | }; |
| 536 | |
| 537 | struct InstHash { |
| 538 | size_t operator()(const Inst *Instr) const { |
| 539 | auto Kind = Instr->getKind(); |
| 540 | auto Result = |
| 541 | std::hash<typename std::underlying_type<Inst::InstKind>::type>()( |
| 542 | Kind); |
| 543 | for (SizeT i = 0; i < Instr->getSrcSize(); ++i) { |
| 544 | Result ^= Instr->getSrc(i)->hashValue(); |
| 545 | } |
| 546 | return Result; |
| 547 | } |
| 548 | }; |
| 549 | struct InstEq { |
| 550 | bool srcEq(const Operand *A, const Operand *B) const { |
| 551 | if (llvm::isa<Variable>(A) || llvm::isa<Constant>(A)) |
| 552 | return (A == B); |
| 553 | return false; |
| 554 | } |
| 555 | bool operator()(const Inst *InstrA, const Inst *InstrB) const { |
| 556 | if ((InstrA->getKind() != InstrB->getKind()) || |
| 557 | (InstrA->getSrcSize() != InstrB->getSrcSize())) |
| 558 | return false; |
| 559 | |
| 560 | if (auto *A = llvm::dyn_cast<InstArithmetic>(InstrA)) { |
| 561 | auto *B = llvm::cast<InstArithmetic>(InstrB); |
| 562 | // A, B are guaranteed to be of the same 'kind' at this point |
| 563 | // So, dyn_cast is not needed |
| 564 | if (A->getOp() != B->getOp()) |
| 565 | return false; |
| 566 | } |
| 567 | // Does not enter loop if different kind or number of operands |
| 568 | for (SizeT i = 0; i < InstrA->getSrcSize(); ++i) { |
| 569 | if (!srcEq(InstrA->getSrc(i), InstrB->getSrc(i))) |
| 570 | return false; |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | for (CfgNode *Node : getNodes()) { |
| 577 | CfgUnorderedSet<Inst *, InstHash, InstEq> Seen; |
| 578 | |
| 579 | CfgUnorderedMap<Variable *, Variable *, VariableHash> Replacements; |
| 580 | // Combining the above two into a single data structure might consume less |
| 581 | // memory but will be slower i.e map of Instruction -> Set of Variables |
| 582 | |
| 583 | CfgUnorderedMap<Variable *, std::vector<Inst *>, VariableHash> Dependency; |
| 584 | // Not necessary for SSA, still keeping it in case this pass is not run at |
| 585 | // the beginning. Remove to improve performace. |
| 586 | |
| 587 | int IterCount = getFlags().getLocalCseMaxIterations(); |
| 588 | |
| 589 | while (IterCount--) { |
| 590 | // TODO : Stats on IterCount -> performance |
| 591 | for (Inst &Instr : Node->getInsts()) { |
| 592 | if (Instr.isDeleted() || !llvm::isa<InstArithmetic>(&Instr)) |
| 593 | continue; |
| 594 | |
| 595 | // Invalidate replacements |
| 596 | auto Iter = Replacements.find(Instr.getDest()); |
| 597 | if (Iter != Replacements.end()) { |
| 598 | Replacements.erase(Iter); |
| 599 | } |
| 600 | |
| 601 | // Invalidate 'seen' instructions whose operands were just updated. |
| 602 | auto DepIter = Dependency.find(Instr.getDest()); |
| 603 | if (DepIter != Dependency.end()) { |
| 604 | for (auto DepInst : DepIter->second) { |
| 605 | Seen.erase(DepInst); |
| 606 | } |
| 607 | } |
| 608 | // The above two can be removed if SSA is assumed. |
| 609 | |
| 610 | // Replace - doing this before checking for repetitions might enable |
| 611 | // more |
| 612 | // optimizations |
| 613 | for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { |
| 614 | auto *Opnd = Instr.getSrc(i); |
| 615 | if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { |
| 616 | if (Replacements.find(Var) != Replacements.end()) { |
| 617 | Instr.replaceSource(i, Replacements[Var]); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Check for repetitions |
| 623 | auto SeenIter = Seen.find(&Instr); |
| 624 | if (SeenIter != Seen.end()) { // seen before |
| 625 | const Inst *Found = *SeenIter; |
| 626 | Replacements[Instr.getDest()] = Found->getDest(); |
| 627 | } else { // new |
| 628 | Seen.insert(&Instr); |
| 629 | |
| 630 | // Update dependencies |
| 631 | for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { |
| 632 | auto *Opnd = Instr.getSrc(i); |
| 633 | if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { |
| 634 | Dependency[Var].push_back(&Instr); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 643 | void Cfg::doArgLowering() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 644 | TimerMarker T(TimerStack::TT_doArgLowering, this); |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 645 | getTarget()->lowerArguments(); |
| 646 | } |
| 647 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 648 | void Cfg::sortAndCombineAllocas(CfgVector<Inst *> &Allocas, |
| 649 | uint32_t CombinedAlignment, InstList &Insts, |
| 650 | AllocaBaseVariableType BaseVariableType) { |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 651 | if (Allocas.empty()) |
| 652 | return; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 653 | // Sort by decreasing alignment. |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 654 | std::sort(Allocas.begin(), Allocas.end(), [](Inst *I1, Inst *I2) { |
| 655 | auto *A1 = llvm::dyn_cast<InstAlloca>(I1); |
| 656 | auto *A2 = llvm::dyn_cast<InstAlloca>(I2); |
| 657 | return A1->getAlignInBytes() > A2->getAlignInBytes(); |
| 658 | }); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 659 | // Process the allocas in order of decreasing stack alignment. This allows |
| 660 | // us to pack less-aligned pieces after more-aligned ones, resulting in less |
| 661 | // stack growth. It also allows there to be at most one stack alignment "and" |
| 662 | // instruction for a whole list of allocas. |
| 663 | uint32_t CurrentOffset = 0; |
| 664 | CfgVector<int32_t> Offsets; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 665 | for (Inst *Instr : Allocas) { |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 666 | auto *Alloca = llvm::cast<InstAlloca>(Instr); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 667 | // Adjust the size of the allocation up to the next multiple of the |
| 668 | // object's alignment. |
| 669 | uint32_t Alignment = std::max(Alloca->getAlignInBytes(), 1u); |
| 670 | auto *ConstSize = |
| 671 | llvm::dyn_cast<ConstantInteger32>(Alloca->getSizeInBytes()); |
| 672 | uint32_t Size = Utils::applyAlignment(ConstSize->getValue(), Alignment); |
| 673 | if (BaseVariableType == BVT_FramePointer) { |
| 674 | // Addressing is relative to the frame pointer. Subtract the offset after |
| 675 | // adding the size of the alloca, because it grows downwards from the |
| 676 | // frame pointer. |
| 677 | Offsets.push_back(-(CurrentOffset + Size)); |
| 678 | } else { |
| 679 | // Addressing is relative to the stack pointer or to a user pointer. Add |
| 680 | // the offset before adding the size of the object, because it grows |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 681 | // upwards from the stack pointer. In addition, if the addressing is |
| 682 | // relative to the stack pointer, we need to add the pre-computed max out |
| 683 | // args size bytes. |
| 684 | const uint32_t OutArgsOffsetOrZero = |
| 685 | (BaseVariableType == BVT_StackPointer) |
| 686 | ? getTarget()->maxOutArgsSizeBytes() |
| 687 | : 0; |
| 688 | Offsets.push_back(CurrentOffset + OutArgsOffsetOrZero); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 689 | } |
| 690 | // Update the running offset of the fused alloca region. |
| 691 | CurrentOffset += Size; |
| 692 | } |
| 693 | // Round the offset up to the alignment granularity to use as the size. |
| 694 | uint32_t TotalSize = Utils::applyAlignment(CurrentOffset, CombinedAlignment); |
| 695 | // Ensure every alloca was assigned an offset. |
| 696 | assert(Allocas.size() == Offsets.size()); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 697 | |
| 698 | switch (BaseVariableType) { |
| 699 | case BVT_UserPointer: { |
| 700 | Variable *BaseVariable = makeVariable(IceType_i32); |
| 701 | for (SizeT i = 0; i < Allocas.size(); ++i) { |
| 702 | auto *Alloca = llvm::cast<InstAlloca>(Allocas[i]); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 703 | // Emit a new addition operation to replace the alloca. |
| 704 | Operand *AllocaOffset = Ctx->getConstantInt32(Offsets[i]); |
| 705 | InstArithmetic *Add = |
| 706 | InstArithmetic::create(this, InstArithmetic::Add, Alloca->getDest(), |
| 707 | BaseVariable, AllocaOffset); |
| 708 | Insts.push_front(Add); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 709 | Alloca->setDeleted(); |
| 710 | } |
| 711 | Operand *AllocaSize = Ctx->getConstantInt32(TotalSize); |
| 712 | InstAlloca *CombinedAlloca = |
| 713 | InstAlloca::create(this, BaseVariable, AllocaSize, CombinedAlignment); |
| 714 | CombinedAlloca->setKnownFrameOffset(); |
| 715 | Insts.push_front(CombinedAlloca); |
| 716 | } break; |
| 717 | case BVT_StackPointer: |
| 718 | case BVT_FramePointer: { |
| 719 | for (SizeT i = 0; i < Allocas.size(); ++i) { |
| 720 | auto *Alloca = llvm::cast<InstAlloca>(Allocas[i]); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 721 | // Emit a fake definition of the rematerializable variable. |
| 722 | Variable *Dest = Alloca->getDest(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 723 | auto *Def = InstFakeDef::create(this, Dest); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 724 | if (BaseVariableType == BVT_StackPointer) |
| 725 | Dest->setRematerializable(getTarget()->getStackReg(), Offsets[i]); |
| 726 | else |
| 727 | Dest->setRematerializable(getTarget()->getFrameReg(), Offsets[i]); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 728 | Insts.push_front(Def); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 729 | Alloca->setDeleted(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 730 | } |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 731 | // Allocate the fixed area in the function prolog. |
| 732 | getTarget()->reserveFixedAllocaArea(TotalSize, CombinedAlignment); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 733 | } break; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 734 | } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 735 | } |
| 736 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 737 | void Cfg::processAllocas(bool SortAndCombine) { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 738 | TimerMarker _(TimerStack::TT_alloca, this); |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 739 | const uint32_t StackAlignment = getTarget()->getStackAlignment(); |
| 740 | CfgNode *EntryNode = getEntryNode(); |
Eric Holk | 67c7c41 | 2016-04-15 13:05:37 -0700 | [diff] [blame] | 741 | assert(EntryNode); |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 742 | // LLVM enforces power of 2 alignment. |
| 743 | assert(llvm::isPowerOf2_32(StackAlignment)); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 744 | // Determine if there are large alignment allocations in the entry block or |
| 745 | // dynamic allocations (variable size in the entry block). |
| 746 | bool HasLargeAlignment = false; |
| 747 | bool HasDynamicAllocation = false; |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 748 | for (Inst &Instr : EntryNode->getInsts()) { |
| 749 | if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 750 | uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 751 | if (AlignmentParam > StackAlignment) |
| 752 | HasLargeAlignment = true; |
| 753 | if (llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 754 | Alloca->setKnownFrameOffset(); |
| 755 | else { |
| 756 | HasDynamicAllocation = true; |
| 757 | // If Allocas are not sorted, the first dynamic allocation causes |
| 758 | // later Allocas to be at unknown offsets relative to the stack/frame. |
| 759 | if (!SortAndCombine) |
| 760 | break; |
| 761 | } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 764 | // Don't do the heavyweight sorting and layout for low optimization levels. |
| 765 | if (!SortAndCombine) |
| 766 | return; |
| 767 | // Any alloca outside the entry block is a dynamic allocation. |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 768 | for (CfgNode *Node : Nodes) { |
| 769 | if (Node == EntryNode) |
| 770 | continue; |
| 771 | for (Inst &Instr : Node->getInsts()) { |
| 772 | if (llvm::isa<InstAlloca>(&Instr)) { |
| 773 | // Allocations outside the entry block require a frame pointer. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 774 | HasDynamicAllocation = true; |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | } |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 778 | if (HasDynamicAllocation) |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | // Mark the target as requiring a frame pointer. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 782 | if (HasLargeAlignment || HasDynamicAllocation) |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 783 | getTarget()->setHasFramePointer(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 784 | // Collect the Allocas into the two vectors. |
| 785 | // Allocas in the entry block that have constant size and alignment less |
| 786 | // than or equal to the function's stack alignment. |
| 787 | CfgVector<Inst *> FixedAllocas; |
| 788 | // Allocas in the entry block that have constant size and alignment greater |
| 789 | // than the function's stack alignment. |
| 790 | CfgVector<Inst *> AlignedAllocas; |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 791 | // Maximum alignment used by any alloca. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 792 | uint32_t MaxAlignment = StackAlignment; |
| 793 | for (Inst &Instr : EntryNode->getInsts()) { |
| 794 | if (auto *Alloca = llvm::dyn_cast<InstAlloca>(&Instr)) { |
| 795 | if (!llvm::isa<Constant>(Alloca->getSizeInBytes())) |
| 796 | continue; |
| 797 | uint32_t AlignmentParam = Alloca->getAlignInBytes(); |
| 798 | // For default align=0, set it to the real value 1, to avoid any |
| 799 | // bit-manipulation problems below. |
| 800 | AlignmentParam = std::max(AlignmentParam, 1u); |
| 801 | assert(llvm::isPowerOf2_32(AlignmentParam)); |
| 802 | if (HasDynamicAllocation && AlignmentParam > StackAlignment) { |
| 803 | // If we have both dynamic allocations and large stack alignments, |
| 804 | // high-alignment allocations are pulled out with their own base. |
| 805 | AlignedAllocas.push_back(Alloca); |
| 806 | } else { |
| 807 | FixedAllocas.push_back(Alloca); |
| 808 | } |
| 809 | MaxAlignment = std::max(AlignmentParam, MaxAlignment); |
| 810 | } |
| 811 | } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 812 | // Add instructions to the head of the entry block in reverse order. |
| 813 | InstList &Insts = getEntryNode()->getInsts(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 814 | if (HasDynamicAllocation && HasLargeAlignment) { |
| 815 | // We are using a frame pointer, but fixed large-alignment alloca addresses, |
| 816 | // do not have a known offset from either the stack or frame pointer. |
| 817 | // They grow up from a user pointer from an alloca. |
| 818 | sortAndCombineAllocas(AlignedAllocas, MaxAlignment, Insts, BVT_UserPointer); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 819 | // Fixed size allocas are addressed relative to the frame pointer. |
| 820 | sortAndCombineAllocas(FixedAllocas, StackAlignment, Insts, |
| 821 | BVT_FramePointer); |
| 822 | } else { |
| 823 | // Otherwise, fixed size allocas are addressed relative to the stack unless |
| 824 | // there are dynamic allocas. |
| 825 | const AllocaBaseVariableType BasePointerType = |
| 826 | (HasDynamicAllocation ? BVT_FramePointer : BVT_StackPointer); |
| 827 | sortAndCombineAllocas(FixedAllocas, MaxAlignment, Insts, BasePointerType); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 828 | } |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 829 | if (!FixedAllocas.empty() || !AlignedAllocas.empty()) |
| 830 | // No use calling findRematerializable() unless there is some |
| 831 | // rematerializable alloca instruction to seed it. |
| 832 | findRematerializable(); |
| 833 | } |
| 834 | |
| 835 | namespace { |
| 836 | |
| 837 | // Helpers for findRematerializable(). For each of them, if a suitable |
| 838 | // rematerialization is found, the instruction's Dest variable is set to be |
| 839 | // rematerializable and it returns true, otherwise it returns false. |
| 840 | |
| 841 | bool rematerializeArithmetic(const Inst *Instr) { |
| 842 | // Check that it's an Arithmetic instruction with an Add operation. |
| 843 | auto *Arith = llvm::dyn_cast<InstArithmetic>(Instr); |
| 844 | if (Arith == nullptr || Arith->getOp() != InstArithmetic::Add) |
| 845 | return false; |
| 846 | // Check that Src(0) is rematerializable. |
| 847 | auto *Src0Var = llvm::dyn_cast<Variable>(Arith->getSrc(0)); |
| 848 | if (Src0Var == nullptr || !Src0Var->isRematerializable()) |
| 849 | return false; |
| 850 | // Check that Src(1) is an immediate. |
| 851 | auto *Src1Imm = llvm::dyn_cast<ConstantInteger32>(Arith->getSrc(1)); |
| 852 | if (Src1Imm == nullptr) |
| 853 | return false; |
| 854 | Arith->getDest()->setRematerializable( |
| 855 | Src0Var->getRegNum(), Src0Var->getStackOffset() + Src1Imm->getValue()); |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | bool rematerializeAssign(const Inst *Instr) { |
| 860 | // An InstAssign only originates from an inttoptr or ptrtoint instruction, |
| 861 | // which never occurs in a MINIMAL build. |
| 862 | if (BuildDefs::minimal()) |
| 863 | return false; |
| 864 | // Check that it's an Assign instruction. |
| 865 | if (!llvm::isa<InstAssign>(Instr)) |
| 866 | return false; |
| 867 | // Check that Src(0) is rematerializable. |
| 868 | auto *Src0Var = llvm::dyn_cast<Variable>(Instr->getSrc(0)); |
| 869 | if (Src0Var == nullptr || !Src0Var->isRematerializable()) |
| 870 | return false; |
| 871 | Instr->getDest()->setRematerializable(Src0Var->getRegNum(), |
| 872 | Src0Var->getStackOffset()); |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | bool rematerializeCast(const Inst *Instr) { |
| 877 | // An pointer-type bitcast never occurs in a MINIMAL build. |
| 878 | if (BuildDefs::minimal()) |
| 879 | return false; |
| 880 | // Check that it's a Cast instruction with a Bitcast operation. |
| 881 | auto *Cast = llvm::dyn_cast<InstCast>(Instr); |
| 882 | if (Cast == nullptr || Cast->getCastKind() != InstCast::Bitcast) |
| 883 | return false; |
| 884 | // Check that Src(0) is rematerializable. |
| 885 | auto *Src0Var = llvm::dyn_cast<Variable>(Cast->getSrc(0)); |
| 886 | if (Src0Var == nullptr || !Src0Var->isRematerializable()) |
| 887 | return false; |
| 888 | // Check that Dest and Src(0) have the same type. |
| 889 | Variable *Dest = Cast->getDest(); |
| 890 | if (Dest->getType() != Src0Var->getType()) |
| 891 | return false; |
| 892 | Dest->setRematerializable(Src0Var->getRegNum(), Src0Var->getStackOffset()); |
| 893 | return true; |
| 894 | } |
| 895 | |
| 896 | } // end of anonymous namespace |
| 897 | |
| 898 | /// Scan the function to find additional rematerializable variables. This is |
| 899 | /// possible when the source operand of an InstAssignment is a rematerializable |
| 900 | /// variable, or the same for a pointer-type InstCast::Bitcast, or when an |
| 901 | /// InstArithmetic is an add of a rematerializable variable and an immediate. |
| 902 | /// Note that InstAssignment instructions and pointer-type InstCast::Bitcast |
| 903 | /// instructions generally only come about from the IceConverter's treatment of |
| 904 | /// inttoptr, ptrtoint, and bitcast instructions. TODO(stichnot): Consider |
| 905 | /// other possibilities, however unlikely, such as InstArithmetic::Sub, or |
| 906 | /// commutativity. |
| 907 | void Cfg::findRematerializable() { |
| 908 | // Scan the instructions in order, and repeat until no new opportunities are |
| 909 | // found. It may take more than one iteration because a variable's defining |
| 910 | // block may happen to come after a block where it is used, depending on the |
| 911 | // CfgNode linearization order. |
| 912 | bool FoundNewAssignment; |
| 913 | do { |
| 914 | FoundNewAssignment = false; |
| 915 | for (CfgNode *Node : getNodes()) { |
| 916 | // No need to process Phi instructions. |
| 917 | for (Inst &Instr : Node->getInsts()) { |
| 918 | if (Instr.isDeleted()) |
| 919 | continue; |
| 920 | Variable *Dest = Instr.getDest(); |
| 921 | if (Dest == nullptr || Dest->isRematerializable()) |
| 922 | continue; |
| 923 | if (rematerializeArithmetic(&Instr) || rematerializeAssign(&Instr) || |
| 924 | rematerializeCast(&Instr)) { |
| 925 | FoundNewAssignment = true; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | } while (FoundNewAssignment); |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 932 | void Cfg::doAddressOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 933 | TimerMarker T(TimerStack::TT_doAddressOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 934 | for (CfgNode *Node : Nodes) |
| 935 | Node->doAddressOpt(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 936 | } |
| 937 | |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 938 | namespace { |
| 939 | // ShuffleVectorUtils implements helper functions for rematerializing |
| 940 | // shufflevector instructions from a sequence of extractelement/insertelement |
| 941 | // instructions. It looks for the following pattern: |
| 942 | // |
| 943 | // %t0 = extractelement A, %n0 |
| 944 | // %t1 = extractelement B, %n1 |
| 945 | // %t2 = extractelement C, %n2 |
| 946 | // ... |
| 947 | // %tN = extractelement N, %nN |
| 948 | // %d0 = insertelement undef, %t0, 0 |
| 949 | // %d1 = insertelement %d0, %t1, 1 |
| 950 | // %d2 = insertelement %d1, %t2, 2 |
| 951 | // ... |
| 952 | // %dest = insertelement %d_N-1, %tN, N |
| 953 | // |
| 954 | // where N is num_element(typeof(%dest)), and A, B, C, ... N are at most two |
| 955 | // distinct variables. |
| 956 | namespace ShuffleVectorUtils { |
| 957 | // findAllInserts is used when searching for all the insertelements that are |
| 958 | // used in a shufflevector operation. This function works recursively, when |
| 959 | // invoked with I = i, the function assumes Insts[i] is the last found |
| 960 | // insertelement in the chain. The next insertelement insertruction is saved in |
| 961 | // Insts[i+1]. |
| 962 | bool findAllInserts(Cfg *Func, GlobalContext *Ctx, VariablesMetadata *VM, |
| 963 | CfgVector<const Inst *> *Insts, SizeT I = 0) { |
| 964 | const bool Verbose = BuildDefs::dump() && Func->isVerbose(IceV_ShufMat); |
| 965 | |
| 966 | if (I > Insts->size()) { |
| 967 | if (Verbose) { |
| 968 | Ctx->getStrDump() << "\tToo many inserts.\n"; |
| 969 | } |
| 970 | return false; |
| 971 | } |
| 972 | |
| 973 | const auto *LastInsert = Insts->at(I); |
| 974 | assert(llvm::isa<InstInsertElement>(LastInsert)); |
| 975 | |
| 976 | if (I == Insts->size() - 1) { |
| 977 | // Matching against undef is not really needed because the value in Src(0) |
| 978 | // will be totally overwritten. We still enforce it anyways because the |
| 979 | // PNaCl toolchain generates the bitcode with it. |
| 980 | if (!llvm::isa<ConstantUndef>(LastInsert->getSrc(0))) { |
| 981 | if (Verbose) { |
| 982 | Ctx->getStrDump() << "\tSrc0 is not undef: " << I << " " |
| 983 | << Insts->size(); |
| 984 | LastInsert->dump(Func); |
| 985 | Ctx->getStrDump() << "\n"; |
| 986 | } |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | // The following loop ensures that the insertelements are sorted. In theory, |
| 991 | // we could relax this restriction and allow any order. As long as each |
| 992 | // index appears exactly once, this chain is still a candidate for becoming |
| 993 | // a shufflevector. The Insts vector is traversed backwards because the |
| 994 | // instructions are "enqueued" in reverse order. |
| 995 | int32_t ExpectedElement = 0; |
| 996 | for (const auto *I : reverse_range(*Insts)) { |
| 997 | if (llvm::cast<ConstantInteger32>(I->getSrc(2))->getValue() != |
| 998 | ExpectedElement) { |
| 999 | return false; |
| 1000 | } |
| 1001 | ++ExpectedElement; |
| 1002 | } |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
| 1006 | const auto *Src0V = llvm::cast<Variable>(LastInsert->getSrc(0)); |
| 1007 | const auto *Def = VM->getSingleDefinition(Src0V); |
| 1008 | |
| 1009 | // Only optimize if the first operand in |
| 1010 | // |
| 1011 | // Dest = insertelement A, B, 10 |
| 1012 | // |
| 1013 | // is singly-def'ed. |
| 1014 | if (Def == nullptr) { |
| 1015 | if (Verbose) { |
| 1016 | Ctx->getStrDump() << "\tmulti-def: "; |
| 1017 | (*Insts)[I]->dump(Func); |
| 1018 | Ctx->getStrDump() << "\n"; |
| 1019 | } |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | // We also require the (single) definition to come from an insertelement |
| 1024 | // instruction. |
| 1025 | if (!llvm::isa<InstInsertElement>(Def)) { |
| 1026 | if (Verbose) { |
| 1027 | Ctx->getStrDump() << "\tnot insert element: "; |
| 1028 | Def->dump(Func); |
| 1029 | Ctx->getStrDump() << "\n"; |
| 1030 | } |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | // Everything seems fine, so we save Def in Insts, and delegate the decision |
| 1035 | // to findAllInserts. |
| 1036 | (*Insts)[I + 1] = Def; |
| 1037 | |
| 1038 | return findAllInserts(Func, Ctx, VM, Insts, I + 1); |
| 1039 | } |
| 1040 | |
| 1041 | // insertsLastElement returns true if Insert is inserting an element in the last |
| 1042 | // position of a vector. |
| 1043 | bool insertsLastElement(const Inst &Insert) { |
| 1044 | const Type DestTy = Insert.getDest()->getType(); |
| 1045 | assert(isVectorType(DestTy)); |
| 1046 | const SizeT Elem = |
| 1047 | llvm::cast<ConstantInteger32>(Insert.getSrc(2))->getValue(); |
| 1048 | return Elem == typeNumElements(DestTy) - 1; |
| 1049 | } |
| 1050 | |
| 1051 | // findAllExtracts goes over all the insertelement instructions that are |
| 1052 | // candidates to be replaced by a shufflevector, and searches for all the |
| 1053 | // definitions of the elements being inserted. If all of the elements are the |
| 1054 | // result of an extractelement instruction, and all of the extractelements |
| 1055 | // operate on at most two different sources, than the instructions can be |
| 1056 | // replaced by a shufflevector. |
| 1057 | bool findAllExtracts(Cfg *Func, GlobalContext *Ctx, VariablesMetadata *VM, |
| 1058 | const CfgVector<const Inst *> &Insts, Variable **Src0, |
| 1059 | Variable **Src1, CfgVector<const Inst *> *Extracts) { |
| 1060 | const bool Verbose = BuildDefs::dump() && Func->isVerbose(IceV_ShufMat); |
| 1061 | |
| 1062 | *Src0 = nullptr; |
| 1063 | *Src1 = nullptr; |
| 1064 | assert(Insts.size() > 0); |
| 1065 | for (SizeT I = 0; I < Insts.size(); ++I) { |
| 1066 | const auto *Insert = Insts.at(I); |
| 1067 | const auto *Src1V = llvm::dyn_cast<Variable>(Insert->getSrc(1)); |
| 1068 | if (Src1V == nullptr) { |
| 1069 | if (Verbose) { |
| 1070 | Ctx->getStrDump() << "src(1) is not a variable: "; |
| 1071 | Insert->dump(Func); |
| 1072 | Ctx->getStrDump() << "\n"; |
| 1073 | } |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | const auto *Def = VM->getSingleDefinition(Src1V); |
| 1078 | if (Def == nullptr) { |
| 1079 | if (Verbose) { |
| 1080 | Ctx->getStrDump() << "multi-def src(1): "; |
| 1081 | Insert->dump(Func); |
| 1082 | Ctx->getStrDump() << "\n"; |
| 1083 | } |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | if (!llvm::isa<InstExtractElement>(Def)) { |
| 1088 | if (Verbose) { |
| 1089 | Ctx->getStrDump() << "not extractelement: "; |
| 1090 | Def->dump(Func); |
| 1091 | Ctx->getStrDump() << "\n"; |
| 1092 | } |
| 1093 | return false; |
| 1094 | } |
| 1095 | |
| 1096 | auto *Src = llvm::cast<Variable>(Def->getSrc(0)); |
| 1097 | if (*Src0 == nullptr) { |
| 1098 | // No sources yet. Save Src to Src0. |
| 1099 | *Src0 = Src; |
| 1100 | } else if (*Src1 == nullptr) { |
| 1101 | // We already have a source, so we might save Src in Src1 -- but only if |
| 1102 | // Src0 is not Src. |
| 1103 | if (*Src0 != Src) { |
| 1104 | *Src1 = Src; |
| 1105 | } |
| 1106 | } else if (Src != *Src0 && Src != *Src1) { |
| 1107 | // More than two sources, so we can't rematerialize the shufflevector |
| 1108 | // instruction. |
| 1109 | if (Verbose) { |
| 1110 | Ctx->getStrDump() << "Can't shuffle more than two sources.\n"; |
| 1111 | } |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | (*Extracts)[I] = Def; |
| 1116 | } |
| 1117 | |
| 1118 | // We should have seen at least one source operand. |
| 1119 | assert(*Src0 != nullptr); |
| 1120 | |
| 1121 | // If a second source was not seen, then we just make Src1 = Src0 to simplify |
| 1122 | // things down stream. This should not matter, as all of the indexes in the |
| 1123 | // shufflevector instruction will point to Src0. |
| 1124 | if (*Src1 == nullptr) { |
| 1125 | *Src1 = *Src0; |
| 1126 | } |
| 1127 | |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | } // end of namespace ShuffleVectorUtils |
| 1132 | } // end of anonymous namespace |
| 1133 | |
| 1134 | void Cfg::materializeVectorShuffles() { |
| 1135 | const bool Verbose = BuildDefs::dump() && isVerbose(IceV_ShufMat); |
| 1136 | |
| 1137 | std::unique_ptr<OstreamLocker> L; |
| 1138 | if (Verbose) { |
| 1139 | L.reset(new OstreamLocker(getContext())); |
| 1140 | getContext()->getStrDump() << "\nShuffle materialization:\n"; |
| 1141 | } |
| 1142 | |
| 1143 | // MaxVectorElements is the maximum number of elements in the vector types |
| 1144 | // handled by Subzero. We use it to create the Inserts and Extracts vectors |
| 1145 | // with the appropriate size, thus avoiding resize() calls. |
| 1146 | const SizeT MaxVectorElements = typeNumElements(IceType_v16i8); |
| 1147 | CfgVector<const Inst *> Inserts(MaxVectorElements); |
| 1148 | CfgVector<const Inst *> Extracts(MaxVectorElements); |
| 1149 | |
| 1150 | TimerMarker T(TimerStack::TT_materializeVectorShuffles, this); |
| 1151 | for (CfgNode *Node : Nodes) { |
| 1152 | for (auto &Instr : Node->getInsts()) { |
| 1153 | if (!llvm::isa<InstInsertElement>(Instr)) { |
| 1154 | continue; |
| 1155 | } |
| 1156 | if (!ShuffleVectorUtils::insertsLastElement(Instr)) { |
| 1157 | // To avoid wasting time, we only start the pattern match at the last |
| 1158 | // insertelement instruction -- and go backwards from there. |
| 1159 | continue; |
| 1160 | } |
| 1161 | if (Verbose) { |
| 1162 | getContext()->getStrDump() << "\tCandidate: "; |
| 1163 | Instr.dump(this); |
| 1164 | getContext()->getStrDump() << "\n"; |
| 1165 | } |
| 1166 | Inserts.resize(typeNumElements(Instr.getDest()->getType())); |
| 1167 | Inserts[0] = &Instr; |
| 1168 | if (!ShuffleVectorUtils::findAllInserts(this, getContext(), |
| 1169 | VMetadata.get(), &Inserts)) { |
| 1170 | // If we fail to find a sequence of insertelements, we stop the |
| 1171 | // optimization. |
| 1172 | if (Verbose) { |
| 1173 | getContext()->getStrDump() << "\tFalse alarm.\n"; |
| 1174 | } |
| 1175 | continue; |
| 1176 | } |
| 1177 | if (Verbose) { |
| 1178 | getContext()->getStrDump() << "\tFound the following insertelement: \n"; |
| 1179 | for (auto *I : reverse_range(Inserts)) { |
| 1180 | getContext()->getStrDump() << "\t\t"; |
| 1181 | I->dump(this); |
| 1182 | getContext()->getStrDump() << "\n"; |
| 1183 | } |
| 1184 | } |
| 1185 | Extracts.resize(Inserts.size()); |
| 1186 | Variable *Src0; |
| 1187 | Variable *Src1; |
| 1188 | if (!ShuffleVectorUtils::findAllExtracts(this, getContext(), |
| 1189 | VMetadata.get(), Inserts, &Src0, |
| 1190 | &Src1, &Extracts)) { |
| 1191 | // If we fail to match the definitions of the insertelements' sources |
| 1192 | // with extractelement instructions -- or if those instructions operate |
| 1193 | // on more than two different variables -- we stop the optimization. |
| 1194 | if (Verbose) { |
| 1195 | getContext()->getStrDump() << "\tFailed to match extractelements.\n"; |
| 1196 | } |
| 1197 | continue; |
| 1198 | } |
| 1199 | if (Verbose) { |
| 1200 | getContext()->getStrDump() |
| 1201 | << "\tFound the following insert/extract element pairs: \n"; |
| 1202 | for (SizeT I = 0; I < Inserts.size(); ++I) { |
| 1203 | const SizeT Pos = Inserts.size() - I - 1; |
| 1204 | getContext()->getStrDump() << "\t\tInsert : "; |
| 1205 | Inserts[Pos]->dump(this); |
| 1206 | getContext()->getStrDump() << "\n\t\tExtract: "; |
| 1207 | Extracts[Pos]->dump(this); |
| 1208 | getContext()->getStrDump() << "\n"; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | assert(Src0 != nullptr); |
| 1213 | assert(Src1 != nullptr); |
| 1214 | |
| 1215 | auto *ShuffleVector = |
| 1216 | InstShuffleVector::create(this, Instr.getDest(), Src0, Src1); |
| 1217 | assert(ShuffleVector->getSrc(0) == Src0); |
| 1218 | assert(ShuffleVector->getSrc(1) == Src1); |
| 1219 | for (SizeT I = 0; I < Extracts.size(); ++I) { |
| 1220 | const SizeT Pos = Extracts.size() - I - 1; |
| 1221 | auto *Index = llvm::cast<ConstantInteger32>(Extracts[Pos]->getSrc(1)); |
| 1222 | if (Src0 == Extracts[Pos]->getSrc(0)) { |
| 1223 | ShuffleVector->addIndex(Index); |
| 1224 | } else { |
| 1225 | ShuffleVector->addIndex(llvm::cast<ConstantInteger32>( |
| 1226 | Ctx->getConstantInt32(Index->getValue() + Extracts.size()))); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | if (Verbose) { |
| 1231 | getContext()->getStrDump() << "Created: "; |
| 1232 | ShuffleVector->dump(this); |
| 1233 | getContext()->getStrDump() << "\n"; |
| 1234 | } |
| 1235 | |
| 1236 | Instr.setDeleted(); |
| 1237 | auto &LoweringContext = getTarget()->getContext(); |
Jim Stichnoth | f5fdd23 | 2016-05-09 12:24:36 -0700 | [diff] [blame] | 1238 | LoweringContext.setInsertPoint(instToIterator(&Instr)); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 1239 | LoweringContext.insert(ShuffleVector); |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 1244 | void Cfg::doNopInsertion() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1245 | if (!getFlags().getShouldDoNopInsertion()) |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 1246 | return; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1247 | TimerMarker T(TimerStack::TT_doNopInsertion, this); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1248 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), RPE_NopInsertion, |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 1249 | SequenceNumber); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1250 | for (CfgNode *Node : Nodes) |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 1251 | Node->doNopInsertion(RNG); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1254 | void Cfg::genCode() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1255 | TimerMarker T(TimerStack::TT_genCode, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1256 | for (CfgNode *Node : Nodes) |
| 1257 | Node->genCode(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // Compute the stack frame layout. |
| 1261 | void Cfg::genFrame() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1262 | TimerMarker T(TimerStack::TT_genFrame, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1263 | getTarget()->addProlog(Entry); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1264 | for (CfgNode *Node : Nodes) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1265 | if (Node->getHasReturn()) |
| 1266 | getTarget()->addEpilog(Node); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1269 | void Cfg::computeLoopNestDepth() { |
| 1270 | TimerMarker T(TimerStack::TT_computeLoopNestDepth, this); |
| 1271 | LoopAnalyzer LA(this); |
| 1272 | LA.computeLoopNestDepth(); |
| 1273 | } |
| 1274 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1275 | // This is a lightweight version of live-range-end calculation. Marks the last |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1276 | // use of only those variables whose definition and uses are completely with a |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1277 | // single block. It is a quick single pass and doesn't need to iterate until |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1278 | // convergence. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1279 | void Cfg::livenessLightweight() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1280 | TimerMarker T(TimerStack::TT_livenessLightweight, this); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1281 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1282 | for (CfgNode *Node : Nodes) |
| 1283 | Node->livenessLightweight(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | void Cfg::liveness(LivenessMode Mode) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1287 | TimerMarker T(TimerStack::TT_liveness, this); |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 1288 | // Destroying the previous (if any) Liveness information clears the Liveness |
| 1289 | // allocator TLS pointer. |
| 1290 | Live = nullptr; |
| 1291 | Live = Liveness::create(this, Mode); |
| 1292 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1293 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1294 | Live->init(); |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 1295 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1296 | // Initialize with all nodes needing to be processed. |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 1297 | BitVector NeedToProcess(Nodes.size(), true); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1298 | while (NeedToProcess.any()) { |
| 1299 | // Iterate in reverse topological order to speed up convergence. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 1300 | for (CfgNode *Node : reverse_range(Nodes)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1301 | if (NeedToProcess[Node->getIndex()]) { |
| 1302 | NeedToProcess[Node->getIndex()] = false; |
| 1303 | bool Changed = Node->liveness(getLiveness()); |
| 1304 | if (Changed) { |
| 1305 | // If the beginning-of-block liveness changed since the last |
| 1306 | // iteration, mark all in-edges as needing to be processed. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1307 | for (CfgNode *Pred : Node->getInEdges()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1308 | NeedToProcess[Pred->getIndex()] = true; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | if (Mode == Liveness_Intervals) { |
| 1314 | // Reset each variable's live range. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1315 | for (Variable *Var : Variables) |
| 1316 | Var->resetLiveRange(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1317 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1318 | // Make a final pass over each node to delete dead instructions, collect the |
| 1319 | // first and last instruction numbers, and add live range segments for that |
| 1320 | // node. |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1321 | for (CfgNode *Node : Nodes) { |
| 1322 | InstNumberT FirstInstNum = Inst::NumberSentinel; |
| 1323 | InstNumberT LastInstNum = Inst::NumberSentinel; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1324 | for (Inst &I : Node->getPhis()) { |
| 1325 | I.deleteIfDead(); |
| 1326 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1327 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1328 | FirstInstNum = I.getNumber(); |
| 1329 | assert(I.getNumber() > LastInstNum); |
| 1330 | LastInstNum = I.getNumber(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1331 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1332 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1333 | for (Inst &I : Node->getInsts()) { |
| 1334 | I.deleteIfDead(); |
| 1335 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1336 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1337 | FirstInstNum = I.getNumber(); |
| 1338 | assert(I.getNumber() > LastInstNum); |
| 1339 | LastInstNum = I.getNumber(); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | if (Mode == Liveness_Intervals) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1343 | // Special treatment for live in-args. Their liveness needs to extend |
| 1344 | // beyond the beginning of the function, otherwise an arg whose only use |
| 1345 | // is in the first instruction will end up having the trivial live range |
| 1346 | // [2,2) and will *not* interfere with other arguments. So if the first |
| 1347 | // instruction of the method is "r=arg1+arg2", both args may be assigned |
| 1348 | // the same register. This is accomplished by extending the entry block's |
| 1349 | // instruction range from [2,n) to [1,n) which will transform the |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 1350 | // problematic [2,2) live ranges into [1,2). This extension works because |
| 1351 | // the entry node is guaranteed to have the lowest instruction numbers. |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 1352 | if (Node == getEntryNode()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 1353 | FirstInstNum = Inst::NumberExtended; |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 1354 | // Just in case the entry node somehow contains no instructions... |
| 1355 | if (LastInstNum == Inst::NumberSentinel) |
| 1356 | LastInstNum = FirstInstNum; |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 1357 | } |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 1358 | // If this node somehow contains no instructions, don't bother trying to |
| 1359 | // add liveness intervals for it, because variables that are live-in and |
| 1360 | // live-out will have a bogus interval added. |
| 1361 | if (FirstInstNum != Inst::NumberSentinel) |
| 1362 | Node->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1363 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1367 | // Traverse every Variable of every Inst and verify that it appears within the |
| 1368 | // Variable's computed live range. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1369 | bool Cfg::validateLiveness() const { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1370 | TimerMarker T(TimerStack::TT_validateLiveness, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1371 | bool Valid = true; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 1372 | OstreamLocker L(Ctx); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1373 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1374 | for (CfgNode *Node : Nodes) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 1375 | Inst *FirstInst = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1376 | for (Inst &Instr : Node->getInsts()) { |
| 1377 | if (Instr.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1378 | continue; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 1379 | if (FirstInst == nullptr) |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1380 | FirstInst = &Instr; |
| 1381 | InstNumberT InstNumber = Instr.getNumber(); |
| 1382 | if (Variable *Dest = Instr.getDest()) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1383 | if (!Dest->getIgnoreLiveness()) { |
| 1384 | bool Invalid = false; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1385 | constexpr bool IsDest = true; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1386 | if (!Dest->getLiveRange().containsValue(InstNumber, IsDest)) |
| 1387 | Invalid = true; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1388 | // Check that this instruction actually *begins* Dest's live range, |
| 1389 | // by checking that Dest is not live in the previous instruction. As |
| 1390 | // a special exception, we don't check this for the first instruction |
| 1391 | // of the block, because a Phi temporary may be live at the end of |
| 1392 | // the previous block, and if it is also assigned in the first |
| 1393 | // instruction of this block, the adjacent live ranges get merged. |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 1394 | if (&Instr != FirstInst && !Instr.isDestRedefined() && |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1395 | Dest->getLiveRange().containsValue(InstNumber - 1, IsDest)) |
| 1396 | Invalid = true; |
| 1397 | if (Invalid) { |
| 1398 | Valid = false; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1399 | Str << "Liveness error: inst " << Instr.getNumber() << " dest "; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1400 | Dest->dump(this); |
| 1401 | Str << " live range " << Dest->getLiveRange() << "\n"; |
| 1402 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1403 | } |
| 1404 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1405 | FOREACH_VAR_IN_INST(Var, Instr) { |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 1406 | static constexpr bool IsDest = false; |
| 1407 | if (!Var->getIgnoreLiveness() && |
| 1408 | !Var->getLiveRange().containsValue(InstNumber, IsDest)) { |
| 1409 | Valid = false; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1410 | Str << "Liveness error: inst " << Instr.getNumber() << " var "; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 1411 | Var->dump(this); |
| 1412 | Str << " live range " << Var->getLiveRange() << "\n"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | return Valid; |
| 1418 | } |
| 1419 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 1420 | void Cfg::contractEmptyNodes() { |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1421 | // If we're decorating the asm output with register liveness info, this |
| 1422 | // information may become corrupted or incorrect after contracting nodes that |
| 1423 | // contain only redundant assignments. As such, we disable this pass when |
| 1424 | // DecorateAsm is specified. This may make the resulting code look more |
| 1425 | // branchy, but it should have no effect on the register assignments. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1426 | if (getFlags().getDecorateAsm()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1427 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 1428 | for (CfgNode *Node : Nodes) { |
| 1429 | Node->contractIfEmpty(); |
| 1430 | } |
| 1431 | } |
| 1432 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 1433 | void Cfg::doBranchOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1434 | TimerMarker T(TimerStack::TT_doBranchOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1435 | for (auto I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 1436 | auto NextNode = I + 1; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 1437 | (*I)->doBranchOpt(NextNode == E ? nullptr : *NextNode); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1441 | void Cfg::markNodesForSandboxing() { |
| 1442 | for (const InstJumpTable *JT : JumpTables) |
| 1443 | for (SizeT I = 0; I < JT->getNumTargets(); ++I) |
| 1444 | JT->getTarget(I)->setNeedsAlignment(); |
| 1445 | } |
| 1446 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1447 | // ======================== Dump routines ======================== // |
| 1448 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1449 | // emitTextHeader() is not target-specific (apart from what is abstracted by |
| 1450 | // the Assembler), so it is defined here rather than in the target lowering |
| 1451 | // class. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1452 | void Cfg::emitTextHeader(GlobalString Name, GlobalContext *Ctx, |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 1453 | const Assembler *Asm) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1454 | if (!BuildDefs::dump()) |
Jim Stichnoth | 729dbd0 | 2015-02-25 14:48:43 -0800 | [diff] [blame] | 1455 | return; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1456 | Ostream &Str = Ctx->getStrEmit(); |
| 1457 | Str << "\t.text\n"; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1458 | if (getFlags().getFunctionSections()) |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1459 | Str << "\t.section\t.text." << Name << ",\"ax\",%progbits\n"; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1460 | if (!Asm->getInternal() || getFlags().getDisableInternal()) { |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1461 | Str << "\t.globl\t" << Name << "\n"; |
| 1462 | Str << "\t.type\t" << Name << ",%function\n"; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1463 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1464 | Str << "\t" << Asm->getAlignDirective() << " " |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 1465 | << Asm->getBundleAlignLog2Bytes() << ",0x"; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 1466 | for (uint8_t I : Asm->getNonExecBundlePadding()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1467 | Str.write_hex(I); |
| 1468 | Str << "\n"; |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1469 | Str << Name << ":\n"; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1472 | void Cfg::emitJumpTables() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1473 | switch (getFlags().getOutFileType()) { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1474 | case FT_Elf: |
| 1475 | case FT_Iasm: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1476 | // The emission needs to be delayed until the after the text section so |
| 1477 | // save the offsets in the global context. |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1478 | for (const InstJumpTable *JumpTable : JumpTables) { |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 1479 | Ctx->addJumpTableData(JumpTable->toJumpTableData(getAssembler())); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1480 | } |
| 1481 | } break; |
| 1482 | case FT_Asm: { |
| 1483 | // Emit the assembly directly so we don't need to hang on to all the names |
| 1484 | for (const InstJumpTable *JumpTable : JumpTables) |
| 1485 | getTarget()->emitJumpTable(this, JumpTable); |
| 1486 | } break; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1490 | void Cfg::emit() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1491 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1492 | return; |
Karl Schimpf | b6e9b89 | 2016-03-08 12:27:12 -0800 | [diff] [blame] | 1493 | TimerMarker T(TimerStack::TT_emitAsm, this); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1494 | if (getFlags().getDecorateAsm()) { |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1495 | renumberInstructions(); |
| 1496 | getVMetadata()->init(VMK_Uses); |
| 1497 | liveness(Liveness_Basic); |
| 1498 | dump("After recomputing liveness for -decorate-asm"); |
| 1499 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 1500 | OstreamLocker L(Ctx); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1501 | Ostream &Str = Ctx->getStrEmit(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1502 | const Assembler *Asm = getAssembler<>(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1503 | const bool NeedSandboxing = getFlags().getUseSandboxing(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1504 | |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1505 | emitTextHeader(FunctionName, Ctx, Asm); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1506 | if (getFlags().getDecorateAsm()) { |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1507 | for (Variable *Var : getVariables()) { |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 1508 | if (Var->getStackOffset() && !Var->isRematerializable()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 1509 | Str << "\t" << Var->getSymbolicStackOffset() << " = " |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1510 | << Var->getStackOffset() << "\n"; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1514 | for (CfgNode *Node : Nodes) { |
| 1515 | if (NeedSandboxing && Node->needsAlignment()) { |
| 1516 | Str << "\t" << Asm->getAlignDirective() << " " |
| 1517 | << Asm->getBundleAlignLog2Bytes() << "\n"; |
| 1518 | } |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1519 | Node->emit(this); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1520 | } |
| 1521 | emitJumpTables(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1522 | Str << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1525 | void Cfg::emitIAS() { |
Karl Schimpf | b6e9b89 | 2016-03-08 12:27:12 -0800 | [diff] [blame] | 1526 | TimerMarker T(TimerStack::TT_emitAsm, this); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1527 | // The emitIAS() routines emit into the internal assembler buffer, so there's |
| 1528 | // no need to lock the streams. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1529 | const bool NeedSandboxing = getFlags().getUseSandboxing(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1530 | for (CfgNode *Node : Nodes) { |
| 1531 | if (NeedSandboxing && Node->needsAlignment()) |
| 1532 | getAssembler()->alignCfgNode(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1533 | Node->emitIAS(this); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1534 | } |
| 1535 | emitJumpTables(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
John Porto | a3984a1 | 2016-04-01 11:14:30 -0700 | [diff] [blame] | 1538 | size_t Cfg::getTotalMemoryMB() const { |
| 1539 | constexpr size_t _1MB = 1024 * 1024; |
| 1540 | assert(Allocator != nullptr); |
| 1541 | assert(CfgAllocatorTraits::current() == Allocator.get()); |
| 1542 | return Allocator->getTotalMemory() / _1MB; |
| 1543 | } |
| 1544 | |
| 1545 | size_t Cfg::getLivenessMemoryMB() const { |
| 1546 | constexpr size_t _1MB = 1024 * 1024; |
| 1547 | if (Live == nullptr) { |
| 1548 | return 0; |
| 1549 | } |
| 1550 | return Live->getAllocator()->getTotalMemory() / _1MB; |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1553 | // Dumps the IR with an optional introductory message. |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 1554 | void Cfg::dump(const char *Message) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1555 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1556 | return; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1557 | if (!isVerbose()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1558 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 1559 | OstreamLocker L(Ctx); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1560 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 1561 | if (Message[0]) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1562 | Str << "================ " << Message << " ================\n"; |
Jim Stichnoth | 3dd1bb8 | 2016-02-27 09:05:50 -0800 | [diff] [blame] | 1563 | if (isVerbose(IceV_Mem)) { |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1564 | Str << "Memory size = " << getTotalMemoryMB() << " MB\n"; |
Jim Stichnoth | 3dd1bb8 | 2016-02-27 09:05:50 -0800 | [diff] [blame] | 1565 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1566 | setCurrentNode(getEntryNode()); |
| 1567 | // Print function name+args |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1568 | if (isVerbose(IceV_Instructions)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1569 | Str << "define "; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1570 | if (getInternal() && !getFlags().getDisableInternal()) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1571 | Str << "internal "; |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1572 | Str << ReturnType << " @" << getFunctionName() << "("; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1573 | for (SizeT i = 0; i < Args.size(); ++i) { |
| 1574 | if (i > 0) |
| 1575 | Str << ", "; |
| 1576 | Str << Args[i]->getType() << " "; |
| 1577 | Args[i]->dump(this); |
| 1578 | } |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 1579 | // Append an extra copy of the function name here, in order to print its |
| 1580 | // size stats but not mess up lit tests. |
| 1581 | Str << ") { # " << getFunctionNameAndSize() << "\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1582 | } |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 1583 | resetCurrentNode(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1584 | if (isVerbose(IceV_Liveness)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1585 | // Print summary info about variables |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1586 | for (Variable *Var : Variables) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1587 | Str << "// multiblock="; |
| 1588 | if (getVMetadata()->isTracked(Var)) |
| 1589 | Str << getVMetadata()->isMultiBlock(Var); |
| 1590 | else |
| 1591 | Str << "?"; |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1592 | Str << " defs="; |
| 1593 | bool FirstPrint = true; |
| 1594 | if (VMetadata->getKind() != VMK_Uses) { |
| 1595 | if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var)) { |
| 1596 | Str << FirstDef->getNumber(); |
| 1597 | FirstPrint = false; |
| 1598 | } |
| 1599 | } |
| 1600 | if (VMetadata->getKind() == VMK_All) { |
| 1601 | for (const Inst *Instr : VMetadata->getLatterDefinitions(Var)) { |
| 1602 | if (!FirstPrint) |
| 1603 | Str << ","; |
| 1604 | Str << Instr->getNumber(); |
| 1605 | FirstPrint = false; |
| 1606 | } |
| 1607 | } |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1608 | Str << " weight=" << Var->getWeight(this) << " "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1609 | Var->dump(this); |
| 1610 | Str << " LIVE=" << Var->getLiveRange() << "\n"; |
| 1611 | } |
| 1612 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1613 | // Print each basic block |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1614 | for (CfgNode *Node : Nodes) |
| 1615 | Node->dump(this); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1616 | if (isVerbose(IceV_Instructions)) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1617 | Str << "}\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | } // end of namespace Ice |