Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 1 | //===-- X86InstComments.cpp - Generate verbose-asm comments for instrs ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 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 defines functionality used to emit comments about X86 instructions to |
| 11 | // an output stream for -fverbose-asm. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86InstComments.h" |
Evan Cheng | ed5e355 | 2011-07-06 22:01:53 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/X86MCTargetDesc.h" |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 17 | #include "Utils/X86ShuffleDecode.h" |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCInst.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 23 | // Top Level Entrypoint |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 26 | /// EmitAnyX86InstComments - This function decodes x86 instructions and prints |
| 27 | /// newline terminated strings to the specified string if desired. This |
| 28 | /// information is shown in disassembly dumps when verbose assembly is enabled. |
| 29 | void llvm::EmitAnyX86InstComments(const MCInst *MI, raw_ostream &OS, |
| 30 | const char *(*getRegName)(unsigned)) { |
| 31 | // If this is a shuffle operation, the switch should fill in this state. |
| 32 | SmallVector<unsigned, 8> ShuffleMask; |
| 33 | const char *DestName = 0, *Src1Name = 0, *Src2Name = 0; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 35 | switch (MI->getOpcode()) { |
| 36 | case X86::INSERTPSrr: |
| 37 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 38 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 39 | DecodeINSERTPSMask(MI->getOperand(3).getImm(), ShuffleMask); |
| 40 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 42 | case X86::MOVLHPSrr: |
| 43 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 44 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
Bruno Cardoso Lopes | 5594560 | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 45 | DecodeMOVLHPSMask(2, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 46 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 48 | case X86::MOVHLPSrr: |
| 49 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 50 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
Bruno Cardoso Lopes | 5594560 | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 51 | DecodeMOVHLPSMask(2, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 52 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 54 | case X86::PSHUFDri: |
| 55 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 56 | // FALL THROUGH. |
| 57 | case X86::PSHUFDmi: |
| 58 | DestName = getRegName(MI->getOperand(0).getReg()); |
| 59 | DecodePSHUFMask(4, MI->getOperand(MI->getNumOperands()-1).getImm(), |
| 60 | ShuffleMask); |
| 61 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 63 | case X86::PSHUFHWri: |
| 64 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 65 | // FALL THROUGH. |
| 66 | case X86::PSHUFHWmi: |
| 67 | DestName = getRegName(MI->getOperand(0).getReg()); |
| 68 | DecodePSHUFHWMask(MI->getOperand(MI->getNumOperands()-1).getImm(), |
| 69 | ShuffleMask); |
| 70 | break; |
| 71 | case X86::PSHUFLWri: |
| 72 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 73 | // FALL THROUGH. |
| 74 | case X86::PSHUFLWmi: |
| 75 | DestName = getRegName(MI->getOperand(0).getReg()); |
| 76 | DecodePSHUFLWMask(MI->getOperand(MI->getNumOperands()-1).getImm(), |
| 77 | ShuffleMask); |
| 78 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 80 | case X86::PUNPCKHBWrr: |
| 81 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 82 | // FALL THROUGH. |
| 83 | case X86::PUNPCKHBWrm: |
| 84 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 85 | DecodePUNPCKHMask(16, ShuffleMask); |
| 86 | break; |
| 87 | case X86::PUNPCKHWDrr: |
| 88 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 89 | // FALL THROUGH. |
| 90 | case X86::PUNPCKHWDrm: |
| 91 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 92 | DecodePUNPCKHMask(8, ShuffleMask); |
| 93 | break; |
| 94 | case X86::PUNPCKHDQrr: |
| 95 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 96 | // FALL THROUGH. |
| 97 | case X86::PUNPCKHDQrm: |
| 98 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 99 | DecodePUNPCKHMask(4, ShuffleMask); |
| 100 | break; |
| 101 | case X86::PUNPCKHQDQrr: |
| 102 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 103 | // FALL THROUGH. |
| 104 | case X86::PUNPCKHQDQrm: |
| 105 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 106 | DecodePUNPCKHMask(2, ShuffleMask); |
| 107 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 109 | case X86::PUNPCKLBWrr: |
| 110 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 111 | // FALL THROUGH. |
| 112 | case X86::PUNPCKLBWrm: |
| 113 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 114 | DecodePUNPCKLBWMask(16, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 115 | break; |
| 116 | case X86::PUNPCKLWDrr: |
| 117 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 118 | // FALL THROUGH. |
| 119 | case X86::PUNPCKLWDrm: |
| 120 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 121 | DecodePUNPCKLWDMask(8, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 122 | break; |
| 123 | case X86::PUNPCKLDQrr: |
| 124 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 125 | // FALL THROUGH. |
| 126 | case X86::PUNPCKLDQrm: |
| 127 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 128 | DecodePUNPCKLDQMask(4, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 129 | break; |
| 130 | case X86::PUNPCKLQDQrr: |
| 131 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 132 | // FALL THROUGH. |
| 133 | case X86::PUNPCKLQDQrm: |
| 134 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 135 | DecodePUNPCKLQDQMask(2, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 136 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 138 | case X86::SHUFPDrri: |
| 139 | DecodeSHUFPSMask(2, MI->getOperand(3).getImm(), ShuffleMask); |
| 140 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 141 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 142 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 144 | case X86::SHUFPSrri: |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 145 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 146 | // FALL THROUGH. |
| 147 | case X86::SHUFPSrmi: |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 148 | DecodeSHUFPSMask(4, MI->getOperand(3).getImm(), ShuffleMask); |
| 149 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 150 | break; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 152 | case X86::UNPCKLPDrr: |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 153 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 154 | // FALL THROUGH. |
| 155 | case X86::UNPCKLPDrm: |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 156 | DecodeUNPCKLPDMask(2, ShuffleMask); |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 157 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 158 | break; |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 159 | case X86::VUNPCKLPDrr: |
| 160 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 161 | // FALL THROUGH. |
| 162 | case X86::VUNPCKLPDrm: |
| 163 | DecodeUNPCKLPDMask(2, ShuffleMask); |
| 164 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 165 | break; |
| 166 | case X86::VUNPCKLPDYrr: |
| 167 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 168 | // FALL THROUGH. |
| 169 | case X86::VUNPCKLPDYrm: |
| 170 | DecodeUNPCKLPDMask(4, ShuffleMask); |
| 171 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 172 | break; |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 173 | case X86::UNPCKLPSrr: |
| 174 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 175 | // FALL THROUGH. |
| 176 | case X86::UNPCKLPSrm: |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 177 | DecodeUNPCKLPSMask(4, ShuffleMask); |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 178 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 179 | break; |
David Greene | c4db4e5 | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 180 | case X86::VUNPCKLPSrr: |
| 181 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 182 | // FALL THROUGH. |
| 183 | case X86::VUNPCKLPSrm: |
| 184 | DecodeUNPCKLPSMask(4, ShuffleMask); |
| 185 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 186 | break; |
| 187 | case X86::VUNPCKLPSYrr: |
| 188 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 189 | // FALL THROUGH. |
| 190 | case X86::VUNPCKLPSYrm: |
| 191 | DecodeUNPCKLPSMask(8, ShuffleMask); |
| 192 | Src1Name = getRegName(MI->getOperand(1).getReg()); |
| 193 | break; |
Chris Lattner | 4644a93 | 2010-08-29 03:08:08 +0000 | [diff] [blame] | 194 | case X86::UNPCKHPDrr: |
| 195 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 196 | // FALL THROUGH. |
| 197 | case X86::UNPCKHPDrm: |
| 198 | DecodeUNPCKHPMask(2, ShuffleMask); |
| 199 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 200 | break; |
| 201 | case X86::UNPCKHPSrr: |
| 202 | Src2Name = getRegName(MI->getOperand(2).getReg()); |
| 203 | // FALL THROUGH. |
| 204 | case X86::UNPCKHPSrm: |
| 205 | DecodeUNPCKHPMask(4, ShuffleMask); |
| 206 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 207 | break; |
Bruno Cardoso Lopes | 65b74e1 | 2011-07-21 01:55:47 +0000 | [diff] [blame] | 208 | case X86::VPERMILPSYri: |
| 209 | DecodeVPERMILPSMask(8, MI->getOperand(2).getImm(), |
| 210 | ShuffleMask); |
| 211 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 212 | break; |
| 213 | case X86::VPERMILPDYri: |
| 214 | DecodeVPERMILPSMask(4, MI->getOperand(2).getImm(), |
| 215 | ShuffleMask); |
| 216 | Src1Name = getRegName(MI->getOperand(0).getReg()); |
| 217 | break; |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 221 | // If this was a shuffle operation, print the shuffle mask. |
| 222 | if (!ShuffleMask.empty()) { |
| 223 | if (DestName == 0) DestName = Src1Name; |
| 224 | OS << (DestName ? DestName : "mem") << " = "; |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 226 | // If the two sources are the same, canonicalize the input elements to be |
| 227 | // from the first src so that we get larger element spans. |
| 228 | if (Src1Name == Src2Name) { |
| 229 | for (unsigned i = 0, e = ShuffleMask.size(); i != e; ++i) { |
| 230 | if ((int)ShuffleMask[i] >= 0 && // Not sentinel. |
| 231 | ShuffleMask[i] >= e) // From second mask. |
| 232 | ShuffleMask[i] -= e; |
| 233 | } |
| 234 | } |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 236 | // The shuffle mask specifies which elements of the src1/src2 fill in the |
| 237 | // destination, with a few sentinel values. Loop through and print them |
| 238 | // out. |
| 239 | for (unsigned i = 0, e = ShuffleMask.size(); i != e; ++i) { |
| 240 | if (i != 0) |
| 241 | OS << ','; |
| 242 | if (ShuffleMask[i] == SM_SentinelZero) { |
| 243 | OS << "zero"; |
| 244 | continue; |
| 245 | } |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 247 | // Otherwise, it must come from src1 or src2. Print the span of elements |
| 248 | // that comes from this src. |
| 249 | bool isSrc1 = ShuffleMask[i] < ShuffleMask.size(); |
| 250 | const char *SrcName = isSrc1 ? Src1Name : Src2Name; |
| 251 | OS << (SrcName ? SrcName : "mem") << '['; |
| 252 | bool IsFirst = true; |
| 253 | while (i != e && |
| 254 | (int)ShuffleMask[i] >= 0 && |
| 255 | (ShuffleMask[i] < ShuffleMask.size()) == isSrc1) { |
| 256 | if (!IsFirst) |
| 257 | OS << ','; |
| 258 | else |
| 259 | IsFirst = false; |
| 260 | OS << ShuffleMask[i] % ShuffleMask.size(); |
| 261 | ++i; |
| 262 | } |
| 263 | OS << ']'; |
| 264 | --i; // For loop increments element #. |
| 265 | } |
| 266 | //MI->print(OS, 0); |
| 267 | OS << "\n"; |
| 268 | } |
Bruno Cardoso Lopes | 6b1d0a3 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 6aa928d | 2010-08-28 20:42:31 +0000 | [diff] [blame] | 270 | } |