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