Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceInst.cpp - High-level instruction 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 |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the Inst class, primarily the various subclass |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 12 | /// constructors and dump routines. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 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 "IceInst.h" |
| 17 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 18 | #include "IceCfg.h" |
| 19 | #include "IceCfgNode.h" |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 20 | #include "IceInstVarIter.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 21 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 22 | #include "IceOperand.h" |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 23 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | |
| 25 | namespace Ice { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 30 | const struct InstArithmeticAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 31 | const char *DisplayString; |
| 32 | bool IsCommutative; |
| 33 | } InstArithmeticAttributes[] = { |
| 34 | #define X(tag, str, commutative) \ |
| 35 | { str, commutative } \ |
| 36 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 37 | ICEINSTARITHMETIC_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 38 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 39 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 40 | |
| 41 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 42 | const struct InstCastAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 43 | const char *DisplayString; |
| 44 | } InstCastAttributes[] = { |
| 45 | #define X(tag, str) \ |
| 46 | { str } \ |
| 47 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 48 | ICEINSTCAST_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 49 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 50 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 51 | |
| 52 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 53 | const struct InstFcmpAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 54 | const char *DisplayString; |
| 55 | } InstFcmpAttributes[] = { |
| 56 | #define X(tag, str) \ |
| 57 | { str } \ |
| 58 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 59 | ICEINSTFCMP_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 60 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 61 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 62 | |
| 63 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 64 | const struct InstIcmpAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 65 | const char *DisplayString; |
| 66 | } InstIcmpAttributes[] = { |
| 67 | #define X(tag, str) \ |
| 68 | { str } \ |
| 69 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 70 | ICEINSTICMP_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 71 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 72 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 73 | |
| 74 | } // end of anonymous namespace |
| 75 | |
| 76 | Inst::Inst(Cfg *Func, InstKind Kind, SizeT MaxSrcs, Variable *Dest) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 77 | : Kind(Kind), Number(Func->newInstNumber()), Dest(Dest), MaxSrcs(MaxSrcs), |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 78 | Srcs(Func->allocateArrayOf<Operand *>(MaxSrcs)), LiveRangesEnded(0) {} |
| 79 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 80 | const char *Inst::getInstName() const { |
Eric Holk | e37076a | 2016-01-27 14:06:35 -0800 | [diff] [blame] | 81 | if (!BuildDefs::dump()) |
| 82 | return "???"; |
| 83 | |
| 84 | switch (Kind) { |
| 85 | #define X(InstrKind, name) \ |
| 86 | case InstrKind: \ |
| 87 | return name |
| 88 | X(Unreachable, "unreachable"); |
| 89 | X(Alloca, "alloca"); |
| 90 | X(Arithmetic, "arithmetic"); |
| 91 | X(Br, "br"); |
| 92 | X(Call, "call"); |
| 93 | X(Cast, "cast"); |
| 94 | X(ExtractElement, "extractelement"); |
| 95 | X(Fcmp, "fcmp"); |
| 96 | X(Icmp, "icmp"); |
| 97 | X(IntrinsicCall, "intrinsiccall"); |
| 98 | X(InsertElement, "insertelement"); |
| 99 | X(Load, "load"); |
| 100 | X(Phi, "phi"); |
| 101 | X(Ret, "ret"); |
| 102 | X(Select, "select"); |
| 103 | X(Store, "store"); |
| 104 | X(Switch, "switch"); |
| 105 | X(Assign, "assign"); |
| 106 | X(BundleLock, "bundlelock"); |
| 107 | X(BundleUnlock, "bundleunlock"); |
| 108 | X(FakeDef, "fakedef"); |
| 109 | X(FakeUse, "fakeuse"); |
| 110 | X(FakeKill, "fakekill"); |
| 111 | X(JumpTable, "jumptable"); |
| 112 | #undef X |
| 113 | default: |
| 114 | assert(Kind >= Target); |
| 115 | return "target"; |
| 116 | } |
| 117 | } |
| 118 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 119 | // Assign the instruction a new number. |
| 120 | void Inst::renumber(Cfg *Func) { |
| 121 | Number = isDeleted() ? NumberDeleted : Func->newInstNumber(); |
| 122 | } |
| 123 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 124 | // Delete the instruction if its tentative Dead flag is still set after |
| 125 | // liveness analysis. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 126 | void Inst::deleteIfDead() { |
| 127 | if (Dead) |
| 128 | setDeleted(); |
| 129 | } |
| 130 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 131 | // If Src is a Variable, it returns true if this instruction ends Src's live |
| 132 | // range. Otherwise, returns false. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 133 | bool Inst::isLastUse(const Operand *TestSrc) const { |
| 134 | if (LiveRangesEnded == 0) |
| 135 | return false; // early-exit optimization |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 136 | if (auto *TestVar = llvm::dyn_cast<const Variable>(TestSrc)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 137 | LREndedBits Mask = LiveRangesEnded; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 138 | FOREACH_VAR_IN_INST(Var, *this) { |
| 139 | if (Var == TestVar) { |
| 140 | // We've found where the variable is used in the instruction. |
| 141 | return Mask & 1; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 142 | } |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 143 | Mask >>= 1; |
| 144 | if (Mask == 0) |
| 145 | return false; // another early-exit optimization |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 150 | |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 151 | // Given an instruction like: |
| 152 | // a = b + c + [x,y] + e |
| 153 | // which was created from OrigInst: |
| 154 | // a = b + c + d + e |
| 155 | // with SpliceAssn spliced in: |
| 156 | // d = [x,y] |
| 157 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 158 | // Reconstruct the LiveRangesEnded bitmask in this instruction by combining the |
| 159 | // LiveRangesEnded values of OrigInst and SpliceAssn. If operands d and [x,y] |
| 160 | // contain a different number of variables, then the bitmask position for e may |
| 161 | // be different in OrigInst and the current instruction, requiring extra shifts |
| 162 | // and masks in the computation. In the example above, OrigInst has variable e |
| 163 | // in bit position 3, whereas the current instruction has e in bit position 4 |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 164 | // because [x,y] consumes 2 bitmask slots while d only consumed 1. |
| 165 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 166 | // Additionally, set HasSideEffects if either OrigInst or SpliceAssn have |
| 167 | // HasSideEffects set. |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 168 | void Inst::spliceLivenessInfo(Inst *OrigInst, Inst *SpliceAssn) { |
| 169 | HasSideEffects |= OrigInst->HasSideEffects; |
| 170 | HasSideEffects |= SpliceAssn->HasSideEffects; |
| 171 | // Find the bitmask index of SpliceAssn's dest within OrigInst. |
| 172 | Variable *SpliceDest = SpliceAssn->getDest(); |
| 173 | SizeT Index = 0; |
| 174 | for (SizeT I = 0; I < OrigInst->getSrcSize(); ++I) { |
| 175 | Operand *Src = OrigInst->getSrc(I); |
| 176 | if (Src == SpliceDest) { |
| 177 | LREndedBits LeftMask = OrigInst->LiveRangesEnded & ((1 << Index) - 1); |
| 178 | LREndedBits RightMask = OrigInst->LiveRangesEnded >> (Index + 1); |
| 179 | LiveRangesEnded = LeftMask | (SpliceAssn->LiveRangesEnded << Index) | |
| 180 | (RightMask << (Index + getSrc(I)->getNumVars())); |
| 181 | return; |
| 182 | } |
| 183 | Index += getSrc(I)->getNumVars(); |
| 184 | } |
| 185 | llvm::report_fatal_error("Failed to find splice operand"); |
| 186 | } |
| 187 | |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 188 | void Inst::livenessLightweight(Cfg *Func, LivenessBV &Live) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 189 | assert(!isDeleted()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 190 | resetLastUses(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 191 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 192 | FOREACH_VAR_IN_INST(Var, *this) { |
| 193 | if (VMetadata->isMultiBlock(Var)) |
| 194 | continue; |
| 195 | SizeT Index = Var->getIndex(); |
| 196 | if (Live[Index]) |
| 197 | continue; |
| 198 | Live[Index] = true; |
| 199 | setLastUse(IndexOfVarInInst(Var)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 203 | bool Inst::liveness(InstNumberT InstNumber, LivenessBV &Live, |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 204 | Liveness *Liveness, LiveBeginEndMap *LiveBegin, |
| 205 | LiveBeginEndMap *LiveEnd) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 206 | assert(!isDeleted()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 207 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 208 | Dead = false; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 209 | if (Dest && !Dest->isRematerializable()) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 210 | SizeT VarNum = Liveness->getLiveIndex(Dest->getIndex()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 211 | if (Live[VarNum]) { |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 212 | if (!isDestRedefined()) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 213 | Live[VarNum] = false; |
Jim Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame] | 214 | if (LiveBegin && Liveness->getRangeMask(Dest->getIndex())) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 215 | LiveBegin->push_back(std::make_pair(VarNum, InstNumber)); |
| 216 | } |
| 217 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 218 | } else { |
| 219 | if (!hasSideEffects()) |
| 220 | Dead = true; |
| 221 | } |
| 222 | } |
| 223 | if (Dead) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 224 | return false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 225 | // Phi arguments only get added to Live in the predecessor node, but we still |
| 226 | // need to update LiveRangesEnded. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 227 | bool IsPhi = llvm::isa<InstPhi>(this); |
| 228 | resetLastUses(); |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 229 | FOREACH_VAR_IN_INST(Var, *this) { |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 230 | if (Var->isRematerializable()) |
| 231 | continue; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 232 | SizeT VarNum = Liveness->getLiveIndex(Var->getIndex()); |
| 233 | if (!Live[VarNum]) { |
| 234 | setLastUse(IndexOfVarInInst(Var)); |
| 235 | if (!IsPhi) { |
| 236 | Live[VarNum] = true; |
| 237 | // For a variable in SSA form, its live range can end at most once in a |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 238 | // basic block. However, after lowering to two-address instructions, we |
| 239 | // end up with sequences like "t=b;t+=c;a=t" where t's live range |
| 240 | // begins and ends twice. ICE only allows a variable to have a single |
| 241 | // liveness interval in a basic block (except for blocks where a |
| 242 | // variable is live-in and live-out but there is a gap in the middle). |
| 243 | // Therefore, this lowered sequence needs to represent a single |
| 244 | // conservative live range for t. Since the instructions are being |
| 245 | // traversed backwards, we make sure LiveEnd is only set once by |
| 246 | // setting it only when LiveEnd[VarNum]==0 (sentinel value). Note that |
| 247 | // it's OK to set LiveBegin multiple times because of the backwards |
| 248 | // traversal. |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 249 | if (LiveEnd && Liveness->getRangeMask(Var->getIndex())) { |
| 250 | // Ideally, we would verify that VarNum wasn't already added in this |
| 251 | // block, but this can't be done very efficiently with LiveEnd as a |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 252 | // vector. Instead, livenessPostprocess() verifies this after the |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 253 | // vector has been sorted. |
| 254 | LiveEnd->push_back(std::make_pair(VarNum, InstNumber)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 259 | return true; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 262 | InstAlloca::InstAlloca(Cfg *Func, Variable *Dest, Operand *ByteCount, |
| 263 | uint32_t AlignInBytes) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 264 | : InstHighLevel(Func, Inst::Alloca, 1, Dest), AlignInBytes(AlignInBytes) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 265 | // Verify AlignInBytes is 0 or a power of 2. |
| 266 | assert(AlignInBytes == 0 || llvm::isPowerOf2_32(AlignInBytes)); |
| 267 | addSource(ByteCount); |
| 268 | } |
| 269 | |
| 270 | InstArithmetic::InstArithmetic(Cfg *Func, OpKind Op, Variable *Dest, |
| 271 | Operand *Source1, Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 272 | : InstHighLevel(Func, Inst::Arithmetic, 2, Dest), Op(Op) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 273 | addSource(Source1); |
| 274 | addSource(Source2); |
| 275 | } |
| 276 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 277 | const char *InstArithmetic::getInstName() const { |
Eric Holk | e37076a | 2016-01-27 14:06:35 -0800 | [diff] [blame] | 278 | if (!BuildDefs::dump()) |
| 279 | return "???"; |
| 280 | |
| 281 | return InstArithmeticAttributes[getOp()].DisplayString; |
| 282 | } |
| 283 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 284 | const char *InstArithmetic::getOpName(OpKind Op) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 285 | return Op < InstArithmetic::_num ? InstArithmeticAttributes[Op].DisplayString |
| 286 | : "???"; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 289 | bool InstArithmetic::isCommutative() const { |
| 290 | return InstArithmeticAttributes[getOp()].IsCommutative; |
| 291 | } |
| 292 | |
| 293 | InstAssign::InstAssign(Cfg *Func, Variable *Dest, Operand *Source) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 294 | : InstHighLevel(Func, Inst::Assign, 1, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 295 | addSource(Source); |
| 296 | } |
| 297 | |
Jim Stichnoth | 28b71be | 2015-10-12 15:24:46 -0700 | [diff] [blame] | 298 | bool InstAssign::isVarAssign() const { return llvm::isa<Variable>(getSrc(0)); } |
| 299 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 300 | // If TargetTrue==TargetFalse, we turn it into an unconditional branch. This |
| 301 | // ensures that, along with the 'switch' instruction semantics, there is at |
| 302 | // most one edge from one node to another. |
Karl Schimpf | c070d6f | 2015-06-26 10:25:33 -0700 | [diff] [blame] | 303 | InstBr::InstBr(Cfg *Func, Operand *Source, CfgNode *TargetTrue_, |
| 304 | CfgNode *TargetFalse_) |
| 305 | : InstHighLevel(Func, Inst::Br, 1, nullptr), TargetFalse(TargetFalse_), |
| 306 | TargetTrue(TargetTrue_) { |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame^] | 307 | if (auto *Constant = llvm::dyn_cast<ConstantInteger32>(Source)) { |
| 308 | int32_t C32 = Constant->getValue(); |
| 309 | if (C32 != 0) { |
| 310 | TargetFalse = TargetTrue; |
| 311 | } |
| 312 | TargetTrue = nullptr; // turn into unconditional version |
| 313 | } else if (TargetTrue == TargetFalse) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 314 | TargetTrue = nullptr; // turn into unconditional version |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 315 | } else { |
| 316 | addSource(Source); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | InstBr::InstBr(Cfg *Func, CfgNode *Target) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 321 | : InstHighLevel(Func, Inst::Br, 0, nullptr), TargetFalse(Target), |
| 322 | TargetTrue(nullptr) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 323 | |
| 324 | NodeList InstBr::getTerminatorEdges() const { |
| 325 | NodeList OutEdges; |
Jim Stichnoth | 9d801a0 | 2014-11-26 14:11:53 -0800 | [diff] [blame] | 326 | OutEdges.reserve(TargetTrue ? 2 : 1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 327 | OutEdges.push_back(TargetFalse); |
| 328 | if (TargetTrue) |
| 329 | OutEdges.push_back(TargetTrue); |
| 330 | return OutEdges; |
| 331 | } |
| 332 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 333 | bool InstBr::repointEdges(CfgNode *OldNode, CfgNode *NewNode) { |
| 334 | bool Found = false; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 335 | if (TargetFalse == OldNode) { |
| 336 | TargetFalse = NewNode; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 337 | Found = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 338 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 339 | if (TargetTrue == OldNode) { |
| 340 | TargetTrue = NewNode; |
| 341 | Found = true; |
| 342 | } |
| 343 | return Found; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 346 | InstCast::InstCast(Cfg *Func, OpKind CastKind, Variable *Dest, Operand *Source) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 347 | : InstHighLevel(Func, Inst::Cast, 1, Dest), CastKind(CastKind) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 348 | addSource(Source); |
| 349 | } |
| 350 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 351 | InstExtractElement::InstExtractElement(Cfg *Func, Variable *Dest, |
| 352 | Operand *Source1, Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 353 | : InstHighLevel(Func, Inst::ExtractElement, 2, Dest) { |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 354 | addSource(Source1); |
| 355 | addSource(Source2); |
| 356 | } |
| 357 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 358 | InstFcmp::InstFcmp(Cfg *Func, FCond Condition, Variable *Dest, Operand *Source1, |
| 359 | Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 360 | : InstHighLevel(Func, Inst::Fcmp, 2, Dest), Condition(Condition) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 361 | addSource(Source1); |
| 362 | addSource(Source2); |
| 363 | } |
| 364 | |
| 365 | InstIcmp::InstIcmp(Cfg *Func, ICond Condition, Variable *Dest, Operand *Source1, |
| 366 | Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 367 | : InstHighLevel(Func, Inst::Icmp, 2, Dest), Condition(Condition) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 368 | addSource(Source1); |
| 369 | addSource(Source2); |
| 370 | } |
| 371 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 372 | InstInsertElement::InstInsertElement(Cfg *Func, Variable *Dest, |
| 373 | Operand *Source1, Operand *Source2, |
| 374 | Operand *Source3) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 375 | : InstHighLevel(Func, Inst::InsertElement, 3, Dest) { |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 376 | addSource(Source1); |
| 377 | addSource(Source2); |
| 378 | addSource(Source3); |
| 379 | } |
| 380 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 381 | InstLoad::InstLoad(Cfg *Func, Variable *Dest, Operand *SourceAddr) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 382 | : InstHighLevel(Func, Inst::Load, 1, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 383 | addSource(SourceAddr); |
| 384 | } |
| 385 | |
| 386 | InstPhi::InstPhi(Cfg *Func, SizeT MaxSrcs, Variable *Dest) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 387 | : InstHighLevel(Func, Phi, MaxSrcs, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 388 | Labels = Func->allocateArrayOf<CfgNode *>(MaxSrcs); |
| 389 | } |
| 390 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 391 | // TODO: A Switch instruction (and maybe others) can add duplicate edges. We |
| 392 | // may want to de-dup Phis and validate consistency (i.e., the source operands |
| 393 | // are the same for duplicate edges), though it seems the current lowering code |
| 394 | // is OK with this situation. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 395 | void InstPhi::addArgument(Operand *Source, CfgNode *Label) { |
| 396 | Labels[getSrcSize()] = Label; |
| 397 | addSource(Source); |
| 398 | } |
| 399 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 400 | // Find the source operand corresponding to the incoming edge for the given |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame^] | 401 | // node. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 402 | Operand *InstPhi::getOperandForTarget(CfgNode *Target) const { |
| 403 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 404 | if (Labels[I] == Target) |
| 405 | return getSrc(I); |
| 406 | } |
| 407 | llvm_unreachable("Phi target not found"); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 408 | return nullptr; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 409 | } |
| 410 | |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame^] | 411 | // Replace the source operand corresponding to the incoming edge for the given |
| 412 | // node by a zero of the appropriate type. |
| 413 | void InstPhi::clearOperandForTarget(CfgNode *Target) { |
| 414 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 415 | if (getLabel(I) == Target) { |
| 416 | Type Ty = Dest->getType(); |
| 417 | Srcs[I] = Target->getCfg()->getContext()->getConstantZero(Ty); |
| 418 | return; |
| 419 | } |
| 420 | } |
| 421 | llvm_unreachable("Phi target not found"); |
| 422 | } |
| 423 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 424 | // Updates liveness for a particular operand based on the given predecessor |
| 425 | // edge. Doesn't mark the operand as live if the Phi instruction is dead or |
| 426 | // deleted. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 427 | void InstPhi::livenessPhiOperand(LivenessBV &Live, CfgNode *Target, |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 428 | Liveness *Liveness) { |
| 429 | if (isDeleted() || Dead) |
| 430 | return; |
| 431 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 432 | if (Labels[I] == Target) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 433 | if (auto *Var = llvm::dyn_cast<Variable>(getSrc(I))) { |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 434 | if (!Var->isRematerializable()) { |
| 435 | SizeT SrcIndex = Liveness->getLiveIndex(Var->getIndex()); |
| 436 | if (!Live[SrcIndex]) { |
| 437 | setLastUse(I); |
| 438 | Live[SrcIndex] = true; |
| 439 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | return; |
| 443 | } |
| 444 | } |
| 445 | llvm_unreachable("Phi operand not found for specified target node"); |
| 446 | } |
| 447 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 448 | // Change "a=phi(...)" to "a_phi=phi(...)" and return a new instruction |
| 449 | // "a=a_phi". |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 450 | Inst *InstPhi::lower(Cfg *Func) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 451 | Variable *Dest = getDest(); |
| 452 | assert(Dest); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 453 | Variable *NewSrc = Func->makeVariable(Dest->getType()); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 454 | if (BuildDefs::dump()) |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 455 | NewSrc->setName(Func, Dest->getName(Func) + "_phi"); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 456 | if (auto *NewSrc64On32 = llvm::dyn_cast<Variable64On32>(NewSrc)) |
| 457 | NewSrc64On32->initHiLo(Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 458 | this->Dest = NewSrc; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 459 | return InstAssign::create(Func, Dest, NewSrc); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 462 | InstRet::InstRet(Cfg *Func, Operand *RetValue) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 463 | : InstHighLevel(Func, Ret, RetValue ? 1 : 0, nullptr) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 464 | if (RetValue) |
| 465 | addSource(RetValue); |
| 466 | } |
| 467 | |
| 468 | InstSelect::InstSelect(Cfg *Func, Variable *Dest, Operand *Condition, |
| 469 | Operand *SourceTrue, Operand *SourceFalse) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 470 | : InstHighLevel(Func, Inst::Select, 3, Dest) { |
Matt Wala | 9cb61e2 | 2014-07-24 09:44:42 -0700 | [diff] [blame] | 471 | assert(typeElementType(Condition->getType()) == IceType_i1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 472 | addSource(Condition); |
| 473 | addSource(SourceTrue); |
| 474 | addSource(SourceFalse); |
| 475 | } |
| 476 | |
| 477 | InstStore::InstStore(Cfg *Func, Operand *Data, Operand *Addr) |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 478 | : InstHighLevel(Func, Inst::Store, 3, nullptr) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 479 | addSource(Data); |
| 480 | addSource(Addr); |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 481 | // The 3rd operand is a dummy placeholder for the RMW beacon. |
| 482 | addSource(Data); |
| 483 | } |
| 484 | |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 485 | Variable *InstStore::getRmwBeacon() const { |
| 486 | return llvm::dyn_cast<Variable>(getSrc(2)); |
| 487 | } |
| 488 | |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 489 | void InstStore::setRmwBeacon(Variable *Beacon) { |
| 490 | Dest = llvm::dyn_cast<Variable>(getData()); |
| 491 | Srcs[2] = Beacon; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | InstSwitch::InstSwitch(Cfg *Func, SizeT NumCases, Operand *Source, |
| 495 | CfgNode *LabelDefault) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 496 | : InstHighLevel(Func, Inst::Switch, 1, nullptr), LabelDefault(LabelDefault), |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 497 | NumCases(NumCases) { |
| 498 | addSource(Source); |
| 499 | Values = Func->allocateArrayOf<uint64_t>(NumCases); |
| 500 | Labels = Func->allocateArrayOf<CfgNode *>(NumCases); |
| 501 | // Initialize in case buggy code doesn't set all entries |
| 502 | for (SizeT I = 0; I < NumCases; ++I) { |
| 503 | Values[I] = 0; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 504 | Labels[I] = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | void InstSwitch::addBranch(SizeT CaseIndex, uint64_t Value, CfgNode *Label) { |
| 509 | assert(CaseIndex < NumCases); |
| 510 | Values[CaseIndex] = Value; |
| 511 | Labels[CaseIndex] = Label; |
| 512 | } |
| 513 | |
| 514 | NodeList InstSwitch::getTerminatorEdges() const { |
| 515 | NodeList OutEdges; |
Jim Stichnoth | 9d801a0 | 2014-11-26 14:11:53 -0800 | [diff] [blame] | 516 | OutEdges.reserve(NumCases + 1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 517 | OutEdges.push_back(LabelDefault); |
| 518 | for (SizeT I = 0; I < NumCases; ++I) { |
| 519 | OutEdges.push_back(Labels[I]); |
| 520 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 521 | std::sort(OutEdges.begin(), OutEdges.end(), |
| 522 | [](const CfgNode *x, const CfgNode *y) { |
| 523 | return x->getIndex() < y->getIndex(); |
| 524 | }); |
| 525 | auto Last = std::unique(OutEdges.begin(), OutEdges.end()); |
| 526 | OutEdges.erase(Last, OutEdges.end()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 527 | return OutEdges; |
| 528 | } |
| 529 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 530 | bool InstSwitch::repointEdges(CfgNode *OldNode, CfgNode *NewNode) { |
| 531 | bool Found = false; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 532 | if (LabelDefault == OldNode) { |
| 533 | LabelDefault = NewNode; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 534 | Found = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 535 | } |
| 536 | for (SizeT I = 0; I < NumCases; ++I) { |
| 537 | if (Labels[I] == OldNode) { |
| 538 | Labels[I] = NewNode; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 539 | Found = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 542 | return Found; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 545 | InstUnreachable::InstUnreachable(Cfg *Func) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 546 | : InstHighLevel(Func, Inst::Unreachable, 0, nullptr) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 547 | |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 548 | InstBundleLock::InstBundleLock(Cfg *Func, InstBundleLock::Option BundleOption) |
| 549 | : InstHighLevel(Func, Inst::BundleLock, 0, nullptr), |
| 550 | BundleOption(BundleOption) {} |
| 551 | |
| 552 | InstBundleUnlock::InstBundleUnlock(Cfg *Func) |
| 553 | : InstHighLevel(Func, Inst::BundleUnlock, 0, nullptr) {} |
| 554 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 555 | InstFakeDef::InstFakeDef(Cfg *Func, Variable *Dest, Variable *Src) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 556 | : InstHighLevel(Func, Inst::FakeDef, Src ? 1 : 0, Dest) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 557 | assert(Dest); |
| 558 | if (Src) |
| 559 | addSource(Src); |
| 560 | } |
| 561 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 562 | InstFakeUse::InstFakeUse(Cfg *Func, Variable *Src, uint32_t Weight) |
| 563 | : InstHighLevel(Func, Inst::FakeUse, Weight, nullptr) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 564 | assert(Src); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 565 | for (uint32_t i = 0; i < Weight; ++i) |
| 566 | addSource(Src); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 569 | InstFakeKill::InstFakeKill(Cfg *Func, const Inst *Linked) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 570 | : InstHighLevel(Func, Inst::FakeKill, 0, nullptr), Linked(Linked) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 571 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 572 | InstJumpTable::InstJumpTable(Cfg *Func, SizeT NumTargets, CfgNode *Default) |
| 573 | : InstHighLevel(Func, Inst::JumpTable, 1, nullptr), |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 574 | Id(Func->getTarget()->makeNextJumpTableNumber()), NumTargets(NumTargets) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 575 | Targets = Func->allocateArrayOf<CfgNode *>(NumTargets); |
| 576 | for (SizeT I = 0; I < NumTargets; ++I) |
| 577 | Targets[I] = Default; |
| 578 | } |
| 579 | |
| 580 | bool InstJumpTable::repointEdges(CfgNode *OldNode, CfgNode *NewNode) { |
| 581 | bool Found = false; |
| 582 | for (SizeT I = 0; I < NumTargets; ++I) { |
| 583 | if (Targets[I] == OldNode) { |
| 584 | Targets[I] = NewNode; |
| 585 | Found = true; |
| 586 | } |
| 587 | } |
| 588 | return Found; |
| 589 | } |
| 590 | |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 591 | Type InstCall::getReturnType() const { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 592 | if (Dest == nullptr) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 593 | return IceType_void; |
| 594 | return Dest->getType(); |
| 595 | } |
| 596 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 597 | // ======================== Dump routines ======================== // |
| 598 | |
| 599 | void Inst::dumpDecorated(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 600 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 601 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 602 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 603 | if (!Func->isVerbose(IceV_Deleted) && (isDeleted() || isRedundantAssign())) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 604 | return; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 605 | if (Func->isVerbose(IceV_InstNumbers)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 606 | char buf[30]; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 607 | InstNumberT Number = getNumber(); |
| 608 | if (Number == NumberDeleted) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 609 | snprintf(buf, llvm::array_lengthof(buf), "[XXX]"); |
| 610 | else |
| 611 | snprintf(buf, llvm::array_lengthof(buf), "[%3d]", Number); |
| 612 | Str << buf; |
| 613 | } |
| 614 | Str << " "; |
| 615 | if (isDeleted()) |
| 616 | Str << " //"; |
| 617 | dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 618 | dumpExtras(Func); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 619 | Str << "\n"; |
| 620 | } |
| 621 | |
| 622 | void Inst::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 623 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 624 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 625 | Ostream &Str = Func->getContext()->getStrDump(); |
| 626 | dumpDest(Func); |
Eric Holk | e37076a | 2016-01-27 14:06:35 -0800 | [diff] [blame] | 627 | Str << " =~ " << getInstName() << " "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 628 | dumpSources(Func); |
| 629 | } |
| 630 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 631 | void Inst::dumpExtras(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 632 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 633 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 634 | Ostream &Str = Func->getContext()->getStrDump(); |
| 635 | bool First = true; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 636 | // Print "LIVEEND={a,b,c}" for all source operands whose live ranges are |
| 637 | // known to end at this instruction. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 638 | if (Func->isVerbose(IceV_Liveness)) { |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 639 | FOREACH_VAR_IN_INST(Var, *this) { |
| 640 | if (isLastUse(Var)) { |
| 641 | if (First) |
| 642 | Str << " // LIVEEND={"; |
| 643 | else |
| 644 | Str << ","; |
| 645 | Var->dump(Func); |
| 646 | First = false; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | if (!First) |
| 650 | Str << "}"; |
| 651 | } |
| 652 | } |
| 653 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 654 | void Inst::dumpSources(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 655 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 656 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 657 | Ostream &Str = Func->getContext()->getStrDump(); |
| 658 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 659 | if (I > 0) |
| 660 | Str << ", "; |
| 661 | getSrc(I)->dump(Func); |
| 662 | } |
| 663 | } |
| 664 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 665 | void Inst::emitSources(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 666 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 667 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 668 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 669 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 670 | if (I > 0) |
| 671 | Str << ", "; |
| 672 | getSrc(I)->emit(Func); |
| 673 | } |
| 674 | } |
| 675 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 676 | void Inst::dumpDest(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 677 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 678 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 679 | if (getDest()) |
| 680 | getDest()->dump(Func); |
| 681 | } |
| 682 | |
| 683 | void InstAlloca::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 684 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 685 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 686 | Ostream &Str = Func->getContext()->getStrDump(); |
| 687 | dumpDest(Func); |
| 688 | Str << " = alloca i8, i32 "; |
| 689 | getSizeInBytes()->dump(Func); |
Jim Stichnoth | 72a8f8d | 2014-09-08 17:56:50 -0700 | [diff] [blame] | 690 | if (getAlignInBytes()) |
| 691 | Str << ", align " << getAlignInBytes(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void InstArithmetic::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 695 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 696 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 697 | Ostream &Str = Func->getContext()->getStrDump(); |
| 698 | dumpDest(Func); |
Eric Holk | e37076a | 2016-01-27 14:06:35 -0800 | [diff] [blame] | 699 | Str << " = " << getInstName() << " " << getDest()->getType() << " "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 700 | dumpSources(Func); |
| 701 | } |
| 702 | |
| 703 | void InstAssign::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 704 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 705 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 706 | Ostream &Str = Func->getContext()->getStrDump(); |
| 707 | dumpDest(Func); |
| 708 | Str << " = " << getDest()->getType() << " "; |
| 709 | dumpSources(Func); |
| 710 | } |
| 711 | |
| 712 | void InstBr::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 713 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 714 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 715 | Ostream &Str = Func->getContext()->getStrDump(); |
| 716 | dumpDest(Func); |
| 717 | Str << "br "; |
| 718 | if (!isUnconditional()) { |
| 719 | Str << "i1 "; |
| 720 | getCondition()->dump(Func); |
| 721 | Str << ", label %" << getTargetTrue()->getName() << ", "; |
| 722 | } |
| 723 | Str << "label %" << getTargetFalse()->getName(); |
| 724 | } |
| 725 | |
| 726 | void InstCall::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 727 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 728 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 729 | Ostream &Str = Func->getContext()->getStrDump(); |
| 730 | if (getDest()) { |
| 731 | dumpDest(Func); |
| 732 | Str << " = "; |
| 733 | } |
| 734 | Str << "call "; |
| 735 | if (getDest()) |
| 736 | Str << getDest()->getType(); |
| 737 | else |
| 738 | Str << "void"; |
| 739 | Str << " "; |
| 740 | getCallTarget()->dump(Func); |
| 741 | Str << "("; |
| 742 | for (SizeT I = 0; I < getNumArgs(); ++I) { |
| 743 | if (I > 0) |
| 744 | Str << ", "; |
| 745 | Str << getArg(I)->getType() << " "; |
| 746 | getArg(I)->dump(Func); |
| 747 | } |
| 748 | Str << ")"; |
| 749 | } |
| 750 | |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 751 | const char *InstCast::getCastName(InstCast::OpKind Kind) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 752 | if (Kind < InstCast::OpKind::_num) |
| 753 | return InstCastAttributes[Kind].DisplayString; |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 754 | llvm_unreachable("Invalid InstCast::OpKind"); |
| 755 | return "???"; |
| 756 | } |
| 757 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 758 | void InstCast::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 759 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 760 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 761 | Ostream &Str = Func->getContext()->getStrDump(); |
| 762 | dumpDest(Func); |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 763 | Str << " = " << getCastName(getCastKind()) << " " << getSrc(0)->getType() |
| 764 | << " "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 765 | dumpSources(Func); |
| 766 | Str << " to " << getDest()->getType(); |
| 767 | } |
| 768 | |
| 769 | void InstIcmp::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 770 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 771 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 772 | Ostream &Str = Func->getContext()->getStrDump(); |
| 773 | dumpDest(Func); |
| 774 | Str << " = icmp " << InstIcmpAttributes[getCondition()].DisplayString << " " |
| 775 | << getSrc(0)->getType() << " "; |
| 776 | dumpSources(Func); |
| 777 | } |
| 778 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 779 | void InstExtractElement::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 780 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 781 | return; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 782 | Ostream &Str = Func->getContext()->getStrDump(); |
| 783 | dumpDest(Func); |
| 784 | Str << " = extractelement "; |
| 785 | Str << getSrc(0)->getType() << " "; |
| 786 | getSrc(0)->dump(Func); |
| 787 | Str << ", "; |
| 788 | Str << getSrc(1)->getType() << " "; |
| 789 | getSrc(1)->dump(Func); |
Derek Schuff | d7ee972 | 2014-07-30 09:39:36 -0700 | [diff] [blame] | 790 | } |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 791 | |
| 792 | void InstInsertElement::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 793 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 794 | return; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 795 | Ostream &Str = Func->getContext()->getStrDump(); |
| 796 | dumpDest(Func); |
| 797 | Str << " = insertelement "; |
| 798 | Str << getSrc(0)->getType() << " "; |
| 799 | getSrc(0)->dump(Func); |
| 800 | Str << ", "; |
| 801 | Str << getSrc(1)->getType() << " "; |
| 802 | getSrc(1)->dump(Func); |
| 803 | Str << ", "; |
| 804 | Str << getSrc(2)->getType() << " "; |
| 805 | getSrc(2)->dump(Func); |
Derek Schuff | d7ee972 | 2014-07-30 09:39:36 -0700 | [diff] [blame] | 806 | } |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 807 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 808 | void InstFcmp::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 809 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 810 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 811 | Ostream &Str = Func->getContext()->getStrDump(); |
| 812 | dumpDest(Func); |
| 813 | Str << " = fcmp " << InstFcmpAttributes[getCondition()].DisplayString << " " |
| 814 | << getSrc(0)->getType() << " "; |
| 815 | dumpSources(Func); |
| 816 | } |
| 817 | |
| 818 | void InstLoad::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 819 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 820 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 821 | Ostream &Str = Func->getContext()->getStrDump(); |
| 822 | dumpDest(Func); |
| 823 | Type Ty = getDest()->getType(); |
Jim Stichnoth | e5b58fb | 2015-06-01 15:17:20 -0700 | [diff] [blame] | 824 | Str << " = load " << Ty << ", " << Ty << "* "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 825 | dumpSources(Func); |
| 826 | Str << ", align " << typeAlignInBytes(Ty); |
| 827 | } |
| 828 | |
| 829 | void InstStore::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 830 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 831 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 832 | Ostream &Str = Func->getContext()->getStrDump(); |
| 833 | Type Ty = getData()->getType(); |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 834 | dumpDest(Func); |
| 835 | if (Dest) |
| 836 | Str << " = "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 837 | Str << "store " << Ty << " "; |
| 838 | getData()->dump(Func); |
| 839 | Str << ", " << Ty << "* "; |
| 840 | getAddr()->dump(Func); |
| 841 | Str << ", align " << typeAlignInBytes(Ty); |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 842 | if (getRmwBeacon()) { |
| 843 | Str << ", beacon "; |
| 844 | getRmwBeacon()->dump(Func); |
| 845 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | void InstSwitch::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 849 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 850 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 851 | Ostream &Str = Func->getContext()->getStrDump(); |
| 852 | Type Ty = getComparison()->getType(); |
| 853 | Str << "switch " << Ty << " "; |
| 854 | getSrc(0)->dump(Func); |
| 855 | Str << ", label %" << getLabelDefault()->getName() << " [\n"; |
| 856 | for (SizeT I = 0; I < getNumCases(); ++I) { |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 857 | Str << " " << Ty << " " << static_cast<int64_t>(getValue(I)) |
| 858 | << ", label %" << getLabel(I)->getName() << "\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 859 | } |
| 860 | Str << " ]"; |
| 861 | } |
| 862 | |
| 863 | void InstPhi::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 864 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 865 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 866 | Ostream &Str = Func->getContext()->getStrDump(); |
| 867 | dumpDest(Func); |
| 868 | Str << " = phi " << getDest()->getType() << " "; |
| 869 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 870 | if (I > 0) |
| 871 | Str << ", "; |
| 872 | Str << "[ "; |
| 873 | getSrc(I)->dump(Func); |
| 874 | Str << ", %" << Labels[I]->getName() << " ]"; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | void InstRet::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 879 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 880 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 881 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 882 | Type Ty = hasRetValue() ? getRetValue()->getType() : IceType_void; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 883 | Str << "ret " << Ty; |
| 884 | if (hasRetValue()) { |
| 885 | Str << " "; |
| 886 | dumpSources(Func); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void InstSelect::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 891 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 892 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 893 | Ostream &Str = Func->getContext()->getStrDump(); |
| 894 | dumpDest(Func); |
| 895 | Operand *Condition = getCondition(); |
| 896 | Operand *TrueOp = getTrueOperand(); |
| 897 | Operand *FalseOp = getFalseOperand(); |
| 898 | Str << " = select " << Condition->getType() << " "; |
| 899 | Condition->dump(Func); |
| 900 | Str << ", " << TrueOp->getType() << " "; |
| 901 | TrueOp->dump(Func); |
| 902 | Str << ", " << FalseOp->getType() << " "; |
| 903 | FalseOp->dump(Func); |
| 904 | } |
| 905 | |
| 906 | void InstUnreachable::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 907 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 908 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 909 | Ostream &Str = Func->getContext()->getStrDump(); |
| 910 | Str << "unreachable"; |
| 911 | } |
| 912 | |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 913 | void InstBundleLock::emit(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 914 | if (!BuildDefs::dump()) |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 915 | return; |
| 916 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 917 | Str << "\t.bundle_lock"; |
| 918 | switch (BundleOption) { |
| 919 | case Opt_None: |
| 920 | break; |
| 921 | case Opt_AlignToEnd: |
Jim Stichnoth | 6106df8 | 2015-12-16 06:17:58 -0800 | [diff] [blame] | 922 | Str << "\t" |
| 923 | "align_to_end"; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 924 | break; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 925 | case Opt_PadToEnd: |
| 926 | Str << "\t" |
| 927 | "align_to_end /* pad_to_end */"; |
| 928 | break; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 929 | } |
Jim Stichnoth | 3343647 | 2015-11-27 14:22:02 -0800 | [diff] [blame] | 930 | Str << "\n"; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | void InstBundleLock::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 934 | if (!BuildDefs::dump()) |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 935 | return; |
| 936 | Ostream &Str = Func->getContext()->getStrDump(); |
| 937 | Str << "bundle_lock"; |
| 938 | switch (BundleOption) { |
| 939 | case Opt_None: |
| 940 | break; |
| 941 | case Opt_AlignToEnd: |
| 942 | Str << " align_to_end"; |
| 943 | break; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 944 | case Opt_PadToEnd: |
| 945 | Str << " pad_to_end"; |
| 946 | break; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
| 950 | void InstBundleUnlock::emit(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 951 | if (!BuildDefs::dump()) |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 952 | return; |
| 953 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 954 | Str << "\t.bundle_unlock"; |
Jim Stichnoth | 3343647 | 2015-11-27 14:22:02 -0800 | [diff] [blame] | 955 | Str << "\n"; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | void InstBundleUnlock::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 959 | if (!BuildDefs::dump()) |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 960 | return; |
| 961 | Ostream &Str = Func->getContext()->getStrDump(); |
| 962 | Str << "bundle_unlock"; |
| 963 | } |
| 964 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 965 | void InstFakeDef::emit(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 966 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 967 | return; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 968 | // Go ahead and "emit" these for now, since they are relatively rare. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 969 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 970 | Str << "\t# "; |
| 971 | getDest()->emit(Func); |
Jim Stichnoth | fbdc7e4 | 2016-02-09 17:25:02 -0800 | [diff] [blame] | 972 | Str << " = def.pseudo"; |
| 973 | if (getSrcSize() > 0) |
| 974 | Str << " "; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 975 | emitSources(Func); |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 976 | Str << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void InstFakeDef::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 980 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 981 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 982 | Ostream &Str = Func->getContext()->getStrDump(); |
| 983 | dumpDest(Func); |
| 984 | Str << " = def.pseudo "; |
| 985 | dumpSources(Func); |
| 986 | } |
| 987 | |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 988 | void InstFakeUse::emit(const Cfg *Func) const { (void)Func; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 989 | |
| 990 | void InstFakeUse::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 991 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 992 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 993 | Ostream &Str = Func->getContext()->getStrDump(); |
| 994 | Str << "use.pseudo "; |
| 995 | dumpSources(Func); |
| 996 | } |
| 997 | |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 998 | void InstFakeKill::emit(const Cfg *Func) const { (void)Func; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 999 | |
| 1000 | void InstFakeKill::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1001 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1002 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1003 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1004 | if (Linked->isDeleted()) |
| 1005 | Str << "// "; |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 1006 | Str << "kill.pseudo scratch_regs"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 1009 | void InstJumpTable::dump(const Cfg *Func) const { |
| 1010 | if (!BuildDefs::dump()) |
| 1011 | return; |
| 1012 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1013 | Str << "jump table ["; |
| 1014 | for (SizeT I = 0; I < NumTargets; ++I) |
| 1015 | Str << "\n " << Targets[I]->getName(); |
| 1016 | Str << "\n ]"; |
| 1017 | } |
| 1018 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1019 | void InstTarget::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1020 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1021 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1022 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1023 | Str << "[TARGET] "; |
| 1024 | Inst::dump(Func); |
| 1025 | } |
| 1026 | |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 1027 | bool checkForRedundantAssign(const Variable *Dest, const Operand *Source) { |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 1028 | const auto *SrcVar = llvm::dyn_cast<const Variable>(Source); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 1029 | if (!SrcVar) |
| 1030 | return false; |
| 1031 | if (Dest->hasReg() && Dest->getRegNum() == SrcVar->getRegNum()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1032 | // TODO: On x86-64, instructions like "mov eax, eax" are used to clear the |
| 1033 | // upper 32 bits of rax. We need to recognize and preserve these. |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 1034 | return true; |
| 1035 | } |
| 1036 | if (!Dest->hasReg() && !SrcVar->hasReg() && |
| 1037 | Dest->getStackOffset() == SrcVar->getStackOffset()) |
| 1038 | return true; |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1042 | } // end of namespace Ice |