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