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 |
| 11 | /// This file implements the Cfg class, including constant pool |
| 12 | /// management. |
| 13 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "IceCfg.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 17 | |
| 18 | #include "IceAssembler.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" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 25 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 27 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 28 | |
| 29 | namespace Ice { |
| 30 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 31 | ICE_TLS_DEFINE_FIELD(const Cfg *, Cfg, CurrentCfg); |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 32 | |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 33 | ArenaAllocator<> *getCurrentCfgAllocator() { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 34 | return Cfg::getCurrentCfgAllocator(); |
| 35 | } |
| 36 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 37 | Cfg::Cfg(GlobalContext *Ctx, uint32_t SequenceNumber) |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 38 | : Ctx(Ctx), SequenceNumber(SequenceNumber), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 39 | VMask(Ctx->getFlags().getVerbose()), NextInstNumber(Inst::NumberInitial), |
| 40 | Allocator(new ArenaAllocator<>()), Live(nullptr), |
| 41 | Target(TargetLowering::createLowering(Ctx->getFlags().getTargetArch(), |
| 42 | this)), |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 43 | VMetadata(new VariablesMetadata(this)), |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 44 | TargetAssembler(TargetLowering::createAssembler( |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 45 | Ctx->getFlags().getTargetArch(), this)) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 46 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 47 | |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 48 | Cfg::~Cfg() { assert(ICE_TLS_GET_FIELD(CurrentCfg) == nullptr); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 49 | |
| 50 | void Cfg::setError(const IceString &Message) { |
| 51 | HasError = true; |
| 52 | ErrorMessage = Message; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 55 | CfgNode *Cfg::makeNode() { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 56 | SizeT LabelIndex = Nodes.size(); |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 57 | CfgNode *Node = CfgNode::create(this, LabelIndex); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 58 | Nodes.push_back(Node); |
| 59 | return Node; |
| 60 | } |
| 61 | |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 62 | void Cfg::swapNodes(NodeList &NewNodes) { |
| 63 | Nodes.swap(NewNodes); |
| 64 | for (SizeT I = 0, NumNodes = getNumNodes(); I < NumNodes; ++I) |
| 65 | Nodes[I]->resetIndex(I); |
| 66 | } |
| 67 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 68 | void Cfg::addArg(Variable *Arg) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 69 | Arg->setIsArg(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 70 | Args.push_back(Arg); |
| 71 | } |
| 72 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 73 | void Cfg::addImplicitArg(Variable *Arg) { |
| 74 | Arg->setIsImplicitArg(); |
| 75 | ImplicitArgs.push_back(Arg); |
| 76 | } |
| 77 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 78 | // Returns whether the stack frame layout has been computed yet. This |
| 79 | // is used for dumping the stack frame location of Variables. |
| 80 | bool Cfg::hasComputedFrame() const { return getTarget()->hasComputedFrame(); } |
| 81 | |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 82 | namespace { |
| 83 | constexpr char BlockNameGlobalPrefix[] = ".L$profiler$block_name$"; |
| 84 | constexpr char BlockStatsGlobalPrefix[] = ".L$profiler$block_info$"; |
| 85 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 86 | VariableDeclaration *nodeNameDeclaration(GlobalContext *Ctx, |
| 87 | const IceString &NodeAsmName) { |
| 88 | VariableDeclaration *Var = VariableDeclaration::create(Ctx); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 89 | Var->setName(BlockNameGlobalPrefix + NodeAsmName); |
| 90 | Var->setIsConstant(true); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 91 | Var->addInitializer(VariableDeclaration::DataInitializer::create( |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 92 | NodeAsmName.data(), NodeAsmName.size() + 1)); |
| 93 | const SizeT Int64ByteSize = typeWidthInBytes(IceType_i64); |
| 94 | Var->setAlignment(Int64ByteSize); // Wasteful, 32-bit could use 4 bytes. |
| 95 | return Var; |
| 96 | } |
| 97 | |
| 98 | VariableDeclaration * |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 99 | blockProfilingInfoDeclaration(GlobalContext *Ctx, const IceString &NodeAsmName, |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 100 | VariableDeclaration *NodeNameDeclaration) { |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 101 | VariableDeclaration *Var = VariableDeclaration::create(Ctx); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 102 | Var->setName(BlockStatsGlobalPrefix + NodeAsmName); |
| 103 | const SizeT Int64ByteSize = typeWidthInBytes(IceType_i64); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 104 | Var->addInitializer( |
| 105 | VariableDeclaration::ZeroInitializer::create(Int64ByteSize)); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 106 | |
| 107 | const RelocOffsetT NodeNameDeclarationOffset = 0; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 108 | Var->addInitializer(VariableDeclaration::RelocInitializer::create( |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 109 | NodeNameDeclaration, NodeNameDeclarationOffset)); |
| 110 | Var->setAlignment(Int64ByteSize); |
| 111 | return Var; |
| 112 | } |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 113 | } // end of anonymous namespace |
| 114 | |
| 115 | void Cfg::profileBlocks() { |
| 116 | if (GlobalInits == nullptr) |
| 117 | GlobalInits.reset(new VariableDeclarationList()); |
| 118 | |
| 119 | for (CfgNode *Node : Nodes) { |
| 120 | IceString NodeAsmName = Node->getAsmName(); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 121 | GlobalInits->push_back(nodeNameDeclaration(Ctx, NodeAsmName)); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 122 | GlobalInits->push_back( |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 123 | blockProfilingInfoDeclaration(Ctx, NodeAsmName, GlobalInits->back())); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 124 | Node->profileExecutionCount(GlobalInits->back()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bool Cfg::isProfileGlobal(const VariableDeclaration &Var) { |
| 129 | return Var.getName().find(BlockStatsGlobalPrefix) == 0; |
| 130 | } |
| 131 | |
| 132 | void Cfg::addCallToProfileSummary() { |
| 133 | // The call(s) to __Sz_profile_summary are added by the profiler in functions |
| 134 | // that cause the program to exit. This function is defined in |
| 135 | // runtime/szrt_profiler.c. |
| 136 | Constant *ProfileSummarySym = |
| 137 | Ctx->getConstantExternSym("__Sz_profile_summary"); |
| 138 | constexpr SizeT NumArgs = 0; |
| 139 | constexpr Variable *Void = nullptr; |
| 140 | constexpr bool HasTailCall = false; |
| 141 | auto *Call = |
| 142 | InstCall::create(this, NumArgs, Void, ProfileSummarySym, HasTailCall); |
| 143 | getEntryNode()->getInsts().push_front(Call); |
| 144 | } |
| 145 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 146 | void Cfg::translate() { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 147 | if (hasError()) |
| 148 | return; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 149 | // FunctionTimer conditionally pushes/pops a TimerMarker if |
| 150 | // TimeEachFunction is enabled. |
| 151 | std::unique_ptr<TimerMarker> FunctionTimer; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 152 | if (BuildDefs::dump()) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 153 | const IceString &TimingFocusOn = |
| 154 | getContext()->getFlags().getTimingFocusOn(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 155 | const IceString &Name = getFunctionName(); |
| 156 | if (TimingFocusOn == "*" || TimingFocusOn == Name) { |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 157 | setFocusedTiming(); |
| 158 | getContext()->resetTimer(GlobalContext::TSK_Default); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 159 | getContext()->setTimerName(GlobalContext::TSK_Default, Name); |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 160 | } |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 161 | if (getContext()->getFlags().getTimeEachFunction()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 162 | FunctionTimer.reset(new TimerMarker( |
| 163 | getContext()->getTimerID(GlobalContext::TSK_Funcs, Name), |
| 164 | getContext(), GlobalContext::TSK_Funcs)); |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 165 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 166 | TimerMarker T(TimerStack::TT_translate, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 167 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 168 | dump("Initial CFG"); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 169 | |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 170 | if (getContext()->getFlags().getEnableBlockProfile()) { |
| 171 | profileBlocks(); |
| 172 | // TODO(jpp): this is fragile, at best. Figure out a better way of detecting |
| 173 | // exit functions. |
| 174 | if (GlobalContext::matchSymbolName(getFunctionName(), "exit")) { |
| 175 | addCallToProfileSummary(); |
| 176 | } |
| 177 | dump("Profiled CFG"); |
| 178 | } |
| 179 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 180 | // The set of translation passes and their order are determined by |
| 181 | // the target. |
| 182 | getTarget()->translate(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 183 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 184 | dump("Final output"); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 185 | if (getFocusedTiming()) |
| 186 | getContext()->dumpTimers(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 189 | void Cfg::computeInOutEdges() { |
| 190 | // Compute the out-edges. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 191 | for (CfgNode *Node : Nodes) |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 192 | Node->computeSuccessors(); |
| 193 | |
| 194 | // Prune any unreachable nodes before computing in-edges. |
| 195 | SizeT NumNodes = getNumNodes(); |
| 196 | llvm::BitVector Reachable(NumNodes); |
| 197 | llvm::BitVector Pending(NumNodes); |
| 198 | Pending.set(getEntryNode()->getIndex()); |
| 199 | while (true) { |
| 200 | int Index = Pending.find_first(); |
| 201 | if (Index == -1) |
| 202 | break; |
| 203 | Pending.reset(Index); |
| 204 | Reachable.set(Index); |
| 205 | CfgNode *Node = Nodes[Index]; |
| 206 | assert(Node->getIndex() == (SizeT)Index); |
| 207 | for (CfgNode *Succ : Node->getOutEdges()) { |
| 208 | SizeT SuccIndex = Succ->getIndex(); |
| 209 | if (!Reachable.test(SuccIndex)) |
| 210 | Pending.set(SuccIndex); |
| 211 | } |
| 212 | } |
| 213 | SizeT Dest = 0; |
| 214 | for (SizeT Source = 0; Source < NumNodes; ++Source) { |
| 215 | if (Reachable.test(Source)) { |
| 216 | Nodes[Dest] = Nodes[Source]; |
| 217 | Nodes[Dest]->resetIndex(Dest); |
| 218 | // Compute the in-edges. |
| 219 | Nodes[Dest]->computePredecessors(); |
| 220 | ++Dest; |
| 221 | } |
| 222 | } |
| 223 | Nodes.resize(Dest); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 226 | void Cfg::renumberInstructions() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 227 | TimerMarker T(TimerStack::TT_renumberInstructions, this); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 228 | NextInstNumber = Inst::NumberInitial; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 229 | for (CfgNode *Node : Nodes) |
| 230 | Node->renumberInstructions(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 233 | // placePhiLoads() must be called before placePhiStores(). |
| 234 | void Cfg::placePhiLoads() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 235 | TimerMarker T(TimerStack::TT_placePhiLoads, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 236 | for (CfgNode *Node : Nodes) |
| 237 | Node->placePhiLoads(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // placePhiStores() must be called after placePhiLoads(). |
| 241 | void Cfg::placePhiStores() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 242 | TimerMarker T(TimerStack::TT_placePhiStores, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 243 | for (CfgNode *Node : Nodes) |
| 244 | Node->placePhiStores(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void Cfg::deletePhis() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 248 | TimerMarker T(TimerStack::TT_deletePhis, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 249 | for (CfgNode *Node : Nodes) |
| 250 | Node->deletePhis(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 253 | void Cfg::advancedPhiLowering() { |
| 254 | TimerMarker T(TimerStack::TT_advancedPhiLowering, this); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 255 | // Clear all previously computed live ranges (but not live-in/live-out bit |
| 256 | // vectors or last-use markers), because the follow-on register allocation is |
| 257 | // only concerned with live ranges across the newly created blocks. |
| 258 | for (Variable *Var : Variables) { |
| 259 | Var->getLiveRange().reset(); |
| 260 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 261 | // This splits edges and appends new nodes to the end of the node |
| 262 | // list. This can invalidate iterators, so don't use an iterator. |
| 263 | SizeT NumNodes = getNumNodes(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 264 | SizeT NumVars = getNumVariables(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 265 | for (SizeT I = 0; I < NumNodes; ++I) |
| 266 | Nodes[I]->advancedPhiLowering(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 267 | |
| 268 | TimerMarker TT(TimerStack::TT_lowerPhiAssignments, this); |
| 269 | if (true) { |
| 270 | // The following code does an in-place update of liveness and live ranges as |
| 271 | // a result of adding the new phi edge split nodes. |
| 272 | getLiveness()->initPhiEdgeSplits(Nodes.begin() + NumNodes, |
| 273 | Variables.begin() + NumVars); |
| 274 | TimerMarker TTT(TimerStack::TT_liveness, this); |
| 275 | // Iterate over the newly added nodes to add their liveness info. |
| 276 | for (auto I = Nodes.begin() + NumNodes, E = Nodes.end(); I != E; ++I) { |
| 277 | InstNumberT FirstInstNum = getNextInstNumber(); |
| 278 | (*I)->renumberInstructions(); |
| 279 | InstNumberT LastInstNum = getNextInstNumber() - 1; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 280 | (*I)->liveness(getLiveness()); |
| 281 | (*I)->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
| 282 | } |
| 283 | } else { |
| 284 | // The following code does a brute-force recalculation of live ranges as a |
| 285 | // result of adding the new phi edge split nodes. The liveness calculation |
| 286 | // is particularly expensive because the new nodes are not yet in a proper |
| 287 | // topological order and so convergence is slower. |
| 288 | // |
| 289 | // This code is kept here for reference and can be temporarily enabled in |
| 290 | // case the incremental code is under suspicion. |
| 291 | renumberInstructions(); |
| 292 | liveness(Liveness_Intervals); |
| 293 | getVMetadata()->init(VMK_All); |
| 294 | } |
| 295 | Target->regAlloc(RAK_Phi); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // Find a reasonable placement for nodes that have not yet been |
| 299 | // placed, while maintaining the same relative ordering among already |
| 300 | // placed nodes. |
| 301 | void Cfg::reorderNodes() { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 302 | // TODO(ascull): it would be nice if the switch tests were always followed |
| 303 | // by the default case to allow for fall through. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 304 | typedef std::list<CfgNode *> PlacedList; |
| 305 | PlacedList Placed; // Nodes with relative placement locked down |
| 306 | PlacedList Unreachable; // Unreachable nodes |
| 307 | PlacedList::iterator NoPlace = Placed.end(); |
| 308 | // Keep track of where each node has been tentatively placed so that |
| 309 | // we can manage insertions into the middle. |
| 310 | std::vector<PlacedList::iterator> PlaceIndex(Nodes.size(), NoPlace); |
| 311 | for (CfgNode *Node : Nodes) { |
| 312 | // The "do ... while(0);" construct is to factor out the |
| 313 | // --PlaceIndex and assert() statements before moving to the next |
| 314 | // node. |
| 315 | do { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 316 | if (Node != getEntryNode() && Node->getInEdges().empty()) { |
| 317 | // The node has essentially been deleted since it is not a |
| 318 | // successor of any other node. |
| 319 | Unreachable.push_back(Node); |
| 320 | PlaceIndex[Node->getIndex()] = Unreachable.end(); |
| 321 | Node->setNeedsPlacement(false); |
| 322 | continue; |
| 323 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 324 | if (!Node->needsPlacement()) { |
| 325 | // Add to the end of the Placed list. |
| 326 | Placed.push_back(Node); |
| 327 | PlaceIndex[Node->getIndex()] = Placed.end(); |
| 328 | continue; |
| 329 | } |
| 330 | Node->setNeedsPlacement(false); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 331 | // Assume for now that the unplaced node is from edge-splitting |
| 332 | // and therefore has 1 in-edge and 1 out-edge (actually, possibly |
| 333 | // more than 1 in-edge if the predecessor node was contracted). |
| 334 | // If this changes in the future, rethink the strategy. |
| 335 | assert(Node->getInEdges().size() >= 1); |
| 336 | assert(Node->getOutEdges().size() == 1); |
| 337 | |
| 338 | // If it's a (non-critical) edge where the successor has a single |
| 339 | // in-edge, then place it before the successor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 340 | CfgNode *Succ = Node->getOutEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 341 | if (Succ->getInEdges().size() == 1 && |
| 342 | PlaceIndex[Succ->getIndex()] != NoPlace) { |
| 343 | Placed.insert(PlaceIndex[Succ->getIndex()], Node); |
| 344 | PlaceIndex[Node->getIndex()] = PlaceIndex[Succ->getIndex()]; |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | // Otherwise, place it after the (first) predecessor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 349 | CfgNode *Pred = Node->getInEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 350 | auto PredPosition = PlaceIndex[Pred->getIndex()]; |
| 351 | // It shouldn't be the case that PredPosition==NoPlace, but if |
| 352 | // that somehow turns out to be true, we just insert Node before |
| 353 | // PredPosition=NoPlace=Placed.end() . |
| 354 | if (PredPosition != NoPlace) |
| 355 | ++PredPosition; |
| 356 | Placed.insert(PredPosition, Node); |
| 357 | PlaceIndex[Node->getIndex()] = PredPosition; |
| 358 | } while (0); |
| 359 | |
| 360 | --PlaceIndex[Node->getIndex()]; |
| 361 | assert(*PlaceIndex[Node->getIndex()] == Node); |
| 362 | } |
| 363 | |
| 364 | // Reorder Nodes according to the built-up lists. |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 365 | NodeList Reordered; |
| 366 | Reordered.reserve(Placed.size() + Unreachable.size()); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 367 | for (CfgNode *Node : Placed) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 368 | Reordered.push_back(Node); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 369 | for (CfgNode *Node : Unreachable) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 370 | Reordered.push_back(Node); |
| 371 | assert(getNumNodes() == Reordered.size()); |
| 372 | swapNodes(Reordered); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 375 | namespace { |
| 376 | void getRandomPostOrder(CfgNode *Node, llvm::BitVector &ToVisit, |
| 377 | Ice::NodeList &PostOrder, |
| 378 | Ice::RandomNumberGenerator *RNG) { |
| 379 | assert(ToVisit[Node->getIndex()]); |
| 380 | ToVisit[Node->getIndex()] = false; |
| 381 | NodeList Outs = Node->getOutEdges(); |
| 382 | Ice::RandomShuffle(Outs.begin(), Outs.end(), |
| 383 | [RNG](int N) { return RNG->next(N); }); |
| 384 | for (CfgNode *Next : Outs) { |
| 385 | if (ToVisit[Next->getIndex()]) |
| 386 | getRandomPostOrder(Next, ToVisit, PostOrder, RNG); |
| 387 | } |
| 388 | PostOrder.push_back(Node); |
| 389 | } |
| 390 | } // end of anonymous namespace |
| 391 | |
| 392 | void Cfg::shuffleNodes() { |
| 393 | if (!Ctx->getFlags().shouldReorderBasicBlocks()) |
| 394 | return; |
| 395 | |
| 396 | NodeList ReversedReachable; |
| 397 | NodeList Unreachable; |
| 398 | llvm::BitVector ToVisit(Nodes.size(), true); |
| 399 | // Traverse from entry node. |
| 400 | getRandomPostOrder(getEntryNode(), ToVisit, ReversedReachable, |
| 401 | &Ctx->getRNG()); |
| 402 | // Collect the unreachable nodes. |
| 403 | for (CfgNode *Node : Nodes) |
| 404 | if (ToVisit[Node->getIndex()]) |
| 405 | Unreachable.push_back(Node); |
| 406 | // Copy the layout list to the Nodes. |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 407 | NodeList Shuffled; |
| 408 | Shuffled.reserve(ReversedReachable.size() + Unreachable.size()); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 409 | for (CfgNode *Node : reverse_range(ReversedReachable)) |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 410 | Shuffled.push_back(Node); |
| 411 | for (CfgNode *Node : Unreachable) |
| 412 | Shuffled.push_back(Node); |
| 413 | assert(Nodes.size() == Shuffled.size()); |
| 414 | swapNodes(Shuffled); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 415 | |
| 416 | dump("After basic block shuffling"); |
| 417 | } |
| 418 | |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 419 | void Cfg::doArgLowering() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 420 | TimerMarker T(TimerStack::TT_doArgLowering, this); |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 421 | getTarget()->lowerArguments(); |
| 422 | } |
| 423 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 424 | void Cfg::doAddressOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 425 | TimerMarker T(TimerStack::TT_doAddressOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 426 | for (CfgNode *Node : Nodes) |
| 427 | Node->doAddressOpt(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 430 | void Cfg::doNopInsertion() { |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 431 | if (!Ctx->getFlags().shouldDoNopInsertion()) |
| 432 | return; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 433 | TimerMarker T(TimerStack::TT_doNopInsertion, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 434 | for (CfgNode *Node : Nodes) |
| 435 | Node->doNopInsertion(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 438 | void Cfg::genCode() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 439 | TimerMarker T(TimerStack::TT_genCode, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 440 | for (CfgNode *Node : Nodes) |
| 441 | Node->genCode(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Compute the stack frame layout. |
| 445 | void Cfg::genFrame() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 446 | TimerMarker T(TimerStack::TT_genFrame, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 447 | getTarget()->addProlog(Entry); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 448 | for (CfgNode *Node : Nodes) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 449 | if (Node->getHasReturn()) |
| 450 | getTarget()->addEpilog(Node); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 453 | // This is a lightweight version of live-range-end calculation. Marks |
| 454 | // the last use of only those variables whose definition and uses are |
| 455 | // completely with a single block. It is a quick single pass and |
| 456 | // doesn't need to iterate until convergence. |
| 457 | void Cfg::livenessLightweight() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 458 | TimerMarker T(TimerStack::TT_livenessLightweight, this); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 459 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 460 | for (CfgNode *Node : Nodes) |
| 461 | Node->livenessLightweight(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void Cfg::liveness(LivenessMode Mode) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 465 | TimerMarker T(TimerStack::TT_liveness, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 466 | Live.reset(new Liveness(this, Mode)); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 467 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 468 | Live->init(); |
| 469 | // Initialize with all nodes needing to be processed. |
| 470 | llvm::BitVector NeedToProcess(Nodes.size(), true); |
| 471 | while (NeedToProcess.any()) { |
| 472 | // Iterate in reverse topological order to speed up convergence. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 473 | for (CfgNode *Node : reverse_range(Nodes)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 474 | if (NeedToProcess[Node->getIndex()]) { |
| 475 | NeedToProcess[Node->getIndex()] = false; |
| 476 | bool Changed = Node->liveness(getLiveness()); |
| 477 | if (Changed) { |
| 478 | // If the beginning-of-block liveness changed since the last |
| 479 | // iteration, mark all in-edges as needing to be processed. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 480 | for (CfgNode *Pred : Node->getInEdges()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 481 | NeedToProcess[Pred->getIndex()] = true; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | if (Mode == Liveness_Intervals) { |
| 487 | // Reset each variable's live range. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 488 | for (Variable *Var : Variables) |
| 489 | Var->resetLiveRange(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 490 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 491 | // Make a final pass over each node to delete dead instructions, |
| 492 | // collect the first and last instruction numbers, and add live |
| 493 | // range segments for that node. |
| 494 | for (CfgNode *Node : Nodes) { |
| 495 | InstNumberT FirstInstNum = Inst::NumberSentinel; |
| 496 | InstNumberT LastInstNum = Inst::NumberSentinel; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 497 | for (Inst &I : Node->getPhis()) { |
| 498 | I.deleteIfDead(); |
| 499 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 500 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 501 | FirstInstNum = I.getNumber(); |
| 502 | assert(I.getNumber() > LastInstNum); |
| 503 | LastInstNum = I.getNumber(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 504 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 505 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 506 | for (Inst &I : Node->getInsts()) { |
| 507 | I.deleteIfDead(); |
| 508 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 509 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 510 | FirstInstNum = I.getNumber(); |
| 511 | assert(I.getNumber() > LastInstNum); |
| 512 | LastInstNum = I.getNumber(); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | if (Mode == Liveness_Intervals) { |
| 516 | // Special treatment for live in-args. Their liveness needs to |
| 517 | // extend beyond the beginning of the function, otherwise an arg |
| 518 | // whose only use is in the first instruction will end up having |
| 519 | // the trivial live range [2,2) and will *not* interfere with |
| 520 | // other arguments. So if the first instruction of the method |
| 521 | // is "r=arg1+arg2", both args may be assigned the same |
| 522 | // register. This is accomplished by extending the entry |
| 523 | // block's instruction range from [2,n) to [1,n) which will |
| 524 | // transform the problematic [2,2) live ranges into [1,2). |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 525 | if (Node == getEntryNode()) { |
| 526 | // TODO(stichnot): Make it a strict requirement that the entry |
| 527 | // node gets the lowest instruction numbers, so that extending |
| 528 | // the live range for in-args is guaranteed to work. |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 529 | FirstInstNum = Inst::NumberExtended; |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 530 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 531 | Node->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 532 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | // Traverse every Variable of every Inst and verify that it |
| 537 | // appears within the Variable's computed live range. |
| 538 | bool Cfg::validateLiveness() const { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 539 | TimerMarker T(TimerStack::TT_validateLiveness, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 540 | bool Valid = true; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 541 | OstreamLocker L(Ctx); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 542 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 543 | for (CfgNode *Node : Nodes) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 544 | Inst *FirstInst = nullptr; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 545 | for (Inst &Inst : Node->getInsts()) { |
| 546 | if (Inst.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 547 | continue; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 548 | if (FirstInst == nullptr) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 549 | FirstInst = &Inst; |
| 550 | InstNumberT InstNumber = Inst.getNumber(); |
| 551 | if (Variable *Dest = Inst.getDest()) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 552 | if (!Dest->getIgnoreLiveness()) { |
| 553 | bool Invalid = false; |
| 554 | const bool IsDest = true; |
| 555 | if (!Dest->getLiveRange().containsValue(InstNumber, IsDest)) |
| 556 | Invalid = true; |
| 557 | // Check that this instruction actually *begins* Dest's live |
| 558 | // range, by checking that Dest is not live in the previous |
| 559 | // instruction. As a special exception, we don't check this |
| 560 | // for the first instruction of the block, because a Phi |
| 561 | // temporary may be live at the end of the previous block, |
| 562 | // and if it is also assigned in the first instruction of |
| 563 | // this block, the adjacent live ranges get merged. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 564 | if (static_cast<class Inst *>(&Inst) != FirstInst && |
| 565 | !Inst.isDestNonKillable() && |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 566 | Dest->getLiveRange().containsValue(InstNumber - 1, IsDest)) |
| 567 | Invalid = true; |
| 568 | if (Invalid) { |
| 569 | Valid = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 570 | Str << "Liveness error: inst " << Inst.getNumber() << " dest "; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 571 | Dest->dump(this); |
| 572 | Str << " live range " << Dest->getLiveRange() << "\n"; |
| 573 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 576 | for (SizeT I = 0; I < Inst.getSrcSize(); ++I) { |
| 577 | Operand *Src = Inst.getSrc(I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 578 | SizeT NumVars = Src->getNumVars(); |
| 579 | for (SizeT J = 0; J < NumVars; ++J) { |
| 580 | const Variable *Var = Src->getVar(J); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 581 | const bool IsDest = false; |
| 582 | if (!Var->getIgnoreLiveness() && |
| 583 | !Var->getLiveRange().containsValue(InstNumber, IsDest)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 584 | Valid = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 585 | Str << "Liveness error: inst " << Inst.getNumber() << " var "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 586 | Var->dump(this); |
| 587 | Str << " live range " << Var->getLiveRange() << "\n"; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | return Valid; |
| 594 | } |
| 595 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 596 | void Cfg::contractEmptyNodes() { |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 597 | // If we're decorating the asm output with register liveness info, |
| 598 | // this information may become corrupted or incorrect after |
| 599 | // contracting nodes that contain only redundant assignments. As |
| 600 | // such, we disable this pass when DecorateAsm is specified. This |
| 601 | // may make the resulting code look more branchy, but it should have |
| 602 | // no effect on the register assignments. |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 603 | if (Ctx->getFlags().getDecorateAsm()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 604 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 605 | for (CfgNode *Node : Nodes) { |
| 606 | Node->contractIfEmpty(); |
| 607 | } |
| 608 | } |
| 609 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 610 | void Cfg::doBranchOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 611 | TimerMarker T(TimerStack::TT_doBranchOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 612 | for (auto I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 613 | auto NextNode = I + 1; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 614 | (*I)->doBranchOpt(NextNode == E ? nullptr : *NextNode); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 618 | void Cfg::markNodesForSandboxing() { |
| 619 | for (const InstJumpTable *JT : JumpTables) |
| 620 | for (SizeT I = 0; I < JT->getNumTargets(); ++I) |
| 621 | JT->getTarget(I)->setNeedsAlignment(); |
| 622 | } |
| 623 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 624 | // ======================== Dump routines ======================== // |
| 625 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 626 | // emitTextHeader() is not target-specific (apart from what is |
| 627 | // abstracted by the Assembler), so it is defined here rather than in |
| 628 | // the target lowering class. |
| 629 | void Cfg::emitTextHeader(const IceString &MangledName, GlobalContext *Ctx, |
| 630 | const Assembler *Asm) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 631 | if (!BuildDefs::dump()) |
Jim Stichnoth | 729dbd0 | 2015-02-25 14:48:43 -0800 | [diff] [blame] | 632 | return; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 633 | Ostream &Str = Ctx->getStrEmit(); |
| 634 | Str << "\t.text\n"; |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 635 | if (Ctx->getFlags().getFunctionSections()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 636 | Str << "\t.section\t.text." << MangledName << ",\"ax\",@progbits\n"; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 637 | if (!Asm->getInternal() || Ctx->getFlags().getDisableInternal()) { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 638 | Str << "\t.globl\t" << MangledName << "\n"; |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 639 | Str << "\t.type\t" << MangledName << ",%function\n"; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 640 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 641 | Str << "\t" << Asm->getAlignDirective() << " " |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 642 | << Asm->getBundleAlignLog2Bytes() << ",0x"; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 643 | for (uint8_t I : Asm->getNonExecBundlePadding()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 644 | Str.write_hex(I); |
| 645 | Str << "\n"; |
| 646 | Str << MangledName << ":\n"; |
| 647 | } |
| 648 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 649 | void Cfg::deleteJumpTableInsts() { |
| 650 | for (InstJumpTable *JumpTable : JumpTables) |
| 651 | JumpTable->setDeleted(); |
| 652 | } |
| 653 | |
| 654 | void Cfg::emitJumpTables() { |
| 655 | switch (Ctx->getFlags().getOutFileType()) { |
| 656 | case FT_Elf: |
| 657 | case FT_Iasm: { |
| 658 | // The emission needs to be delayed until the after the text section so save |
| 659 | // the offsets in the global context. |
| 660 | IceString MangledName = Ctx->mangleName(getFunctionName()); |
| 661 | for (const InstJumpTable *JumpTable : JumpTables) { |
| 662 | SizeT NumTargets = JumpTable->getNumTargets(); |
| 663 | JumpTableData &JT = |
| 664 | Ctx->addJumpTable(MangledName, JumpTable->getId(), NumTargets); |
| 665 | for (SizeT I = 0; I < NumTargets; ++I) { |
| 666 | SizeT Index = JumpTable->getTarget(I)->getIndex(); |
Jan Voung | c2ec581 | 2015-08-05 09:35:18 -0700 | [diff] [blame] | 667 | JT.pushTarget(getAssembler()->getCfgNodeLabel(Index)->getPosition()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | } break; |
| 671 | case FT_Asm: { |
| 672 | // Emit the assembly directly so we don't need to hang on to all the names |
| 673 | for (const InstJumpTable *JumpTable : JumpTables) |
| 674 | getTarget()->emitJumpTable(this, JumpTable); |
| 675 | } break; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 679 | void Cfg::emit() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 680 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 681 | return; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 682 | TimerMarker T(TimerStack::TT_emit, this); |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 683 | if (Ctx->getFlags().getDecorateAsm()) { |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 684 | renumberInstructions(); |
| 685 | getVMetadata()->init(VMK_Uses); |
| 686 | liveness(Liveness_Basic); |
| 687 | dump("After recomputing liveness for -decorate-asm"); |
| 688 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 689 | OstreamLocker L(Ctx); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 690 | Ostream &Str = Ctx->getStrEmit(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 691 | IceString MangledName = Ctx->mangleName(getFunctionName()); |
| 692 | const Assembler *Asm = getAssembler<>(); |
| 693 | const bool NeedSandboxing = Ctx->getFlags().getUseSandboxing(); |
| 694 | |
| 695 | emitTextHeader(MangledName, Ctx, Asm); |
| 696 | deleteJumpTableInsts(); |
| 697 | for (CfgNode *Node : Nodes) { |
| 698 | if (NeedSandboxing && Node->needsAlignment()) { |
| 699 | Str << "\t" << Asm->getAlignDirective() << " " |
| 700 | << Asm->getBundleAlignLog2Bytes() << "\n"; |
| 701 | } |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 702 | Node->emit(this); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 703 | } |
| 704 | emitJumpTables(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 705 | Str << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 708 | void Cfg::emitIAS() { |
| 709 | TimerMarker T(TimerStack::TT_emit, this); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 710 | // The emitIAS() routines emit into the internal assembler buffer, |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 711 | // so there's no need to lock the streams. |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 712 | deleteJumpTableInsts(); |
| 713 | const bool NeedSandboxing = Ctx->getFlags().getUseSandboxing(); |
| 714 | for (CfgNode *Node : Nodes) { |
| 715 | if (NeedSandboxing && Node->needsAlignment()) |
| 716 | getAssembler()->alignCfgNode(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 717 | Node->emitIAS(this); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 718 | } |
| 719 | emitJumpTables(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 722 | // Dumps the IR with an optional introductory message. |
| 723 | void Cfg::dump(const IceString &Message) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 724 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 725 | return; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 726 | if (!isVerbose()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 727 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 728 | OstreamLocker L(Ctx); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 729 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 730 | if (!Message.empty()) |
| 731 | Str << "================ " << Message << " ================\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 732 | setCurrentNode(getEntryNode()); |
| 733 | // Print function name+args |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 734 | if (isVerbose(IceV_Instructions)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 735 | Str << "define "; |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 736 | if (getInternal() && !Ctx->getFlags().getDisableInternal()) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 737 | Str << "internal "; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 738 | Str << ReturnType << " @" << Ctx->mangleName(getFunctionName()) << "("; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 739 | for (SizeT i = 0; i < Args.size(); ++i) { |
| 740 | if (i > 0) |
| 741 | Str << ", "; |
| 742 | Str << Args[i]->getType() << " "; |
| 743 | Args[i]->dump(this); |
| 744 | } |
| 745 | Str << ") {\n"; |
| 746 | } |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 747 | resetCurrentNode(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 748 | if (isVerbose(IceV_Liveness)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 749 | // Print summary info about variables |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 750 | for (Variable *Var : Variables) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 751 | Str << "// multiblock="; |
| 752 | if (getVMetadata()->isTracked(Var)) |
| 753 | Str << getVMetadata()->isMultiBlock(Var); |
| 754 | else |
| 755 | Str << "?"; |
| 756 | Str << " weight=" << Var->getWeight() << " "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 757 | Var->dump(this); |
| 758 | Str << " LIVE=" << Var->getLiveRange() << "\n"; |
| 759 | } |
| 760 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 761 | // Print each basic block |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 762 | for (CfgNode *Node : Nodes) |
| 763 | Node->dump(this); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 764 | if (isVerbose(IceV_Instructions)) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 765 | Str << "}\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | } // end of namespace Ice |