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