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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Inst class, primarily the various |
| 11 | // subclass constructors and dump routines. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "IceCfg.h" |
| 16 | #include "IceCfgNode.h" |
| 17 | #include "IceInst.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 18 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 19 | #include "IceOperand.h" |
| 20 | |
| 21 | namespace Ice { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 26 | const struct InstArithmeticAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 27 | const char *DisplayString; |
| 28 | bool IsCommutative; |
| 29 | } InstArithmeticAttributes[] = { |
| 30 | #define X(tag, str, commutative) \ |
| 31 | { str, commutative } \ |
| 32 | , |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 33 | ICEINSTARITHMETIC_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 35 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 36 | |
| 37 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 38 | const struct InstCastAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 39 | const char *DisplayString; |
| 40 | } InstCastAttributes[] = { |
| 41 | #define X(tag, str) \ |
| 42 | { str } \ |
| 43 | , |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 44 | ICEINSTCAST_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 45 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 46 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 47 | |
| 48 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 49 | const struct InstFcmpAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 50 | const char *DisplayString; |
| 51 | } InstFcmpAttributes[] = { |
| 52 | #define X(tag, str) \ |
| 53 | { str } \ |
| 54 | , |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 55 | ICEINSTFCMP_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 56 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 57 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 58 | |
| 59 | // Using non-anonymous struct so that array_lengthof works. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 60 | const struct InstIcmpAttributes_ { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 61 | const char *DisplayString; |
| 62 | } InstIcmpAttributes[] = { |
| 63 | #define X(tag, str) \ |
| 64 | { str } \ |
| 65 | , |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 66 | ICEINSTICMP_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 67 | #undef X |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 68 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 69 | |
| 70 | } // end of anonymous namespace |
| 71 | |
| 72 | Inst::Inst(Cfg *Func, InstKind Kind, SizeT MaxSrcs, Variable *Dest) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 73 | : Kind(Kind), Number(Func->newInstNumber()), Deleted(false), Dead(false), |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 74 | HasSideEffects(false), IsDestNonKillable(false), Dest(Dest), |
| 75 | MaxSrcs(MaxSrcs), NumSrcs(0), |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 76 | Srcs(Func->allocateArrayOf<Operand *>(MaxSrcs)), LiveRangesEnded(0) {} |
| 77 | |
| 78 | // Assign the instruction a new number. |
| 79 | void Inst::renumber(Cfg *Func) { |
| 80 | Number = isDeleted() ? NumberDeleted : Func->newInstNumber(); |
| 81 | } |
| 82 | |
| 83 | // Delete the instruction if its tentative Dead flag is still set |
| 84 | // after liveness analysis. |
| 85 | void Inst::deleteIfDead() { |
| 86 | if (Dead) |
| 87 | setDeleted(); |
| 88 | } |
| 89 | |
| 90 | // If Src is a Variable, it returns true if this instruction ends |
| 91 | // Src's live range. Otherwise, returns false. |
| 92 | bool Inst::isLastUse(const Operand *TestSrc) const { |
| 93 | if (LiveRangesEnded == 0) |
| 94 | return false; // early-exit optimization |
| 95 | if (const Variable *TestVar = llvm::dyn_cast<const Variable>(TestSrc)) { |
| 96 | LREndedBits Mask = LiveRangesEnded; |
| 97 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 98 | Operand *Src = getSrc(I); |
| 99 | SizeT NumVars = Src->getNumVars(); |
| 100 | for (SizeT J = 0; J < NumVars; ++J) { |
| 101 | const Variable *Var = Src->getVar(J); |
| 102 | if (Var == TestVar) { |
| 103 | // We've found where the variable is used in the instruction. |
| 104 | return Mask & 1; |
| 105 | } |
| 106 | Mask >>= 1; |
| 107 | if (Mask == 0) |
| 108 | return false; // another early-exit optimization |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 114 | |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 115 | void Inst::livenessLightweight(Cfg *Func, LivenessBV &Live) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 116 | assert(!isDeleted()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 117 | resetLastUses(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 118 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 119 | SizeT VarIndex = 0; |
| 120 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 121 | Operand *Src = getSrc(I); |
| 122 | SizeT NumVars = Src->getNumVars(); |
| 123 | for (SizeT J = 0; J < NumVars; ++J, ++VarIndex) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 124 | const Variable *Var = Src->getVar(J); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 125 | if (VMetadata->isMultiBlock(Var)) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 126 | continue; |
| 127 | SizeT Index = Var->getIndex(); |
| 128 | if (Live[Index]) |
| 129 | continue; |
| 130 | Live[Index] = true; |
| 131 | setLastUse(VarIndex); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 136 | bool Inst::liveness(InstNumberT InstNumber, LivenessBV &Live, |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 137 | Liveness *Liveness, LiveBeginEndMap *LiveBegin, |
| 138 | LiveBeginEndMap *LiveEnd) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 139 | assert(!isDeleted()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 140 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 141 | Dead = false; |
| 142 | if (Dest) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 143 | SizeT VarNum = Liveness->getLiveIndex(Dest->getIndex()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 144 | if (Live[VarNum]) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 145 | if (!isDestNonKillable()) { |
| 146 | Live[VarNum] = false; |
| 147 | if (LiveBegin) { |
| 148 | LiveBegin->push_back(std::make_pair(VarNum, InstNumber)); |
| 149 | } |
| 150 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 151 | } else { |
| 152 | if (!hasSideEffects()) |
| 153 | Dead = true; |
| 154 | } |
| 155 | } |
| 156 | if (Dead) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 157 | return false; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 158 | // Phi arguments only get added to Live in the predecessor node, but |
| 159 | // we still need to update LiveRangesEnded. |
| 160 | bool IsPhi = llvm::isa<InstPhi>(this); |
| 161 | resetLastUses(); |
| 162 | SizeT VarIndex = 0; |
| 163 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 164 | Operand *Src = getSrc(I); |
| 165 | SizeT NumVars = Src->getNumVars(); |
| 166 | for (SizeT J = 0; J < NumVars; ++J, ++VarIndex) { |
| 167 | const Variable *Var = Src->getVar(J); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 168 | SizeT VarNum = Liveness->getLiveIndex(Var->getIndex()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 169 | if (!Live[VarNum]) { |
| 170 | setLastUse(VarIndex); |
| 171 | if (!IsPhi) { |
| 172 | Live[VarNum] = true; |
| 173 | // For a variable in SSA form, its live range can end at |
| 174 | // most once in a basic block. However, after lowering to |
| 175 | // two-address instructions, we end up with sequences like |
| 176 | // "t=b;t+=c;a=t" where t's live range begins and ends |
| 177 | // twice. ICE only allows a variable to have a single |
| 178 | // liveness interval in a basic block (except for blocks |
| 179 | // where a variable is live-in and live-out but there is a |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 180 | // gap in the middle). Therefore, this lowered sequence |
| 181 | // needs to represent a single conservative live range for |
| 182 | // t. Since the instructions are being traversed backwards, |
| 183 | // we make sure LiveEnd is only set once by setting it only |
| 184 | // when LiveEnd[VarNum]==0 (sentinel value). Note that it's |
| 185 | // OK to set LiveBegin multiple times because of the |
| 186 | // backwards traversal. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 187 | if (LiveEnd) { |
| 188 | // Ideally, we would verify that VarNum wasn't already |
| 189 | // added in this block, but this can't be done very |
| 190 | // efficiently with LiveEnd as a vector. Instead, |
| 191 | // livenessPostprocess() verifies this after the vector |
| 192 | // has been sorted. |
| 193 | LiveEnd->push_back(std::make_pair(VarNum, InstNumber)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 199 | return true; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | InstAlloca::InstAlloca(Cfg *Func, Operand *ByteCount, uint32_t AlignInBytes, |
| 203 | Variable *Dest) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 204 | : InstHighLevel(Func, Inst::Alloca, 1, Dest), AlignInBytes(AlignInBytes) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 205 | // Verify AlignInBytes is 0 or a power of 2. |
| 206 | assert(AlignInBytes == 0 || llvm::isPowerOf2_32(AlignInBytes)); |
| 207 | addSource(ByteCount); |
| 208 | } |
| 209 | |
| 210 | InstArithmetic::InstArithmetic(Cfg *Func, OpKind Op, Variable *Dest, |
| 211 | Operand *Source1, Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 212 | : InstHighLevel(Func, Inst::Arithmetic, 2, Dest), Op(Op) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 213 | addSource(Source1); |
| 214 | addSource(Source2); |
| 215 | } |
| 216 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 217 | const char *InstArithmetic::getOpName(OpKind Op) { |
| 218 | size_t OpIndex = static_cast<size_t>(Op); |
| 219 | return OpIndex < InstArithmetic::_num |
| 220 | ? InstArithmeticAttributes[OpIndex].DisplayString |
| 221 | : "???"; |
| 222 | } |
| 223 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 224 | bool InstArithmetic::isCommutative() const { |
| 225 | return InstArithmeticAttributes[getOp()].IsCommutative; |
| 226 | } |
| 227 | |
| 228 | InstAssign::InstAssign(Cfg *Func, Variable *Dest, Operand *Source) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 229 | : InstHighLevel(Func, Inst::Assign, 1, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 230 | addSource(Source); |
| 231 | } |
| 232 | |
| 233 | // If TargetTrue==TargetFalse, we turn it into an unconditional |
| 234 | // branch. This ensures that, along with the 'switch' instruction |
| 235 | // semantics, there is at most one edge from one node to another. |
| 236 | InstBr::InstBr(Cfg *Func, Operand *Source, CfgNode *TargetTrue, |
| 237 | CfgNode *TargetFalse) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 238 | : InstHighLevel(Func, Inst::Br, 1, nullptr), TargetFalse(TargetFalse), |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 239 | TargetTrue(TargetTrue) { |
| 240 | if (TargetTrue == TargetFalse) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 241 | TargetTrue = nullptr; // turn into unconditional version |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 242 | } else { |
| 243 | addSource(Source); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | InstBr::InstBr(Cfg *Func, CfgNode *Target) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 248 | : InstHighLevel(Func, Inst::Br, 0, nullptr), TargetFalse(Target), |
| 249 | TargetTrue(nullptr) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 250 | |
| 251 | NodeList InstBr::getTerminatorEdges() const { |
| 252 | NodeList OutEdges; |
Jim Stichnoth | 9d801a0 | 2014-11-26 14:11:53 -0800 | [diff] [blame] | 253 | OutEdges.reserve(TargetTrue ? 2 : 1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 254 | OutEdges.push_back(TargetFalse); |
| 255 | if (TargetTrue) |
| 256 | OutEdges.push_back(TargetTrue); |
| 257 | return OutEdges; |
| 258 | } |
| 259 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 260 | bool InstBr::repointEdge(CfgNode *OldNode, CfgNode *NewNode) { |
| 261 | if (TargetFalse == OldNode) { |
| 262 | TargetFalse = NewNode; |
| 263 | return true; |
| 264 | } else if (TargetTrue == OldNode) { |
| 265 | TargetTrue = NewNode; |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 271 | InstCast::InstCast(Cfg *Func, OpKind CastKind, Variable *Dest, Operand *Source) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 272 | : InstHighLevel(Func, Inst::Cast, 1, Dest), CastKind(CastKind) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 273 | addSource(Source); |
| 274 | } |
| 275 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 276 | InstExtractElement::InstExtractElement(Cfg *Func, Variable *Dest, |
| 277 | Operand *Source1, Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 278 | : InstHighLevel(Func, Inst::ExtractElement, 2, Dest) { |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 279 | addSource(Source1); |
| 280 | addSource(Source2); |
| 281 | } |
| 282 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 283 | InstFcmp::InstFcmp(Cfg *Func, FCond Condition, Variable *Dest, Operand *Source1, |
| 284 | Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 285 | : InstHighLevel(Func, Inst::Fcmp, 2, Dest), Condition(Condition) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 286 | addSource(Source1); |
| 287 | addSource(Source2); |
| 288 | } |
| 289 | |
| 290 | InstIcmp::InstIcmp(Cfg *Func, ICond Condition, Variable *Dest, Operand *Source1, |
| 291 | Operand *Source2) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 292 | : InstHighLevel(Func, Inst::Icmp, 2, Dest), Condition(Condition) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 293 | addSource(Source1); |
| 294 | addSource(Source2); |
| 295 | } |
| 296 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 297 | InstInsertElement::InstInsertElement(Cfg *Func, Variable *Dest, |
| 298 | Operand *Source1, Operand *Source2, |
| 299 | Operand *Source3) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 300 | : InstHighLevel(Func, Inst::InsertElement, 3, Dest) { |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 301 | addSource(Source1); |
| 302 | addSource(Source2); |
| 303 | addSource(Source3); |
| 304 | } |
| 305 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 306 | InstLoad::InstLoad(Cfg *Func, Variable *Dest, Operand *SourceAddr) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 307 | : InstHighLevel(Func, Inst::Load, 1, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 308 | addSource(SourceAddr); |
| 309 | } |
| 310 | |
| 311 | InstPhi::InstPhi(Cfg *Func, SizeT MaxSrcs, Variable *Dest) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 312 | : InstHighLevel(Func, Phi, MaxSrcs, Dest) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 313 | Labels = Func->allocateArrayOf<CfgNode *>(MaxSrcs); |
| 314 | } |
| 315 | |
| 316 | // TODO: A Switch instruction (and maybe others) can add duplicate |
| 317 | // edges. We may want to de-dup Phis and validate consistency (i.e., |
| 318 | // the source operands are the same for duplicate edges), though it |
| 319 | // seems the current lowering code is OK with this situation. |
| 320 | void InstPhi::addArgument(Operand *Source, CfgNode *Label) { |
| 321 | Labels[getSrcSize()] = Label; |
| 322 | addSource(Source); |
| 323 | } |
| 324 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 325 | // Find the source operand corresponding to the incoming edge for the |
| 326 | // given node. TODO: This uses a linear-time search, which could be |
| 327 | // improved if it becomes a problem. |
| 328 | Operand *InstPhi::getOperandForTarget(CfgNode *Target) const { |
| 329 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 330 | if (Labels[I] == Target) |
| 331 | return getSrc(I); |
| 332 | } |
| 333 | llvm_unreachable("Phi target not found"); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 334 | return nullptr; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 337 | // Updates liveness for a particular operand based on the given |
| 338 | // predecessor edge. Doesn't mark the operand as live if the Phi |
| 339 | // instruction is dead or deleted. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 340 | void InstPhi::livenessPhiOperand(LivenessBV &Live, CfgNode *Target, |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 341 | Liveness *Liveness) { |
| 342 | if (isDeleted() || Dead) |
| 343 | return; |
| 344 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 345 | if (Labels[I] == Target) { |
| 346 | if (Variable *Var = llvm::dyn_cast<Variable>(getSrc(I))) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 347 | SizeT SrcIndex = Liveness->getLiveIndex(Var->getIndex()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 348 | if (!Live[SrcIndex]) { |
| 349 | setLastUse(I); |
| 350 | Live[SrcIndex] = true; |
| 351 | } |
| 352 | } |
| 353 | return; |
| 354 | } |
| 355 | } |
| 356 | llvm_unreachable("Phi operand not found for specified target node"); |
| 357 | } |
| 358 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 359 | // Change "a=phi(...)" to "a_phi=phi(...)" and return a new |
| 360 | // instruction "a=a_phi". |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 361 | Inst *InstPhi::lower(Cfg *Func) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 362 | Variable *Dest = getDest(); |
| 363 | assert(Dest); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 364 | Variable *NewSrc = Func->makeVariable(Dest->getType()); |
| 365 | if (ALLOW_DUMP) |
| 366 | NewSrc->setName(Func, Dest->getName(Func) + "_phi"); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 367 | this->Dest = NewSrc; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 368 | return InstAssign::create(Func, Dest, NewSrc); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 371 | InstRet::InstRet(Cfg *Func, Operand *RetValue) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 372 | : InstHighLevel(Func, Ret, RetValue ? 1 : 0, nullptr) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 373 | if (RetValue) |
| 374 | addSource(RetValue); |
| 375 | } |
| 376 | |
| 377 | InstSelect::InstSelect(Cfg *Func, Variable *Dest, Operand *Condition, |
| 378 | Operand *SourceTrue, Operand *SourceFalse) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 379 | : InstHighLevel(Func, Inst::Select, 3, Dest) { |
Matt Wala | 9cb61e2 | 2014-07-24 09:44:42 -0700 | [diff] [blame] | 380 | assert(typeElementType(Condition->getType()) == IceType_i1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 381 | addSource(Condition); |
| 382 | addSource(SourceTrue); |
| 383 | addSource(SourceFalse); |
| 384 | } |
| 385 | |
| 386 | InstStore::InstStore(Cfg *Func, Operand *Data, Operand *Addr) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 387 | : InstHighLevel(Func, Inst::Store, 2, nullptr) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 388 | addSource(Data); |
| 389 | addSource(Addr); |
| 390 | } |
| 391 | |
| 392 | InstSwitch::InstSwitch(Cfg *Func, SizeT NumCases, Operand *Source, |
| 393 | CfgNode *LabelDefault) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 394 | : InstHighLevel(Func, Inst::Switch, 1, nullptr), LabelDefault(LabelDefault), |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 395 | NumCases(NumCases) { |
| 396 | addSource(Source); |
| 397 | Values = Func->allocateArrayOf<uint64_t>(NumCases); |
| 398 | Labels = Func->allocateArrayOf<CfgNode *>(NumCases); |
| 399 | // Initialize in case buggy code doesn't set all entries |
| 400 | for (SizeT I = 0; I < NumCases; ++I) { |
| 401 | Values[I] = 0; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 402 | Labels[I] = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | void InstSwitch::addBranch(SizeT CaseIndex, uint64_t Value, CfgNode *Label) { |
| 407 | assert(CaseIndex < NumCases); |
| 408 | Values[CaseIndex] = Value; |
| 409 | Labels[CaseIndex] = Label; |
| 410 | } |
| 411 | |
| 412 | NodeList InstSwitch::getTerminatorEdges() const { |
| 413 | NodeList OutEdges; |
Jim Stichnoth | 9d801a0 | 2014-11-26 14:11:53 -0800 | [diff] [blame] | 414 | OutEdges.reserve(NumCases + 1); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 415 | OutEdges.push_back(LabelDefault); |
| 416 | for (SizeT I = 0; I < NumCases; ++I) { |
| 417 | OutEdges.push_back(Labels[I]); |
| 418 | } |
| 419 | return OutEdges; |
| 420 | } |
| 421 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 422 | bool InstSwitch::repointEdge(CfgNode *OldNode, CfgNode *NewNode) { |
| 423 | if (LabelDefault == OldNode) { |
| 424 | LabelDefault = NewNode; |
| 425 | return true; |
| 426 | } |
| 427 | for (SizeT I = 0; I < NumCases; ++I) { |
| 428 | if (Labels[I] == OldNode) { |
| 429 | Labels[I] = NewNode; |
| 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 436 | InstUnreachable::InstUnreachable(Cfg *Func) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 437 | : InstHighLevel(Func, Inst::Unreachable, 0, nullptr) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 438 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 439 | InstFakeDef::InstFakeDef(Cfg *Func, Variable *Dest, Variable *Src) |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 440 | : InstHighLevel(Func, Inst::FakeDef, Src ? 1 : 0, Dest) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 441 | assert(Dest); |
| 442 | if (Src) |
| 443 | addSource(Src); |
| 444 | } |
| 445 | |
| 446 | InstFakeUse::InstFakeUse(Cfg *Func, Variable *Src) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 447 | : InstHighLevel(Func, Inst::FakeUse, 1, nullptr) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 448 | assert(Src); |
| 449 | addSource(Src); |
| 450 | } |
| 451 | |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 452 | InstFakeKill::InstFakeKill(Cfg *Func, const Inst *Linked) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 453 | : InstHighLevel(Func, Inst::FakeKill, 0, nullptr), Linked(Linked) {} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 454 | |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 455 | Type InstCall::getReturnType() const { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 456 | if (Dest == nullptr) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 457 | return IceType_void; |
| 458 | return Dest->getType(); |
| 459 | } |
| 460 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 461 | // ======================== Dump routines ======================== // |
| 462 | |
| 463 | void Inst::dumpDecorated(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 464 | if (!ALLOW_DUMP) |
| 465 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 466 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 467 | if (!Func->isVerbose(IceV_Deleted) && (isDeleted() || isRedundantAssign())) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 468 | return; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 469 | if (Func->isVerbose(IceV_InstNumbers)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 470 | char buf[30]; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 471 | InstNumberT Number = getNumber(); |
| 472 | if (Number == NumberDeleted) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 473 | snprintf(buf, llvm::array_lengthof(buf), "[XXX]"); |
| 474 | else |
| 475 | snprintf(buf, llvm::array_lengthof(buf), "[%3d]", Number); |
| 476 | Str << buf; |
| 477 | } |
| 478 | Str << " "; |
| 479 | if (isDeleted()) |
| 480 | Str << " //"; |
| 481 | dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 482 | dumpExtras(Func); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 483 | Str << "\n"; |
| 484 | } |
| 485 | |
| 486 | void Inst::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 487 | if (!ALLOW_DUMP) |
| 488 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 489 | Ostream &Str = Func->getContext()->getStrDump(); |
| 490 | dumpDest(Func); |
| 491 | Str << " =~ "; |
| 492 | dumpSources(Func); |
| 493 | } |
| 494 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 495 | void Inst::dumpExtras(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 496 | if (!ALLOW_DUMP) |
| 497 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 498 | Ostream &Str = Func->getContext()->getStrDump(); |
| 499 | bool First = true; |
| 500 | // Print "LIVEEND={a,b,c}" for all source operands whose live ranges |
| 501 | // are known to end at this instruction. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 502 | if (Func->isVerbose(IceV_Liveness)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 503 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 504 | Operand *Src = getSrc(I); |
| 505 | SizeT NumVars = Src->getNumVars(); |
| 506 | for (SizeT J = 0; J < NumVars; ++J) { |
| 507 | const Variable *Var = Src->getVar(J); |
| 508 | if (isLastUse(Var)) { |
| 509 | if (First) |
| 510 | Str << " // LIVEEND={"; |
| 511 | else |
| 512 | Str << ","; |
| 513 | Var->dump(Func); |
| 514 | First = false; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | if (!First) |
| 519 | Str << "}"; |
| 520 | } |
| 521 | } |
| 522 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 523 | void Inst::dumpSources(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 524 | if (!ALLOW_DUMP) |
| 525 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 526 | Ostream &Str = Func->getContext()->getStrDump(); |
| 527 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 528 | if (I > 0) |
| 529 | Str << ", "; |
| 530 | getSrc(I)->dump(Func); |
| 531 | } |
| 532 | } |
| 533 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 534 | void Inst::emitSources(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 535 | if (!ALLOW_DUMP) |
| 536 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 537 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 538 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 539 | if (I > 0) |
| 540 | Str << ", "; |
| 541 | getSrc(I)->emit(Func); |
| 542 | } |
| 543 | } |
| 544 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 545 | void Inst::dumpDest(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 546 | if (!ALLOW_DUMP) |
| 547 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 548 | if (getDest()) |
| 549 | getDest()->dump(Func); |
| 550 | } |
| 551 | |
| 552 | void InstAlloca::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 553 | if (!ALLOW_DUMP) |
| 554 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 555 | Ostream &Str = Func->getContext()->getStrDump(); |
| 556 | dumpDest(Func); |
| 557 | Str << " = alloca i8, i32 "; |
| 558 | getSizeInBytes()->dump(Func); |
Jim Stichnoth | 72a8f8d | 2014-09-08 17:56:50 -0700 | [diff] [blame] | 559 | if (getAlignInBytes()) |
| 560 | Str << ", align " << getAlignInBytes(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void InstArithmetic::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 564 | if (!ALLOW_DUMP) |
| 565 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 566 | Ostream &Str = Func->getContext()->getStrDump(); |
| 567 | dumpDest(Func); |
| 568 | Str << " = " << InstArithmeticAttributes[getOp()].DisplayString << " " |
| 569 | << getDest()->getType() << " "; |
| 570 | dumpSources(Func); |
| 571 | } |
| 572 | |
| 573 | void InstAssign::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 574 | if (!ALLOW_DUMP) |
| 575 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 576 | Ostream &Str = Func->getContext()->getStrDump(); |
| 577 | dumpDest(Func); |
| 578 | Str << " = " << getDest()->getType() << " "; |
| 579 | dumpSources(Func); |
| 580 | } |
| 581 | |
| 582 | void InstBr::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 583 | if (!ALLOW_DUMP) |
| 584 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 585 | Ostream &Str = Func->getContext()->getStrDump(); |
| 586 | dumpDest(Func); |
| 587 | Str << "br "; |
| 588 | if (!isUnconditional()) { |
| 589 | Str << "i1 "; |
| 590 | getCondition()->dump(Func); |
| 591 | Str << ", label %" << getTargetTrue()->getName() << ", "; |
| 592 | } |
| 593 | Str << "label %" << getTargetFalse()->getName(); |
| 594 | } |
| 595 | |
| 596 | void InstCall::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 597 | if (!ALLOW_DUMP) |
| 598 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 599 | Ostream &Str = Func->getContext()->getStrDump(); |
| 600 | if (getDest()) { |
| 601 | dumpDest(Func); |
| 602 | Str << " = "; |
| 603 | } |
| 604 | Str << "call "; |
| 605 | if (getDest()) |
| 606 | Str << getDest()->getType(); |
| 607 | else |
| 608 | Str << "void"; |
| 609 | Str << " "; |
| 610 | getCallTarget()->dump(Func); |
| 611 | Str << "("; |
| 612 | for (SizeT I = 0; I < getNumArgs(); ++I) { |
| 613 | if (I > 0) |
| 614 | Str << ", "; |
| 615 | Str << getArg(I)->getType() << " "; |
| 616 | getArg(I)->dump(Func); |
| 617 | } |
| 618 | Str << ")"; |
| 619 | } |
| 620 | |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 621 | const char *InstCast::getCastName(InstCast::OpKind Kind) { |
| 622 | size_t Index = static_cast<size_t>(Kind); |
| 623 | if (Index < InstCast::OpKind::_num) |
| 624 | return InstCastAttributes[Index].DisplayString; |
| 625 | llvm_unreachable("Invalid InstCast::OpKind"); |
| 626 | return "???"; |
| 627 | } |
| 628 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 629 | void InstCast::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 630 | if (!ALLOW_DUMP) |
| 631 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 632 | Ostream &Str = Func->getContext()->getStrDump(); |
| 633 | dumpDest(Func); |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 634 | Str << " = " << getCastName(getCastKind()) << " " << getSrc(0)->getType() |
| 635 | << " "; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 636 | dumpSources(Func); |
| 637 | Str << " to " << getDest()->getType(); |
| 638 | } |
| 639 | |
| 640 | void InstIcmp::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 641 | if (!ALLOW_DUMP) |
| 642 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 643 | Ostream &Str = Func->getContext()->getStrDump(); |
| 644 | dumpDest(Func); |
| 645 | Str << " = icmp " << InstIcmpAttributes[getCondition()].DisplayString << " " |
| 646 | << getSrc(0)->getType() << " "; |
| 647 | dumpSources(Func); |
| 648 | } |
| 649 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 650 | void InstExtractElement::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 651 | if (!ALLOW_DUMP) |
| 652 | return; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 653 | Ostream &Str = Func->getContext()->getStrDump(); |
| 654 | dumpDest(Func); |
| 655 | Str << " = extractelement "; |
| 656 | Str << getSrc(0)->getType() << " "; |
| 657 | getSrc(0)->dump(Func); |
| 658 | Str << ", "; |
| 659 | Str << getSrc(1)->getType() << " "; |
| 660 | getSrc(1)->dump(Func); |
Derek Schuff | d7ee972 | 2014-07-30 09:39:36 -0700 | [diff] [blame] | 661 | } |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 662 | |
| 663 | void InstInsertElement::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 664 | if (!ALLOW_DUMP) |
| 665 | return; |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 666 | Ostream &Str = Func->getContext()->getStrDump(); |
| 667 | dumpDest(Func); |
| 668 | Str << " = insertelement "; |
| 669 | Str << getSrc(0)->getType() << " "; |
| 670 | getSrc(0)->dump(Func); |
| 671 | Str << ", "; |
| 672 | Str << getSrc(1)->getType() << " "; |
| 673 | getSrc(1)->dump(Func); |
| 674 | Str << ", "; |
| 675 | Str << getSrc(2)->getType() << " "; |
| 676 | getSrc(2)->dump(Func); |
Derek Schuff | d7ee972 | 2014-07-30 09:39:36 -0700 | [diff] [blame] | 677 | } |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 678 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 679 | void InstFcmp::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 680 | if (!ALLOW_DUMP) |
| 681 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 682 | Ostream &Str = Func->getContext()->getStrDump(); |
| 683 | dumpDest(Func); |
| 684 | Str << " = fcmp " << InstFcmpAttributes[getCondition()].DisplayString << " " |
| 685 | << getSrc(0)->getType() << " "; |
| 686 | dumpSources(Func); |
| 687 | } |
| 688 | |
| 689 | void InstLoad::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 690 | if (!ALLOW_DUMP) |
| 691 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 692 | Ostream &Str = Func->getContext()->getStrDump(); |
| 693 | dumpDest(Func); |
| 694 | Type Ty = getDest()->getType(); |
| 695 | Str << " = load " << Ty << "* "; |
| 696 | dumpSources(Func); |
| 697 | Str << ", align " << typeAlignInBytes(Ty); |
| 698 | } |
| 699 | |
| 700 | void InstStore::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 701 | if (!ALLOW_DUMP) |
| 702 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 703 | Ostream &Str = Func->getContext()->getStrDump(); |
| 704 | Type Ty = getData()->getType(); |
| 705 | Str << "store " << Ty << " "; |
| 706 | getData()->dump(Func); |
| 707 | Str << ", " << Ty << "* "; |
| 708 | getAddr()->dump(Func); |
| 709 | Str << ", align " << typeAlignInBytes(Ty); |
| 710 | } |
| 711 | |
| 712 | void InstSwitch::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 713 | if (!ALLOW_DUMP) |
| 714 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 715 | Ostream &Str = Func->getContext()->getStrDump(); |
| 716 | Type Ty = getComparison()->getType(); |
| 717 | Str << "switch " << Ty << " "; |
| 718 | getSrc(0)->dump(Func); |
| 719 | Str << ", label %" << getLabelDefault()->getName() << " [\n"; |
| 720 | for (SizeT I = 0; I < getNumCases(); ++I) { |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 721 | Str << " " << Ty << " " << static_cast<int64_t>(getValue(I)) |
| 722 | << ", label %" << getLabel(I)->getName() << "\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 723 | } |
| 724 | Str << " ]"; |
| 725 | } |
| 726 | |
| 727 | void InstPhi::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 728 | if (!ALLOW_DUMP) |
| 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 << " = phi " << getDest()->getType() << " "; |
| 733 | for (SizeT I = 0; I < getSrcSize(); ++I) { |
| 734 | if (I > 0) |
| 735 | Str << ", "; |
| 736 | Str << "[ "; |
| 737 | getSrc(I)->dump(Func); |
| 738 | Str << ", %" << Labels[I]->getName() << " ]"; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void InstRet::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 743 | if (!ALLOW_DUMP) |
| 744 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 745 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 746 | Type Ty = hasRetValue() ? getRetValue()->getType() : IceType_void; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 747 | Str << "ret " << Ty; |
| 748 | if (hasRetValue()) { |
| 749 | Str << " "; |
| 750 | dumpSources(Func); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void InstSelect::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 755 | if (!ALLOW_DUMP) |
| 756 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 757 | Ostream &Str = Func->getContext()->getStrDump(); |
| 758 | dumpDest(Func); |
| 759 | Operand *Condition = getCondition(); |
| 760 | Operand *TrueOp = getTrueOperand(); |
| 761 | Operand *FalseOp = getFalseOperand(); |
| 762 | Str << " = select " << Condition->getType() << " "; |
| 763 | Condition->dump(Func); |
| 764 | Str << ", " << TrueOp->getType() << " "; |
| 765 | TrueOp->dump(Func); |
| 766 | Str << ", " << FalseOp->getType() << " "; |
| 767 | FalseOp->dump(Func); |
| 768 | } |
| 769 | |
| 770 | void InstUnreachable::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 771 | if (!ALLOW_DUMP) |
| 772 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 773 | Ostream &Str = Func->getContext()->getStrDump(); |
| 774 | Str << "unreachable"; |
| 775 | } |
| 776 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 777 | void InstFakeDef::emit(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 778 | if (!ALLOW_DUMP) |
| 779 | return; |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 780 | // Go ahead and "emit" these for now, since they are relatively |
| 781 | // rare. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 782 | Ostream &Str = Func->getContext()->getStrEmit(); |
| 783 | Str << "\t# "; |
| 784 | getDest()->emit(Func); |
| 785 | Str << " = def.pseudo "; |
| 786 | emitSources(Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void InstFakeDef::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 790 | if (!ALLOW_DUMP) |
| 791 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 792 | Ostream &Str = Func->getContext()->getStrDump(); |
| 793 | dumpDest(Func); |
| 794 | Str << " = def.pseudo "; |
| 795 | dumpSources(Func); |
| 796 | } |
| 797 | |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 798 | void InstFakeUse::emit(const Cfg *Func) const { (void)Func; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 799 | |
| 800 | void InstFakeUse::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 801 | if (!ALLOW_DUMP) |
| 802 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 803 | Ostream &Str = Func->getContext()->getStrDump(); |
| 804 | Str << "use.pseudo "; |
| 805 | dumpSources(Func); |
| 806 | } |
| 807 | |
Jim Stichnoth | bb8b624 | 2014-11-04 09:10:01 -0800 | [diff] [blame] | 808 | void InstFakeKill::emit(const Cfg *Func) const { (void)Func; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 809 | |
| 810 | void InstFakeKill::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 811 | if (!ALLOW_DUMP) |
| 812 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 813 | Ostream &Str = Func->getContext()->getStrDump(); |
| 814 | if (Linked->isDeleted()) |
| 815 | Str << "// "; |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 816 | Str << "kill.pseudo scratch_regs"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | void InstTarget::dump(const Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 820 | if (!ALLOW_DUMP) |
| 821 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 822 | Ostream &Str = Func->getContext()->getStrDump(); |
| 823 | Str << "[TARGET] "; |
| 824 | Inst::dump(Func); |
| 825 | } |
| 826 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 827 | } // end of namespace Ice |