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 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame^] | 15 | #include "assembler.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" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceInst.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 22 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 23 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 24 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 25 | |
| 26 | namespace Ice { |
| 27 | |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 28 | thread_local const Cfg *Cfg::CurrentCfg = nullptr; |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 29 | |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 30 | ArenaAllocator<> *getCurrentCfgAllocator() { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 31 | return Cfg::getCurrentCfgAllocator(); |
| 32 | } |
| 33 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | Cfg::Cfg(GlobalContext *Ctx) |
| 35 | : Ctx(Ctx), FunctionName(""), ReturnType(IceType_void), |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 36 | IsInternalLinkage(false), HasError(false), FocusedTiming(false), |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 37 | ErrorMessage(""), Entry(nullptr), NextInstNumber(Inst::NumberInitial), |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 38 | Allocator(new ArenaAllocator<>()), Live(nullptr), |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 39 | Target(TargetLowering::createLowering(Ctx->getTargetArch(), this)), |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 40 | VMetadata(new VariablesMetadata(this)), |
| 41 | TargetAssembler( |
| 42 | TargetLowering::createAssembler(Ctx->getTargetArch(), this)), |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 43 | CurrentNode(nullptr) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 44 | assert(!Ctx->isIRGenerationDisabled() && |
| 45 | "Attempt to build cfg when IR generation disabled"); |
| 46 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 47 | |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 48 | Cfg::~Cfg() { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 49 | // TODO(stichnot,kschimpf): Set CurrentCfg=nullptr in the dtor for |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 50 | // safety. This can't be done currently because the translator |
| 51 | // manages the Cfg by creating a new Cfg (which sets CurrentCfg to |
| 52 | // the new value), then deleting the old Cfg (which would then reset |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 53 | // CurrentCfg to nullptr). |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 54 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 55 | |
| 56 | void Cfg::setError(const IceString &Message) { |
| 57 | HasError = true; |
| 58 | ErrorMessage = Message; |
| 59 | Ctx->getStrDump() << "ICE translation error: " << ErrorMessage << "\n"; |
| 60 | } |
| 61 | |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 62 | CfgNode *Cfg::makeNode() { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 63 | SizeT LabelIndex = Nodes.size(); |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 64 | CfgNode *Node = CfgNode::create(this, LabelIndex); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 65 | Nodes.push_back(Node); |
| 66 | return Node; |
| 67 | } |
| 68 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 69 | void Cfg::addArg(Variable *Arg) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 70 | Arg->setIsArg(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 71 | Args.push_back(Arg); |
| 72 | } |
| 73 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 74 | void Cfg::addImplicitArg(Variable *Arg) { |
| 75 | Arg->setIsImplicitArg(); |
| 76 | ImplicitArgs.push_back(Arg); |
| 77 | } |
| 78 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 79 | // Returns whether the stack frame layout has been computed yet. This |
| 80 | // is used for dumping the stack frame location of Variables. |
| 81 | bool Cfg::hasComputedFrame() const { return getTarget()->hasComputedFrame(); } |
| 82 | |
| 83 | void Cfg::translate() { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 84 | if (hasError()) |
| 85 | return; |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 86 | if (ALLOW_DUMP) { |
| 87 | const IceString &TimingFocusOn = getContext()->getFlags().TimingFocusOn; |
| 88 | if (TimingFocusOn == "*" || TimingFocusOn == getFunctionName()) { |
| 89 | setFocusedTiming(); |
| 90 | getContext()->resetTimer(GlobalContext::TSK_Default); |
| 91 | getContext()->setTimerName(GlobalContext::TSK_Default, getFunctionName()); |
| 92 | } |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 93 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 94 | TimerMarker T(TimerStack::TT_translate, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 95 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 96 | dump("Initial CFG"); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 97 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 98 | // The set of translation passes and their order are determined by |
| 99 | // the target. |
| 100 | getTarget()->translate(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 101 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 102 | dump("Final output"); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 103 | if (getFocusedTiming()) |
| 104 | getContext()->dumpTimers(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 107 | void Cfg::computePredecessors() { |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 108 | for (CfgNode *Node : Nodes) |
| 109 | Node->computePredecessors(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 112 | void Cfg::renumberInstructions() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 113 | TimerMarker T(TimerStack::TT_renumberInstructions, this); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 114 | NextInstNumber = Inst::NumberInitial; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 115 | for (CfgNode *Node : Nodes) |
| 116 | Node->renumberInstructions(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 119 | // placePhiLoads() must be called before placePhiStores(). |
| 120 | void Cfg::placePhiLoads() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 121 | TimerMarker T(TimerStack::TT_placePhiLoads, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 122 | for (CfgNode *Node : Nodes) |
| 123 | Node->placePhiLoads(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // placePhiStores() must be called after placePhiLoads(). |
| 127 | void Cfg::placePhiStores() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 128 | TimerMarker T(TimerStack::TT_placePhiStores, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 129 | for (CfgNode *Node : Nodes) |
| 130 | Node->placePhiStores(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void Cfg::deletePhis() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 134 | TimerMarker T(TimerStack::TT_deletePhis, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 135 | for (CfgNode *Node : Nodes) |
| 136 | Node->deletePhis(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 139 | void Cfg::advancedPhiLowering() { |
| 140 | TimerMarker T(TimerStack::TT_advancedPhiLowering, this); |
| 141 | // This splits edges and appends new nodes to the end of the node |
| 142 | // list. This can invalidate iterators, so don't use an iterator. |
| 143 | SizeT NumNodes = getNumNodes(); |
| 144 | for (SizeT I = 0; I < NumNodes; ++I) |
| 145 | Nodes[I]->advancedPhiLowering(); |
| 146 | } |
| 147 | |
| 148 | // Find a reasonable placement for nodes that have not yet been |
| 149 | // placed, while maintaining the same relative ordering among already |
| 150 | // placed nodes. |
| 151 | void Cfg::reorderNodes() { |
| 152 | typedef std::list<CfgNode *> PlacedList; |
| 153 | PlacedList Placed; // Nodes with relative placement locked down |
| 154 | PlacedList Unreachable; // Unreachable nodes |
| 155 | PlacedList::iterator NoPlace = Placed.end(); |
| 156 | // Keep track of where each node has been tentatively placed so that |
| 157 | // we can manage insertions into the middle. |
| 158 | std::vector<PlacedList::iterator> PlaceIndex(Nodes.size(), NoPlace); |
| 159 | for (CfgNode *Node : Nodes) { |
| 160 | // The "do ... while(0);" construct is to factor out the |
| 161 | // --PlaceIndex and assert() statements before moving to the next |
| 162 | // node. |
| 163 | do { |
| 164 | if (!Node->needsPlacement()) { |
| 165 | // Add to the end of the Placed list. |
| 166 | Placed.push_back(Node); |
| 167 | PlaceIndex[Node->getIndex()] = Placed.end(); |
| 168 | continue; |
| 169 | } |
| 170 | Node->setNeedsPlacement(false); |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 171 | if (Node != getEntryNode() && Node->getInEdges().empty()) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 172 | // The node has essentially been deleted since it is not a |
| 173 | // successor of any other node. |
| 174 | Unreachable.push_back(Node); |
| 175 | PlaceIndex[Node->getIndex()] = Unreachable.end(); |
| 176 | continue; |
| 177 | } |
| 178 | // Assume for now that the unplaced node is from edge-splitting |
| 179 | // and therefore has 1 in-edge and 1 out-edge (actually, possibly |
| 180 | // more than 1 in-edge if the predecessor node was contracted). |
| 181 | // If this changes in the future, rethink the strategy. |
| 182 | assert(Node->getInEdges().size() >= 1); |
| 183 | assert(Node->getOutEdges().size() == 1); |
| 184 | |
| 185 | // If it's a (non-critical) edge where the successor has a single |
| 186 | // in-edge, then place it before the successor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 187 | CfgNode *Succ = Node->getOutEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 188 | if (Succ->getInEdges().size() == 1 && |
| 189 | PlaceIndex[Succ->getIndex()] != NoPlace) { |
| 190 | Placed.insert(PlaceIndex[Succ->getIndex()], Node); |
| 191 | PlaceIndex[Node->getIndex()] = PlaceIndex[Succ->getIndex()]; |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | // Otherwise, place it after the (first) predecessor. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 196 | CfgNode *Pred = Node->getInEdges().front(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 197 | auto PredPosition = PlaceIndex[Pred->getIndex()]; |
| 198 | // It shouldn't be the case that PredPosition==NoPlace, but if |
| 199 | // that somehow turns out to be true, we just insert Node before |
| 200 | // PredPosition=NoPlace=Placed.end() . |
| 201 | if (PredPosition != NoPlace) |
| 202 | ++PredPosition; |
| 203 | Placed.insert(PredPosition, Node); |
| 204 | PlaceIndex[Node->getIndex()] = PredPosition; |
| 205 | } while (0); |
| 206 | |
| 207 | --PlaceIndex[Node->getIndex()]; |
| 208 | assert(*PlaceIndex[Node->getIndex()] == Node); |
| 209 | } |
| 210 | |
| 211 | // Reorder Nodes according to the built-up lists. |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 212 | SizeT OldSize = Nodes.size(); |
| 213 | (void)OldSize; |
| 214 | Nodes.clear(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 215 | for (CfgNode *Node : Placed) |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 216 | Nodes.push_back(Node); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 217 | for (CfgNode *Node : Unreachable) |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 218 | Nodes.push_back(Node); |
| 219 | assert(Nodes.size() == OldSize); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 222 | void Cfg::doArgLowering() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 223 | TimerMarker T(TimerStack::TT_doArgLowering, this); |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 224 | getTarget()->lowerArguments(); |
| 225 | } |
| 226 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 227 | void Cfg::doAddressOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 228 | TimerMarker T(TimerStack::TT_doAddressOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 229 | for (CfgNode *Node : Nodes) |
| 230 | Node->doAddressOpt(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 233 | void Cfg::doNopInsertion() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 234 | TimerMarker T(TimerStack::TT_doNopInsertion, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 235 | for (CfgNode *Node : Nodes) |
| 236 | Node->doNopInsertion(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 239 | void Cfg::genCode() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 240 | TimerMarker T(TimerStack::TT_genCode, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 241 | for (CfgNode *Node : Nodes) |
| 242 | Node->genCode(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Compute the stack frame layout. |
| 246 | void Cfg::genFrame() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 247 | TimerMarker T(TimerStack::TT_genFrame, this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 248 | getTarget()->addProlog(Entry); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 249 | for (CfgNode *Node : Nodes) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 250 | if (Node->getHasReturn()) |
| 251 | getTarget()->addEpilog(Node); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 254 | // This is a lightweight version of live-range-end calculation. Marks |
| 255 | // the last use of only those variables whose definition and uses are |
| 256 | // completely with a single block. It is a quick single pass and |
| 257 | // doesn't need to iterate until convergence. |
| 258 | void Cfg::livenessLightweight() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 259 | TimerMarker T(TimerStack::TT_livenessLightweight, this); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 260 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 261 | for (CfgNode *Node : Nodes) |
| 262 | Node->livenessLightweight(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void Cfg::liveness(LivenessMode Mode) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 266 | TimerMarker T(TimerStack::TT_liveness, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 267 | Live.reset(new Liveness(this, Mode)); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 268 | getVMetadata()->init(VMK_Uses); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 269 | Live->init(); |
| 270 | // Initialize with all nodes needing to be processed. |
| 271 | llvm::BitVector NeedToProcess(Nodes.size(), true); |
| 272 | while (NeedToProcess.any()) { |
| 273 | // Iterate in reverse topological order to speed up convergence. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 274 | for (CfgNode *Node : reverse_range(Nodes)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 275 | if (NeedToProcess[Node->getIndex()]) { |
| 276 | NeedToProcess[Node->getIndex()] = false; |
| 277 | bool Changed = Node->liveness(getLiveness()); |
| 278 | if (Changed) { |
| 279 | // If the beginning-of-block liveness changed since the last |
| 280 | // iteration, mark all in-edges as needing to be processed. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 281 | for (CfgNode *Pred : Node->getInEdges()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 282 | NeedToProcess[Pred->getIndex()] = true; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | if (Mode == Liveness_Intervals) { |
| 288 | // Reset each variable's live range. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 289 | for (Variable *Var : Variables) |
| 290 | Var->resetLiveRange(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 291 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 292 | // Make a final pass over each node to delete dead instructions, |
| 293 | // collect the first and last instruction numbers, and add live |
| 294 | // range segments for that node. |
| 295 | for (CfgNode *Node : Nodes) { |
| 296 | InstNumberT FirstInstNum = Inst::NumberSentinel; |
| 297 | InstNumberT LastInstNum = Inst::NumberSentinel; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 298 | for (Inst &I : Node->getPhis()) { |
| 299 | I.deleteIfDead(); |
| 300 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 301 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 302 | FirstInstNum = I.getNumber(); |
| 303 | assert(I.getNumber() > LastInstNum); |
| 304 | LastInstNum = I.getNumber(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 305 | } |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 306 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 307 | for (Inst &I : Node->getInsts()) { |
| 308 | I.deleteIfDead(); |
| 309 | if (Mode == Liveness_Intervals && !I.isDeleted()) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 310 | if (FirstInstNum == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 311 | FirstInstNum = I.getNumber(); |
| 312 | assert(I.getNumber() > LastInstNum); |
| 313 | LastInstNum = I.getNumber(); |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | if (Mode == Liveness_Intervals) { |
| 317 | // Special treatment for live in-args. Their liveness needs to |
| 318 | // extend beyond the beginning of the function, otherwise an arg |
| 319 | // whose only use is in the first instruction will end up having |
| 320 | // the trivial live range [2,2) and will *not* interfere with |
| 321 | // other arguments. So if the first instruction of the method |
| 322 | // is "r=arg1+arg2", both args may be assigned the same |
| 323 | // register. This is accomplished by extending the entry |
| 324 | // block's instruction range from [2,n) to [1,n) which will |
| 325 | // transform the problematic [2,2) live ranges into [1,2). |
| 326 | if (FirstInstNum == Inst::NumberInitial) |
| 327 | FirstInstNum = Inst::NumberExtended; |
| 328 | Node->livenessAddIntervals(getLiveness(), FirstInstNum, LastInstNum); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 329 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | // Traverse every Variable of every Inst and verify that it |
| 334 | // appears within the Variable's computed live range. |
| 335 | bool Cfg::validateLiveness() const { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 336 | TimerMarker T(TimerStack::TT_validateLiveness, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 337 | bool Valid = true; |
| 338 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 339 | for (CfgNode *Node : Nodes) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 340 | Inst *FirstInst = nullptr; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 341 | for (Inst &Inst : Node->getInsts()) { |
| 342 | if (Inst.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 343 | continue; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 344 | if (FirstInst == nullptr) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 345 | FirstInst = &Inst; |
| 346 | InstNumberT InstNumber = Inst.getNumber(); |
| 347 | if (Variable *Dest = Inst.getDest()) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 348 | if (!Dest->getIgnoreLiveness()) { |
| 349 | bool Invalid = false; |
| 350 | const bool IsDest = true; |
| 351 | if (!Dest->getLiveRange().containsValue(InstNumber, IsDest)) |
| 352 | Invalid = true; |
| 353 | // Check that this instruction actually *begins* Dest's live |
| 354 | // range, by checking that Dest is not live in the previous |
| 355 | // instruction. As a special exception, we don't check this |
| 356 | // for the first instruction of the block, because a Phi |
| 357 | // temporary may be live at the end of the previous block, |
| 358 | // and if it is also assigned in the first instruction of |
| 359 | // this block, the adjacent live ranges get merged. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 360 | if (static_cast<class Inst *>(&Inst) != FirstInst && |
| 361 | !Inst.isDestNonKillable() && |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 362 | Dest->getLiveRange().containsValue(InstNumber - 1, IsDest)) |
| 363 | Invalid = true; |
| 364 | if (Invalid) { |
| 365 | Valid = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 366 | Str << "Liveness error: inst " << Inst.getNumber() << " dest "; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 367 | Dest->dump(this); |
| 368 | Str << " live range " << Dest->getLiveRange() << "\n"; |
| 369 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 372 | for (SizeT I = 0; I < Inst.getSrcSize(); ++I) { |
| 373 | Operand *Src = Inst.getSrc(I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 374 | SizeT NumVars = Src->getNumVars(); |
| 375 | for (SizeT J = 0; J < NumVars; ++J) { |
| 376 | const Variable *Var = Src->getVar(J); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 377 | const bool IsDest = false; |
| 378 | if (!Var->getIgnoreLiveness() && |
| 379 | !Var->getLiveRange().containsValue(InstNumber, IsDest)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 380 | Valid = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 381 | Str << "Liveness error: inst " << Inst.getNumber() << " var "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 382 | Var->dump(this); |
| 383 | Str << " live range " << Var->getLiveRange() << "\n"; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | return Valid; |
| 390 | } |
| 391 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 392 | void Cfg::contractEmptyNodes() { |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 393 | // If we're decorating the asm output with register liveness info, |
| 394 | // this information may become corrupted or incorrect after |
| 395 | // contracting nodes that contain only redundant assignments. As |
| 396 | // such, we disable this pass when DecorateAsm is specified. This |
| 397 | // may make the resulting code look more branchy, but it should have |
| 398 | // no effect on the register assignments. |
| 399 | if (Ctx->getFlags().DecorateAsm) |
| 400 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 401 | for (CfgNode *Node : Nodes) { |
| 402 | Node->contractIfEmpty(); |
| 403 | } |
| 404 | } |
| 405 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 406 | void Cfg::doBranchOpt() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 407 | TimerMarker T(TimerStack::TT_doBranchOpt, this); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 408 | for (auto I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
| 409 | auto NextNode = I; |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 410 | ++NextNode; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 411 | (*I)->doBranchOpt(NextNode == E ? nullptr : *NextNode); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 415 | // ======================== Dump routines ======================== // |
| 416 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 417 | void Cfg::emitTextHeader(const IceString &MangledName) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 418 | // Note: Still used by emit IAS. |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 419 | Ostream &Str = Ctx->getStrEmit(); |
| 420 | Str << "\t.text\n"; |
| 421 | if (Ctx->getFlags().FunctionSections) |
| 422 | Str << "\t.section\t.text." << MangledName << ",\"ax\",@progbits\n"; |
| 423 | if (!getInternal() || Ctx->getFlags().DisableInternal) { |
| 424 | Str << "\t.globl\t" << MangledName << "\n"; |
| 425 | Str << "\t.type\t" << MangledName << ",@function\n"; |
| 426 | } |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 427 | Assembler *Asm = getAssembler<Assembler>(); |
| 428 | Str << "\t.p2align " << Asm->getBundleAlignLog2Bytes() << ",0x"; |
| 429 | for (uint8_t I : Asm->getNonExecBundlePadding()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 430 | Str.write_hex(I); |
| 431 | Str << "\n"; |
| 432 | Str << MangledName << ":\n"; |
| 433 | } |
| 434 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 435 | void Cfg::emit() { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 436 | if (!ALLOW_DUMP) |
| 437 | return; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 438 | TimerMarker T(TimerStack::TT_emit, this); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 439 | if (Ctx->getFlags().DecorateAsm) { |
| 440 | renumberInstructions(); |
| 441 | getVMetadata()->init(VMK_Uses); |
| 442 | liveness(Liveness_Basic); |
| 443 | dump("After recomputing liveness for -decorate-asm"); |
| 444 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 445 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 446 | IceString MangledName = getContext()->mangleName(getFunctionName()); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 447 | emitTextHeader(MangledName); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 448 | for (CfgNode *Node : Nodes) |
| 449 | Node->emit(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 450 | Str << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 453 | void Cfg::emitIAS() { |
| 454 | TimerMarker T(TimerStack::TT_emit, this); |
| 455 | assert(!Ctx->getFlags().DecorateAsm); |
| 456 | IceString MangledName = getContext()->mangleName(getFunctionName()); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 457 | if (!Ctx->getFlags().UseELFWriter) |
| 458 | emitTextHeader(MangledName); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 459 | for (CfgNode *Node : Nodes) |
| 460 | Node->emitIAS(this); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 461 | // Now write the function to the file and track. |
| 462 | if (Ctx->getFlags().UseELFWriter) { |
| 463 | getAssembler<Assembler>()->alignFunction(); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame^] | 464 | Ctx->getObjectWriter()->writeFunctionCode(MangledName, getInternal(), |
| 465 | getAssembler<Assembler>()); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 466 | } else { |
| 467 | getAssembler<Assembler>()->emitIASBytes(Ctx); |
| 468 | } |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 471 | // Dumps the IR with an optional introductory message. |
| 472 | void Cfg::dump(const IceString &Message) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 473 | if (!ALLOW_DUMP) |
| 474 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 475 | if (!Ctx->isVerbose()) |
| 476 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 477 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 478 | if (!Message.empty()) |
| 479 | Str << "================ " << Message << " ================\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 480 | setCurrentNode(getEntryNode()); |
| 481 | // Print function name+args |
| 482 | if (getContext()->isVerbose(IceV_Instructions)) { |
| 483 | Str << "define "; |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 484 | if (getInternal() && !Ctx->getFlags().DisableInternal) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 485 | Str << "internal "; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 486 | Str << ReturnType << " @" << Ctx->mangleName(getFunctionName()) << "("; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 487 | for (SizeT i = 0; i < Args.size(); ++i) { |
| 488 | if (i > 0) |
| 489 | Str << ", "; |
| 490 | Str << Args[i]->getType() << " "; |
| 491 | Args[i]->dump(this); |
| 492 | } |
| 493 | Str << ") {\n"; |
| 494 | } |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 495 | resetCurrentNode(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 496 | if (getContext()->isVerbose(IceV_Liveness)) { |
| 497 | // Print summary info about variables |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 498 | for (Variable *Var : Variables) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 499 | Str << "// multiblock="; |
| 500 | if (getVMetadata()->isTracked(Var)) |
| 501 | Str << getVMetadata()->isMultiBlock(Var); |
| 502 | else |
| 503 | Str << "?"; |
| 504 | Str << " weight=" << Var->getWeight() << " "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 505 | Var->dump(this); |
| 506 | Str << " LIVE=" << Var->getLiveRange() << "\n"; |
| 507 | } |
| 508 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 509 | // Print each basic block |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 510 | for (CfgNode *Node : Nodes) |
| 511 | Node->dump(this); |
| 512 | if (getContext()->isVerbose(IceV_Instructions)) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 513 | Str << "}\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | } // end of namespace Ice |