Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceOperand.cpp - High-level operand 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 | // |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 10 | // This file implements the Operand class and its target-independent |
| 11 | // subclasses, primarily for the methods of the Variable class. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "IceCfg.h" |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 16 | #include "IceCfgNode.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 17 | #include "IceInst.h" |
| 18 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 19 | #include "IceTargetLowering.h" // dumping stack/frame pointer register |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 20 | |
| 21 | namespace Ice { |
| 22 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 23 | bool operator==(const RelocatableTuple &A, const RelocatableTuple &B) { |
| 24 | return A.Offset == B.Offset && A.Name == B.Name; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 27 | bool operator<(const RegWeight &A, const RegWeight &B) { |
| 28 | return A.getWeight() < B.getWeight(); |
| 29 | } |
| 30 | bool operator<=(const RegWeight &A, const RegWeight &B) { return !(B < A); } |
| 31 | bool operator==(const RegWeight &A, const RegWeight &B) { |
| 32 | return !(B < A) && !(A < B); |
| 33 | } |
| 34 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 35 | void LiveRange::addSegment(InstNumberT Start, InstNumberT End) { |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 36 | if (!Range.empty()) { |
| 37 | // Check for merge opportunity. |
| 38 | InstNumberT CurrentEnd = Range.back().second; |
| 39 | assert(Start >= CurrentEnd); |
| 40 | if (Start == CurrentEnd) { |
| 41 | Range.back().second = End; |
| 42 | return; |
| 43 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 44 | } |
| 45 | Range.push_back(RangeElementType(Start, End)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Returns true if this live range ends before Other's live range |
| 49 | // starts. This means that the highest instruction number in this |
| 50 | // live range is less than or equal to the lowest instruction number |
| 51 | // of the Other live range. |
| 52 | bool LiveRange::endsBefore(const LiveRange &Other) const { |
| 53 | // Neither range should be empty, but let's be graceful. |
| 54 | if (Range.empty() || Other.Range.empty()) |
| 55 | return true; |
| 56 | InstNumberT MyEnd = (*Range.rbegin()).second; |
| 57 | InstNumberT OtherStart = (*Other.Range.begin()).first; |
| 58 | return MyEnd <= OtherStart; |
| 59 | } |
| 60 | |
| 61 | // Returns true if there is any overlap between the two live ranges. |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 62 | bool LiveRange::overlaps(const LiveRange &Other, bool UseTrimmed) const { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 63 | // Do a two-finger walk through the two sorted lists of segments. |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 64 | auto I1 = (UseTrimmed ? TrimmedBegin : Range.begin()), |
| 65 | I2 = (UseTrimmed ? Other.TrimmedBegin : Other.Range.begin()); |
| 66 | auto E1 = Range.end(), E2 = Other.Range.end(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 67 | while (I1 != E1 && I2 != E2) { |
| 68 | if (I1->second <= I2->first) { |
| 69 | ++I1; |
| 70 | continue; |
| 71 | } |
| 72 | if (I2->second <= I1->first) { |
| 73 | ++I2; |
| 74 | continue; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 81 | bool LiveRange::overlapsInst(InstNumberT OtherBegin, bool UseTrimmed) const { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 82 | bool Result = false; |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 83 | for (auto I = (UseTrimmed ? TrimmedBegin : Range.begin()), E = Range.end(); |
| 84 | I != E; ++I) { |
| 85 | if (OtherBegin < I->first) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 86 | Result = false; |
| 87 | break; |
| 88 | } |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 89 | if (OtherBegin < I->second) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 90 | Result = true; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | #if 0 |
| 95 | // An equivalent but less inefficient implementation: |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 96 | LiveRange Temp; |
| 97 | Temp.addSegment(OtherBegin, OtherBegin + 1); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 98 | bool Validation = overlaps(Temp); |
| 99 | assert(Result == Validation); |
| 100 | #endif |
| 101 | return Result; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Returns true if the live range contains the given instruction |
| 105 | // number. This is only used for validating the live range |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 106 | // calculation. The IsDest argument indicates whether the Variable |
| 107 | // being tested is used in the Dest position (as opposed to a Src |
| 108 | // position). |
| 109 | bool LiveRange::containsValue(InstNumberT Value, bool IsDest) const { |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 110 | for (const RangeElementType &I : Range) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 111 | if (I.first <= Value && |
| 112 | (Value < I.second || (!IsDest && Value == I.second))) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 118 | void LiveRange::trim(InstNumberT Lower) { |
| 119 | while (TrimmedBegin != Range.end() && TrimmedBegin->second <= Lower) |
| 120 | ++TrimmedBegin; |
| 121 | } |
| 122 | |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 123 | IceString Variable::getName(const Cfg *Func) const { |
| 124 | if (Func && NameIndex >= 0) |
| 125 | return Func->getIdentifierName(NameIndex); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 126 | return "__" + std::to_string(getIndex()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 129 | Variable *Variable::asType(Type Ty) { |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 130 | // Note: This returns a Variable, even if the "this" object is a |
| 131 | // subclass of Variable. |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 132 | if (!ALLOW_DUMP || getType() == Ty) |
| 133 | return this; |
| 134 | Variable *V = new (getCurrentCfgAllocator()->Allocate<Variable>()) |
| 135 | Variable(kVariable, Ty, Number); |
| 136 | V->NameIndex = NameIndex; |
| 137 | V->RegNum = RegNum; |
| 138 | V->StackOffset = StackOffset; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 139 | return V; |
| 140 | } |
| 141 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 142 | void VariableTracking::markUse(MetadataKind TrackingKind, const Inst *Instr, |
| 143 | const CfgNode *Node, bool IsFromDef, |
| 144 | bool IsImplicit) { |
| 145 | (void)TrackingKind; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 146 | if (MultiBlock == MBS_MultiBlock) |
| 147 | return; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 148 | // TODO(stichnot): If the use occurs as a source operand in the |
| 149 | // first instruction of the block, and its definition is in this |
| 150 | // block's only predecessor, we might consider not marking this as a |
| 151 | // separate use. This may also apply if it's the first instruction |
| 152 | // of the block that actually uses a Variable. |
| 153 | assert(Node); |
| 154 | bool MakeMulti = false; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 155 | if (IsImplicit) |
| 156 | MakeMulti = true; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 157 | // A phi source variable conservatively needs to be marked as |
| 158 | // multi-block, even if its definition is in the same block. This |
| 159 | // is because there can be additional control flow before branching |
| 160 | // back to this node, and the variable is live throughout those |
| 161 | // nodes. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 162 | if (!IsFromDef && Instr && llvm::isa<InstPhi>(Instr)) |
| 163 | MakeMulti = true; |
| 164 | |
| 165 | if (!MakeMulti) { |
| 166 | switch (MultiBlock) { |
| 167 | case MBS_Unknown: |
| 168 | MultiBlock = MBS_SingleBlock; |
| 169 | SingleUseNode = Node; |
| 170 | break; |
| 171 | case MBS_SingleBlock: |
| 172 | if (SingleUseNode != Node) |
| 173 | MakeMulti = true; |
| 174 | break; |
| 175 | case MBS_MultiBlock: |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (MakeMulti) { |
| 181 | MultiBlock = MBS_MultiBlock; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 182 | SingleUseNode = nullptr; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 186 | void VariableTracking::markDef(MetadataKind TrackingKind, const Inst *Instr, |
| 187 | const CfgNode *Node) { |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 188 | // TODO(stichnot): If the definition occurs in the last instruction |
| 189 | // of the block, consider not marking this as a separate use. But |
| 190 | // be careful not to omit all uses of the variable if markDef() and |
| 191 | // markUse() both use this optimization. |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 192 | assert(Node); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 193 | // Verify that instructions are added in increasing order. |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 194 | #ifndef NDEBUG |
| 195 | if (TrackingKind == VMK_All) { |
| 196 | const Inst *LastInstruction = |
| 197 | Definitions.empty() ? FirstOrSingleDefinition : Definitions.back(); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 198 | assert(LastInstruction == nullptr || |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 199 | Instr->getNumber() >= LastInstruction->getNumber()); |
| 200 | } |
| 201 | #endif |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 202 | const bool IsFromDef = true; |
| 203 | const bool IsImplicit = false; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 204 | markUse(TrackingKind, Instr, Node, IsFromDef, IsImplicit); |
| 205 | if (TrackingKind == VMK_Uses) |
| 206 | return; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 207 | if (FirstOrSingleDefinition == nullptr) |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 208 | FirstOrSingleDefinition = Instr; |
| 209 | else if (TrackingKind == VMK_All) |
| 210 | Definitions.push_back(Instr); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 211 | switch (MultiDef) { |
| 212 | case MDS_Unknown: |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 213 | assert(SingleDefNode == nullptr); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 214 | MultiDef = MDS_SingleDef; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 215 | SingleDefNode = Node; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 216 | break; |
| 217 | case MDS_SingleDef: |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 218 | assert(SingleDefNode); |
| 219 | if (Node == SingleDefNode) { |
| 220 | MultiDef = MDS_MultiDefSingleBlock; |
| 221 | } else { |
| 222 | MultiDef = MDS_MultiDefMultiBlock; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 223 | SingleDefNode = nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 224 | } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 225 | break; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 226 | case MDS_MultiDefSingleBlock: |
| 227 | assert(SingleDefNode); |
| 228 | if (Node != SingleDefNode) { |
| 229 | MultiDef = MDS_MultiDefMultiBlock; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 230 | SingleDefNode = nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 231 | } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 232 | break; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 233 | case MDS_MultiDefMultiBlock: |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 234 | assert(SingleDefNode == nullptr); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | const Inst *VariableTracking::getFirstDefinition() const { |
| 240 | switch (MultiDef) { |
| 241 | case MDS_Unknown: |
| 242 | case MDS_MultiDefMultiBlock: |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 243 | return nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 244 | case MDS_SingleDef: |
| 245 | case MDS_MultiDefSingleBlock: |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 246 | assert(FirstOrSingleDefinition); |
| 247 | return FirstOrSingleDefinition; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 248 | } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 249 | return nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | const Inst *VariableTracking::getSingleDefinition() const { |
| 253 | switch (MultiDef) { |
| 254 | case MDS_Unknown: |
| 255 | case MDS_MultiDefMultiBlock: |
| 256 | case MDS_MultiDefSingleBlock: |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 257 | return nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 258 | case MDS_SingleDef: |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 259 | assert(FirstOrSingleDefinition); |
| 260 | return FirstOrSingleDefinition; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 261 | } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 262 | return nullptr; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 265 | void VariablesMetadata::init(MetadataKind TrackingKind) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 266 | TimerMarker T(TimerStack::TT_vmetadata, Func); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 267 | Kind = TrackingKind; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 268 | Metadata.clear(); |
| 269 | Metadata.resize(Func->getNumVariables()); |
| 270 | |
| 271 | // Mark implicit args as being used in the entry node. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 272 | for (Variable *Var : Func->getImplicitArgs()) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 273 | const Inst *NoInst = nullptr; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 274 | const CfgNode *EntryNode = Func->getEntryNode(); |
| 275 | const bool IsFromDef = false; |
| 276 | const bool IsImplicit = true; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 277 | Metadata[Var->getIndex()].markUse(Kind, NoInst, EntryNode, IsFromDef, |
| 278 | IsImplicit); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 281 | for (CfgNode *Node : Func->getNodes()) |
| 282 | addNode(Node); |
| 283 | } |
| 284 | |
| 285 | void VariablesMetadata::addNode(CfgNode *Node) { |
| 286 | if (Func->getNumVariables() >= Metadata.size()) |
| 287 | Metadata.resize(Func->getNumVariables()); |
| 288 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 289 | for (Inst &I : Node->getPhis()) { |
| 290 | if (I.isDeleted()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 291 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 292 | if (Variable *Dest = I.getDest()) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 293 | SizeT DestNum = Dest->getIndex(); |
| 294 | assert(DestNum < Metadata.size()); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 295 | Metadata[DestNum].markDef(Kind, &I, Node); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 296 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 297 | for (SizeT SrcNum = 0; SrcNum < I.getSrcSize(); ++SrcNum) { |
| 298 | if (const Variable *Var = llvm::dyn_cast<Variable>(I.getSrc(SrcNum))) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 299 | SizeT VarNum = Var->getIndex(); |
| 300 | assert(VarNum < Metadata.size()); |
| 301 | const bool IsFromDef = false; |
| 302 | const bool IsImplicit = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 303 | Metadata[VarNum].markUse(Kind, &I, Node, IsFromDef, IsImplicit); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 304 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 308 | for (Inst &I : Node->getInsts()) { |
| 309 | if (I.isDeleted()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 310 | continue; |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 311 | // Note: The implicit definitions (and uses) from InstFakeKill are |
| 312 | // deliberately ignored. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 313 | if (Variable *Dest = I.getDest()) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 314 | SizeT DestNum = Dest->getIndex(); |
| 315 | assert(DestNum < Metadata.size()); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 316 | Metadata[DestNum].markDef(Kind, &I, Node); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 317 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 318 | for (SizeT SrcNum = 0; SrcNum < I.getSrcSize(); ++SrcNum) { |
| 319 | Operand *Src = I.getSrc(SrcNum); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 320 | SizeT NumVars = Src->getNumVars(); |
| 321 | for (SizeT J = 0; J < NumVars; ++J) { |
| 322 | const Variable *Var = Src->getVar(J); |
| 323 | SizeT VarNum = Var->getIndex(); |
| 324 | assert(VarNum < Metadata.size()); |
| 325 | const bool IsFromDef = false; |
| 326 | const bool IsImplicit = false; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 327 | Metadata[VarNum].markUse(Kind, &I, Node, IsFromDef, IsImplicit); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | bool VariablesMetadata::isMultiDef(const Variable *Var) const { |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 334 | assert(Kind != VMK_Uses); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 335 | if (Var->getIsArg()) |
| 336 | return false; |
| 337 | if (!isTracked(Var)) |
| 338 | return true; // conservative answer |
| 339 | SizeT VarNum = Var->getIndex(); |
| 340 | // Conservatively return true if the state is unknown. |
| 341 | return Metadata[VarNum].getMultiDef() != VariableTracking::MDS_SingleDef; |
| 342 | } |
| 343 | |
| 344 | bool VariablesMetadata::isMultiBlock(const Variable *Var) const { |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 345 | if (Var->getIsArg()) |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 346 | return true; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 347 | if (!isTracked(Var)) |
| 348 | return true; // conservative answer |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 349 | SizeT VarNum = Var->getIndex(); |
| 350 | // Conservatively return true if the state is unknown. |
| 351 | return Metadata[VarNum].getMultiBlock() != VariableTracking::MBS_SingleBlock; |
| 352 | } |
| 353 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 354 | const Inst *VariablesMetadata::getFirstDefinition(const Variable *Var) const { |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 355 | assert(Kind != VMK_Uses); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 356 | if (!isTracked(Var)) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 357 | return nullptr; // conservative answer |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 358 | SizeT VarNum = Var->getIndex(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 359 | return Metadata[VarNum].getFirstDefinition(); |
| 360 | } |
| 361 | |
| 362 | const Inst *VariablesMetadata::getSingleDefinition(const Variable *Var) const { |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 363 | assert(Kind != VMK_Uses); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 364 | if (!isTracked(Var)) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 365 | return nullptr; // conservative answer |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 366 | SizeT VarNum = Var->getIndex(); |
| 367 | return Metadata[VarNum].getSingleDefinition(); |
| 368 | } |
| 369 | |
| 370 | const InstDefList & |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 371 | VariablesMetadata::getLatterDefinitions(const Variable *Var) const { |
| 372 | assert(Kind == VMK_All); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 373 | if (!isTracked(Var)) |
| 374 | return NoDefinitions; |
| 375 | SizeT VarNum = Var->getIndex(); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 376 | return Metadata[VarNum].getLatterDefinitions(); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | const CfgNode *VariablesMetadata::getLocalUseNode(const Variable *Var) const { |
| 380 | if (!isTracked(Var)) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 381 | return nullptr; // conservative answer |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 382 | SizeT VarNum = Var->getIndex(); |
| 383 | return Metadata[VarNum].getNode(); |
| 384 | } |
| 385 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 386 | const InstDefList VariablesMetadata::NoDefinitions; |
| 387 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 388 | // ======================== dump routines ======================== // |
| 389 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 390 | void Variable::emit(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 391 | if (ALLOW_DUMP) |
| 392 | Func->getTarget()->emitVariable(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 395 | void Variable::dump(const Cfg *Func, Ostream &Str) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 396 | if (!ALLOW_DUMP) |
| 397 | return; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 398 | if (Func == nullptr) { |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 399 | Str << "%" << getName(Func); |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 400 | return; |
| 401 | } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 402 | if (Func->isVerbose(IceV_RegOrigins) || |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 403 | (!hasReg() && !Func->getTarget()->hasComputedFrame())) |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 404 | Str << "%" << getName(Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 405 | if (hasReg()) { |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 406 | if (Func->isVerbose(IceV_RegOrigins)) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 407 | Str << ":"; |
| 408 | Str << Func->getTarget()->getRegName(RegNum, getType()); |
| 409 | } else if (Func->getTarget()->hasComputedFrame()) { |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 410 | if (Func->isVerbose(IceV_RegOrigins)) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 411 | Str << ":"; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 412 | Str << "[" |
| 413 | << Func->getTarget()->getRegName( |
| 414 | Func->getTarget()->getFrameOrStackReg(), IceType_i32); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 415 | int32_t Offset = getStackOffset(); |
| 416 | if (Offset) { |
| 417 | if (Offset > 0) |
| 418 | Str << "+"; |
| 419 | Str << Offset; |
| 420 | } |
| 421 | Str << "]"; |
| 422 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 425 | void ConstantRelocatable::emitWithoutDollar(GlobalContext *Ctx) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 426 | if (!ALLOW_DUMP) |
| 427 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 428 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 429 | if (SuppressMangling) |
| 430 | Str << Name; |
| 431 | else |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 432 | Str << Ctx->mangleName(Name); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 433 | if (Offset) { |
| 434 | if (Offset > 0) |
| 435 | Str << "+"; |
| 436 | Str << Offset; |
| 437 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 440 | void ConstantRelocatable::emit(GlobalContext *Ctx) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 441 | if (!ALLOW_DUMP) |
| 442 | return; |
Jim Stichnoth | bca2f65 | 2014-11-01 10:13:54 -0700 | [diff] [blame] | 443 | Ostream &Str = Ctx->getStrEmit(); |
| 444 | Str << "$"; |
| 445 | emitWithoutDollar(Ctx); |
| 446 | } |
| 447 | |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 448 | void ConstantRelocatable::dump(const Cfg *Func, Ostream &Str) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 449 | if (!ALLOW_DUMP) |
| 450 | return; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 451 | Str << "@"; |
| 452 | if (Func && !SuppressMangling) { |
| 453 | Str << Func->getContext()->mangleName(Name); |
| 454 | } else { |
| 455 | Str << Name; |
| 456 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 457 | if (Offset) |
| 458 | Str << "+" << Offset; |
| 459 | } |
| 460 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 461 | void LiveRange::dump(Ostream &Str) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 462 | if (!ALLOW_DUMP) |
| 463 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 464 | Str << "(weight=" << Weight << ") "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 465 | bool First = true; |
| 466 | for (const RangeElementType &I : Range) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 467 | if (!First) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 468 | Str << ", "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 469 | First = false; |
| 470 | Str << "[" << I.first << ":" << I.second << ")"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | Ostream &operator<<(Ostream &Str, const LiveRange &L) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 475 | if (!ALLOW_DUMP) |
| 476 | return Str; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 477 | L.dump(Str); |
| 478 | return Str; |
| 479 | } |
| 480 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 481 | Ostream &operator<<(Ostream &Str, const RegWeight &W) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 482 | if (!ALLOW_DUMP) |
| 483 | return Str; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 484 | if (W.getWeight() == RegWeight::Inf) |
| 485 | Str << "Inf"; |
| 486 | else |
| 487 | Str << W.getWeight(); |
| 488 | return Str; |
| 489 | } |
| 490 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 491 | } // end of namespace Ice |