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