John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceTargetLoweringX86BaseImpl.h - x86 lowering -*- C++ -*-==// |
| 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 TargetLoweringX86Base class, which consists almost |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 12 | /// entirely of the lowering sequence for each high-level instruction. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |
| 17 | #define SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |
| 18 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 19 | #include "IceCfg.h" |
| 20 | #include "IceCfgNode.h" |
| 21 | #include "IceClFlags.h" |
| 22 | #include "IceDefs.h" |
| 23 | #include "IceELFObjectWriter.h" |
| 24 | #include "IceGlobalInits.h" |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 25 | #include "IceInstVarIter.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 26 | #include "IceLiveness.h" |
| 27 | #include "IceOperand.h" |
Jan Voung | 5348369 | 2015-07-16 10:47:46 -0700 | [diff] [blame] | 28 | #include "IcePhiLoweringImpl.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 29 | #include "IceUtils.h" |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 30 | #include "IceInstX86Base.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 32 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 33 | #include <stack> |
| 34 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 35 | namespace Ice { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 36 | namespace X86 { |
| 37 | template <typename T> struct PoolTypeConverter {}; |
| 38 | |
| 39 | template <> struct PoolTypeConverter<float> { |
| 40 | using PrimitiveIntType = uint32_t; |
| 41 | using IceType = ConstantFloat; |
| 42 | static const Type Ty = IceType_f32; |
| 43 | static const char *TypeName; |
| 44 | static const char *AsmTag; |
| 45 | static const char *PrintfString; |
| 46 | }; |
| 47 | |
| 48 | template <> struct PoolTypeConverter<double> { |
| 49 | using PrimitiveIntType = uint64_t; |
| 50 | using IceType = ConstantDouble; |
| 51 | static const Type Ty = IceType_f64; |
| 52 | static const char *TypeName; |
| 53 | static const char *AsmTag; |
| 54 | static const char *PrintfString; |
| 55 | }; |
| 56 | |
| 57 | // Add converter for int type constant pooling |
| 58 | template <> struct PoolTypeConverter<uint32_t> { |
| 59 | using PrimitiveIntType = uint32_t; |
| 60 | using IceType = ConstantInteger32; |
| 61 | static const Type Ty = IceType_i32; |
| 62 | static const char *TypeName; |
| 63 | static const char *AsmTag; |
| 64 | static const char *PrintfString; |
| 65 | }; |
| 66 | |
| 67 | // Add converter for int type constant pooling |
| 68 | template <> struct PoolTypeConverter<uint16_t> { |
| 69 | using PrimitiveIntType = uint32_t; |
| 70 | using IceType = ConstantInteger32; |
| 71 | static const Type Ty = IceType_i16; |
| 72 | static const char *TypeName; |
| 73 | static const char *AsmTag; |
| 74 | static const char *PrintfString; |
| 75 | }; |
| 76 | |
| 77 | // Add converter for int type constant pooling |
| 78 | template <> struct PoolTypeConverter<uint8_t> { |
| 79 | using PrimitiveIntType = uint32_t; |
| 80 | using IceType = ConstantInteger32; |
| 81 | static const Type Ty = IceType_i8; |
| 82 | static const char *TypeName; |
| 83 | static const char *AsmTag; |
| 84 | static const char *PrintfString; |
| 85 | }; |
| 86 | } // end of namespace X86 |
| 87 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 88 | namespace X86NAMESPACE { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 89 | |
Eric Holk | d6cf6b3 | 2016-02-17 11:09:48 -0800 | [diff] [blame] | 90 | using Utils::BoolFlagSaver; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 91 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 92 | template <typename Traits> class BoolFoldingEntry { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 93 | BoolFoldingEntry(const BoolFoldingEntry &) = delete; |
| 94 | |
| 95 | public: |
| 96 | BoolFoldingEntry() = default; |
| 97 | explicit BoolFoldingEntry(Inst *I); |
| 98 | BoolFoldingEntry &operator=(const BoolFoldingEntry &) = default; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 99 | /// Instr is the instruction producing the i1-type variable of interest. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 100 | Inst *Instr = nullptr; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 101 | /// IsComplex is the cached result of BoolFolding::hasComplexLowering(Instr). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 102 | bool IsComplex = false; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 103 | /// IsLiveOut is initialized conservatively to true, and is set to false when |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 104 | /// we encounter an instruction that ends Var's live range. We disable the |
| 105 | /// folding optimization when Var is live beyond this basic block. Note that |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 106 | /// if liveness analysis is not performed (e.g. in Om1 mode), IsLiveOut will |
| 107 | /// always be true and the folding optimization will never be performed. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 108 | bool IsLiveOut = true; |
| 109 | // NumUses counts the number of times Var is used as a source operand in the |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 110 | // basic block. If IsComplex is true and there is more than one use of Var, |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 111 | // then the folding optimization is disabled for Var. |
| 112 | uint32_t NumUses = 0; |
| 113 | }; |
| 114 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 115 | template <typename Traits> class BoolFolding { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 116 | public: |
| 117 | enum BoolFoldingProducerKind { |
| 118 | PK_None, |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 119 | // TODO(jpp): PK_Icmp32 is no longer meaningful. Rename to PK_IcmpNative. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 120 | PK_Icmp32, |
| 121 | PK_Icmp64, |
| 122 | PK_Fcmp, |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 123 | PK_Trunc, |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 124 | PK_Arith // A flag-setting arithmetic instruction. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 127 | /// Currently the actual enum values are not used (other than CK_None), but we |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 128 | /// go ahead and produce them anyway for symmetry with the |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 129 | /// BoolFoldingProducerKind. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 130 | enum BoolFoldingConsumerKind { CK_None, CK_Br, CK_Select, CK_Sext, CK_Zext }; |
| 131 | |
| 132 | private: |
| 133 | BoolFolding(const BoolFolding &) = delete; |
| 134 | BoolFolding &operator=(const BoolFolding &) = delete; |
| 135 | |
| 136 | public: |
| 137 | BoolFolding() = default; |
| 138 | static BoolFoldingProducerKind getProducerKind(const Inst *Instr); |
| 139 | static BoolFoldingConsumerKind getConsumerKind(const Inst *Instr); |
| 140 | static bool hasComplexLowering(const Inst *Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 141 | static bool isValidFolding(BoolFoldingProducerKind ProducerKind, |
| 142 | BoolFoldingConsumerKind ConsumerKind); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 143 | void init(CfgNode *Node); |
| 144 | const Inst *getProducerFor(const Operand *Opnd) const; |
| 145 | void dump(const Cfg *Func) const; |
| 146 | |
| 147 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 148 | /// Returns true if Producers contains a valid entry for the given VarNum. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 149 | bool containsValid(SizeT VarNum) const { |
| 150 | auto Element = Producers.find(VarNum); |
| 151 | return Element != Producers.end() && Element->second.Instr != nullptr; |
| 152 | } |
| 153 | void setInvalid(SizeT VarNum) { Producers[VarNum].Instr = nullptr; } |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 154 | void invalidateProducersOnStore(const Inst *Instr); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 155 | /// Producers maps Variable::Number to a BoolFoldingEntry. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 156 | CfgUnorderedMap<SizeT, BoolFoldingEntry<Traits>> Producers; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 159 | template <typename Traits> |
| 160 | BoolFoldingEntry<Traits>::BoolFoldingEntry(Inst *I) |
| 161 | : Instr(I), IsComplex(BoolFolding<Traits>::hasComplexLowering(I)) {} |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 162 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 163 | template <typename Traits> |
| 164 | typename BoolFolding<Traits>::BoolFoldingProducerKind |
| 165 | BoolFolding<Traits>::getProducerKind(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 166 | if (llvm::isa<InstIcmp>(Instr)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 167 | if (Traits::Is64Bit || Instr->getSrc(0)->getType() != IceType_i64) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 168 | return PK_Icmp32; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 169 | return PK_Icmp64; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 170 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 171 | if (llvm::isa<InstFcmp>(Instr)) |
| 172 | return PK_Fcmp; |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 173 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Instr)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 174 | if (Traits::Is64Bit || Arith->getSrc(0)->getType() != IceType_i64) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 175 | switch (Arith->getOp()) { |
| 176 | default: |
| 177 | return PK_None; |
| 178 | case InstArithmetic::And: |
| 179 | case InstArithmetic::Or: |
| 180 | return PK_Arith; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return PK_None; // TODO(stichnot): remove this |
| 185 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 186 | if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 187 | switch (Cast->getCastKind()) { |
| 188 | default: |
| 189 | return PK_None; |
| 190 | case InstCast::Trunc: |
| 191 | return PK_Trunc; |
| 192 | } |
| 193 | } |
| 194 | return PK_None; |
| 195 | } |
| 196 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 197 | template <typename Traits> |
| 198 | typename BoolFolding<Traits>::BoolFoldingConsumerKind |
| 199 | BoolFolding<Traits>::getConsumerKind(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 200 | if (llvm::isa<InstBr>(Instr)) |
| 201 | return CK_Br; |
| 202 | if (llvm::isa<InstSelect>(Instr)) |
| 203 | return CK_Select; |
| 204 | return CK_None; // TODO(stichnot): remove this |
| 205 | |
| 206 | if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 207 | switch (Cast->getCastKind()) { |
| 208 | default: |
| 209 | return CK_None; |
| 210 | case InstCast::Sext: |
| 211 | return CK_Sext; |
| 212 | case InstCast::Zext: |
| 213 | return CK_Zext; |
| 214 | } |
| 215 | } |
| 216 | return CK_None; |
| 217 | } |
| 218 | |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 219 | /// Returns true if the producing instruction has a "complex" lowering sequence. |
| 220 | /// This generally means that its lowering sequence requires more than one |
| 221 | /// conditional branch, namely 64-bit integer compares and some floating-point |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 222 | /// compares. When this is true, and there is more than one consumer, we prefer |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 223 | /// to disable the folding optimization because it minimizes branches. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 224 | template <typename Traits> |
| 225 | bool BoolFolding<Traits>::hasComplexLowering(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 226 | switch (getProducerKind(Instr)) { |
| 227 | default: |
| 228 | return false; |
| 229 | case PK_Icmp64: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 230 | return !Traits::Is64Bit; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 231 | case PK_Fcmp: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 232 | return Traits::TableFcmp[llvm::cast<InstFcmp>(Instr)->getCondition()].C2 != |
| 233 | Traits::Cond::Br_None; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 237 | template <typename Traits> |
| 238 | bool BoolFolding<Traits>::isValidFolding( |
| 239 | typename BoolFolding<Traits>::BoolFoldingProducerKind ProducerKind, |
| 240 | typename BoolFolding<Traits>::BoolFoldingConsumerKind ConsumerKind) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 241 | switch (ProducerKind) { |
| 242 | default: |
| 243 | return false; |
| 244 | case PK_Icmp32: |
| 245 | case PK_Icmp64: |
| 246 | case PK_Fcmp: |
| 247 | return (ConsumerKind == CK_Br) || (ConsumerKind == CK_Select); |
| 248 | case PK_Arith: |
| 249 | return ConsumerKind == CK_Br; |
| 250 | } |
| 251 | } |
| 252 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 253 | template <typename Traits> void BoolFolding<Traits>::init(CfgNode *Node) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 254 | Producers.clear(); |
| 255 | for (Inst &Instr : Node->getInsts()) { |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 256 | if (Instr.isDeleted()) |
| 257 | continue; |
| 258 | invalidateProducersOnStore(&Instr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 259 | // Check whether Instr is a valid producer. |
| 260 | Variable *Var = Instr.getDest(); |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 261 | if (Var // only consider instructions with an actual dest var |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 262 | && Var->getType() == IceType_i1 // only bool-type dest vars |
| 263 | && getProducerKind(&Instr) != PK_None) { // white-listed instructions |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 264 | Producers[Var->getIndex()] = BoolFoldingEntry<Traits>(&Instr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 265 | } |
| 266 | // Check each src variable against the map. |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 267 | FOREACH_VAR_IN_INST(Var, Instr) { |
| 268 | SizeT VarNum = Var->getIndex(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 269 | if (!containsValid(VarNum)) |
| 270 | continue; |
| 271 | // All valid consumers use Var as the first source operand |
| 272 | if (IndexOfVarOperandInInst(Var) != 0) { |
| 273 | setInvalid(VarNum); |
| 274 | continue; |
| 275 | } |
| 276 | // Consumer instructions must be white-listed |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 277 | typename BoolFolding<Traits>::BoolFoldingConsumerKind ConsumerKind = |
| 278 | getConsumerKind(&Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 279 | if (ConsumerKind == CK_None) { |
| 280 | setInvalid(VarNum); |
| 281 | continue; |
| 282 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 283 | typename BoolFolding<Traits>::BoolFoldingProducerKind ProducerKind = |
| 284 | getProducerKind(Producers[VarNum].Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 285 | if (!isValidFolding(ProducerKind, ConsumerKind)) { |
| 286 | setInvalid(VarNum); |
| 287 | continue; |
| 288 | } |
| 289 | // Avoid creating multiple copies of complex producer instructions. |
| 290 | if (Producers[VarNum].IsComplex && Producers[VarNum].NumUses > 0) { |
| 291 | setInvalid(VarNum); |
| 292 | continue; |
| 293 | } |
| 294 | ++Producers[VarNum].NumUses; |
| 295 | if (Instr.isLastUse(Var)) { |
| 296 | Producers[VarNum].IsLiveOut = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | for (auto &I : Producers) { |
| 301 | // Ignore entries previously marked invalid. |
| 302 | if (I.second.Instr == nullptr) |
| 303 | continue; |
| 304 | // Disable the producer if its dest may be live beyond this block. |
| 305 | if (I.second.IsLiveOut) { |
| 306 | setInvalid(I.first); |
| 307 | continue; |
| 308 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 309 | // Mark as "dead" rather than outright deleting. This is so that other |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 310 | // peephole style optimizations during or before lowering have access to |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 311 | // this instruction in undeleted form. See for example |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 312 | // tryOptimizedCmpxchgCmpBr(). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 313 | I.second.Instr->setDead(); |
| 314 | } |
| 315 | } |
| 316 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 317 | template <typename Traits> |
| 318 | const Inst *BoolFolding<Traits>::getProducerFor(const Operand *Opnd) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 319 | auto *Var = llvm::dyn_cast<const Variable>(Opnd); |
| 320 | if (Var == nullptr) |
| 321 | return nullptr; |
| 322 | SizeT VarNum = Var->getIndex(); |
| 323 | auto Element = Producers.find(VarNum); |
| 324 | if (Element == Producers.end()) |
| 325 | return nullptr; |
| 326 | return Element->second.Instr; |
| 327 | } |
| 328 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 329 | template <typename Traits> |
| 330 | void BoolFolding<Traits>::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 331 | if (!BuildDefs::dump() || !Func->isVerbose(IceV_Folding)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 332 | return; |
| 333 | OstreamLocker L(Func->getContext()); |
| 334 | Ostream &Str = Func->getContext()->getStrDump(); |
| 335 | for (auto &I : Producers) { |
| 336 | if (I.second.Instr == nullptr) |
| 337 | continue; |
| 338 | Str << "Found foldable producer:\n "; |
| 339 | I.second.Instr->dump(Func); |
| 340 | Str << "\n"; |
| 341 | } |
| 342 | } |
| 343 | |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 344 | /// If the given instruction has potential memory side effects (e.g. store, rmw, |
| 345 | /// or a call instruction with potential memory side effects), then we must not |
| 346 | /// allow a pre-store Producer instruction with memory operands to be folded |
| 347 | /// into a post-store Consumer instruction. If this is detected, the Producer |
| 348 | /// is invalidated. |
| 349 | /// |
| 350 | /// We use the Producer's IsLiveOut field to determine whether any potential |
| 351 | /// Consumers come after this store instruction. The IsLiveOut field is |
| 352 | /// initialized to true, and BoolFolding::init() sets IsLiveOut to false when it |
| 353 | /// sees the variable's definitive last use (indicating the variable is not in |
| 354 | /// the node's live-out set). Thus if we see here that IsLiveOut is false, we |
| 355 | /// know that there can be no consumers after the store, and therefore we know |
| 356 | /// the folding is safe despite the store instruction. |
| 357 | template <typename Traits> |
| 358 | void BoolFolding<Traits>::invalidateProducersOnStore(const Inst *Instr) { |
| 359 | if (!Instr->isMemoryWrite()) |
| 360 | return; |
| 361 | for (auto &ProducerPair : Producers) { |
| 362 | if (!ProducerPair.second.IsLiveOut) |
| 363 | continue; |
| 364 | Inst *PInst = ProducerPair.second.Instr; |
| 365 | if (PInst == nullptr) |
| 366 | continue; |
| 367 | bool HasMemOperand = false; |
| 368 | const SizeT SrcSize = PInst->getSrcSize(); |
| 369 | for (SizeT I = 0; I < SrcSize; ++I) { |
| 370 | if (llvm::isa<typename Traits::X86OperandMem>(PInst->getSrc(I))) { |
| 371 | HasMemOperand = true; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | if (!HasMemOperand) |
| 376 | continue; |
| 377 | setInvalid(ProducerPair.first); |
| 378 | } |
| 379 | } |
| 380 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 381 | template <typename TraitsType> |
| 382 | void TargetX86Base<TraitsType>::initNodeForLowering(CfgNode *Node) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 383 | FoldingInfo.init(Node); |
| 384 | FoldingInfo.dump(Func); |
| 385 | } |
| 386 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 387 | template <typename TraitsType> |
| 388 | TargetX86Base<TraitsType>::TargetX86Base(Cfg *Func) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 389 | : TargetLowering(Func), NeedSandboxing(SandboxingType == ST_NaCl) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 390 | static_assert( |
| 391 | (Traits::InstructionSet::End - Traits::InstructionSet::Begin) == |
| 392 | (TargetInstructionSet::X86InstructionSet_End - |
| 393 | TargetInstructionSet::X86InstructionSet_Begin), |
| 394 | "Traits::InstructionSet range different from TargetInstructionSet"); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 395 | if (getFlags().getTargetInstructionSet() != |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 396 | TargetInstructionSet::BaseInstructionSet) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 397 | InstructionSet = static_cast<InstructionSetEnum>( |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 398 | (getFlags().getTargetInstructionSet() - |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 399 | TargetInstructionSet::X86InstructionSet_Begin) + |
| 400 | Traits::InstructionSet::Begin); |
| 401 | } |
Jim Stichnoth | 94844f1 | 2015-11-04 16:06:16 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 404 | template <typename TraitsType> |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 405 | void TargetX86Base<TraitsType>::staticInit(GlobalContext *Ctx) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 406 | RegNumT::setLimit(Traits::RegisterSet::Reg_NUM); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 407 | Traits::initRegisterSet(getFlags(), &TypeToRegisterSet, &RegisterAliases); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 408 | for (size_t i = 0; i < TypeToRegisterSet.size(); ++i) |
| 409 | TypeToRegisterSetUnfiltered[i] = TypeToRegisterSet[i]; |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 410 | filterTypeToRegisterSet(Ctx, Traits::RegisterSet::Reg_NUM, |
| 411 | TypeToRegisterSet.data(), TypeToRegisterSet.size(), |
Jim Stichnoth | 2544d4d | 2016-01-22 13:07:46 -0800 | [diff] [blame] | 412 | Traits::getRegName, getRegClassName); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 413 | PcRelFixup = Traits::FK_PcRel; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 414 | AbsFixup = getFlags().getUseNonsfi() ? Traits::FK_Gotoff : Traits::FK_Abs; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 417 | template <typename TraitsType> |
| 418 | bool TargetX86Base<TraitsType>::shouldBePooled(const Constant *C) { |
| 419 | if (auto *ConstFloat = llvm::dyn_cast<ConstantFloat>(C)) { |
| 420 | return !Utils::isPositiveZero(ConstFloat->getValue()); |
| 421 | } |
| 422 | if (auto *ConstDouble = llvm::dyn_cast<ConstantDouble>(C)) { |
| 423 | return !Utils::isPositiveZero(ConstDouble->getValue()); |
| 424 | } |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 425 | if (getFlags().getRandomizeAndPoolImmediatesOption() != RPI_Pool) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | return C->shouldBeRandomizedOrPooled(); |
| 429 | } |
| 430 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 431 | template <typename TraitsType> void TargetX86Base<TraitsType>::translateO2() { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 432 | TimerMarker T(TimerStack::TT_O2, Func); |
| 433 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 434 | if (SandboxingType != ST_None) { |
| 435 | initRebasePtr(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 436 | } |
| 437 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 438 | genTargetHelperCalls(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 439 | Func->dump("After target helper call insertion"); |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 440 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 441 | // Merge Alloca instructions, and lay out the stack. |
| 442 | static constexpr bool SortAndCombineAllocas = true; |
| 443 | Func->processAllocas(SortAndCombineAllocas); |
| 444 | Func->dump("After Alloca processing"); |
| 445 | |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 446 | if (getFlags().getEnableExperimental()) { |
| 447 | Func->localCSE(); |
| 448 | Func->dump("After Local CSE"); |
| 449 | } |
| 450 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 451 | if (!getFlags().getEnablePhiEdgeSplit()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 452 | // Lower Phi instructions. |
| 453 | Func->placePhiLoads(); |
| 454 | if (Func->hasError()) |
| 455 | return; |
| 456 | Func->placePhiStores(); |
| 457 | if (Func->hasError()) |
| 458 | return; |
| 459 | Func->deletePhis(); |
| 460 | if (Func->hasError()) |
| 461 | return; |
| 462 | Func->dump("After Phi lowering"); |
| 463 | } |
| 464 | |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 465 | // Run this early so it can be used to focus optimizations on potentially hot |
| 466 | // code. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 467 | // TODO(stichnot,ascull): currently only used for regalloc not |
| 468 | // expensive high level optimizations which could be focused on potentially |
| 469 | // hot code. |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 470 | Func->computeLoopNestDepth(); |
| 471 | Func->dump("After loop nest depth analysis"); |
| 472 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 473 | // Address mode optimization. |
| 474 | Func->getVMetadata()->init(VMK_SingleDefs); |
| 475 | Func->doAddressOpt(); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 476 | Func->materializeVectorShuffles(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 477 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 478 | // Find read-modify-write opportunities. Do this after address mode |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 479 | // optimization so that doAddressOpt() doesn't need to be applied to RMW |
| 480 | // instructions as well. |
| 481 | findRMW(); |
| 482 | Func->dump("After RMW transform"); |
| 483 | |
| 484 | // Argument lowering |
| 485 | Func->doArgLowering(); |
| 486 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 487 | // Target lowering. This requires liveness analysis for some parts of the |
| 488 | // lowering decisions, such as compare/branch fusing. If non-lightweight |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 489 | // liveness analysis is used, the instructions need to be renumbered first |
| 490 | // TODO: This renumbering should only be necessary if we're actually |
| 491 | // calculating live intervals, which we only do for register allocation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 492 | Func->renumberInstructions(); |
| 493 | if (Func->hasError()) |
| 494 | return; |
| 495 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 496 | // TODO: It should be sufficient to use the fastest liveness calculation, |
| 497 | // i.e. livenessLightweight(). However, for some reason that slows down the |
| 498 | // rest of the translation. Investigate. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 499 | Func->liveness(Liveness_Basic); |
| 500 | if (Func->hasError()) |
| 501 | return; |
| 502 | Func->dump("After x86 address mode opt"); |
| 503 | |
| 504 | // Disable constant blinding or pooling for load optimization. |
| 505 | { |
| 506 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
| 507 | doLoadOpt(); |
| 508 | } |
| 509 | Func->genCode(); |
| 510 | if (Func->hasError()) |
| 511 | return; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 512 | if (SandboxingType != ST_None) { |
| 513 | initSandbox(); |
| 514 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 515 | Func->dump("After x86 codegen"); |
| 516 | |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 517 | // Register allocation. This requires instruction renumbering and full |
| 518 | // liveness analysis. Loops must be identified before liveness so variable |
| 519 | // use weights are correct. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 520 | Func->renumberInstructions(); |
| 521 | if (Func->hasError()) |
| 522 | return; |
| 523 | Func->liveness(Liveness_Intervals); |
| 524 | if (Func->hasError()) |
| 525 | return; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 526 | // The post-codegen dump is done here, after liveness analysis and associated |
| 527 | // cleanup, to make the dump cleaner and more useful. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 528 | Func->dump("After initial x8632 codegen"); |
Jim Stichnoth | 2943d77 | 2016-06-21 11:22:17 -0700 | [diff] [blame^] | 529 | // Validate the live range computations. The expensive validation call is |
| 530 | // deliberately only made when assertions are enabled. |
| 531 | assert(Func->validateLiveness()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 532 | Func->getVMetadata()->init(VMK_All); |
| 533 | regAlloc(RAK_Global); |
| 534 | if (Func->hasError()) |
| 535 | return; |
| 536 | Func->dump("After linear scan regalloc"); |
| 537 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 538 | if (getFlags().getEnablePhiEdgeSplit()) { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 539 | Func->advancedPhiLowering(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 540 | Func->dump("After advanced Phi lowering"); |
| 541 | } |
| 542 | |
| 543 | // Stack frame mapping. |
| 544 | Func->genFrame(); |
| 545 | if (Func->hasError()) |
| 546 | return; |
| 547 | Func->dump("After stack frame mapping"); |
| 548 | |
| 549 | Func->contractEmptyNodes(); |
| 550 | Func->reorderNodes(); |
| 551 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 552 | // Shuffle basic block order if -reorder-basic-blocks is enabled. |
| 553 | Func->shuffleNodes(); |
| 554 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 555 | // Branch optimization. This needs to be done just before code emission. In |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 556 | // particular, no transformations that insert or reorder CfgNodes should be |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 557 | // done after branch optimization. We go ahead and do it before nop insertion |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 558 | // to reduce the amount of work needed for searching for opportunities. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 559 | Func->doBranchOpt(); |
| 560 | Func->dump("After branch optimization"); |
| 561 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 562 | // Nop insertion if -nop-insertion is enabled. |
| 563 | Func->doNopInsertion(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 564 | |
| 565 | // Mark nodes that require sandbox alignment |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 566 | if (NeedSandboxing) { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 567 | Func->markNodesForSandboxing(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 568 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 569 | } |
| 570 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 571 | template <typename TraitsType> void TargetX86Base<TraitsType>::translateOm1() { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 572 | TimerMarker T(TimerStack::TT_Om1, Func); |
| 573 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 574 | if (SandboxingType != ST_None) { |
| 575 | initRebasePtr(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 576 | } |
| 577 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 578 | genTargetHelperCalls(); |
| 579 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 580 | // Do not merge Alloca instructions, and lay out the stack. |
| 581 | static constexpr bool SortAndCombineAllocas = false; |
| 582 | Func->processAllocas(SortAndCombineAllocas); |
| 583 | Func->dump("After Alloca processing"); |
| 584 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 585 | Func->placePhiLoads(); |
| 586 | if (Func->hasError()) |
| 587 | return; |
| 588 | Func->placePhiStores(); |
| 589 | if (Func->hasError()) |
| 590 | return; |
| 591 | Func->deletePhis(); |
| 592 | if (Func->hasError()) |
| 593 | return; |
| 594 | Func->dump("After Phi lowering"); |
| 595 | |
| 596 | Func->doArgLowering(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 597 | Func->genCode(); |
| 598 | if (Func->hasError()) |
| 599 | return; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 600 | if (SandboxingType != ST_None) { |
| 601 | initSandbox(); |
| 602 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 603 | Func->dump("After initial x8632 codegen"); |
| 604 | |
| 605 | regAlloc(RAK_InfOnly); |
| 606 | if (Func->hasError()) |
| 607 | return; |
| 608 | Func->dump("After regalloc of infinite-weight variables"); |
| 609 | |
| 610 | Func->genFrame(); |
| 611 | if (Func->hasError()) |
| 612 | return; |
| 613 | Func->dump("After stack frame mapping"); |
| 614 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 615 | // Shuffle basic block order if -reorder-basic-blocks is enabled. |
| 616 | Func->shuffleNodes(); |
| 617 | |
| 618 | // Nop insertion if -nop-insertion is enabled. |
| 619 | Func->doNopInsertion(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 620 | |
| 621 | // Mark nodes that require sandbox alignment |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 622 | if (NeedSandboxing) |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 623 | Func->markNodesForSandboxing(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 624 | } |
| 625 | |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 626 | inline bool canRMW(const InstArithmetic *Arith) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 627 | Type Ty = Arith->getDest()->getType(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 628 | // X86 vector instructions write to a register and have no RMW option. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 629 | if (isVectorType(Ty)) |
| 630 | return false; |
| 631 | bool isI64 = Ty == IceType_i64; |
| 632 | |
| 633 | switch (Arith->getOp()) { |
| 634 | // Not handled for lack of simple lowering: |
| 635 | // shift on i64 |
| 636 | // mul, udiv, urem, sdiv, srem, frem |
| 637 | // Not handled for lack of RMW instructions: |
| 638 | // fadd, fsub, fmul, fdiv (also vector types) |
| 639 | default: |
| 640 | return false; |
| 641 | case InstArithmetic::Add: |
| 642 | case InstArithmetic::Sub: |
| 643 | case InstArithmetic::And: |
| 644 | case InstArithmetic::Or: |
| 645 | case InstArithmetic::Xor: |
| 646 | return true; |
| 647 | case InstArithmetic::Shl: |
| 648 | case InstArithmetic::Lshr: |
| 649 | case InstArithmetic::Ashr: |
| 650 | return false; // TODO(stichnot): implement |
| 651 | return !isI64; |
| 652 | } |
| 653 | } |
| 654 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 655 | template <typename TraitsType> |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 656 | bool isSameMemAddressOperand(const Operand *A, const Operand *B) { |
| 657 | if (A == B) |
| 658 | return true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 659 | if (auto *MemA = |
| 660 | llvm::dyn_cast<typename TargetX86Base<TraitsType>::X86OperandMem>( |
| 661 | A)) { |
| 662 | if (auto *MemB = |
| 663 | llvm::dyn_cast<typename TargetX86Base<TraitsType>::X86OperandMem>( |
| 664 | B)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 665 | return MemA->getBase() == MemB->getBase() && |
| 666 | MemA->getOffset() == MemB->getOffset() && |
| 667 | MemA->getIndex() == MemB->getIndex() && |
| 668 | MemA->getShift() == MemB->getShift() && |
| 669 | MemA->getSegmentRegister() == MemB->getSegmentRegister(); |
| 670 | } |
| 671 | } |
| 672 | return false; |
| 673 | } |
| 674 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 675 | template <typename TraitsType> void TargetX86Base<TraitsType>::findRMW() { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 676 | TimerMarker _(TimerStack::TT_findRMW, Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 677 | Func->dump("Before RMW"); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 678 | if (Func->isVerbose(IceV_RMW)) |
| 679 | Func->getContext()->lockStr(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 680 | for (CfgNode *Node : Func->getNodes()) { |
| 681 | // Walk through the instructions, considering each sequence of 3 |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 682 | // instructions, and look for the particular RMW pattern. Note that this |
| 683 | // search can be "broken" (false negatives) if there are intervening |
| 684 | // deleted instructions, or intervening instructions that could be safely |
| 685 | // moved out of the way to reveal an RMW pattern. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 686 | auto E = Node->getInsts().end(); |
| 687 | auto I1 = E, I2 = E, I3 = Node->getInsts().begin(); |
| 688 | for (; I3 != E; I1 = I2, I2 = I3, ++I3) { |
| 689 | // Make I3 skip over deleted instructions. |
| 690 | while (I3 != E && I3->isDeleted()) |
| 691 | ++I3; |
| 692 | if (I1 == E || I2 == E || I3 == E) |
| 693 | continue; |
| 694 | assert(!I1->isDeleted()); |
| 695 | assert(!I2->isDeleted()); |
| 696 | assert(!I3->isDeleted()); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 697 | auto *Load = llvm::dyn_cast<InstLoad>(I1); |
| 698 | auto *Arith = llvm::dyn_cast<InstArithmetic>(I2); |
| 699 | auto *Store = llvm::dyn_cast<InstStore>(I3); |
| 700 | if (!Load || !Arith || !Store) |
| 701 | continue; |
| 702 | // Look for: |
| 703 | // a = Load addr |
| 704 | // b = <op> a, other |
| 705 | // Store b, addr |
| 706 | // Change to: |
| 707 | // a = Load addr |
| 708 | // b = <op> a, other |
| 709 | // x = FakeDef |
| 710 | // RMW <op>, addr, other, x |
| 711 | // b = Store b, addr, x |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 712 | // Note that inferTwoAddress() makes sure setDestRedefined() gets called |
| 713 | // on the updated Store instruction, to avoid liveness problems later. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 714 | // |
| 715 | // With this transformation, the Store instruction acquires a Dest |
| 716 | // variable and is now subject to dead code elimination if there are no |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 717 | // more uses of "b". Variable "x" is a beacon for determining whether the |
| 718 | // Store instruction gets dead-code eliminated. If the Store instruction |
| 719 | // is eliminated, then it must be the case that the RMW instruction ends |
| 720 | // x's live range, and therefore the RMW instruction will be retained and |
| 721 | // later lowered. On the other hand, if the RMW instruction does not end |
| 722 | // x's live range, then the Store instruction must still be present, and |
| 723 | // therefore the RMW instruction is ignored during lowering because it is |
| 724 | // redundant with the Store instruction. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 725 | // |
| 726 | // Note that if "a" has further uses, the RMW transformation may still |
| 727 | // trigger, resulting in two loads and one store, which is worse than the |
| 728 | // original one load and one store. However, this is probably rare, and |
| 729 | // caching probably keeps it just as fast. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 730 | if (!isSameMemAddressOperand<TraitsType>(Load->getSourceAddress(), |
| 731 | Store->getAddr())) |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 732 | continue; |
| 733 | Operand *ArithSrcFromLoad = Arith->getSrc(0); |
| 734 | Operand *ArithSrcOther = Arith->getSrc(1); |
| 735 | if (ArithSrcFromLoad != Load->getDest()) { |
| 736 | if (!Arith->isCommutative() || ArithSrcOther != Load->getDest()) |
| 737 | continue; |
| 738 | std::swap(ArithSrcFromLoad, ArithSrcOther); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 739 | } |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 740 | if (Arith->getDest() != Store->getData()) |
| 741 | continue; |
| 742 | if (!canRMW(Arith)) |
| 743 | continue; |
| 744 | if (Func->isVerbose(IceV_RMW)) { |
| 745 | Ostream &Str = Func->getContext()->getStrDump(); |
| 746 | Str << "Found RMW in " << Func->getFunctionName() << ":\n "; |
| 747 | Load->dump(Func); |
| 748 | Str << "\n "; |
| 749 | Arith->dump(Func); |
| 750 | Str << "\n "; |
| 751 | Store->dump(Func); |
| 752 | Str << "\n"; |
| 753 | } |
| 754 | Variable *Beacon = Func->makeVariable(IceType_i32); |
| 755 | Beacon->setMustNotHaveReg(); |
| 756 | Store->setRmwBeacon(Beacon); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 757 | auto *BeaconDef = InstFakeDef::create(Func, Beacon); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 758 | Node->getInsts().insert(I3, BeaconDef); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 759 | auto *RMW = InstX86FakeRMW::create(Func, ArithSrcOther, Store->getAddr(), |
| 760 | Beacon, Arith->getOp()); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 761 | Node->getInsts().insert(I3, RMW); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 764 | if (Func->isVerbose(IceV_RMW)) |
| 765 | Func->getContext()->unlockStr(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // Converts a ConstantInteger32 operand into its constant value, or |
| 769 | // MemoryOrderInvalid if the operand is not a ConstantInteger32. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 770 | inline uint64_t getConstantMemoryOrder(Operand *Opnd) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 771 | if (auto *Integer = llvm::dyn_cast<ConstantInteger32>(Opnd)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 772 | return Integer->getValue(); |
| 773 | return Intrinsics::MemoryOrderInvalid; |
| 774 | } |
| 775 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 776 | /// Determines whether the dest of a Load instruction can be folded into one of |
| 777 | /// the src operands of a 2-operand instruction. This is true as long as the |
| 778 | /// load dest matches exactly one of the binary instruction's src operands. |
| 779 | /// Replaces Src0 or Src1 with LoadSrc if the answer is true. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 780 | inline bool canFoldLoadIntoBinaryInst(Operand *LoadSrc, Variable *LoadDest, |
| 781 | Operand *&Src0, Operand *&Src1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 782 | if (Src0 == LoadDest && Src1 != LoadDest) { |
| 783 | Src0 = LoadSrc; |
| 784 | return true; |
| 785 | } |
| 786 | if (Src0 != LoadDest && Src1 == LoadDest) { |
| 787 | Src1 = LoadSrc; |
| 788 | return true; |
| 789 | } |
| 790 | return false; |
| 791 | } |
| 792 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 793 | template <typename TraitsType> void TargetX86Base<TraitsType>::doLoadOpt() { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 794 | TimerMarker _(TimerStack::TT_loadOpt, Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 795 | for (CfgNode *Node : Func->getNodes()) { |
| 796 | Context.init(Node); |
| 797 | while (!Context.atEnd()) { |
| 798 | Variable *LoadDest = nullptr; |
| 799 | Operand *LoadSrc = nullptr; |
| 800 | Inst *CurInst = Context.getCur(); |
| 801 | Inst *Next = Context.getNextInst(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 802 | // Determine whether the current instruction is a Load instruction or |
| 803 | // equivalent. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 804 | if (auto *Load = llvm::dyn_cast<InstLoad>(CurInst)) { |
| 805 | // An InstLoad always qualifies. |
| 806 | LoadDest = Load->getDest(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 807 | constexpr bool DoLegalize = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 808 | LoadSrc = formMemoryOperand(Load->getSourceAddress(), |
| 809 | LoadDest->getType(), DoLegalize); |
| 810 | } else if (auto *Intrin = llvm::dyn_cast<InstIntrinsicCall>(CurInst)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 811 | // An AtomicLoad intrinsic qualifies as long as it has a valid memory |
| 812 | // ordering, and can be implemented in a single instruction (i.e., not |
| 813 | // i64 on x86-32). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 814 | Intrinsics::IntrinsicID ID = Intrin->getIntrinsicInfo().ID; |
| 815 | if (ID == Intrinsics::AtomicLoad && |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 816 | (Traits::Is64Bit || Intrin->getDest()->getType() != IceType_i64) && |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 817 | Intrinsics::isMemoryOrderValid( |
| 818 | ID, getConstantMemoryOrder(Intrin->getArg(1)))) { |
| 819 | LoadDest = Intrin->getDest(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 820 | constexpr bool DoLegalize = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 821 | LoadSrc = formMemoryOperand(Intrin->getArg(0), LoadDest->getType(), |
| 822 | DoLegalize); |
| 823 | } |
| 824 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 825 | // A Load instruction can be folded into the following instruction only |
| 826 | // if the following instruction ends the Load's Dest variable's live |
| 827 | // range. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 828 | if (LoadDest && Next && Next->isLastUse(LoadDest)) { |
| 829 | assert(LoadSrc); |
| 830 | Inst *NewInst = nullptr; |
| 831 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Next)) { |
| 832 | Operand *Src0 = Arith->getSrc(0); |
| 833 | Operand *Src1 = Arith->getSrc(1); |
| 834 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 835 | NewInst = InstArithmetic::create(Func, Arith->getOp(), |
| 836 | Arith->getDest(), Src0, Src1); |
| 837 | } |
| 838 | } else if (auto *Icmp = llvm::dyn_cast<InstIcmp>(Next)) { |
| 839 | Operand *Src0 = Icmp->getSrc(0); |
| 840 | Operand *Src1 = Icmp->getSrc(1); |
| 841 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 842 | NewInst = InstIcmp::create(Func, Icmp->getCondition(), |
| 843 | Icmp->getDest(), Src0, Src1); |
| 844 | } |
| 845 | } else if (auto *Fcmp = llvm::dyn_cast<InstFcmp>(Next)) { |
| 846 | Operand *Src0 = Fcmp->getSrc(0); |
| 847 | Operand *Src1 = Fcmp->getSrc(1); |
| 848 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 849 | NewInst = InstFcmp::create(Func, Fcmp->getCondition(), |
| 850 | Fcmp->getDest(), Src0, Src1); |
| 851 | } |
| 852 | } else if (auto *Select = llvm::dyn_cast<InstSelect>(Next)) { |
| 853 | Operand *Src0 = Select->getTrueOperand(); |
| 854 | Operand *Src1 = Select->getFalseOperand(); |
| 855 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 856 | NewInst = InstSelect::create(Func, Select->getDest(), |
| 857 | Select->getCondition(), Src0, Src1); |
| 858 | } |
| 859 | } else if (auto *Cast = llvm::dyn_cast<InstCast>(Next)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 860 | // The load dest can always be folded into a Cast instruction. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 861 | auto *Src0 = llvm::dyn_cast<Variable>(Cast->getSrc(0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 862 | if (Src0 == LoadDest) { |
| 863 | NewInst = InstCast::create(Func, Cast->getCastKind(), |
| 864 | Cast->getDest(), LoadSrc); |
| 865 | } |
| 866 | } |
| 867 | if (NewInst) { |
| 868 | CurInst->setDeleted(); |
| 869 | Next->setDeleted(); |
| 870 | Context.insert(NewInst); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 871 | // Update NewInst->LiveRangesEnded so that target lowering may |
| 872 | // benefit. Also update NewInst->HasSideEffects. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 873 | NewInst->spliceLivenessInfo(Next, CurInst); |
| 874 | } |
| 875 | } |
| 876 | Context.advanceCur(); |
| 877 | Context.advanceNext(); |
| 878 | } |
| 879 | } |
| 880 | Func->dump("After load optimization"); |
| 881 | } |
| 882 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 883 | template <typename TraitsType> |
| 884 | bool TargetX86Base<TraitsType>::doBranchOpt(Inst *I, const CfgNode *NextNode) { |
| 885 | if (auto *Br = llvm::dyn_cast<InstX86Br>(I)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 886 | return Br->optimizeBranch(NextNode); |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 891 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 892 | Variable *TargetX86Base<TraitsType>::getPhysicalRegister(RegNumT RegNum, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 893 | Type Ty) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 894 | if (Ty == IceType_void) |
| 895 | Ty = IceType_i32; |
| 896 | if (PhysicalRegisters[Ty].empty()) |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 897 | PhysicalRegisters[Ty].resize(Traits::RegisterSet::Reg_NUM); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 898 | assert(unsigned(RegNum) < PhysicalRegisters[Ty].size()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 899 | Variable *Reg = PhysicalRegisters[Ty][RegNum]; |
| 900 | if (Reg == nullptr) { |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 901 | Reg = Func->makeVariable(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 902 | Reg->setRegNum(RegNum); |
| 903 | PhysicalRegisters[Ty][RegNum] = Reg; |
Jim Stichnoth | 6966055 | 2015-09-18 06:41:02 -0700 | [diff] [blame] | 904 | // Specially mark a named physical register as an "argument" so that it is |
| 905 | // considered live upon function entry. Otherwise it's possible to get |
| 906 | // liveness validation errors for saving callee-save registers. |
| 907 | Func->addImplicitArg(Reg); |
| 908 | // Don't bother tracking the live range of a named physical register. |
| 909 | Reg->setIgnoreLiveness(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 910 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 911 | assert(Traits::getGprForType(Ty, RegNum) == RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 912 | return Reg; |
| 913 | } |
| 914 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 915 | template <typename TraitsType> |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 916 | const char *TargetX86Base<TraitsType>::getRegName(RegNumT RegNum, |
| 917 | Type Ty) const { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 918 | return Traits::getRegName(Traits::getGprForType(Ty, RegNum)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 919 | } |
| 920 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 921 | template <typename TraitsType> |
| 922 | void TargetX86Base<TraitsType>::emitVariable(const Variable *Var) const { |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 923 | if (!BuildDefs::dump()) |
| 924 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 925 | Ostream &Str = Ctx->getStrEmit(); |
| 926 | if (Var->hasReg()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 927 | const bool Is64BitSandboxing = Traits::Is64Bit && NeedSandboxing; |
| 928 | const Type VarType = (Var->isRematerializable() && Is64BitSandboxing) |
| 929 | ? IceType_i64 |
| 930 | : Var->getType(); |
| 931 | Str << "%" << getRegName(Var->getRegNum(), VarType); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 932 | return; |
| 933 | } |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 934 | if (Var->mustHaveReg()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 935 | llvm::report_fatal_error("Infinite-weight Variable (" + Var->getName() + |
Jim Stichnoth | 45bec54 | 2016-02-05 10:26:09 -0800 | [diff] [blame] | 936 | ") has no register assigned - function " + |
| 937 | Func->getFunctionName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 938 | } |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 939 | const int32_t Offset = Var->getStackOffset(); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 940 | auto BaseRegNum = Var->getBaseRegNum(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 941 | if (BaseRegNum.hasNoValue()) |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 942 | BaseRegNum = getFrameOrStackReg(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 943 | // Print in the form "Offset(%reg)", taking care that: |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 944 | // - Offset is never printed when it is 0 |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 945 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 946 | const bool DecorateAsm = getFlags().getDecorateAsm(); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 947 | // Only print Offset when it is nonzero, regardless of DecorateAsm. |
| 948 | if (Offset) { |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 949 | if (DecorateAsm) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 950 | Str << Var->getSymbolicStackOffset(); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 951 | } else { |
| 952 | Str << Offset; |
| 953 | } |
| 954 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 955 | const Type FrameSPTy = Traits::WordType; |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 956 | Str << "(%" << getRegName(BaseRegNum, FrameSPTy) << ")"; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 957 | } |
| 958 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 959 | template <typename TraitsType> |
| 960 | typename TargetX86Base<TraitsType>::X86Address |
| 961 | TargetX86Base<TraitsType>::stackVarToAsmOperand(const Variable *Var) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 962 | if (Var->hasReg()) |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 963 | llvm::report_fatal_error("Stack Variable has a register assigned"); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 964 | if (Var->mustHaveReg()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 965 | llvm::report_fatal_error("Infinite-weight Variable (" + Var->getName() + |
Jim Stichnoth | 45bec54 | 2016-02-05 10:26:09 -0800 | [diff] [blame] | 966 | ") has no register assigned - function " + |
| 967 | Func->getFunctionName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 968 | } |
| 969 | int32_t Offset = Var->getStackOffset(); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 970 | auto BaseRegNum = Var->getBaseRegNum(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 971 | if (Var->getBaseRegNum().hasNoValue()) |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 972 | BaseRegNum = getFrameOrStackReg(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 973 | return X86Address(Traits::getEncodedGPR(BaseRegNum), Offset, |
| 974 | AssemblerFixup::NoFixup); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 975 | } |
| 976 | |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 977 | template <typename TraitsType> |
| 978 | void TargetX86Base<TraitsType>::addProlog(CfgNode *Node) { |
| 979 | // Stack frame layout: |
| 980 | // |
| 981 | // +------------------------+ |
| 982 | // | 1. return address | |
| 983 | // +------------------------+ |
| 984 | // | 2. preserved registers | |
| 985 | // +------------------------+ |
| 986 | // | 3. padding | |
| 987 | // +------------------------+ |
| 988 | // | 4. global spill area | |
| 989 | // +------------------------+ |
| 990 | // | 5. padding | |
| 991 | // +------------------------+ |
| 992 | // | 6. local spill area | |
| 993 | // +------------------------+ |
| 994 | // | 7. padding | |
| 995 | // +------------------------+ |
| 996 | // | 8. allocas | |
| 997 | // +------------------------+ |
| 998 | // | 9. padding | |
| 999 | // +------------------------+ |
| 1000 | // | 10. out args | |
| 1001 | // +------------------------+ <--- StackPointer |
| 1002 | // |
| 1003 | // The following variables record the size in bytes of the given areas: |
| 1004 | // * X86_RET_IP_SIZE_BYTES: area 1 |
| 1005 | // * PreservedRegsSizeBytes: area 2 |
| 1006 | // * SpillAreaPaddingBytes: area 3 |
| 1007 | // * GlobalsSize: area 4 |
| 1008 | // * GlobalsAndSubsequentPaddingSize: areas 4 - 5 |
| 1009 | // * LocalsSpillAreaSize: area 6 |
| 1010 | // * SpillAreaSizeBytes: areas 3 - 10 |
| 1011 | // * maxOutArgsSizeBytes(): area 10 |
| 1012 | |
| 1013 | // Determine stack frame offsets for each Variable without a register |
| 1014 | // assignment. This can be done as one variable per stack slot. Or, do |
| 1015 | // coalescing by running the register allocator again with an infinite set of |
| 1016 | // registers (as a side effect, this gives variables a second chance at |
| 1017 | // physical register assignment). |
| 1018 | // |
| 1019 | // A middle ground approach is to leverage sparsity and allocate one block of |
| 1020 | // space on the frame for globals (variables with multi-block lifetime), and |
| 1021 | // one block to share for locals (single-block lifetime). |
| 1022 | |
| 1023 | Context.init(Node); |
| 1024 | Context.setInsertPoint(Context.getCur()); |
| 1025 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1026 | SmallBitVector CalleeSaves = getRegisterSet(RegSet_CalleeSave, RegSet_None); |
| 1027 | RegsUsed = SmallBitVector(CalleeSaves.size()); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1028 | VarList SortedSpilledVariables, VariablesLinkedToSpillSlots; |
| 1029 | size_t GlobalsSize = 0; |
| 1030 | // If there is a separate locals area, this represents that area. Otherwise |
| 1031 | // it counts any variable not counted by GlobalsSize. |
| 1032 | SpillAreaSizeBytes = 0; |
| 1033 | // If there is a separate locals area, this specifies the alignment for it. |
| 1034 | uint32_t LocalsSlotsAlignmentBytes = 0; |
| 1035 | // The entire spill locations area gets aligned to largest natural alignment |
| 1036 | // of the variables that have a spill slot. |
| 1037 | uint32_t SpillAreaAlignmentBytes = 0; |
| 1038 | // A spill slot linked to a variable with a stack slot should reuse that |
| 1039 | // stack slot. |
| 1040 | std::function<bool(Variable *)> TargetVarHook = |
| 1041 | [&VariablesLinkedToSpillSlots](Variable *Var) { |
| 1042 | if (auto *SpillVar = |
| 1043 | llvm::dyn_cast<typename Traits::SpillVariable>(Var)) { |
| 1044 | assert(Var->mustNotHaveReg()); |
| 1045 | if (SpillVar->getLinkedTo() && !SpillVar->getLinkedTo()->hasReg()) { |
| 1046 | VariablesLinkedToSpillSlots.push_back(Var); |
| 1047 | return true; |
| 1048 | } |
| 1049 | } |
| 1050 | return false; |
| 1051 | }; |
| 1052 | |
| 1053 | // Compute the list of spilled variables and bounds for GlobalsSize, etc. |
| 1054 | getVarStackSlotParams(SortedSpilledVariables, RegsUsed, &GlobalsSize, |
| 1055 | &SpillAreaSizeBytes, &SpillAreaAlignmentBytes, |
| 1056 | &LocalsSlotsAlignmentBytes, TargetVarHook); |
| 1057 | uint32_t LocalsSpillAreaSize = SpillAreaSizeBytes; |
| 1058 | SpillAreaSizeBytes += GlobalsSize; |
| 1059 | |
| 1060 | // Add push instructions for preserved registers. |
| 1061 | uint32_t NumCallee = 0; |
| 1062 | size_t PreservedRegsSizeBytes = 0; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1063 | SmallBitVector Pushed(CalleeSaves.size()); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1064 | for (RegNumT i : RegNumBVIter(CalleeSaves)) { |
| 1065 | const auto Canonical = Traits::getBaseReg(i); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1066 | assert(Canonical == Traits::getBaseReg(Canonical)); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1067 | if (RegsUsed[i]) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1068 | Pushed[Canonical] = true; |
| 1069 | } |
| 1070 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1071 | for (RegNumT RegNum : RegNumBVIter(Pushed)) { |
| 1072 | assert(RegNum == Traits::getBaseReg(RegNum)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1073 | ++NumCallee; |
| 1074 | PreservedRegsSizeBytes += typeWidthInBytes(Traits::WordType); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1075 | _push_reg(getPhysicalRegister(RegNum, Traits::WordType)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1076 | } |
| 1077 | Ctx->statsUpdateRegistersSaved(NumCallee); |
| 1078 | |
| 1079 | // Generate "push frameptr; mov frameptr, stackptr" |
| 1080 | if (IsEbpBasedFrame) { |
| 1081 | assert((RegsUsed & getRegisterSet(RegSet_FramePointer, RegSet_None)) |
| 1082 | .count() == 0); |
| 1083 | PreservedRegsSizeBytes += typeWidthInBytes(Traits::WordType); |
| 1084 | _link_bp(); |
| 1085 | } |
| 1086 | |
| 1087 | // Align the variables area. SpillAreaPaddingBytes is the size of the region |
| 1088 | // after the preserved registers and before the spill areas. |
| 1089 | // LocalsSlotsPaddingBytes is the amount of padding between the globals and |
| 1090 | // locals area if they are separate. |
| 1091 | assert(SpillAreaAlignmentBytes <= Traits::X86_STACK_ALIGNMENT_BYTES); |
| 1092 | assert(LocalsSlotsAlignmentBytes <= SpillAreaAlignmentBytes); |
| 1093 | uint32_t SpillAreaPaddingBytes = 0; |
| 1094 | uint32_t LocalsSlotsPaddingBytes = 0; |
| 1095 | alignStackSpillAreas(Traits::X86_RET_IP_SIZE_BYTES + PreservedRegsSizeBytes, |
| 1096 | SpillAreaAlignmentBytes, GlobalsSize, |
| 1097 | LocalsSlotsAlignmentBytes, &SpillAreaPaddingBytes, |
| 1098 | &LocalsSlotsPaddingBytes); |
| 1099 | SpillAreaSizeBytes += SpillAreaPaddingBytes + LocalsSlotsPaddingBytes; |
| 1100 | uint32_t GlobalsAndSubsequentPaddingSize = |
| 1101 | GlobalsSize + LocalsSlotsPaddingBytes; |
| 1102 | |
| 1103 | // Functions returning scalar floating point types may need to convert values |
| 1104 | // from an in-register xmm value to the top of the x87 floating point stack. |
| 1105 | // This is done by a movp[sd] and an fld[sd]. Ensure there is enough scratch |
| 1106 | // space on the stack for this. |
| 1107 | const Type ReturnType = Func->getReturnType(); |
| 1108 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 1109 | if (isScalarFloatingType(ReturnType)) { |
| 1110 | // Avoid misaligned double-precicion load/store. |
| 1111 | NeedsStackAlignment = true; |
| 1112 | SpillAreaSizeBytes = |
| 1113 | std::max(typeWidthInBytesOnStack(ReturnType), SpillAreaSizeBytes); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | // Align esp if necessary. |
| 1118 | if (NeedsStackAlignment) { |
| 1119 | uint32_t StackOffset = |
| 1120 | Traits::X86_RET_IP_SIZE_BYTES + PreservedRegsSizeBytes; |
| 1121 | uint32_t StackSize = |
| 1122 | Traits::applyStackAlignment(StackOffset + SpillAreaSizeBytes); |
| 1123 | StackSize = Traits::applyStackAlignment(StackSize + maxOutArgsSizeBytes()); |
| 1124 | SpillAreaSizeBytes = StackSize - StackOffset; |
| 1125 | } else { |
| 1126 | SpillAreaSizeBytes += maxOutArgsSizeBytes(); |
| 1127 | } |
| 1128 | |
| 1129 | // Combine fixed allocations into SpillAreaSizeBytes if we are emitting the |
| 1130 | // fixed allocations in the prolog. |
| 1131 | if (PrologEmitsFixedAllocas) |
| 1132 | SpillAreaSizeBytes += FixedAllocaSizeBytes; |
| 1133 | if (SpillAreaSizeBytes) { |
| 1134 | // Generate "sub stackptr, SpillAreaSizeBytes" |
| 1135 | _sub_sp(Ctx->getConstantInt32(SpillAreaSizeBytes)); |
| 1136 | // If the fixed allocas are aligned more than the stack frame, align the |
| 1137 | // stack pointer accordingly. |
| 1138 | if (PrologEmitsFixedAllocas && |
| 1139 | FixedAllocaAlignBytes > Traits::X86_STACK_ALIGNMENT_BYTES) { |
| 1140 | assert(IsEbpBasedFrame); |
| 1141 | _and(getPhysicalRegister(getStackReg(), Traits::WordType), |
| 1142 | Ctx->getConstantInt32(-FixedAllocaAlignBytes)); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | // Account for known-frame-offset alloca instructions that were not already |
| 1147 | // combined into the prolog. |
| 1148 | if (!PrologEmitsFixedAllocas) |
| 1149 | SpillAreaSizeBytes += FixedAllocaSizeBytes; |
| 1150 | |
| 1151 | Ctx->statsUpdateFrameBytes(SpillAreaSizeBytes); |
| 1152 | |
| 1153 | // Fill in stack offsets for stack args, and copy args into registers for |
| 1154 | // those that were register-allocated. Args are pushed right to left, so |
| 1155 | // Arg[0] is closest to the stack/frame pointer. |
| 1156 | Variable *FramePtr = |
| 1157 | getPhysicalRegister(getFrameOrStackReg(), Traits::WordType); |
| 1158 | size_t BasicFrameOffset = |
| 1159 | PreservedRegsSizeBytes + Traits::X86_RET_IP_SIZE_BYTES; |
| 1160 | if (!IsEbpBasedFrame) |
| 1161 | BasicFrameOffset += SpillAreaSizeBytes; |
| 1162 | |
| 1163 | emitGetIP(Node); |
| 1164 | |
| 1165 | const VarList &Args = Func->getArgs(); |
| 1166 | size_t InArgsSizeBytes = 0; |
| 1167 | unsigned NumXmmArgs = 0; |
| 1168 | unsigned NumGPRArgs = 0; |
| 1169 | for (Variable *Arg : Args) { |
| 1170 | // Skip arguments passed in registers. |
| 1171 | if (isVectorType(Arg->getType())) { |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1172 | if (Traits::getRegisterForXmmArgNum(NumXmmArgs).hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1173 | ++NumXmmArgs; |
| 1174 | continue; |
| 1175 | } |
| 1176 | } else if (isScalarFloatingType(Arg->getType())) { |
| 1177 | if (Traits::X86_PASS_SCALAR_FP_IN_XMM && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1178 | Traits::getRegisterForXmmArgNum(NumXmmArgs).hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1179 | ++NumXmmArgs; |
| 1180 | continue; |
| 1181 | } |
| 1182 | } else { |
| 1183 | assert(isScalarIntegerType(Arg->getType())); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1184 | if (Traits::getRegisterForGprArgNum(Traits::WordType, NumGPRArgs) |
| 1185 | .hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1186 | ++NumGPRArgs; |
| 1187 | continue; |
| 1188 | } |
| 1189 | } |
| 1190 | // For esp-based frames where the allocas are done outside the prolog, the |
| 1191 | // esp value may not stabilize to its home value until after all the |
| 1192 | // fixed-size alloca instructions have executed. In this case, a stack |
| 1193 | // adjustment is needed when accessing in-args in order to copy them into |
| 1194 | // registers. |
| 1195 | size_t StackAdjBytes = 0; |
| 1196 | if (!IsEbpBasedFrame && !PrologEmitsFixedAllocas) |
| 1197 | StackAdjBytes -= FixedAllocaSizeBytes; |
| 1198 | finishArgumentLowering(Arg, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1199 | InArgsSizeBytes); |
| 1200 | } |
| 1201 | |
| 1202 | // Fill in stack offsets for locals. |
| 1203 | assignVarStackSlots(SortedSpilledVariables, SpillAreaPaddingBytes, |
| 1204 | SpillAreaSizeBytes, GlobalsAndSubsequentPaddingSize, |
| 1205 | IsEbpBasedFrame); |
| 1206 | // Assign stack offsets to variables that have been linked to spilled |
| 1207 | // variables. |
| 1208 | for (Variable *Var : VariablesLinkedToSpillSlots) { |
| 1209 | Variable *Linked = |
| 1210 | (llvm::cast<typename Traits::SpillVariable>(Var))->getLinkedTo(); |
| 1211 | Var->setStackOffset(Linked->getStackOffset()); |
| 1212 | } |
| 1213 | this->HasComputedFrame = true; |
| 1214 | |
| 1215 | if (BuildDefs::dump() && Func->isVerbose(IceV_Frame)) { |
| 1216 | OstreamLocker L(Func->getContext()); |
| 1217 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1218 | |
| 1219 | Str << "Stack layout:\n"; |
| 1220 | uint32_t EspAdjustmentPaddingSize = |
| 1221 | SpillAreaSizeBytes - LocalsSpillAreaSize - |
| 1222 | GlobalsAndSubsequentPaddingSize - SpillAreaPaddingBytes - |
| 1223 | maxOutArgsSizeBytes(); |
| 1224 | Str << " in-args = " << InArgsSizeBytes << " bytes\n" |
| 1225 | << " return address = " << Traits::X86_RET_IP_SIZE_BYTES << " bytes\n" |
| 1226 | << " preserved registers = " << PreservedRegsSizeBytes << " bytes\n" |
| 1227 | << " spill area padding = " << SpillAreaPaddingBytes << " bytes\n" |
| 1228 | << " globals spill area = " << GlobalsSize << " bytes\n" |
| 1229 | << " globals-locals spill areas intermediate padding = " |
| 1230 | << GlobalsAndSubsequentPaddingSize - GlobalsSize << " bytes\n" |
| 1231 | << " locals spill area = " << LocalsSpillAreaSize << " bytes\n" |
| 1232 | << " esp alignment padding = " << EspAdjustmentPaddingSize |
| 1233 | << " bytes\n"; |
| 1234 | |
| 1235 | Str << "Stack details:\n" |
| 1236 | << " esp adjustment = " << SpillAreaSizeBytes << " bytes\n" |
| 1237 | << " spill area alignment = " << SpillAreaAlignmentBytes << " bytes\n" |
| 1238 | << " outgoing args size = " << maxOutArgsSizeBytes() << " bytes\n" |
| 1239 | << " locals spill area alignment = " << LocalsSlotsAlignmentBytes |
| 1240 | << " bytes\n" |
| 1241 | << " is ebp based = " << IsEbpBasedFrame << "\n"; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1245 | /// Helper function for addProlog(). |
| 1246 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1247 | /// This assumes Arg is an argument passed on the stack. This sets the frame |
| 1248 | /// offset for Arg and updates InArgsSizeBytes according to Arg's width. For an |
| 1249 | /// I64 arg that has been split into Lo and Hi components, it calls itself |
| 1250 | /// recursively on the components, taking care to handle Lo first because of the |
| 1251 | /// little-endian architecture. Lastly, this function generates an instruction |
| 1252 | /// to copy Arg into its assigned register if applicable. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1253 | template <typename TraitsType> |
| 1254 | void TargetX86Base<TraitsType>::finishArgumentLowering( |
| 1255 | Variable *Arg, Variable *FramePtr, size_t BasicFrameOffset, |
| 1256 | size_t StackAdjBytes, size_t &InArgsSizeBytes) { |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1257 | if (!Traits::Is64Bit) { |
| 1258 | if (auto *Arg64On32 = llvm::dyn_cast<Variable64On32>(Arg)) { |
| 1259 | Variable *Lo = Arg64On32->getLo(); |
| 1260 | Variable *Hi = Arg64On32->getHi(); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1261 | finishArgumentLowering(Lo, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1262 | InArgsSizeBytes); |
| 1263 | finishArgumentLowering(Hi, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1264 | InArgsSizeBytes); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1265 | return; |
| 1266 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1267 | } |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1268 | Type Ty = Arg->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1269 | if (isVectorType(Ty)) { |
| 1270 | InArgsSizeBytes = Traits::applyStackAlignment(InArgsSizeBytes); |
| 1271 | } |
| 1272 | Arg->setStackOffset(BasicFrameOffset + InArgsSizeBytes); |
| 1273 | InArgsSizeBytes += typeWidthInBytesOnStack(Ty); |
| 1274 | if (Arg->hasReg()) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1275 | assert(Ty != IceType_i64 || Traits::Is64Bit); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1276 | auto *Mem = X86OperandMem::create( |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1277 | Func, Ty, FramePtr, |
| 1278 | Ctx->getConstantInt32(Arg->getStackOffset() + StackAdjBytes)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1279 | if (isVectorType(Arg->getType())) { |
| 1280 | _movp(Arg, Mem); |
| 1281 | } else { |
| 1282 | _mov(Arg, Mem); |
| 1283 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1284 | // This argument-copying instruction uses an explicit X86OperandMem |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1285 | // operand instead of a Variable, so its fill-from-stack operation has to |
| 1286 | // be tracked separately for statistics. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1287 | Ctx->statsUpdateFills(); |
| 1288 | } |
| 1289 | } |
| 1290 | |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1291 | template <typename TraitsType> |
| 1292 | void TargetX86Base<TraitsType>::addEpilog(CfgNode *Node) { |
| 1293 | InstList &Insts = Node->getInsts(); |
| 1294 | InstList::reverse_iterator RI, E; |
| 1295 | for (RI = Insts.rbegin(), E = Insts.rend(); RI != E; ++RI) { |
| 1296 | if (llvm::isa<typename Traits::Insts::Ret>(*RI)) |
| 1297 | break; |
| 1298 | } |
| 1299 | if (RI == E) |
| 1300 | return; |
| 1301 | |
| 1302 | // Convert the reverse_iterator position into its corresponding (forward) |
| 1303 | // iterator position. |
| 1304 | InstList::iterator InsertPoint = RI.base(); |
| 1305 | --InsertPoint; |
| 1306 | Context.init(Node); |
| 1307 | Context.setInsertPoint(InsertPoint); |
| 1308 | |
| 1309 | if (IsEbpBasedFrame) { |
| 1310 | _unlink_bp(); |
| 1311 | } else { |
| 1312 | // add stackptr, SpillAreaSizeBytes |
| 1313 | if (SpillAreaSizeBytes != 0) { |
| 1314 | _add_sp(Ctx->getConstantInt32(SpillAreaSizeBytes)); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | // Add pop instructions for preserved registers. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1319 | SmallBitVector CalleeSaves = getRegisterSet(RegSet_CalleeSave, RegSet_None); |
| 1320 | SmallBitVector Popped(CalleeSaves.size()); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1321 | for (int32_t i = CalleeSaves.size() - 1; i >= 0; --i) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1322 | const auto RegNum = RegNumT::fromInt(i); |
| 1323 | if (RegNum == getFrameReg() && IsEbpBasedFrame) |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1324 | continue; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1325 | const RegNumT Canonical = Traits::getBaseReg(RegNum); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1326 | if (CalleeSaves[i] && RegsUsed[i]) { |
| 1327 | Popped[Canonical] = true; |
| 1328 | } |
| 1329 | } |
| 1330 | for (int32_t i = Popped.size() - 1; i >= 0; --i) { |
| 1331 | if (!Popped[i]) |
| 1332 | continue; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1333 | const auto RegNum = RegNumT::fromInt(i); |
| 1334 | assert(RegNum == Traits::getBaseReg(RegNum)); |
| 1335 | _pop(getPhysicalRegister(RegNum, Traits::WordType)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (!NeedSandboxing) { |
| 1339 | return; |
| 1340 | } |
| 1341 | emitSandboxedReturn(); |
| 1342 | if (RI->getSrcSize()) { |
| 1343 | auto *RetValue = llvm::cast<Variable>(RI->getSrc(0)); |
| 1344 | Context.insert<InstFakeUse>(RetValue); |
| 1345 | } |
| 1346 | RI->setDeleted(); |
| 1347 | } |
| 1348 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1349 | template <typename TraitsType> Type TargetX86Base<TraitsType>::stackSlotType() { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1350 | return Traits::WordType; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1353 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1354 | template <typename T> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1355 | typename std::enable_if<!T::Is64Bit, Operand>::type * |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1356 | TargetX86Base<TraitsType>::loOperand(Operand *Operand) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1357 | assert(Operand->getType() == IceType_i64 || |
| 1358 | Operand->getType() == IceType_f64); |
| 1359 | if (Operand->getType() != IceType_i64 && Operand->getType() != IceType_f64) |
| 1360 | return Operand; |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1361 | if (auto *Var64On32 = llvm::dyn_cast<Variable64On32>(Operand)) |
| 1362 | return Var64On32->getLo(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1363 | if (auto *Const = llvm::dyn_cast<ConstantInteger64>(Operand)) { |
| 1364 | auto *ConstInt = llvm::dyn_cast<ConstantInteger32>( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1365 | Ctx->getConstantInt32(static_cast<int32_t>(Const->getValue()))); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1366 | // Check if we need to blind/pool the constant. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1367 | return legalize(ConstInt); |
| 1368 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1369 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Operand)) { |
| 1370 | auto *MemOperand = X86OperandMem::create( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1371 | Func, IceType_i32, Mem->getBase(), Mem->getOffset(), Mem->getIndex(), |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1372 | Mem->getShift(), Mem->getSegmentRegister(), Mem->getIsRebased()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1373 | // Test if we should randomize or pool the offset, if so randomize it or |
| 1374 | // pool it then create mem operand with the blinded/pooled constant. |
| 1375 | // Otherwise, return the mem operand as ordinary mem operand. |
| 1376 | return legalize(MemOperand); |
| 1377 | } |
| 1378 | llvm_unreachable("Unsupported operand type"); |
| 1379 | return nullptr; |
| 1380 | } |
| 1381 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1382 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1383 | template <typename T> |
| 1384 | typename std::enable_if<!T::Is64Bit, Operand>::type * |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1385 | TargetX86Base<TraitsType>::hiOperand(Operand *Operand) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1386 | assert(Operand->getType() == IceType_i64 || |
| 1387 | Operand->getType() == IceType_f64); |
| 1388 | if (Operand->getType() != IceType_i64 && Operand->getType() != IceType_f64) |
| 1389 | return Operand; |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1390 | if (auto *Var64On32 = llvm::dyn_cast<Variable64On32>(Operand)) |
| 1391 | return Var64On32->getHi(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1392 | if (auto *Const = llvm::dyn_cast<ConstantInteger64>(Operand)) { |
| 1393 | auto *ConstInt = llvm::dyn_cast<ConstantInteger32>( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1394 | Ctx->getConstantInt32(static_cast<int32_t>(Const->getValue() >> 32))); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1395 | // Check if we need to blind/pool the constant. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1396 | return legalize(ConstInt); |
| 1397 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1398 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Operand)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1399 | Constant *Offset = Mem->getOffset(); |
| 1400 | if (Offset == nullptr) { |
| 1401 | Offset = Ctx->getConstantInt32(4); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1402 | } else if (auto *IntOffset = llvm::dyn_cast<ConstantInteger32>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1403 | Offset = Ctx->getConstantInt32(4 + IntOffset->getValue()); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1404 | } else if (auto *SymOffset = llvm::dyn_cast<ConstantRelocatable>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1405 | assert(!Utils::WouldOverflowAdd(SymOffset->getOffset(), 4)); |
| 1406 | Offset = |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1407 | Ctx->getConstantSym(4 + SymOffset->getOffset(), SymOffset->getName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1408 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1409 | auto *MemOperand = X86OperandMem::create( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1410 | Func, IceType_i32, Mem->getBase(), Offset, Mem->getIndex(), |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1411 | Mem->getShift(), Mem->getSegmentRegister(), Mem->getIsRebased()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1412 | // Test if the Offset is an eligible i32 constants for randomization and |
| 1413 | // pooling. Blind/pool it if it is. Otherwise return as oridinary mem |
| 1414 | // operand. |
| 1415 | return legalize(MemOperand); |
| 1416 | } |
| 1417 | llvm_unreachable("Unsupported operand type"); |
| 1418 | return nullptr; |
| 1419 | } |
| 1420 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1421 | template <typename TraitsType> |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1422 | SmallBitVector |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1423 | TargetX86Base<TraitsType>::getRegisterSet(RegSetMask Include, |
| 1424 | RegSetMask Exclude) const { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1425 | return Traits::getRegisterSet(getFlags(), Include, Exclude); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1428 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1429 | void TargetX86Base<TraitsType>::lowerAlloca(const InstAlloca *Instr) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1430 | // Conservatively require the stack to be aligned. Some stack adjustment |
| 1431 | // operations implemented below assume that the stack is aligned before the |
| 1432 | // alloca. All the alloca code ensures that the stack alignment is preserved |
| 1433 | // after the alloca. The stack alignment restriction can be relaxed in some |
| 1434 | // cases. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1435 | NeedsStackAlignment = true; |
| 1436 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1437 | // For default align=0, set it to the real value 1, to avoid any |
| 1438 | // bit-manipulation problems below. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1439 | const uint32_t AlignmentParam = std::max(1u, Instr->getAlignInBytes()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1440 | |
| 1441 | // LLVM enforces power of 2 alignment. |
| 1442 | assert(llvm::isPowerOf2_32(AlignmentParam)); |
| 1443 | assert(llvm::isPowerOf2_32(Traits::X86_STACK_ALIGNMENT_BYTES)); |
| 1444 | |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1445 | const uint32_t Alignment = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1446 | std::max(AlignmentParam, Traits::X86_STACK_ALIGNMENT_BYTES); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1447 | const bool OverAligned = Alignment > Traits::X86_STACK_ALIGNMENT_BYTES; |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 1448 | const bool OptM1 = Func->getOptLevel() == Opt_m1; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1449 | const bool AllocaWithKnownOffset = Instr->getKnownFrameOffset(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1450 | const bool UseFramePointer = |
| 1451 | hasFramePointer() || OverAligned || !AllocaWithKnownOffset || OptM1; |
| 1452 | |
| 1453 | if (UseFramePointer) |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1454 | setHasFramePointer(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1455 | |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1456 | Variable *esp = getPhysicalRegister(getStackReg(), Traits::WordType); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1457 | if (OverAligned) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1458 | _and(esp, Ctx->getConstantInt32(-Alignment)); |
| 1459 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1460 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1461 | Variable *Dest = Instr->getDest(); |
| 1462 | Operand *TotalSize = legalize(Instr->getSizeInBytes()); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1463 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1464 | if (const auto *ConstantTotalSize = |
| 1465 | llvm::dyn_cast<ConstantInteger32>(TotalSize)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1466 | const uint32_t Value = |
| 1467 | Utils::applyAlignment(ConstantTotalSize->getValue(), Alignment); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1468 | if (UseFramePointer) { |
| 1469 | _sub_sp(Ctx->getConstantInt32(Value)); |
| 1470 | } else { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1471 | // If we don't need a Frame Pointer, this alloca has a known offset to the |
| 1472 | // stack pointer. We don't need adjust the stack pointer, nor assign any |
| 1473 | // value to Dest, as Dest is rematerializable. |
| 1474 | assert(Dest->isRematerializable()); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1475 | FixedAllocaSizeBytes += Value; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 1476 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1477 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1478 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1479 | // Non-constant sizes need to be adjusted to the next highest multiple of |
| 1480 | // the required alignment at runtime. |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1481 | Variable *T = nullptr; |
| 1482 | if (Traits::Is64Bit && TotalSize->getType() != IceType_i64 && |
| 1483 | !NeedSandboxing) { |
| 1484 | T = makeReg(IceType_i64); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1485 | _movzx(T, TotalSize); |
| 1486 | } else { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1487 | T = makeReg(IceType_i32); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1488 | _mov(T, TotalSize); |
| 1489 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1490 | _add(T, Ctx->getConstantInt32(Alignment - 1)); |
| 1491 | _and(T, Ctx->getConstantInt32(-Alignment)); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1492 | _sub_sp(T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1493 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1494 | // Add enough to the returned address to account for the out args area. |
| 1495 | uint32_t OutArgsSize = maxOutArgsSizeBytes(); |
| 1496 | if (OutArgsSize > 0) { |
| 1497 | Variable *T = makeReg(IceType_i32); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1498 | auto *CalculateOperand = X86OperandMem::create( |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1499 | Func, IceType_void, esp, Ctx->getConstantInt(IceType_i32, OutArgsSize)); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1500 | _lea(T, CalculateOperand); |
| 1501 | _mov(Dest, T); |
| 1502 | } else { |
| 1503 | _mov(Dest, esp); |
| 1504 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1507 | template <typename TraitsType> |
| 1508 | void TargetX86Base<TraitsType>::lowerArguments() { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1509 | const bool OptM1 = Func->getOptLevel() == Opt_m1; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1510 | VarList &Args = Func->getArgs(); |
| 1511 | unsigned NumXmmArgs = 0; |
| 1512 | bool XmmSlotsRemain = true; |
| 1513 | unsigned NumGprArgs = 0; |
| 1514 | bool GprSlotsRemain = true; |
| 1515 | |
| 1516 | Context.init(Func->getEntryNode()); |
| 1517 | Context.setInsertPoint(Context.getCur()); |
| 1518 | |
| 1519 | for (SizeT i = 0, End = Args.size(); |
| 1520 | i < End && (XmmSlotsRemain || GprSlotsRemain); ++i) { |
| 1521 | Variable *Arg = Args[i]; |
| 1522 | Type Ty = Arg->getType(); |
| 1523 | Variable *RegisterArg = nullptr; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1524 | RegNumT RegNum; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1525 | if (isVectorType(Ty)) { |
| 1526 | RegNum = Traits::getRegisterForXmmArgNum(NumXmmArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1527 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1528 | XmmSlotsRemain = false; |
| 1529 | continue; |
| 1530 | } |
| 1531 | ++NumXmmArgs; |
| 1532 | RegisterArg = Func->makeVariable(Ty); |
| 1533 | } else if (isScalarFloatingType(Ty)) { |
| 1534 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 1535 | continue; |
| 1536 | } |
| 1537 | RegNum = Traits::getRegisterForXmmArgNum(NumXmmArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1538 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1539 | XmmSlotsRemain = false; |
| 1540 | continue; |
| 1541 | } |
| 1542 | ++NumXmmArgs; |
| 1543 | RegisterArg = Func->makeVariable(Ty); |
| 1544 | } else if (isScalarIntegerType(Ty)) { |
| 1545 | RegNum = Traits::getRegisterForGprArgNum(Ty, NumGprArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1546 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1547 | GprSlotsRemain = false; |
| 1548 | continue; |
| 1549 | } |
| 1550 | ++NumGprArgs; |
| 1551 | RegisterArg = Func->makeVariable(Ty); |
| 1552 | } |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1553 | assert(RegNum.hasValue()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1554 | assert(RegisterArg != nullptr); |
| 1555 | // Replace Arg in the argument list with the home register. Then generate |
| 1556 | // an instruction in the prolog to copy the home register to the assigned |
| 1557 | // location of Arg. |
| 1558 | if (BuildDefs::dump()) |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 1559 | RegisterArg->setName(Func, "home_reg:" + Arg->getName()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1560 | RegisterArg->setRegNum(RegNum); |
| 1561 | RegisterArg->setIsArg(); |
| 1562 | Arg->setIsArg(false); |
| 1563 | |
| 1564 | Args[i] = RegisterArg; |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1565 | // When not Om1, do the assignment through a temporary, instead of directly |
| 1566 | // from the pre-colored variable, so that a subsequent availabilityGet() |
| 1567 | // call has a chance to work. (In Om1, don't bother creating extra |
| 1568 | // instructions with extra variables to register-allocate.) |
| 1569 | if (OptM1) { |
| 1570 | Context.insert<InstAssign>(Arg, RegisterArg); |
| 1571 | } else { |
| 1572 | Variable *Tmp = makeReg(RegisterArg->getType()); |
| 1573 | Context.insert<InstAssign>(Tmp, RegisterArg); |
| 1574 | Context.insert<InstAssign>(Arg, Tmp); |
| 1575 | } |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1576 | } |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1577 | if (!OptM1) |
| 1578 | Context.availabilityUpdate(); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1581 | /// Strength-reduce scalar integer multiplication by a constant (for i32 or |
| 1582 | /// narrower) for certain constants. The lea instruction can be used to multiply |
| 1583 | /// by 3, 5, or 9, and the lsh instruction can be used to multiply by powers of |
| 1584 | /// 2. These can be combined such that e.g. multiplying by 100 can be done as 2 |
| 1585 | /// lea-based multiplies by 5, combined with left-shifting by 2. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1586 | template <typename TraitsType> |
| 1587 | bool TargetX86Base<TraitsType>::optimizeScalarMul(Variable *Dest, Operand *Src0, |
| 1588 | int32_t Src1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1589 | // Disable this optimization for Om1 and O0, just to keep things simple |
| 1590 | // there. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 1591 | if (Func->getOptLevel() < Opt_1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1592 | return false; |
| 1593 | Type Ty = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1594 | if (Src1 == -1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1595 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1596 | _mov(T, Src0); |
| 1597 | _neg(T); |
| 1598 | _mov(Dest, T); |
| 1599 | return true; |
| 1600 | } |
| 1601 | if (Src1 == 0) { |
| 1602 | _mov(Dest, Ctx->getConstantZero(Ty)); |
| 1603 | return true; |
| 1604 | } |
| 1605 | if (Src1 == 1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1606 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1607 | _mov(T, Src0); |
| 1608 | _mov(Dest, T); |
| 1609 | return true; |
| 1610 | } |
| 1611 | // Don't bother with the edge case where Src1 == MININT. |
| 1612 | if (Src1 == -Src1) |
| 1613 | return false; |
| 1614 | const bool Src1IsNegative = Src1 < 0; |
| 1615 | if (Src1IsNegative) |
| 1616 | Src1 = -Src1; |
| 1617 | uint32_t Count9 = 0; |
| 1618 | uint32_t Count5 = 0; |
| 1619 | uint32_t Count3 = 0; |
| 1620 | uint32_t Count2 = 0; |
| 1621 | uint32_t CountOps = 0; |
| 1622 | while (Src1 > 1) { |
| 1623 | if (Src1 % 9 == 0) { |
| 1624 | ++CountOps; |
| 1625 | ++Count9; |
| 1626 | Src1 /= 9; |
| 1627 | } else if (Src1 % 5 == 0) { |
| 1628 | ++CountOps; |
| 1629 | ++Count5; |
| 1630 | Src1 /= 5; |
| 1631 | } else if (Src1 % 3 == 0) { |
| 1632 | ++CountOps; |
| 1633 | ++Count3; |
| 1634 | Src1 /= 3; |
| 1635 | } else if (Src1 % 2 == 0) { |
| 1636 | if (Count2 == 0) |
| 1637 | ++CountOps; |
| 1638 | ++Count2; |
| 1639 | Src1 /= 2; |
| 1640 | } else { |
| 1641 | return false; |
| 1642 | } |
| 1643 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1644 | // Lea optimization only works for i16 and i32 types, not i8. |
| 1645 | if (Ty != IceType_i32 && !(Traits::Is64Bit && Ty == IceType_i64) && |
| 1646 | (Count3 || Count5 || Count9)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1647 | return false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1648 | // Limit the number of lea/shl operations for a single multiply, to a |
| 1649 | // somewhat arbitrary choice of 3. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1650 | constexpr uint32_t MaxOpsForOptimizedMul = 3; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1651 | if (CountOps > MaxOpsForOptimizedMul) |
| 1652 | return false; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1653 | Variable *T = makeReg(Traits::WordType); |
| 1654 | if (typeWidthInBytes(Src0->getType()) < typeWidthInBytes(T->getType())) { |
| 1655 | _movzx(T, Src0); |
| 1656 | } else { |
| 1657 | _mov(T, Src0); |
| 1658 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1659 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 1660 | for (uint32_t i = 0; i < Count9; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1661 | constexpr uint16_t Shift = 3; // log2(9-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1662 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1663 | } |
| 1664 | for (uint32_t i = 0; i < Count5; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1665 | constexpr uint16_t Shift = 2; // log2(5-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1666 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1667 | } |
| 1668 | for (uint32_t i = 0; i < Count3; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1669 | constexpr uint16_t Shift = 1; // log2(3-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1670 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1671 | } |
| 1672 | if (Count2) { |
| 1673 | _shl(T, Ctx->getConstantInt(Ty, Count2)); |
| 1674 | } |
| 1675 | if (Src1IsNegative) |
| 1676 | _neg(T); |
| 1677 | _mov(Dest, T); |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1681 | template <typename TraitsType> |
| 1682 | void TargetX86Base<TraitsType>::lowerShift64(InstArithmetic::OpKind Op, |
| 1683 | Operand *Src0Lo, Operand *Src0Hi, |
| 1684 | Operand *Src1Lo, Variable *DestLo, |
| 1685 | Variable *DestHi) { |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1686 | // TODO: Refactor the similarities between Shl, Lshr, and Ashr. |
| 1687 | Variable *T_1 = nullptr, *T_2 = nullptr, *T_3 = nullptr; |
| 1688 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 1689 | Constant *SignExtend = Ctx->getConstantInt32(0x1f); |
| 1690 | if (auto *ConstantShiftAmount = llvm::dyn_cast<ConstantInteger32>(Src1Lo)) { |
| 1691 | uint32_t ShiftAmount = ConstantShiftAmount->getValue(); |
| 1692 | if (ShiftAmount > 32) { |
| 1693 | Constant *ReducedShift = Ctx->getConstantInt32(ShiftAmount - 32); |
| 1694 | switch (Op) { |
| 1695 | default: |
| 1696 | assert(0 && "non-shift op"); |
| 1697 | break; |
| 1698 | case InstArithmetic::Shl: { |
| 1699 | // a=b<<c ==> |
| 1700 | // t2 = b.lo |
| 1701 | // t2 = shl t2, ShiftAmount-32 |
| 1702 | // t3 = t2 |
| 1703 | // t2 = 0 |
| 1704 | _mov(T_2, Src0Lo); |
| 1705 | _shl(T_2, ReducedShift); |
| 1706 | _mov(DestHi, T_2); |
| 1707 | _mov(DestLo, Zero); |
| 1708 | } break; |
| 1709 | case InstArithmetic::Lshr: { |
| 1710 | // a=b>>c (unsigned) ==> |
| 1711 | // t2 = b.hi |
| 1712 | // t2 = shr t2, ShiftAmount-32 |
| 1713 | // a.lo = t2 |
| 1714 | // a.hi = 0 |
| 1715 | _mov(T_2, Src0Hi); |
| 1716 | _shr(T_2, ReducedShift); |
| 1717 | _mov(DestLo, T_2); |
| 1718 | _mov(DestHi, Zero); |
| 1719 | } break; |
| 1720 | case InstArithmetic::Ashr: { |
| 1721 | // a=b>>c (signed) ==> |
| 1722 | // t3 = b.hi |
| 1723 | // t3 = sar t3, 0x1f |
| 1724 | // t2 = b.hi |
| 1725 | // t2 = shrd t2, t3, ShiftAmount-32 |
| 1726 | // a.lo = t2 |
| 1727 | // a.hi = t3 |
| 1728 | _mov(T_3, Src0Hi); |
| 1729 | _sar(T_3, SignExtend); |
| 1730 | _mov(T_2, Src0Hi); |
| 1731 | _shrd(T_2, T_3, ReducedShift); |
| 1732 | _mov(DestLo, T_2); |
| 1733 | _mov(DestHi, T_3); |
| 1734 | } break; |
| 1735 | } |
| 1736 | } else if (ShiftAmount == 32) { |
| 1737 | switch (Op) { |
| 1738 | default: |
| 1739 | assert(0 && "non-shift op"); |
| 1740 | break; |
| 1741 | case InstArithmetic::Shl: { |
| 1742 | // a=b<<c ==> |
| 1743 | // t2 = b.lo |
| 1744 | // a.hi = t2 |
| 1745 | // a.lo = 0 |
| 1746 | _mov(T_2, Src0Lo); |
| 1747 | _mov(DestHi, T_2); |
| 1748 | _mov(DestLo, Zero); |
| 1749 | } break; |
| 1750 | case InstArithmetic::Lshr: { |
| 1751 | // a=b>>c (unsigned) ==> |
| 1752 | // t2 = b.hi |
| 1753 | // a.lo = t2 |
| 1754 | // a.hi = 0 |
| 1755 | _mov(T_2, Src0Hi); |
| 1756 | _mov(DestLo, T_2); |
| 1757 | _mov(DestHi, Zero); |
| 1758 | } break; |
| 1759 | case InstArithmetic::Ashr: { |
| 1760 | // a=b>>c (signed) ==> |
| 1761 | // t2 = b.hi |
| 1762 | // a.lo = t2 |
| 1763 | // t3 = b.hi |
| 1764 | // t3 = sar t3, 0x1f |
| 1765 | // a.hi = t3 |
| 1766 | _mov(T_2, Src0Hi); |
| 1767 | _mov(DestLo, T_2); |
| 1768 | _mov(T_3, Src0Hi); |
| 1769 | _sar(T_3, SignExtend); |
| 1770 | _mov(DestHi, T_3); |
| 1771 | } break; |
| 1772 | } |
| 1773 | } else { |
| 1774 | // COMMON PREFIX OF: a=b SHIFT_OP c ==> |
| 1775 | // t2 = b.lo |
| 1776 | // t3 = b.hi |
| 1777 | _mov(T_2, Src0Lo); |
| 1778 | _mov(T_3, Src0Hi); |
| 1779 | switch (Op) { |
| 1780 | default: |
| 1781 | assert(0 && "non-shift op"); |
| 1782 | break; |
| 1783 | case InstArithmetic::Shl: { |
| 1784 | // a=b<<c ==> |
| 1785 | // t3 = shld t3, t2, ShiftAmount |
| 1786 | // t2 = shl t2, ShiftAmount |
| 1787 | _shld(T_3, T_2, ConstantShiftAmount); |
| 1788 | _shl(T_2, ConstantShiftAmount); |
| 1789 | } break; |
| 1790 | case InstArithmetic::Lshr: { |
| 1791 | // a=b>>c (unsigned) ==> |
| 1792 | // t2 = shrd t2, t3, ShiftAmount |
| 1793 | // t3 = shr t3, ShiftAmount |
| 1794 | _shrd(T_2, T_3, ConstantShiftAmount); |
| 1795 | _shr(T_3, ConstantShiftAmount); |
| 1796 | } break; |
| 1797 | case InstArithmetic::Ashr: { |
| 1798 | // a=b>>c (signed) ==> |
| 1799 | // t2 = shrd t2, t3, ShiftAmount |
| 1800 | // t3 = sar t3, ShiftAmount |
| 1801 | _shrd(T_2, T_3, ConstantShiftAmount); |
| 1802 | _sar(T_3, ConstantShiftAmount); |
| 1803 | } break; |
| 1804 | } |
| 1805 | // COMMON SUFFIX OF: a=b SHIFT_OP c ==> |
| 1806 | // a.lo = t2 |
| 1807 | // a.hi = t3 |
| 1808 | _mov(DestLo, T_2); |
| 1809 | _mov(DestHi, T_3); |
| 1810 | } |
| 1811 | } else { |
| 1812 | // NON-CONSTANT CASES. |
| 1813 | Constant *BitTest = Ctx->getConstantInt32(0x20); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1814 | InstX86Label *Label = InstX86Label::create(Func, this); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1815 | // COMMON PREFIX OF: a=b SHIFT_OP c ==> |
| 1816 | // t1:ecx = c.lo & 0xff |
| 1817 | // t2 = b.lo |
| 1818 | // t3 = b.hi |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1819 | T_1 = copyToReg8(Src1Lo, Traits::RegisterSet::Reg_cl); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1820 | _mov(T_2, Src0Lo); |
| 1821 | _mov(T_3, Src0Hi); |
| 1822 | switch (Op) { |
| 1823 | default: |
| 1824 | assert(0 && "non-shift op"); |
| 1825 | break; |
| 1826 | case InstArithmetic::Shl: { |
| 1827 | // a=b<<c ==> |
| 1828 | // t3 = shld t3, t2, t1 |
| 1829 | // t2 = shl t2, t1 |
| 1830 | // test t1, 0x20 |
| 1831 | // je L1 |
| 1832 | // use(t3) |
| 1833 | // t3 = t2 |
| 1834 | // t2 = 0 |
| 1835 | _shld(T_3, T_2, T_1); |
| 1836 | _shl(T_2, T_1); |
| 1837 | _test(T_1, BitTest); |
| 1838 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1839 | // T_2 and T_3 are being assigned again because of the intra-block control |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1840 | // flow, so we need to use _redefined to avoid liveness problems. |
| 1841 | _redefined(_mov(T_3, T_2)); |
| 1842 | _redefined(_mov(T_2, Zero)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1843 | } break; |
| 1844 | case InstArithmetic::Lshr: { |
| 1845 | // a=b>>c (unsigned) ==> |
| 1846 | // t2 = shrd t2, t3, t1 |
| 1847 | // t3 = shr t3, t1 |
| 1848 | // test t1, 0x20 |
| 1849 | // je L1 |
| 1850 | // use(t2) |
| 1851 | // t2 = t3 |
| 1852 | // t3 = 0 |
| 1853 | _shrd(T_2, T_3, T_1); |
| 1854 | _shr(T_3, T_1); |
| 1855 | _test(T_1, BitTest); |
| 1856 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1857 | // T_2 and T_3 are being assigned again because of the intra-block control |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1858 | // flow, so we need to use _redefined to avoid liveness problems. |
| 1859 | _redefined(_mov(T_2, T_3)); |
| 1860 | _redefined(_mov(T_3, Zero)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1861 | } break; |
| 1862 | case InstArithmetic::Ashr: { |
| 1863 | // a=b>>c (signed) ==> |
| 1864 | // t2 = shrd t2, t3, t1 |
| 1865 | // t3 = sar t3, t1 |
| 1866 | // test t1, 0x20 |
| 1867 | // je L1 |
| 1868 | // use(t2) |
| 1869 | // t2 = t3 |
| 1870 | // t3 = sar t3, 0x1f |
| 1871 | Constant *SignExtend = Ctx->getConstantInt32(0x1f); |
| 1872 | _shrd(T_2, T_3, T_1); |
| 1873 | _sar(T_3, T_1); |
| 1874 | _test(T_1, BitTest); |
| 1875 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1876 | // T_2 and T_3 are being assigned again because of the intra-block control |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 1877 | // flow, so T_2 needs to use _redefined to avoid liveness problems. T_3 |
| 1878 | // doesn't need special treatment because it is reassigned via _sar |
| 1879 | // instead of _mov. |
| 1880 | _redefined(_mov(T_2, T_3)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1881 | _sar(T_3, SignExtend); |
| 1882 | } break; |
| 1883 | } |
| 1884 | // COMMON SUFFIX OF: a=b SHIFT_OP c ==> |
| 1885 | // L1: |
| 1886 | // a.lo = t2 |
| 1887 | // a.hi = t3 |
| 1888 | Context.insert(Label); |
| 1889 | _mov(DestLo, T_2); |
| 1890 | _mov(DestHi, T_3); |
| 1891 | } |
| 1892 | } |
| 1893 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1894 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1895 | void TargetX86Base<TraitsType>::lowerArithmetic(const InstArithmetic *Instr) { |
| 1896 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 1897 | if (Dest->isRematerializable()) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 1898 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 1899 | return; |
| 1900 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1901 | Type Ty = Dest->getType(); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1902 | Operand *Src0 = legalize(Instr->getSrc(0)); |
| 1903 | Operand *Src1 = legalize(Instr->getSrc(1)); |
| 1904 | if (Instr->isCommutative()) { |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1905 | uint32_t SwapCount = 0; |
| 1906 | if (!llvm::isa<Variable>(Src0) && llvm::isa<Variable>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1907 | std::swap(Src0, Src1); |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1908 | ++SwapCount; |
| 1909 | } |
| 1910 | if (llvm::isa<Constant>(Src0) && !llvm::isa<Constant>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1911 | std::swap(Src0, Src1); |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1912 | ++SwapCount; |
| 1913 | } |
| 1914 | // Improve two-address code patterns by avoiding a copy to the dest |
| 1915 | // register when one of the source operands ends its lifetime here. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1916 | if (!Instr->isLastUse(Src0) && Instr->isLastUse(Src1)) { |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1917 | std::swap(Src0, Src1); |
| 1918 | ++SwapCount; |
| 1919 | } |
| 1920 | assert(SwapCount <= 1); |
Karl Schimpf | a313a12 | 2015-10-08 10:40:57 -0700 | [diff] [blame] | 1921 | (void)SwapCount; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1922 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1923 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1924 | // These x86-32 helper-call-involved instructions are lowered in this |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1925 | // separate switch. This is because loOperand() and hiOperand() may insert |
| 1926 | // redundant instructions for constant blinding and pooling. Such redundant |
| 1927 | // instructions will fail liveness analysis under -Om1 setting. And, |
| 1928 | // actually these arguments do not need to be processed with loOperand() |
| 1929 | // and hiOperand() to be used. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1930 | switch (Instr->getOp()) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1931 | case InstArithmetic::Udiv: |
| 1932 | case InstArithmetic::Sdiv: |
| 1933 | case InstArithmetic::Urem: |
| 1934 | case InstArithmetic::Srem: |
| 1935 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1936 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1937 | default: |
| 1938 | break; |
| 1939 | } |
| 1940 | |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 1941 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 1942 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1943 | Operand *Src0Lo = loOperand(Src0); |
| 1944 | Operand *Src0Hi = hiOperand(Src0); |
| 1945 | Operand *Src1Lo = loOperand(Src1); |
| 1946 | Operand *Src1Hi = hiOperand(Src1); |
| 1947 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1948 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1949 | case InstArithmetic::_num: |
| 1950 | llvm_unreachable("Unknown arithmetic operator"); |
| 1951 | break; |
| 1952 | case InstArithmetic::Add: |
| 1953 | _mov(T_Lo, Src0Lo); |
| 1954 | _add(T_Lo, Src1Lo); |
| 1955 | _mov(DestLo, T_Lo); |
| 1956 | _mov(T_Hi, Src0Hi); |
| 1957 | _adc(T_Hi, Src1Hi); |
| 1958 | _mov(DestHi, T_Hi); |
| 1959 | break; |
| 1960 | case InstArithmetic::And: |
| 1961 | _mov(T_Lo, Src0Lo); |
| 1962 | _and(T_Lo, Src1Lo); |
| 1963 | _mov(DestLo, T_Lo); |
| 1964 | _mov(T_Hi, Src0Hi); |
| 1965 | _and(T_Hi, Src1Hi); |
| 1966 | _mov(DestHi, T_Hi); |
| 1967 | break; |
| 1968 | case InstArithmetic::Or: |
| 1969 | _mov(T_Lo, Src0Lo); |
| 1970 | _or(T_Lo, Src1Lo); |
| 1971 | _mov(DestLo, T_Lo); |
| 1972 | _mov(T_Hi, Src0Hi); |
| 1973 | _or(T_Hi, Src1Hi); |
| 1974 | _mov(DestHi, T_Hi); |
| 1975 | break; |
| 1976 | case InstArithmetic::Xor: |
| 1977 | _mov(T_Lo, Src0Lo); |
| 1978 | _xor(T_Lo, Src1Lo); |
| 1979 | _mov(DestLo, T_Lo); |
| 1980 | _mov(T_Hi, Src0Hi); |
| 1981 | _xor(T_Hi, Src1Hi); |
| 1982 | _mov(DestHi, T_Hi); |
| 1983 | break; |
| 1984 | case InstArithmetic::Sub: |
| 1985 | _mov(T_Lo, Src0Lo); |
| 1986 | _sub(T_Lo, Src1Lo); |
| 1987 | _mov(DestLo, T_Lo); |
| 1988 | _mov(T_Hi, Src0Hi); |
| 1989 | _sbb(T_Hi, Src1Hi); |
| 1990 | _mov(DestHi, T_Hi); |
| 1991 | break; |
| 1992 | case InstArithmetic::Mul: { |
| 1993 | Variable *T_1 = nullptr, *T_2 = nullptr, *T_3 = nullptr; |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 1994 | Variable *T_4Lo = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 1995 | Variable *T_4Hi = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1996 | // gcc does the following: |
| 1997 | // a=b*c ==> |
| 1998 | // t1 = b.hi; t1 *=(imul) c.lo |
| 1999 | // t2 = c.hi; t2 *=(imul) b.lo |
| 2000 | // t3:eax = b.lo |
| 2001 | // t4.hi:edx,t4.lo:eax = t3:eax *(mul) c.lo |
| 2002 | // a.lo = t4.lo |
| 2003 | // t4.hi += t1 |
| 2004 | // t4.hi += t2 |
| 2005 | // a.hi = t4.hi |
| 2006 | // The mul instruction cannot take an immediate operand. |
| 2007 | Src1Lo = legalize(Src1Lo, Legal_Reg | Legal_Mem); |
| 2008 | _mov(T_1, Src0Hi); |
| 2009 | _imul(T_1, Src1Lo); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 2010 | _mov(T_3, Src0Lo, Traits::RegisterSet::Reg_eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2011 | _mul(T_4Lo, T_3, Src1Lo); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2012 | // The mul instruction produces two dest variables, edx:eax. We create a |
| 2013 | // fake definition of edx to account for this. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 2014 | Context.insert<InstFakeDef>(T_4Hi, T_4Lo); |
Jim Stichnoth | 28df6ba | 2016-02-05 15:43:24 -0800 | [diff] [blame] | 2015 | Context.insert<InstFakeUse>(T_4Hi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2016 | _mov(DestLo, T_4Lo); |
| 2017 | _add(T_4Hi, T_1); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 2018 | _mov(T_2, Src1Hi); |
| 2019 | _imul(T_2, Src0Lo); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2020 | _add(T_4Hi, T_2); |
| 2021 | _mov(DestHi, T_4Hi); |
| 2022 | } break; |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 2023 | case InstArithmetic::Shl: |
| 2024 | case InstArithmetic::Lshr: |
| 2025 | case InstArithmetic::Ashr: |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2026 | lowerShift64(Instr->getOp(), Src0Lo, Src0Hi, Src1Lo, DestLo, DestHi); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 2027 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2028 | case InstArithmetic::Fadd: |
| 2029 | case InstArithmetic::Fsub: |
| 2030 | case InstArithmetic::Fmul: |
| 2031 | case InstArithmetic::Fdiv: |
| 2032 | case InstArithmetic::Frem: |
| 2033 | llvm_unreachable("FP instruction with i64 type"); |
| 2034 | break; |
| 2035 | case InstArithmetic::Udiv: |
| 2036 | case InstArithmetic::Sdiv: |
| 2037 | case InstArithmetic::Urem: |
| 2038 | case InstArithmetic::Srem: |
| 2039 | llvm_unreachable("Call-helper-involved instruction for i64 type \ |
| 2040 | should have already been handled before"); |
| 2041 | break; |
| 2042 | } |
| 2043 | return; |
| 2044 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2045 | if (isVectorType(Ty)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2046 | // TODO: Trap on integer divide and integer modulo by zero. See: |
| 2047 | // https://code.google.com/p/nativeclient/issues/detail?id=3899 |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2048 | if (llvm::isa<X86OperandMem>(Src1)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 2049 | Src1 = legalizeToReg(Src1); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2050 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2051 | case InstArithmetic::_num: |
| 2052 | llvm_unreachable("Unknown arithmetic operator"); |
| 2053 | break; |
| 2054 | case InstArithmetic::Add: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2055 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2056 | _movp(T, Src0); |
| 2057 | _padd(T, Src1); |
| 2058 | _movp(Dest, T); |
| 2059 | } break; |
| 2060 | case InstArithmetic::And: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2061 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2062 | _movp(T, Src0); |
| 2063 | _pand(T, Src1); |
| 2064 | _movp(Dest, T); |
| 2065 | } break; |
| 2066 | case InstArithmetic::Or: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2067 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2068 | _movp(T, Src0); |
| 2069 | _por(T, Src1); |
| 2070 | _movp(Dest, T); |
| 2071 | } break; |
| 2072 | case InstArithmetic::Xor: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2073 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2074 | _movp(T, Src0); |
| 2075 | _pxor(T, Src1); |
| 2076 | _movp(Dest, T); |
| 2077 | } break; |
| 2078 | case InstArithmetic::Sub: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2079 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2080 | _movp(T, Src0); |
| 2081 | _psub(T, Src1); |
| 2082 | _movp(Dest, T); |
| 2083 | } break; |
| 2084 | case InstArithmetic::Mul: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2085 | bool TypesAreValidForPmull = Ty == IceType_v4i32 || Ty == IceType_v8i16; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2086 | bool InstructionSetIsValidForPmull = |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2087 | Ty == IceType_v8i16 || InstructionSet >= Traits::SSE4_1; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2088 | if (TypesAreValidForPmull && InstructionSetIsValidForPmull) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2089 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2090 | _movp(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2091 | _pmull(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2092 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2093 | } else if (Ty == IceType_v4i32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2094 | // Lowering sequence: |
| 2095 | // Note: The mask arguments have index 0 on the left. |
| 2096 | // |
| 2097 | // movups T1, Src0 |
| 2098 | // pshufd T2, Src0, {1,0,3,0} |
| 2099 | // pshufd T3, Src1, {1,0,3,0} |
| 2100 | // # T1 = {Src0[0] * Src1[0], Src0[2] * Src1[2]} |
| 2101 | // pmuludq T1, Src1 |
| 2102 | // # T2 = {Src0[1] * Src1[1], Src0[3] * Src1[3]} |
| 2103 | // pmuludq T2, T3 |
| 2104 | // # T1 = {lo(T1[0]), lo(T1[2]), lo(T2[0]), lo(T2[2])} |
| 2105 | // shufps T1, T2, {0,2,0,2} |
| 2106 | // pshufd T4, T1, {0,2,1,3} |
| 2107 | // movups Dest, T4 |
| 2108 | |
| 2109 | // Mask that directs pshufd to create a vector with entries |
| 2110 | // Src[1, 0, 3, 0] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2111 | constexpr unsigned Constant1030 = 0x31; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2112 | Constant *Mask1030 = Ctx->getConstantInt32(Constant1030); |
| 2113 | // Mask that directs shufps to create a vector with entries |
| 2114 | // Dest[0, 2], Src[0, 2] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2115 | constexpr unsigned Mask0202 = 0x88; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2116 | // Mask that directs pshufd to create a vector with entries |
| 2117 | // Src[0, 2, 1, 3] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2118 | constexpr unsigned Mask0213 = 0xd8; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2119 | Variable *T1 = makeReg(IceType_v4i32); |
| 2120 | Variable *T2 = makeReg(IceType_v4i32); |
| 2121 | Variable *T3 = makeReg(IceType_v4i32); |
| 2122 | Variable *T4 = makeReg(IceType_v4i32); |
| 2123 | _movp(T1, Src0); |
| 2124 | _pshufd(T2, Src0, Mask1030); |
| 2125 | _pshufd(T3, Src1, Mask1030); |
| 2126 | _pmuludq(T1, Src1); |
| 2127 | _pmuludq(T2, T3); |
| 2128 | _shufps(T1, T2, Ctx->getConstantInt32(Mask0202)); |
| 2129 | _pshufd(T4, T1, Ctx->getConstantInt32(Mask0213)); |
| 2130 | _movp(Dest, T4); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2131 | } else if (Ty == IceType_v16i8) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2132 | llvm::report_fatal_error("Scalarized operation was expected"); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2133 | } else { |
| 2134 | llvm::report_fatal_error("Invalid vector multiply type"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } break; |
| 2137 | case InstArithmetic::Shl: |
| 2138 | case InstArithmetic::Lshr: |
| 2139 | case InstArithmetic::Ashr: |
| 2140 | case InstArithmetic::Udiv: |
| 2141 | case InstArithmetic::Urem: |
| 2142 | case InstArithmetic::Sdiv: |
| 2143 | case InstArithmetic::Srem: |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2144 | llvm::report_fatal_error("Scalarized operation was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2145 | break; |
| 2146 | case InstArithmetic::Fadd: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2147 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2148 | _movp(T, Src0); |
| 2149 | _addps(T, Src1); |
| 2150 | _movp(Dest, T); |
| 2151 | } break; |
| 2152 | case InstArithmetic::Fsub: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2153 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2154 | _movp(T, Src0); |
| 2155 | _subps(T, Src1); |
| 2156 | _movp(Dest, T); |
| 2157 | } break; |
| 2158 | case InstArithmetic::Fmul: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2159 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2160 | _movp(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2161 | _mulps(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2162 | _movp(Dest, T); |
| 2163 | } break; |
| 2164 | case InstArithmetic::Fdiv: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2165 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2166 | _movp(T, Src0); |
| 2167 | _divps(T, Src1); |
| 2168 | _movp(Dest, T); |
| 2169 | } break; |
| 2170 | case InstArithmetic::Frem: |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2171 | llvm::report_fatal_error("Scalarized operation was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2172 | break; |
| 2173 | } |
| 2174 | return; |
| 2175 | } |
| 2176 | Variable *T_edx = nullptr; |
| 2177 | Variable *T = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2178 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2179 | case InstArithmetic::_num: |
| 2180 | llvm_unreachable("Unknown arithmetic operator"); |
| 2181 | break; |
| 2182 | case InstArithmetic::Add: |
| 2183 | _mov(T, Src0); |
| 2184 | _add(T, Src1); |
| 2185 | _mov(Dest, T); |
| 2186 | break; |
| 2187 | case InstArithmetic::And: |
| 2188 | _mov(T, Src0); |
| 2189 | _and(T, Src1); |
| 2190 | _mov(Dest, T); |
| 2191 | break; |
| 2192 | case InstArithmetic::Or: |
| 2193 | _mov(T, Src0); |
| 2194 | _or(T, Src1); |
| 2195 | _mov(Dest, T); |
| 2196 | break; |
| 2197 | case InstArithmetic::Xor: |
| 2198 | _mov(T, Src0); |
| 2199 | _xor(T, Src1); |
| 2200 | _mov(Dest, T); |
| 2201 | break; |
| 2202 | case InstArithmetic::Sub: |
| 2203 | _mov(T, Src0); |
| 2204 | _sub(T, Src1); |
| 2205 | _mov(Dest, T); |
| 2206 | break; |
| 2207 | case InstArithmetic::Mul: |
| 2208 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
| 2209 | if (optimizeScalarMul(Dest, Src0, C->getValue())) |
| 2210 | return; |
| 2211 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2212 | // The 8-bit version of imul only allows the form "imul r/m8" where T must |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2213 | // be in al. |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2214 | if (isByteSizedArithType(Ty)) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2215 | _mov(T, Src0, Traits::RegisterSet::Reg_al); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2216 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2217 | _imul(T, Src0 == Src1 ? T : Src1); |
| 2218 | _mov(Dest, T); |
| 2219 | } else if (auto *ImmConst = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2220 | T = makeReg(Ty); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2221 | _imul_imm(T, Src0, ImmConst); |
| 2222 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2223 | } else { |
| 2224 | _mov(T, Src0); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2225 | _imul(T, Src0 == Src1 ? T : Src1); |
| 2226 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2227 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2228 | break; |
| 2229 | case InstArithmetic::Shl: |
| 2230 | _mov(T, Src0); |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 2231 | if (!llvm::isa<ConstantInteger32>(Src1) && |
| 2232 | !llvm::isa<ConstantInteger64>(Src1)) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2233 | Src1 = copyToReg8(Src1, Traits::RegisterSet::Reg_cl); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2234 | _shl(T, Src1); |
| 2235 | _mov(Dest, T); |
| 2236 | break; |
| 2237 | case InstArithmetic::Lshr: |
| 2238 | _mov(T, Src0); |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 2239 | if (!llvm::isa<ConstantInteger32>(Src1) && |
| 2240 | !llvm::isa<ConstantInteger64>(Src1)) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2241 | Src1 = copyToReg8(Src1, Traits::RegisterSet::Reg_cl); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2242 | _shr(T, Src1); |
| 2243 | _mov(Dest, T); |
| 2244 | break; |
| 2245 | case InstArithmetic::Ashr: |
| 2246 | _mov(T, Src0); |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 2247 | if (!llvm::isa<ConstantInteger32>(Src1) && |
| 2248 | !llvm::isa<ConstantInteger64>(Src1)) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2249 | Src1 = copyToReg8(Src1, Traits::RegisterSet::Reg_cl); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2250 | _sar(T, Src1); |
| 2251 | _mov(Dest, T); |
| 2252 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2253 | case InstArithmetic::Udiv: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2254 | // div and idiv are the few arithmetic operators that do not allow |
| 2255 | // immediates as the operand. |
| 2256 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2257 | RegNumT Eax; |
| 2258 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2259 | switch (Ty) { |
| 2260 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2261 | llvm::report_fatal_error("Bad type for udiv"); |
| 2262 | case IceType_i64: |
| 2263 | Eax = Traits::getRaxOrDie(); |
| 2264 | Edx = Traits::getRdxOrDie(); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2265 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2266 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2267 | Eax = Traits::RegisterSet::Reg_eax; |
| 2268 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2269 | break; |
| 2270 | case IceType_i16: |
| 2271 | Eax = Traits::RegisterSet::Reg_ax; |
| 2272 | Edx = Traits::RegisterSet::Reg_dx; |
| 2273 | break; |
| 2274 | case IceType_i8: |
| 2275 | Eax = Traits::RegisterSet::Reg_al; |
| 2276 | Edx = Traits::RegisterSet::Reg_ah; |
| 2277 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2278 | } |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2279 | T_edx = makeReg(Ty, Edx); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2280 | _mov(T, Src0, Eax); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2281 | _mov(T_edx, Ctx->getConstantZero(Ty)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2282 | _div(T, Src1, T_edx); |
| 2283 | _mov(Dest, T); |
| 2284 | } break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2285 | case InstArithmetic::Sdiv: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2286 | // TODO(stichnot): Enable this after doing better performance and cross |
| 2287 | // testing. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 2288 | if (false && Func->getOptLevel() >= Opt_1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2289 | // Optimize division by constant power of 2, but not for Om1 or O0, just |
| 2290 | // to keep things simple there. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2291 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2292 | const int32_t Divisor = C->getValue(); |
| 2293 | const uint32_t UDivisor = Divisor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2294 | if (Divisor > 0 && llvm::isPowerOf2_32(UDivisor)) { |
| 2295 | uint32_t LogDiv = llvm::Log2_32(UDivisor); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2296 | // LLVM does the following for dest=src/(1<<log): |
| 2297 | // t=src |
| 2298 | // sar t,typewidth-1 // -1 if src is negative, 0 if not |
| 2299 | // shr t,typewidth-log |
| 2300 | // add t,src |
| 2301 | // sar t,log |
| 2302 | // dest=t |
| 2303 | uint32_t TypeWidth = Traits::X86_CHAR_BIT * typeWidthInBytes(Ty); |
| 2304 | _mov(T, Src0); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2305 | // If for some reason we are dividing by 1, just treat it like an |
| 2306 | // assignment. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2307 | if (LogDiv > 0) { |
| 2308 | // The initial sar is unnecessary when dividing by 2. |
| 2309 | if (LogDiv > 1) |
| 2310 | _sar(T, Ctx->getConstantInt(Ty, TypeWidth - 1)); |
| 2311 | _shr(T, Ctx->getConstantInt(Ty, TypeWidth - LogDiv)); |
| 2312 | _add(T, Src0); |
| 2313 | _sar(T, Ctx->getConstantInt(Ty, LogDiv)); |
| 2314 | } |
| 2315 | _mov(Dest, T); |
| 2316 | return; |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2321 | switch (Ty) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2322 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2323 | llvm::report_fatal_error("Bad type for sdiv"); |
| 2324 | case IceType_i64: |
| 2325 | T_edx = makeReg(Ty, Traits::getRdxOrDie()); |
| 2326 | _mov(T, Src0, Traits::getRaxOrDie()); |
| 2327 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2328 | case IceType_i32: |
| 2329 | T_edx = makeReg(Ty, Traits::RegisterSet::Reg_edx); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 2330 | _mov(T, Src0, Traits::RegisterSet::Reg_eax); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2331 | break; |
| 2332 | case IceType_i16: |
| 2333 | T_edx = makeReg(Ty, Traits::RegisterSet::Reg_dx); |
| 2334 | _mov(T, Src0, Traits::RegisterSet::Reg_ax); |
| 2335 | break; |
| 2336 | case IceType_i8: |
| 2337 | T_edx = makeReg(IceType_i16, Traits::RegisterSet::Reg_ax); |
| 2338 | _mov(T, Src0, Traits::RegisterSet::Reg_al); |
| 2339 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2340 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2341 | _cbwdq(T_edx, T); |
| 2342 | _idiv(T, Src1, T_edx); |
| 2343 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2344 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2345 | case InstArithmetic::Urem: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2346 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2347 | RegNumT Eax; |
| 2348 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2349 | switch (Ty) { |
| 2350 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2351 | llvm::report_fatal_error("Bad type for urem"); |
| 2352 | case IceType_i64: |
| 2353 | Eax = Traits::getRaxOrDie(); |
| 2354 | Edx = Traits::getRdxOrDie(); |
| 2355 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2356 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2357 | Eax = Traits::RegisterSet::Reg_eax; |
| 2358 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2359 | break; |
| 2360 | case IceType_i16: |
| 2361 | Eax = Traits::RegisterSet::Reg_ax; |
| 2362 | Edx = Traits::RegisterSet::Reg_dx; |
| 2363 | break; |
| 2364 | case IceType_i8: |
| 2365 | Eax = Traits::RegisterSet::Reg_al; |
| 2366 | Edx = Traits::RegisterSet::Reg_ah; |
| 2367 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2368 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2369 | T_edx = makeReg(Ty, Edx); |
| 2370 | _mov(T_edx, Ctx->getConstantZero(Ty)); |
| 2371 | _mov(T, Src0, Eax); |
| 2372 | _div(T_edx, Src1, T); |
Jim Stichnoth | 2655d96 | 2016-04-21 05:38:15 -0700 | [diff] [blame] | 2373 | if (Ty == IceType_i8) { |
| 2374 | // Register ah must be moved into one of {al,bl,cl,dl} before it can be |
| 2375 | // moved into a general 8-bit register. |
| 2376 | auto *T_AhRcvr = makeReg(Ty); |
| 2377 | T_AhRcvr->setRegClass(RCX86_IsAhRcvr); |
| 2378 | _mov(T_AhRcvr, T_edx); |
| 2379 | T_edx = T_AhRcvr; |
| 2380 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2381 | _mov(Dest, T_edx); |
| 2382 | } break; |
| 2383 | case InstArithmetic::Srem: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2384 | // TODO(stichnot): Enable this after doing better performance and cross |
| 2385 | // testing. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 2386 | if (false && Func->getOptLevel() >= Opt_1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2387 | // Optimize mod by constant power of 2, but not for Om1 or O0, just to |
| 2388 | // keep things simple there. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2389 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2390 | const int32_t Divisor = C->getValue(); |
| 2391 | const uint32_t UDivisor = Divisor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2392 | if (Divisor > 0 && llvm::isPowerOf2_32(UDivisor)) { |
| 2393 | uint32_t LogDiv = llvm::Log2_32(UDivisor); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2394 | // LLVM does the following for dest=src%(1<<log): |
| 2395 | // t=src |
| 2396 | // sar t,typewidth-1 // -1 if src is negative, 0 if not |
| 2397 | // shr t,typewidth-log |
| 2398 | // add t,src |
| 2399 | // and t, -(1<<log) |
| 2400 | // sub t,src |
| 2401 | // neg t |
| 2402 | // dest=t |
| 2403 | uint32_t TypeWidth = Traits::X86_CHAR_BIT * typeWidthInBytes(Ty); |
| 2404 | // If for some reason we are dividing by 1, just assign 0. |
| 2405 | if (LogDiv == 0) { |
| 2406 | _mov(Dest, Ctx->getConstantZero(Ty)); |
| 2407 | return; |
| 2408 | } |
| 2409 | _mov(T, Src0); |
| 2410 | // The initial sar is unnecessary when dividing by 2. |
| 2411 | if (LogDiv > 1) |
| 2412 | _sar(T, Ctx->getConstantInt(Ty, TypeWidth - 1)); |
| 2413 | _shr(T, Ctx->getConstantInt(Ty, TypeWidth - LogDiv)); |
| 2414 | _add(T, Src0); |
| 2415 | _and(T, Ctx->getConstantInt(Ty, -(1 << LogDiv))); |
| 2416 | _sub(T, Src0); |
| 2417 | _neg(T); |
| 2418 | _mov(Dest, T); |
| 2419 | return; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2424 | RegNumT Eax; |
| 2425 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2426 | switch (Ty) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2427 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2428 | llvm::report_fatal_error("Bad type for srem"); |
| 2429 | case IceType_i64: |
| 2430 | Eax = Traits::getRaxOrDie(); |
| 2431 | Edx = Traits::getRdxOrDie(); |
| 2432 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2433 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2434 | Eax = Traits::RegisterSet::Reg_eax; |
| 2435 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2436 | break; |
| 2437 | case IceType_i16: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2438 | Eax = Traits::RegisterSet::Reg_ax; |
| 2439 | Edx = Traits::RegisterSet::Reg_dx; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2440 | break; |
| 2441 | case IceType_i8: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2442 | Eax = Traits::RegisterSet::Reg_al; |
| 2443 | Edx = Traits::RegisterSet::Reg_ah; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2444 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2445 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2446 | T_edx = makeReg(Ty, Edx); |
| 2447 | _mov(T, Src0, Eax); |
| 2448 | _cbwdq(T_edx, T); |
| 2449 | _idiv(T_edx, Src1, T); |
Jim Stichnoth | 2655d96 | 2016-04-21 05:38:15 -0700 | [diff] [blame] | 2450 | if (Ty == IceType_i8) { |
| 2451 | // Register ah must be moved into one of {al,bl,cl,dl} before it can be |
| 2452 | // moved into a general 8-bit register. |
| 2453 | auto *T_AhRcvr = makeReg(Ty); |
| 2454 | T_AhRcvr->setRegClass(RCX86_IsAhRcvr); |
| 2455 | _mov(T_AhRcvr, T_edx); |
| 2456 | T_edx = T_AhRcvr; |
| 2457 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2458 | _mov(Dest, T_edx); |
| 2459 | } break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2460 | case InstArithmetic::Fadd: |
| 2461 | _mov(T, Src0); |
| 2462 | _addss(T, Src1); |
| 2463 | _mov(Dest, T); |
| 2464 | break; |
| 2465 | case InstArithmetic::Fsub: |
| 2466 | _mov(T, Src0); |
| 2467 | _subss(T, Src1); |
| 2468 | _mov(Dest, T); |
| 2469 | break; |
| 2470 | case InstArithmetic::Fmul: |
| 2471 | _mov(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2472 | _mulss(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2473 | _mov(Dest, T); |
| 2474 | break; |
| 2475 | case InstArithmetic::Fdiv: |
| 2476 | _mov(T, Src0); |
| 2477 | _divss(T, Src1); |
| 2478 | _mov(Dest, T); |
| 2479 | break; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2480 | case InstArithmetic::Frem: |
| 2481 | llvm::report_fatal_error("Helper call was expected"); |
| 2482 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2486 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2487 | void TargetX86Base<TraitsType>::lowerAssign(const InstAssign *Instr) { |
| 2488 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 2489 | if (Dest->isRematerializable()) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 2490 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 2491 | return; |
| 2492 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2493 | Operand *Src = Instr->getSrc(0); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2494 | assert(Dest->getType() == Src->getType()); |
| 2495 | lowerMove(Dest, Src, false); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2496 | } |
| 2497 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2498 | template <typename TraitsType> |
| 2499 | void TargetX86Base<TraitsType>::lowerBr(const InstBr *Br) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2500 | if (Br->isUnconditional()) { |
| 2501 | _br(Br->getTargetUnconditional()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2502 | return; |
| 2503 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2504 | Operand *Cond = Br->getCondition(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2505 | |
| 2506 | // Handle folding opportunities. |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2507 | if (const Inst *Producer = FoldingInfo.getProducerFor(Cond)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2508 | assert(Producer->isDeleted()); |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2509 | switch (BoolFolding<Traits>::getProducerKind(Producer)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2510 | default: |
| 2511 | break; |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2512 | case BoolFolding<Traits>::PK_Icmp32: |
| 2513 | case BoolFolding<Traits>::PK_Icmp64: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2514 | lowerIcmpAndConsumer(llvm::cast<InstIcmp>(Producer), Br); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2515 | return; |
| 2516 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2517 | case BoolFolding<Traits>::PK_Fcmp: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2518 | lowerFcmpAndConsumer(llvm::cast<InstFcmp>(Producer), Br); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 2519 | return; |
| 2520 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2521 | case BoolFolding<Traits>::PK_Arith: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2522 | lowerArithAndConsumer(llvm::cast<InstArithmetic>(Producer), Br); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 2523 | return; |
| 2524 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2527 | Operand *Src0 = legalize(Cond, Legal_Reg | Legal_Mem); |
| 2528 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 2529 | _cmp(Src0, Zero); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2530 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2533 | // constexprMax returns a (constexpr) max(S0, S1), and it is used for defining |
| 2534 | // OperandList in lowerCall. std::max() is supposed to work, but it doesn't. |
| 2535 | inline constexpr SizeT constexprMax(SizeT S0, SizeT S1) { |
| 2536 | return S0 < S1 ? S1 : S0; |
| 2537 | } |
| 2538 | |
| 2539 | template <typename TraitsType> |
| 2540 | void TargetX86Base<TraitsType>::lowerCall(const InstCall *Instr) { |
| 2541 | // Common x86 calling convention lowering: |
| 2542 | // |
| 2543 | // * At the point before the call, the stack must be aligned to 16 bytes. |
| 2544 | // |
| 2545 | // * Non-register arguments are pushed onto the stack in right-to-left order, |
| 2546 | // such that the left-most argument ends up on the top of the stack at the |
| 2547 | // lowest memory address. |
| 2548 | // |
| 2549 | // * Stack arguments of vector type are aligned to start at the next highest |
| 2550 | // multiple of 16 bytes. Other stack arguments are aligned to the next word |
| 2551 | // size boundary (4 or 8 bytes, respectively). |
| 2552 | NeedsStackAlignment = true; |
| 2553 | |
| 2554 | using OperandList = |
| 2555 | llvm::SmallVector<Operand *, constexprMax(Traits::X86_MAX_XMM_ARGS, |
| 2556 | Traits::X86_MAX_GPR_ARGS)>; |
| 2557 | OperandList XmmArgs; |
| 2558 | CfgVector<std::pair<const Type, Operand *>> GprArgs; |
| 2559 | OperandList StackArgs, StackArgLocations; |
| 2560 | uint32_t ParameterAreaSizeBytes = 0; |
| 2561 | |
| 2562 | // Classify each argument operand according to the location where the argument |
| 2563 | // is passed. |
| 2564 | for (SizeT i = 0, NumArgs = Instr->getNumArgs(); i < NumArgs; ++i) { |
| 2565 | Operand *Arg = Instr->getArg(i); |
| 2566 | const Type Ty = Arg->getType(); |
| 2567 | // The PNaCl ABI requires the width of arguments to be at least 32 bits. |
| 2568 | assert(typeWidthInBytes(Ty) >= 4); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2569 | if (isVectorType(Ty) && |
| 2570 | Traits::getRegisterForXmmArgNum(XmmArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2571 | XmmArgs.push_back(Arg); |
| 2572 | } else if (isScalarFloatingType(Ty) && Traits::X86_PASS_SCALAR_FP_IN_XMM && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2573 | Traits::getRegisterForXmmArgNum(XmmArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2574 | XmmArgs.push_back(Arg); |
| 2575 | } else if (isScalarIntegerType(Ty) && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2576 | Traits::getRegisterForGprArgNum(Ty, GprArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2577 | GprArgs.emplace_back(Ty, Arg); |
| 2578 | } else { |
| 2579 | // Place on stack. |
| 2580 | StackArgs.push_back(Arg); |
| 2581 | if (isVectorType(Arg->getType())) { |
| 2582 | ParameterAreaSizeBytes = |
| 2583 | Traits::applyStackAlignment(ParameterAreaSizeBytes); |
| 2584 | } |
| 2585 | Variable *esp = getPhysicalRegister(getStackReg(), Traits::WordType); |
| 2586 | Constant *Loc = Ctx->getConstantInt32(ParameterAreaSizeBytes); |
| 2587 | StackArgLocations.push_back( |
| 2588 | Traits::X86OperandMem::create(Func, Ty, esp, Loc)); |
| 2589 | ParameterAreaSizeBytes += typeWidthInBytesOnStack(Arg->getType()); |
| 2590 | } |
| 2591 | } |
| 2592 | // Ensure there is enough space for the fstp/movs for floating returns. |
| 2593 | Variable *Dest = Instr->getDest(); |
| 2594 | const Type DestTy = Dest ? Dest->getType() : IceType_void; |
John Porto | 4ab4fbe | 2016-01-20 13:44:30 -0800 | [diff] [blame] | 2595 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2596 | if (isScalarFloatingType(DestTy)) { |
| 2597 | ParameterAreaSizeBytes = |
| 2598 | std::max(static_cast<size_t>(ParameterAreaSizeBytes), |
| 2599 | typeWidthInBytesOnStack(DestTy)); |
| 2600 | } |
| 2601 | } |
| 2602 | // Adjust the parameter area so that the stack is aligned. It is assumed that |
| 2603 | // the stack is already aligned at the start of the calling sequence. |
| 2604 | ParameterAreaSizeBytes = Traits::applyStackAlignment(ParameterAreaSizeBytes); |
| 2605 | assert(ParameterAreaSizeBytes <= maxOutArgsSizeBytes()); |
| 2606 | // Copy arguments that are passed on the stack to the appropriate stack |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2607 | // locations. We make sure legalize() is called on each argument at this |
| 2608 | // point, to allow availabilityGet() to work. |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2609 | for (SizeT i = 0, NumStackArgs = StackArgs.size(); i < NumStackArgs; ++i) { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2610 | lowerStore( |
| 2611 | InstStore::create(Func, legalize(StackArgs[i]), StackArgLocations[i])); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2612 | } |
| 2613 | // Copy arguments to be passed in registers to the appropriate registers. |
| 2614 | for (SizeT i = 0, NumXmmArgs = XmmArgs.size(); i < NumXmmArgs; ++i) { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2615 | XmmArgs[i] = |
| 2616 | legalizeToReg(legalize(XmmArgs[i]), Traits::getRegisterForXmmArgNum(i)); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2617 | } |
| 2618 | // Materialize moves for arguments passed in GPRs. |
| 2619 | for (SizeT i = 0, NumGprArgs = GprArgs.size(); i < NumGprArgs; ++i) { |
| 2620 | const Type SignatureTy = GprArgs[i].first; |
Jim Stichnoth | e450656 | 2016-06-20 06:44:07 -0700 | [diff] [blame] | 2621 | Operand *Arg = |
| 2622 | legalize(GprArgs[i].second, Legal_Default | Legal_Rematerializable); |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2623 | GprArgs[i].second = |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2624 | legalizeToReg(Arg, Traits::getRegisterForGprArgNum(Arg->getType(), i)); |
| 2625 | assert(SignatureTy == IceType_i64 || SignatureTy == IceType_i32); |
| 2626 | assert(SignatureTy == Arg->getType()); |
| 2627 | (void)SignatureTy; |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2628 | } |
| 2629 | // Generate a FakeUse of register arguments so that they do not get dead code |
| 2630 | // eliminated as a result of the FakeKill of scratch registers after the call. |
| 2631 | // These need to be right before the call instruction. |
| 2632 | for (auto *Arg : XmmArgs) { |
| 2633 | Context.insert<InstFakeUse>(llvm::cast<Variable>(Arg)); |
| 2634 | } |
| 2635 | for (auto &ArgPair : GprArgs) { |
| 2636 | Context.insert<InstFakeUse>(llvm::cast<Variable>(ArgPair.second)); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2637 | } |
| 2638 | // Generate the call instruction. Assign its result to a temporary with high |
| 2639 | // register allocation weight. |
| 2640 | // ReturnReg doubles as ReturnRegLo as necessary. |
| 2641 | Variable *ReturnReg = nullptr; |
| 2642 | Variable *ReturnRegHi = nullptr; |
| 2643 | if (Dest) { |
| 2644 | switch (DestTy) { |
| 2645 | case IceType_NUM: |
| 2646 | case IceType_void: |
| 2647 | case IceType_i1: |
| 2648 | case IceType_i8: |
| 2649 | case IceType_i16: |
| 2650 | llvm::report_fatal_error("Invalid Call dest type"); |
| 2651 | break; |
| 2652 | case IceType_i32: |
| 2653 | ReturnReg = makeReg(DestTy, Traits::RegisterSet::Reg_eax); |
| 2654 | break; |
| 2655 | case IceType_i64: |
| 2656 | if (Traits::Is64Bit) { |
Jim Stichnoth | ee1aae8 | 2016-02-02 09:29:21 -0800 | [diff] [blame] | 2657 | ReturnReg = makeReg(IceType_i64, Traits::getRaxOrDie()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2658 | } else { |
| 2659 | ReturnReg = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 2660 | ReturnRegHi = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 2661 | } |
| 2662 | break; |
| 2663 | case IceType_f32: |
| 2664 | case IceType_f64: |
| 2665 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2666 | // Leave ReturnReg==ReturnRegHi==nullptr, and capture the result with |
| 2667 | // the fstp instruction. |
| 2668 | break; |
| 2669 | } |
| 2670 | // Fallthrough intended. |
| 2671 | case IceType_v4i1: |
| 2672 | case IceType_v8i1: |
| 2673 | case IceType_v16i1: |
| 2674 | case IceType_v16i8: |
| 2675 | case IceType_v8i16: |
| 2676 | case IceType_v4i32: |
| 2677 | case IceType_v4f32: |
| 2678 | ReturnReg = makeReg(DestTy, Traits::RegisterSet::Reg_xmm0); |
| 2679 | break; |
| 2680 | } |
| 2681 | } |
| 2682 | // Emit the call to the function. |
| 2683 | Operand *CallTarget = |
| 2684 | legalize(Instr->getCallTarget(), Legal_Reg | Legal_Imm | Legal_AddrAbs); |
| 2685 | Inst *NewCall = emitCallToTarget(CallTarget, ReturnReg); |
| 2686 | // Keep the upper return register live on 32-bit platform. |
| 2687 | if (ReturnRegHi) |
| 2688 | Context.insert<InstFakeDef>(ReturnRegHi); |
| 2689 | // Mark the call as killing all the caller-save registers. |
| 2690 | Context.insert<InstFakeKill>(NewCall); |
| 2691 | // Handle x86-32 floating point returns. |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2692 | if (Dest != nullptr && isScalarFloatingType(DestTy) && |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2693 | !Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2694 | // Special treatment for an FP function which returns its result in st(0). |
| 2695 | // If Dest ends up being a physical xmm register, the fstp emit code will |
| 2696 | // route st(0) through the space reserved in the function argument area |
| 2697 | // we allocated. |
| 2698 | _fstp(Dest); |
| 2699 | // Create a fake use of Dest in case it actually isn't used, because st(0) |
| 2700 | // still needs to be popped. |
| 2701 | Context.insert<InstFakeUse>(Dest); |
| 2702 | } |
| 2703 | // Generate a FakeUse to keep the call live if necessary. |
| 2704 | if (Instr->hasSideEffects() && ReturnReg) { |
| 2705 | Context.insert<InstFakeUse>(ReturnReg); |
| 2706 | } |
| 2707 | // Process the return value, if any. |
| 2708 | if (Dest == nullptr) |
| 2709 | return; |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2710 | // Assign the result of the call to Dest. Route it through a temporary so |
| 2711 | // that the local register availability peephole can be subsequently used. |
| 2712 | Variable *Tmp = nullptr; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2713 | if (isVectorType(DestTy)) { |
| 2714 | assert(ReturnReg && "Vector type requires a return register"); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2715 | Tmp = makeReg(DestTy); |
| 2716 | _movp(Tmp, ReturnReg); |
| 2717 | _movp(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2718 | } else if (isScalarFloatingType(DestTy)) { |
| 2719 | if (Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2720 | assert(ReturnReg && "FP type requires a return register"); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2721 | _mov(Tmp, ReturnReg); |
| 2722 | _mov(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2723 | } |
| 2724 | } else { |
| 2725 | assert(isScalarIntegerType(DestTy)); |
| 2726 | assert(ReturnReg && "Integer type requires a return register"); |
| 2727 | if (DestTy == IceType_i64 && !Traits::Is64Bit) { |
| 2728 | assert(ReturnRegHi && "64-bit type requires two return registers"); |
| 2729 | auto *Dest64On32 = llvm::cast<Variable64On32>(Dest); |
| 2730 | Variable *DestLo = Dest64On32->getLo(); |
| 2731 | Variable *DestHi = Dest64On32->getHi(); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2732 | _mov(Tmp, ReturnReg); |
| 2733 | _mov(DestLo, Tmp); |
| 2734 | Variable *TmpHi = nullptr; |
| 2735 | _mov(TmpHi, ReturnRegHi); |
| 2736 | _mov(DestHi, TmpHi); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2737 | } else { |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2738 | _mov(Tmp, ReturnReg); |
| 2739 | _mov(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2744 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2745 | void TargetX86Base<TraitsType>::lowerCast(const InstCast *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2746 | // a = cast(b) ==> t=cast(b); a=t; (link t->b, link a->t, no overlap) |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2747 | InstCast::OpKind CastKind = Instr->getCastKind(); |
| 2748 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2749 | Type DestTy = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2750 | switch (CastKind) { |
| 2751 | default: |
| 2752 | Func->setError("Cast type not supported"); |
| 2753 | return; |
| 2754 | case InstCast::Sext: { |
| 2755 | // Src0RM is the source operand legalized to physical register or memory, |
| 2756 | // but not immediate, since the relevant x86 native instructions don't |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2757 | // allow an immediate operand. If the operand is an immediate, we could |
| 2758 | // consider computing the strength-reduced result at translation time, but |
| 2759 | // we're unlikely to see something like that in the bitcode that the |
| 2760 | // optimizer wouldn't have already taken care of. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2761 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2762 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2763 | if (DestTy == IceType_v16i8) { |
| 2764 | // onemask = materialize(1,1,...); dst = (src & onemask) > 0 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2765 | Variable *OneMask = makeVectorOfOnes(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2766 | Variable *T = makeReg(DestTy); |
| 2767 | _movp(T, Src0RM); |
| 2768 | _pand(T, OneMask); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2769 | Variable *Zeros = makeVectorOfZeros(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2770 | _pcmpgt(T, Zeros); |
| 2771 | _movp(Dest, T); |
| 2772 | } else { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 2773 | /// width = width(elty) - 1; dest = (src << width) >> width |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2774 | SizeT ShiftAmount = |
| 2775 | Traits::X86_CHAR_BIT * typeWidthInBytes(typeElementType(DestTy)) - |
| 2776 | 1; |
| 2777 | Constant *ShiftConstant = Ctx->getConstantInt8(ShiftAmount); |
| 2778 | Variable *T = makeReg(DestTy); |
| 2779 | _movp(T, Src0RM); |
| 2780 | _psll(T, ShiftConstant); |
| 2781 | _psra(T, ShiftConstant); |
| 2782 | _movp(Dest, T); |
| 2783 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2784 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2785 | // t1=movsx src; t2=t1; t2=sar t2, 31; dst.lo=t1; dst.hi=t2 |
| 2786 | Constant *Shift = Ctx->getConstantInt32(31); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 2787 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 2788 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2789 | Variable *T_Lo = makeReg(DestLo->getType()); |
| 2790 | if (Src0RM->getType() == IceType_i32) { |
| 2791 | _mov(T_Lo, Src0RM); |
| 2792 | } else if (Src0RM->getType() == IceType_i1) { |
| 2793 | _movzx(T_Lo, Src0RM); |
| 2794 | _shl(T_Lo, Shift); |
| 2795 | _sar(T_Lo, Shift); |
| 2796 | } else { |
| 2797 | _movsx(T_Lo, Src0RM); |
| 2798 | } |
| 2799 | _mov(DestLo, T_Lo); |
| 2800 | Variable *T_Hi = nullptr; |
| 2801 | _mov(T_Hi, T_Lo); |
| 2802 | if (Src0RM->getType() != IceType_i1) |
| 2803 | // For i1, the sar instruction is already done above. |
| 2804 | _sar(T_Hi, Shift); |
| 2805 | _mov(DestHi, T_Hi); |
| 2806 | } else if (Src0RM->getType() == IceType_i1) { |
| 2807 | // t1 = src |
| 2808 | // shl t1, dst_bitwidth - 1 |
| 2809 | // sar t1, dst_bitwidth - 1 |
| 2810 | // dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2811 | size_t DestBits = Traits::X86_CHAR_BIT * typeWidthInBytes(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2812 | Constant *ShiftAmount = Ctx->getConstantInt32(DestBits - 1); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2813 | Variable *T = makeReg(DestTy); |
| 2814 | if (typeWidthInBytes(DestTy) <= typeWidthInBytes(Src0RM->getType())) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2815 | _mov(T, Src0RM); |
| 2816 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2817 | // Widen the source using movsx or movzx. (It doesn't matter which one, |
| 2818 | // since the following shl/sar overwrite the bits.) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2819 | _movzx(T, Src0RM); |
| 2820 | } |
| 2821 | _shl(T, ShiftAmount); |
| 2822 | _sar(T, ShiftAmount); |
| 2823 | _mov(Dest, T); |
| 2824 | } else { |
| 2825 | // t1 = movsx src; dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2826 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2827 | _movsx(T, Src0RM); |
| 2828 | _mov(Dest, T); |
| 2829 | } |
| 2830 | break; |
| 2831 | } |
| 2832 | case InstCast::Zext: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2833 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2834 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2835 | // onemask = materialize(1,1,...); dest = onemask & src |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2836 | Variable *OneMask = makeVectorOfOnes(DestTy); |
| 2837 | Variable *T = makeReg(DestTy); |
| 2838 | _movp(T, Src0RM); |
| 2839 | _pand(T, OneMask); |
| 2840 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2841 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2842 | // t1=movzx src; dst.lo=t1; dst.hi=0 |
| 2843 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 2844 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 2845 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2846 | Variable *Tmp = makeReg(DestLo->getType()); |
| 2847 | if (Src0RM->getType() == IceType_i32) { |
| 2848 | _mov(Tmp, Src0RM); |
| 2849 | } else { |
| 2850 | _movzx(Tmp, Src0RM); |
| 2851 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2852 | _mov(DestLo, Tmp); |
| 2853 | _mov(DestHi, Zero); |
| 2854 | } else if (Src0RM->getType() == IceType_i1) { |
Jim Stichnoth | 485d077 | 2015-10-09 06:52:19 -0700 | [diff] [blame] | 2855 | // t = Src0RM; Dest = t |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2856 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2857 | if (DestTy == IceType_i8) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2858 | _mov(T, Src0RM); |
| 2859 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2860 | assert(DestTy != IceType_i1); |
| 2861 | assert(Traits::Is64Bit || DestTy != IceType_i64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2862 | // Use 32-bit for both 16-bit and 32-bit, since 32-bit ops are shorter. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2863 | // In x86-64 we need to widen T to 64-bits to ensure that T -- if |
| 2864 | // written to the stack (i.e., in -Om1) will be fully zero-extended. |
| 2865 | T = makeReg(DestTy == IceType_i64 ? IceType_i64 : IceType_i32); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2866 | _movzx(T, Src0RM); |
| 2867 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2868 | _mov(Dest, T); |
| 2869 | } else { |
| 2870 | // t1 = movzx src; dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2871 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2872 | _movzx(T, Src0RM); |
| 2873 | _mov(Dest, T); |
| 2874 | } |
| 2875 | break; |
| 2876 | } |
| 2877 | case InstCast::Trunc: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2878 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2879 | // onemask = materialize(1,1,...); dst = src & onemask |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2880 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2881 | Type Src0Ty = Src0RM->getType(); |
| 2882 | Variable *OneMask = makeVectorOfOnes(Src0Ty); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2883 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2884 | _movp(T, Src0RM); |
| 2885 | _pand(T, OneMask); |
| 2886 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2887 | } else if (DestTy == IceType_i1 || DestTy == IceType_i8) { |
| 2888 | // Make sure we truncate from and into valid registers. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2889 | Operand *Src0 = legalizeUndef(Instr->getSrc(0)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2890 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) |
| 2891 | Src0 = loOperand(Src0); |
| 2892 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 2893 | Variable *T = copyToReg8(Src0RM); |
| 2894 | if (DestTy == IceType_i1) |
| 2895 | _and(T, Ctx->getConstantInt1(1)); |
| 2896 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2897 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2898 | Operand *Src0 = legalizeUndef(Instr->getSrc(0)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2899 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2900 | Src0 = loOperand(Src0); |
| 2901 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 2902 | // t1 = trunc Src0RM; Dest = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2903 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2904 | _mov(T, Src0RM); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2905 | _mov(Dest, T); |
| 2906 | } |
| 2907 | break; |
| 2908 | } |
| 2909 | case InstCast::Fptrunc: |
| 2910 | case InstCast::Fpext: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2911 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2912 | // t1 = cvt Src0RM; Dest = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2913 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2914 | _cvt(T, Src0RM, Traits::Insts::Cvt::Float2float); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2915 | _mov(Dest, T); |
| 2916 | break; |
| 2917 | } |
| 2918 | case InstCast::Fptosi: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2919 | if (isVectorType(DestTy)) { |
| 2920 | assert(DestTy == IceType_v4i32 && |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2921 | Instr->getSrc(0)->getType() == IceType_v4f32); |
| 2922 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2923 | if (llvm::isa<X86OperandMem>(Src0RM)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 2924 | Src0RM = legalizeToReg(Src0RM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2925 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2926 | _cvt(T, Src0RM, Traits::Insts::Cvt::Tps2dq); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2927 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2928 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2929 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2930 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2931 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2932 | // t1.i32 = cvt Src0RM; t2.dest_type = t1; Dest = t2.dest_type |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2933 | Variable *T_1 = nullptr; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2934 | if (Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2935 | T_1 = makeReg(IceType_i64); |
| 2936 | } else { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2937 | assert(DestTy != IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2938 | T_1 = makeReg(IceType_i32); |
| 2939 | } |
| 2940 | // cvt() requires its integer argument to be a GPR. |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2941 | Variable *T_2 = makeReg(DestTy); |
| 2942 | if (isByteSizedType(DestTy)) { |
| 2943 | assert(T_1->getType() == IceType_i32); |
| 2944 | T_1->setRegClass(RCX86_Is32To8); |
| 2945 | T_2->setRegClass(RCX86_IsTrunc8Rcvr); |
| 2946 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2947 | _cvt(T_1, Src0RM, Traits::Insts::Cvt::Tss2si); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2948 | _mov(T_2, T_1); // T_1 and T_2 may have different integer types |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2949 | if (DestTy == IceType_i1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2950 | _and(T_2, Ctx->getConstantInt1(1)); |
| 2951 | _mov(Dest, T_2); |
| 2952 | } |
| 2953 | break; |
| 2954 | case InstCast::Fptoui: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2955 | if (isVectorType(DestTy)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2956 | llvm::report_fatal_error("Helper call was expected"); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2957 | } else if (DestTy == IceType_i64 || |
| 2958 | (!Traits::Is64Bit && DestTy == IceType_i32)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2959 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2960 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2961 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2962 | // t1.i32 = cvt Src0RM; t2.dest_type = t1; Dest = t2.dest_type |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2963 | assert(DestTy != IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2964 | Variable *T_1 = nullptr; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2965 | if (Traits::Is64Bit && DestTy == IceType_i32) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2966 | T_1 = makeReg(IceType_i64); |
| 2967 | } else { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2968 | assert(DestTy != IceType_i32); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2969 | T_1 = makeReg(IceType_i32); |
| 2970 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2971 | Variable *T_2 = makeReg(DestTy); |
| 2972 | if (isByteSizedType(DestTy)) { |
| 2973 | assert(T_1->getType() == IceType_i32); |
| 2974 | T_1->setRegClass(RCX86_Is32To8); |
| 2975 | T_2->setRegClass(RCX86_IsTrunc8Rcvr); |
| 2976 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2977 | _cvt(T_1, Src0RM, Traits::Insts::Cvt::Tss2si); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2978 | _mov(T_2, T_1); // T_1 and T_2 may have different integer types |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2979 | if (DestTy == IceType_i1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2980 | _and(T_2, Ctx->getConstantInt1(1)); |
| 2981 | _mov(Dest, T_2); |
| 2982 | } |
| 2983 | break; |
| 2984 | case InstCast::Sitofp: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2985 | if (isVectorType(DestTy)) { |
| 2986 | assert(DestTy == IceType_v4f32 && |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2987 | Instr->getSrc(0)->getType() == IceType_v4i32); |
| 2988 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2989 | if (llvm::isa<X86OperandMem>(Src0RM)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 2990 | Src0RM = legalizeToReg(Src0RM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2991 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2992 | _cvt(T, Src0RM, Traits::Insts::Cvt::Dq2ps); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2993 | _movp(Dest, T); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2994 | } else if (!Traits::Is64Bit && Instr->getSrc(0)->getType() == IceType_i64) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2995 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2996 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2997 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2998 | // Sign-extend the operand. |
| 2999 | // t1.i32 = movsx Src0RM; t2 = Cvt t1.i32; Dest = t2 |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3000 | Variable *T_1 = nullptr; |
| 3001 | if (Traits::Is64Bit && Src0RM->getType() == IceType_i64) { |
| 3002 | T_1 = makeReg(IceType_i64); |
| 3003 | } else { |
| 3004 | assert(Src0RM->getType() != IceType_i64); |
| 3005 | T_1 = makeReg(IceType_i32); |
| 3006 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3007 | Variable *T_2 = makeReg(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3008 | if (Src0RM->getType() == T_1->getType()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3009 | _mov(T_1, Src0RM); |
| 3010 | else |
| 3011 | _movsx(T_1, Src0RM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 3012 | _cvt(T_2, T_1, Traits::Insts::Cvt::Si2ss); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3013 | _mov(Dest, T_2); |
| 3014 | } |
| 3015 | break; |
| 3016 | case InstCast::Uitofp: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3017 | Operand *Src0 = Instr->getSrc(0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3018 | if (isVectorType(Src0->getType())) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3019 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3020 | } else if (Src0->getType() == IceType_i64 || |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3021 | (!Traits::Is64Bit && Src0->getType() == IceType_i32)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3022 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3023 | } else { |
| 3024 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3025 | // Zero-extend the operand. |
| 3026 | // t1.i32 = movzx Src0RM; t2 = Cvt t1.i32; Dest = t2 |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3027 | Variable *T_1 = nullptr; |
| 3028 | if (Traits::Is64Bit && Src0RM->getType() == IceType_i32) { |
| 3029 | T_1 = makeReg(IceType_i64); |
| 3030 | } else { |
| 3031 | assert(Src0RM->getType() != IceType_i64); |
| 3032 | assert(Traits::Is64Bit || Src0RM->getType() != IceType_i32); |
| 3033 | T_1 = makeReg(IceType_i32); |
| 3034 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3035 | Variable *T_2 = makeReg(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3036 | if (Src0RM->getType() == T_1->getType()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3037 | _mov(T_1, Src0RM); |
| 3038 | else |
| 3039 | _movzx(T_1, Src0RM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 3040 | _cvt(T_2, T_1, Traits::Insts::Cvt::Si2ss); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3041 | _mov(Dest, T_2); |
| 3042 | } |
| 3043 | break; |
| 3044 | } |
| 3045 | case InstCast::Bitcast: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3046 | Operand *Src0 = Instr->getSrc(0); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3047 | if (DestTy == Src0->getType()) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3048 | auto *Assign = InstAssign::create(Func, Dest, Src0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3049 | lowerAssign(Assign); |
| 3050 | return; |
| 3051 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3052 | switch (DestTy) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3053 | default: |
| 3054 | llvm_unreachable("Unexpected Bitcast dest type"); |
| 3055 | case IceType_i8: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3056 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3057 | } break; |
| 3058 | case IceType_i16: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3059 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3060 | } break; |
| 3061 | case IceType_i32: |
| 3062 | case IceType_f32: { |
Jim Stichnoth | a1410df | 2016-06-16 10:02:48 -0700 | [diff] [blame] | 3063 | Variable *Src0R = legalizeToReg(Src0); |
| 3064 | Variable *T = makeReg(DestTy); |
| 3065 | _movd(T, Src0R); |
| 3066 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3067 | } break; |
| 3068 | case IceType_i64: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3069 | assert(Src0->getType() == IceType_f64); |
| 3070 | if (Traits::Is64Bit) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3071 | Variable *Src0R = legalizeToReg(Src0); |
| 3072 | Variable *T = makeReg(IceType_i64); |
| 3073 | _movd(T, Src0R); |
| 3074 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3075 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3076 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3077 | // a.i64 = bitcast b.f64 ==> |
| 3078 | // s.f64 = spill b.f64 |
| 3079 | // t_lo.i32 = lo(s.f64) |
| 3080 | // a_lo.i32 = t_lo.i32 |
| 3081 | // t_hi.i32 = hi(s.f64) |
| 3082 | // a_hi.i32 = t_hi.i32 |
| 3083 | Operand *SpillLo, *SpillHi; |
| 3084 | if (auto *Src0Var = llvm::dyn_cast<Variable>(Src0RM)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3085 | SpillVariable *SpillVar = |
| 3086 | Func->makeVariable<SpillVariable>(IceType_f64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3087 | SpillVar->setLinkedTo(Src0Var); |
| 3088 | Variable *Spill = SpillVar; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3089 | Spill->setMustNotHaveReg(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3090 | _movq(Spill, Src0RM); |
| 3091 | SpillLo = Traits::VariableSplit::create(Func, Spill, |
| 3092 | Traits::VariableSplit::Low); |
| 3093 | SpillHi = Traits::VariableSplit::create(Func, Spill, |
| 3094 | Traits::VariableSplit::High); |
| 3095 | } else { |
| 3096 | SpillLo = loOperand(Src0RM); |
| 3097 | SpillHi = hiOperand(Src0RM); |
| 3098 | } |
| 3099 | |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3100 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 3101 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3102 | Variable *T_Lo = makeReg(IceType_i32); |
| 3103 | Variable *T_Hi = makeReg(IceType_i32); |
| 3104 | |
| 3105 | _mov(T_Lo, SpillLo); |
| 3106 | _mov(DestLo, T_Lo); |
| 3107 | _mov(T_Hi, SpillHi); |
| 3108 | _mov(DestHi, T_Hi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3109 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3110 | } break; |
| 3111 | case IceType_f64: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3112 | assert(Src0->getType() == IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3113 | if (Traits::Is64Bit) { |
| 3114 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3115 | Variable *T = makeReg(IceType_f64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3116 | _movd(T, Src0RM); |
| 3117 | _mov(Dest, T); |
| 3118 | } else { |
| 3119 | Src0 = legalize(Src0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3120 | if (llvm::isa<X86OperandMem>(Src0)) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3121 | Variable *T = Func->makeVariable(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3122 | _movq(T, Src0); |
| 3123 | _movq(Dest, T); |
| 3124 | break; |
| 3125 | } |
| 3126 | // a.f64 = bitcast b.i64 ==> |
| 3127 | // t_lo.i32 = b_lo.i32 |
| 3128 | // FakeDef(s.f64) |
| 3129 | // lo(s.f64) = t_lo.i32 |
| 3130 | // t_hi.i32 = b_hi.i32 |
| 3131 | // hi(s.f64) = t_hi.i32 |
| 3132 | // a.f64 = s.f64 |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3133 | SpillVariable *SpillVar = |
| 3134 | Func->makeVariable<SpillVariable>(IceType_f64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3135 | SpillVar->setLinkedTo(Dest); |
| 3136 | Variable *Spill = SpillVar; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3137 | Spill->setMustNotHaveReg(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3138 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3139 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3140 | auto *SpillLo = Traits::VariableSplit::create( |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3141 | Func, Spill, Traits::VariableSplit::Low); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3142 | auto *SpillHi = Traits::VariableSplit::create( |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3143 | Func, Spill, Traits::VariableSplit::High); |
| 3144 | _mov(T_Lo, loOperand(Src0)); |
| 3145 | // Technically, the Spill is defined after the _store happens, but |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3146 | // SpillLo is considered a "use" of Spill so define Spill before it is |
| 3147 | // used. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3148 | Context.insert<InstFakeDef>(Spill); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3149 | _store(T_Lo, SpillLo); |
| 3150 | _mov(T_Hi, hiOperand(Src0)); |
| 3151 | _store(T_Hi, SpillHi); |
| 3152 | _movq(Dest, Spill); |
| 3153 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3154 | } break; |
| 3155 | case IceType_v8i1: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3156 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3157 | } break; |
| 3158 | case IceType_v16i1: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3159 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3160 | } break; |
| 3161 | case IceType_v8i16: |
| 3162 | case IceType_v16i8: |
| 3163 | case IceType_v4i32: |
| 3164 | case IceType_v4f32: { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3165 | _movp(Dest, legalizeToReg(Src0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3166 | } break; |
| 3167 | } |
| 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3173 | template <typename TraitsType> |
| 3174 | void TargetX86Base<TraitsType>::lowerExtractElement( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3175 | const InstExtractElement *Instr) { |
| 3176 | Operand *SourceVectNotLegalized = Instr->getSrc(0); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3177 | auto *ElementIndex = llvm::dyn_cast<ConstantInteger32>(Instr->getSrc(1)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3178 | // Only constant indices are allowed in PNaCl IR. |
| 3179 | assert(ElementIndex); |
| 3180 | |
| 3181 | unsigned Index = ElementIndex->getValue(); |
| 3182 | Type Ty = SourceVectNotLegalized->getType(); |
| 3183 | Type ElementTy = typeElementType(Ty); |
| 3184 | Type InVectorElementTy = Traits::getInVectorElementType(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3185 | |
| 3186 | // TODO(wala): Determine the best lowering sequences for each type. |
| 3187 | bool CanUsePextr = Ty == IceType_v8i16 || Ty == IceType_v8i1 || |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 3188 | (InstructionSet >= Traits::SSE4_1 && Ty != IceType_v4f32); |
| 3189 | Variable *ExtractedElementR = |
| 3190 | makeReg(CanUsePextr ? IceType_i32 : InVectorElementTy); |
| 3191 | if (CanUsePextr) { |
| 3192 | // Use pextrb, pextrw, or pextrd. The "b" and "w" versions clear the upper |
| 3193 | // bits of the destination register, so we represent this by always |
| 3194 | // extracting into an i32 register. The _mov into Dest below will do |
| 3195 | // truncation as necessary. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3196 | Constant *Mask = Ctx->getConstantInt32(Index); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3197 | Variable *SourceVectR = legalizeToReg(SourceVectNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3198 | _pextr(ExtractedElementR, SourceVectR, Mask); |
| 3199 | } else if (Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v4i1) { |
| 3200 | // Use pshufd and movd/movss. |
| 3201 | Variable *T = nullptr; |
| 3202 | if (Index) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3203 | // The shuffle only needs to occur if the element to be extracted is not |
| 3204 | // at the lowest index. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3205 | Constant *Mask = Ctx->getConstantInt32(Index); |
| 3206 | T = makeReg(Ty); |
| 3207 | _pshufd(T, legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem), Mask); |
| 3208 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3209 | T = legalizeToReg(SourceVectNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | if (InVectorElementTy == IceType_i32) { |
| 3213 | _movd(ExtractedElementR, T); |
| 3214 | } else { // Ty == IceType_f32 |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3215 | // TODO(wala): _movss is only used here because _mov does not allow a |
| 3216 | // vector source and a scalar destination. _mov should be able to be |
| 3217 | // used here. |
| 3218 | // _movss is a binary instruction, so the FakeDef is needed to keep the |
| 3219 | // live range analysis consistent. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3220 | Context.insert<InstFakeDef>(ExtractedElementR); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3221 | _movss(ExtractedElementR, T); |
| 3222 | } |
| 3223 | } else { |
| 3224 | assert(Ty == IceType_v16i8 || Ty == IceType_v16i1); |
| 3225 | // Spill the value to a stack slot and do the extraction in memory. |
| 3226 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3227 | // TODO(wala): use legalize(SourceVectNotLegalized, Legal_Mem) when support |
| 3228 | // for legalizing to mem is implemented. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3229 | Variable *Slot = Func->makeVariable(Ty); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3230 | Slot->setMustNotHaveReg(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3231 | _movp(Slot, legalizeToReg(SourceVectNotLegalized)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3232 | |
| 3233 | // Compute the location of the element in memory. |
| 3234 | unsigned Offset = Index * typeWidthInBytes(InVectorElementTy); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3235 | X86OperandMem *Loc = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3236 | getMemoryOperandForStackSlot(InVectorElementTy, Slot, Offset); |
| 3237 | _mov(ExtractedElementR, Loc); |
| 3238 | } |
| 3239 | |
| 3240 | if (ElementTy == IceType_i1) { |
| 3241 | // Truncate extracted integers to i1s if necessary. |
| 3242 | Variable *T = makeReg(IceType_i1); |
| 3243 | InstCast *Cast = |
| 3244 | InstCast::create(Func, InstCast::Trunc, T, ExtractedElementR); |
| 3245 | lowerCast(Cast); |
| 3246 | ExtractedElementR = T; |
| 3247 | } |
| 3248 | |
| 3249 | // Copy the element to the destination. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3250 | Variable *Dest = Instr->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3251 | _mov(Dest, ExtractedElementR); |
| 3252 | } |
| 3253 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3254 | template <typename TraitsType> |
| 3255 | void TargetX86Base<TraitsType>::lowerFcmp(const InstFcmp *Fcmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3256 | Variable *Dest = Fcmp->getDest(); |
| 3257 | |
| 3258 | if (isVectorType(Dest->getType())) { |
| 3259 | lowerFcmpVector(Fcmp); |
| 3260 | } else { |
| 3261 | constexpr Inst *Consumer = nullptr; |
| 3262 | lowerFcmpAndConsumer(Fcmp, Consumer); |
| 3263 | } |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3266 | template <typename TraitsType> |
| 3267 | void TargetX86Base<TraitsType>::lowerFcmpAndConsumer(const InstFcmp *Fcmp, |
| 3268 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3269 | Operand *Src0 = Fcmp->getSrc(0); |
| 3270 | Operand *Src1 = Fcmp->getSrc(1); |
| 3271 | Variable *Dest = Fcmp->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3272 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3273 | if (isVectorType(Dest->getType())) |
| 3274 | llvm::report_fatal_error("Vector compare/branch cannot be folded"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3275 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3276 | if (Consumer != nullptr) { |
| 3277 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3278 | if (lowerOptimizeFcmpSelect(Fcmp, Select)) |
| 3279 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3280 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | // Lowering a = fcmp cond, b, c |
| 3284 | // ucomiss b, c /* only if C1 != Br_None */ |
| 3285 | // /* but swap b,c order if SwapOperands==true */ |
| 3286 | // mov a, <default> |
| 3287 | // j<C1> label /* only if C1 != Br_None */ |
| 3288 | // j<C2> label /* only if C2 != Br_None */ |
| 3289 | // FakeUse(a) /* only if C1 != Br_None */ |
| 3290 | // mov a, !<default> /* only if C1 != Br_None */ |
| 3291 | // label: /* only if C1 != Br_None */ |
| 3292 | // |
| 3293 | // setcc lowering when C1 != Br_None && C2 == Br_None: |
| 3294 | // ucomiss b, c /* but swap b,c order if SwapOperands==true */ |
| 3295 | // setcc a, C1 |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3296 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3297 | assert(Condition < Traits::TableFcmpSize); |
| 3298 | if (Traits::TableFcmp[Condition].SwapScalarOperands) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3299 | std::swap(Src0, Src1); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3300 | const bool HasC1 = (Traits::TableFcmp[Condition].C1 != Traits::Cond::Br_None); |
| 3301 | const bool HasC2 = (Traits::TableFcmp[Condition].C2 != Traits::Cond::Br_None); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3302 | if (HasC1) { |
| 3303 | Src0 = legalize(Src0); |
| 3304 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 3305 | Variable *T = nullptr; |
| 3306 | _mov(T, Src0); |
| 3307 | _ucomiss(T, Src1RM); |
| 3308 | if (!HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3309 | assert(Traits::TableFcmp[Condition].Default); |
| 3310 | setccOrConsumer(Traits::TableFcmp[Condition].C1, Dest, Consumer); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3311 | return; |
| 3312 | } |
| 3313 | } |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3314 | int32_t IntDefault = Traits::TableFcmp[Condition].Default; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3315 | if (Consumer == nullptr) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3316 | Constant *Default = Ctx->getConstantInt(Dest->getType(), IntDefault); |
| 3317 | _mov(Dest, Default); |
| 3318 | if (HasC1) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3319 | InstX86Label *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3320 | _br(Traits::TableFcmp[Condition].C1, Label); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3321 | if (HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3322 | _br(Traits::TableFcmp[Condition].C2, Label); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3323 | } |
| 3324 | Constant *NonDefault = Ctx->getConstantInt(Dest->getType(), !IntDefault); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3325 | _redefined(_mov(Dest, NonDefault)); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3326 | Context.insert(Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3327 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3328 | return; |
| 3329 | } |
| 3330 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3331 | CfgNode *TrueSucc = Br->getTargetTrue(); |
| 3332 | CfgNode *FalseSucc = Br->getTargetFalse(); |
| 3333 | if (IntDefault != 0) |
| 3334 | std::swap(TrueSucc, FalseSucc); |
| 3335 | if (HasC1) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3336 | _br(Traits::TableFcmp[Condition].C1, FalseSucc); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3337 | if (HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3338 | _br(Traits::TableFcmp[Condition].C2, FalseSucc); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3339 | } |
| 3340 | _br(TrueSucc); |
| 3341 | return; |
| 3342 | } |
| 3343 | _br(FalseSucc); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3344 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3345 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3346 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3347 | Operand *SrcT = Select->getTrueOperand(); |
| 3348 | Operand *SrcF = Select->getFalseOperand(); |
| 3349 | Variable *SelectDest = Select->getDest(); |
| 3350 | if (IntDefault != 0) |
| 3351 | std::swap(SrcT, SrcF); |
| 3352 | lowerMove(SelectDest, SrcF, false); |
| 3353 | if (HasC1) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3354 | InstX86Label *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3355 | _br(Traits::TableFcmp[Condition].C1, Label); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3356 | if (HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3357 | _br(Traits::TableFcmp[Condition].C2, Label); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3358 | } |
| 3359 | static constexpr bool IsRedefinition = true; |
| 3360 | lowerMove(SelectDest, SrcT, IsRedefinition); |
| 3361 | Context.insert(Label); |
| 3362 | } |
| 3363 | return; |
| 3364 | } |
| 3365 | llvm::report_fatal_error("Unexpected consumer type"); |
| 3366 | } |
| 3367 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3368 | template <typename TraitsType> |
| 3369 | void TargetX86Base<TraitsType>::lowerFcmpVector(const InstFcmp *Fcmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3370 | Operand *Src0 = Fcmp->getSrc(0); |
| 3371 | Operand *Src1 = Fcmp->getSrc(1); |
| 3372 | Variable *Dest = Fcmp->getDest(); |
| 3373 | |
| 3374 | if (!isVectorType(Dest->getType())) |
| 3375 | llvm::report_fatal_error("Expected vector compare"); |
| 3376 | |
| 3377 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3378 | assert(Condition < Traits::TableFcmpSize); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3379 | |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3380 | if (Traits::TableFcmp[Condition].SwapVectorOperands) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3381 | std::swap(Src0, Src1); |
| 3382 | |
| 3383 | Variable *T = nullptr; |
| 3384 | |
| 3385 | if (Condition == InstFcmp::True) { |
| 3386 | // makeVectorOfOnes() requires an integer vector type. |
| 3387 | T = makeVectorOfMinusOnes(IceType_v4i32); |
| 3388 | } else if (Condition == InstFcmp::False) { |
| 3389 | T = makeVectorOfZeros(Dest->getType()); |
| 3390 | } else { |
| 3391 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3392 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3393 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3394 | Src1RM = legalizeToReg(Src1RM); |
| 3395 | |
| 3396 | switch (Condition) { |
| 3397 | default: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3398 | const CmppsCond Predicate = Traits::TableFcmp[Condition].Predicate; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3399 | assert(Predicate != Traits::Cond::Cmpps_Invalid); |
| 3400 | T = makeReg(Src0RM->getType()); |
| 3401 | _movp(T, Src0RM); |
| 3402 | _cmpps(T, Src1RM, Predicate); |
| 3403 | } break; |
| 3404 | case InstFcmp::One: { |
| 3405 | // Check both unequal and ordered. |
| 3406 | T = makeReg(Src0RM->getType()); |
| 3407 | Variable *T2 = makeReg(Src0RM->getType()); |
| 3408 | _movp(T, Src0RM); |
| 3409 | _cmpps(T, Src1RM, Traits::Cond::Cmpps_neq); |
| 3410 | _movp(T2, Src0RM); |
| 3411 | _cmpps(T2, Src1RM, Traits::Cond::Cmpps_ord); |
| 3412 | _pand(T, T2); |
| 3413 | } break; |
| 3414 | case InstFcmp::Ueq: { |
| 3415 | // Check both equal or unordered. |
| 3416 | T = makeReg(Src0RM->getType()); |
| 3417 | Variable *T2 = makeReg(Src0RM->getType()); |
| 3418 | _movp(T, Src0RM); |
| 3419 | _cmpps(T, Src1RM, Traits::Cond::Cmpps_eq); |
| 3420 | _movp(T2, Src0RM); |
| 3421 | _cmpps(T2, Src1RM, Traits::Cond::Cmpps_unord); |
| 3422 | _por(T, T2); |
| 3423 | } break; |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | assert(T != nullptr); |
| 3428 | _movp(Dest, T); |
| 3429 | eliminateNextVectorSextInstruction(Dest); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3432 | inline bool isZero(const Operand *Opnd) { |
| 3433 | if (auto *C64 = llvm::dyn_cast<ConstantInteger64>(Opnd)) |
| 3434 | return C64->getValue() == 0; |
| 3435 | if (auto *C32 = llvm::dyn_cast<ConstantInteger32>(Opnd)) |
| 3436 | return C32->getValue() == 0; |
| 3437 | return false; |
| 3438 | } |
| 3439 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3440 | template <typename TraitsType> |
| 3441 | void TargetX86Base<TraitsType>::lowerIcmpAndConsumer(const InstIcmp *Icmp, |
| 3442 | const Inst *Consumer) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3443 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3444 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3445 | Variable *Dest = Icmp->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3446 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3447 | if (isVectorType(Dest->getType())) |
| 3448 | llvm::report_fatal_error("Vector compare/branch cannot be folded"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3449 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3450 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3451 | lowerIcmp64(Icmp, Consumer); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3452 | return; |
| 3453 | } |
| 3454 | |
| 3455 | // cmp b, c |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3456 | if (isZero(Src1)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3457 | switch (Icmp->getCondition()) { |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3458 | default: |
| 3459 | break; |
| 3460 | case InstIcmp::Uge: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3461 | movOrConsumer(true, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3462 | return; |
| 3463 | case InstIcmp::Ult: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3464 | movOrConsumer(false, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3468 | Operand *Src0RM = legalizeSrc0ForCmp(Src0, Src1); |
| 3469 | _cmp(Src0RM, Src1); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3470 | setccOrConsumer(Traits::getIcmp32Mapping(Icmp->getCondition()), Dest, |
| 3471 | Consumer); |
| 3472 | } |
| 3473 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3474 | template <typename TraitsType> |
| 3475 | void TargetX86Base<TraitsType>::lowerIcmpVector(const InstIcmp *Icmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3476 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3477 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3478 | Variable *Dest = Icmp->getDest(); |
| 3479 | |
| 3480 | if (!isVectorType(Dest->getType())) |
| 3481 | llvm::report_fatal_error("Expected a vector compare"); |
| 3482 | |
| 3483 | Type Ty = Src0->getType(); |
| 3484 | // Promote i1 vectors to 128 bit integer vector types. |
| 3485 | if (typeElementType(Ty) == IceType_i1) { |
| 3486 | Type NewTy = IceType_NUM; |
| 3487 | switch (Ty) { |
| 3488 | default: |
| 3489 | llvm::report_fatal_error("unexpected type"); |
| 3490 | break; |
| 3491 | case IceType_v4i1: |
| 3492 | NewTy = IceType_v4i32; |
| 3493 | break; |
| 3494 | case IceType_v8i1: |
| 3495 | NewTy = IceType_v8i16; |
| 3496 | break; |
| 3497 | case IceType_v16i1: |
| 3498 | NewTy = IceType_v16i8; |
| 3499 | break; |
| 3500 | } |
| 3501 | Variable *NewSrc0 = Func->makeVariable(NewTy); |
| 3502 | Variable *NewSrc1 = Func->makeVariable(NewTy); |
| 3503 | lowerCast(InstCast::create(Func, InstCast::Sext, NewSrc0, Src0)); |
| 3504 | lowerCast(InstCast::create(Func, InstCast::Sext, NewSrc1, Src1)); |
| 3505 | Src0 = NewSrc0; |
| 3506 | Src1 = NewSrc1; |
| 3507 | Ty = NewTy; |
| 3508 | } |
| 3509 | |
| 3510 | InstIcmp::ICond Condition = Icmp->getCondition(); |
| 3511 | |
| 3512 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3513 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 3514 | |
| 3515 | // SSE2 only has signed comparison operations. Transform unsigned inputs in |
| 3516 | // a manner that allows for the use of signed comparison operations by |
| 3517 | // flipping the high order bits. |
| 3518 | if (Condition == InstIcmp::Ugt || Condition == InstIcmp::Uge || |
| 3519 | Condition == InstIcmp::Ult || Condition == InstIcmp::Ule) { |
| 3520 | Variable *T0 = makeReg(Ty); |
| 3521 | Variable *T1 = makeReg(Ty); |
| 3522 | Variable *HighOrderBits = makeVectorOfHighOrderBits(Ty); |
| 3523 | _movp(T0, Src0RM); |
| 3524 | _pxor(T0, HighOrderBits); |
| 3525 | _movp(T1, Src1RM); |
| 3526 | _pxor(T1, HighOrderBits); |
| 3527 | Src0RM = T0; |
| 3528 | Src1RM = T1; |
| 3529 | } |
| 3530 | |
| 3531 | Variable *T = makeReg(Ty); |
| 3532 | switch (Condition) { |
| 3533 | default: |
| 3534 | llvm_unreachable("unexpected condition"); |
| 3535 | break; |
| 3536 | case InstIcmp::Eq: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3537 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3538 | Src1RM = legalizeToReg(Src1RM); |
| 3539 | _movp(T, Src0RM); |
| 3540 | _pcmpeq(T, Src1RM); |
| 3541 | } break; |
| 3542 | case InstIcmp::Ne: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3543 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3544 | Src1RM = legalizeToReg(Src1RM); |
| 3545 | _movp(T, Src0RM); |
| 3546 | _pcmpeq(T, Src1RM); |
| 3547 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3548 | _pxor(T, MinusOne); |
| 3549 | } break; |
| 3550 | case InstIcmp::Ugt: |
| 3551 | case InstIcmp::Sgt: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3552 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3553 | Src1RM = legalizeToReg(Src1RM); |
| 3554 | _movp(T, Src0RM); |
| 3555 | _pcmpgt(T, Src1RM); |
| 3556 | } break; |
| 3557 | case InstIcmp::Uge: |
| 3558 | case InstIcmp::Sge: { |
| 3559 | // !(Src1RM > Src0RM) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3560 | if (llvm::isa<X86OperandMem>(Src0RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3561 | Src0RM = legalizeToReg(Src0RM); |
| 3562 | _movp(T, Src1RM); |
| 3563 | _pcmpgt(T, Src0RM); |
| 3564 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3565 | _pxor(T, MinusOne); |
| 3566 | } break; |
| 3567 | case InstIcmp::Ult: |
| 3568 | case InstIcmp::Slt: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3569 | if (llvm::isa<X86OperandMem>(Src0RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3570 | Src0RM = legalizeToReg(Src0RM); |
| 3571 | _movp(T, Src1RM); |
| 3572 | _pcmpgt(T, Src0RM); |
| 3573 | } break; |
| 3574 | case InstIcmp::Ule: |
| 3575 | case InstIcmp::Sle: { |
| 3576 | // !(Src0RM > Src1RM) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3577 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3578 | Src1RM = legalizeToReg(Src1RM); |
| 3579 | _movp(T, Src0RM); |
| 3580 | _pcmpgt(T, Src1RM); |
| 3581 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3582 | _pxor(T, MinusOne); |
| 3583 | } break; |
| 3584 | } |
| 3585 | |
| 3586 | _movp(Dest, T); |
| 3587 | eliminateNextVectorSextInstruction(Dest); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3590 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3591 | template <typename T> |
| 3592 | typename std::enable_if<!T::Is64Bit, void>::type |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3593 | TargetX86Base<TraitsType>::lowerIcmp64(const InstIcmp *Icmp, |
| 3594 | const Inst *Consumer) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3595 | // a=icmp cond, b, c ==> cmp b,c; a=1; br cond,L1; FakeUse(a); a=0; L1: |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3596 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3597 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3598 | Variable *Dest = Icmp->getDest(); |
| 3599 | InstIcmp::ICond Condition = Icmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3600 | assert(Condition < Traits::TableIcmp64Size); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3601 | Operand *Src0LoRM = nullptr; |
| 3602 | Operand *Src0HiRM = nullptr; |
| 3603 | // Legalize the portions of Src0 that are going to be needed. |
| 3604 | if (isZero(Src1)) { |
| 3605 | switch (Condition) { |
| 3606 | default: |
| 3607 | llvm_unreachable("unexpected condition"); |
| 3608 | break; |
| 3609 | // These two are not optimized, so we fall through to the general case, |
| 3610 | // which needs the upper and lower halves legalized. |
| 3611 | case InstIcmp::Sgt: |
| 3612 | case InstIcmp::Sle: |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 3613 | // These four compare after performing an "or" of the high and low half, so |
| 3614 | // they need the upper and lower halves legalized. |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3615 | case InstIcmp::Eq: |
| 3616 | case InstIcmp::Ule: |
| 3617 | case InstIcmp::Ne: |
| 3618 | case InstIcmp::Ugt: |
| 3619 | Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem); |
| 3620 | // These two test only the high half's sign bit, so they need only |
| 3621 | // the upper half legalized. |
| 3622 | case InstIcmp::Sge: |
| 3623 | case InstIcmp::Slt: |
| 3624 | Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem); |
| 3625 | break; |
| 3626 | |
| 3627 | // These two move constants and hence need no legalization. |
| 3628 | case InstIcmp::Uge: |
| 3629 | case InstIcmp::Ult: |
| 3630 | break; |
| 3631 | } |
| 3632 | } else { |
| 3633 | Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem); |
| 3634 | Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem); |
| 3635 | } |
| 3636 | // Optimize comparisons with zero. |
| 3637 | if (isZero(Src1)) { |
| 3638 | Constant *SignMask = Ctx->getConstantInt32(0x80000000); |
| 3639 | Variable *Temp = nullptr; |
| 3640 | switch (Condition) { |
| 3641 | default: |
| 3642 | llvm_unreachable("unexpected condition"); |
| 3643 | break; |
| 3644 | case InstIcmp::Eq: |
| 3645 | case InstIcmp::Ule: |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 3646 | // Mov Src0HiRM first, because it was legalized most recently, and will |
| 3647 | // sometimes avoid a move before the OR. |
| 3648 | _mov(Temp, Src0HiRM); |
| 3649 | _or(Temp, Src0LoRM); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3650 | Context.insert<InstFakeUse>(Temp); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3651 | setccOrConsumer(Traits::Cond::Br_e, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3652 | return; |
| 3653 | case InstIcmp::Ne: |
| 3654 | case InstIcmp::Ugt: |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 3655 | // Mov Src0HiRM first, because it was legalized most recently, and will |
| 3656 | // sometimes avoid a move before the OR. |
| 3657 | _mov(Temp, Src0HiRM); |
| 3658 | _or(Temp, Src0LoRM); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3659 | Context.insert<InstFakeUse>(Temp); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3660 | setccOrConsumer(Traits::Cond::Br_ne, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3661 | return; |
| 3662 | case InstIcmp::Uge: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3663 | movOrConsumer(true, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3664 | return; |
| 3665 | case InstIcmp::Ult: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3666 | movOrConsumer(false, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3667 | return; |
| 3668 | case InstIcmp::Sgt: |
| 3669 | break; |
| 3670 | case InstIcmp::Sge: |
| 3671 | _test(Src0HiRM, SignMask); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3672 | setccOrConsumer(Traits::Cond::Br_e, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3673 | return; |
| 3674 | case InstIcmp::Slt: |
| 3675 | _test(Src0HiRM, SignMask); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3676 | setccOrConsumer(Traits::Cond::Br_ne, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3677 | return; |
| 3678 | case InstIcmp::Sle: |
| 3679 | break; |
| 3680 | } |
| 3681 | } |
| 3682 | // Handle general compares. |
| 3683 | Operand *Src1LoRI = legalize(loOperand(Src1), Legal_Reg | Legal_Imm); |
| 3684 | Operand *Src1HiRI = legalize(hiOperand(Src1), Legal_Reg | Legal_Imm); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3685 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3686 | Constant *Zero = Ctx->getConstantInt(Dest->getType(), 0); |
| 3687 | Constant *One = Ctx->getConstantInt(Dest->getType(), 1); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3688 | InstX86Label *LabelFalse = InstX86Label::create(Func, this); |
| 3689 | InstX86Label *LabelTrue = InstX86Label::create(Func, this); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3690 | _mov(Dest, One); |
| 3691 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3692 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3693 | _br(Traits::TableIcmp64[Condition].C1, LabelTrue); |
| 3694 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3695 | _br(Traits::TableIcmp64[Condition].C2, LabelFalse); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3696 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3697 | _br(Traits::TableIcmp64[Condition].C3, LabelTrue); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3698 | Context.insert(LabelFalse); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3699 | _redefined(_mov(Dest, Zero)); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3700 | Context.insert(LabelTrue); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3701 | return; |
| 3702 | } |
| 3703 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3704 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3705 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3706 | _br(Traits::TableIcmp64[Condition].C1, Br->getTargetTrue()); |
| 3707 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3708 | _br(Traits::TableIcmp64[Condition].C2, Br->getTargetFalse()); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3709 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3710 | _br(Traits::TableIcmp64[Condition].C3, Br->getTargetTrue(), |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3711 | Br->getTargetFalse()); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3712 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3713 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3714 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3715 | Operand *SrcT = Select->getTrueOperand(); |
| 3716 | Operand *SrcF = Select->getFalseOperand(); |
| 3717 | Variable *SelectDest = Select->getDest(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3718 | InstX86Label *LabelFalse = InstX86Label::create(Func, this); |
| 3719 | InstX86Label *LabelTrue = InstX86Label::create(Func, this); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3720 | lowerMove(SelectDest, SrcT, false); |
| 3721 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3722 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3723 | _br(Traits::TableIcmp64[Condition].C1, LabelTrue); |
| 3724 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3725 | _br(Traits::TableIcmp64[Condition].C2, LabelFalse); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3726 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3727 | _br(Traits::TableIcmp64[Condition].C3, LabelTrue); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3728 | Context.insert(LabelFalse); |
| 3729 | static constexpr bool IsRedefinition = true; |
| 3730 | lowerMove(SelectDest, SrcF, IsRedefinition); |
| 3731 | Context.insert(LabelTrue); |
| 3732 | return; |
| 3733 | } |
| 3734 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3735 | } |
| 3736 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3737 | template <typename TraitsType> |
| 3738 | void TargetX86Base<TraitsType>::setccOrConsumer(BrCond Condition, |
| 3739 | Variable *Dest, |
| 3740 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3741 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3742 | _setcc(Dest, Condition); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3743 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3744 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3745 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
| 3746 | _br(Condition, Br->getTargetTrue(), Br->getTargetFalse()); |
| 3747 | return; |
| 3748 | } |
| 3749 | if (const auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3750 | Operand *SrcT = Select->getTrueOperand(); |
| 3751 | Operand *SrcF = Select->getFalseOperand(); |
| 3752 | Variable *SelectDest = Select->getDest(); |
| 3753 | lowerSelectMove(SelectDest, Condition, SrcT, SrcF); |
| 3754 | return; |
| 3755 | } |
| 3756 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3759 | template <typename TraitsType> |
| 3760 | void TargetX86Base<TraitsType>::movOrConsumer(bool IcmpResult, Variable *Dest, |
| 3761 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3762 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3763 | _mov(Dest, Ctx->getConstantInt(Dest->getType(), (IcmpResult ? 1 : 0))); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3764 | return; |
| 3765 | } |
| 3766 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3767 | // TODO(sehr,stichnot): This could be done with a single unconditional |
| 3768 | // branch instruction, but subzero doesn't know how to handle the resulting |
| 3769 | // control flow graph changes now. Make it do so to eliminate mov and cmp. |
| 3770 | _mov(Dest, Ctx->getConstantInt(Dest->getType(), (IcmpResult ? 1 : 0))); |
| 3771 | _cmp(Dest, Ctx->getConstantInt(Dest->getType(), 0)); |
| 3772 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3773 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3774 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3775 | if (const auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3776 | Operand *Src = nullptr; |
| 3777 | if (IcmpResult) { |
| 3778 | Src = legalize(Select->getTrueOperand(), Legal_Reg | Legal_Imm); |
| 3779 | } else { |
| 3780 | Src = legalize(Select->getFalseOperand(), Legal_Reg | Legal_Imm); |
| 3781 | } |
| 3782 | Variable *SelectDest = Select->getDest(); |
| 3783 | lowerMove(SelectDest, Src, false); |
| 3784 | return; |
| 3785 | } |
| 3786 | llvm::report_fatal_error("Unexpected consumer type"); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3787 | } |
| 3788 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3789 | template <typename TraitsType> |
| 3790 | void TargetX86Base<TraitsType>::lowerArithAndConsumer( |
| 3791 | const InstArithmetic *Arith, const Inst *Consumer) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3792 | Variable *T = nullptr; |
| 3793 | Operand *Src0 = legalize(Arith->getSrc(0)); |
| 3794 | Operand *Src1 = legalize(Arith->getSrc(1)); |
| 3795 | Variable *Dest = Arith->getDest(); |
| 3796 | switch (Arith->getOp()) { |
| 3797 | default: |
| 3798 | llvm_unreachable("arithmetic operator not AND or OR"); |
| 3799 | break; |
| 3800 | case InstArithmetic::And: |
| 3801 | _mov(T, Src0); |
| 3802 | // Test cannot have an address in the second position. Since T is |
| 3803 | // guaranteed to be a register and Src1 could be a memory load, ensure |
| 3804 | // that the second argument is a register. |
| 3805 | if (llvm::isa<Constant>(Src1)) |
| 3806 | _test(T, Src1); |
| 3807 | else |
| 3808 | _test(Src1, T); |
| 3809 | break; |
| 3810 | case InstArithmetic::Or: |
| 3811 | _mov(T, Src0); |
| 3812 | _or(T, Src1); |
| 3813 | break; |
| 3814 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3815 | |
| 3816 | if (Consumer == nullptr) { |
| 3817 | llvm::report_fatal_error("Expected a consumer instruction"); |
| 3818 | } |
| 3819 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3820 | Context.insert<InstFakeUse>(T); |
| 3821 | Context.insert<InstFakeDef>(Dest); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3822 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
| 3823 | return; |
| 3824 | } |
| 3825 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3826 | } |
| 3827 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3828 | template <typename TraitsType> |
| 3829 | void TargetX86Base<TraitsType>::lowerInsertElement( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3830 | const InstInsertElement *Instr) { |
| 3831 | Operand *SourceVectNotLegalized = Instr->getSrc(0); |
| 3832 | Operand *ElementToInsertNotLegalized = Instr->getSrc(1); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3833 | auto *ElementIndex = llvm::dyn_cast<ConstantInteger32>(Instr->getSrc(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3834 | // Only constant indices are allowed in PNaCl IR. |
| 3835 | assert(ElementIndex); |
| 3836 | unsigned Index = ElementIndex->getValue(); |
| 3837 | assert(Index < typeNumElements(SourceVectNotLegalized->getType())); |
| 3838 | |
| 3839 | Type Ty = SourceVectNotLegalized->getType(); |
| 3840 | Type ElementTy = typeElementType(Ty); |
| 3841 | Type InVectorElementTy = Traits::getInVectorElementType(Ty); |
| 3842 | |
| 3843 | if (ElementTy == IceType_i1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3844 | // Expand the element to the appropriate size for it to be inserted in the |
| 3845 | // vector. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3846 | Variable *Expanded = Func->makeVariable(InVectorElementTy); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3847 | auto *Cast = InstCast::create(Func, InstCast::Zext, Expanded, |
| 3848 | ElementToInsertNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3849 | lowerCast(Cast); |
| 3850 | ElementToInsertNotLegalized = Expanded; |
| 3851 | } |
| 3852 | |
| 3853 | if (Ty == IceType_v8i16 || Ty == IceType_v8i1 || |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 3854 | InstructionSet >= Traits::SSE4_1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3855 | // Use insertps, pinsrb, pinsrw, or pinsrd. |
| 3856 | Operand *ElementRM = |
| 3857 | legalize(ElementToInsertNotLegalized, Legal_Reg | Legal_Mem); |
| 3858 | Operand *SourceVectRM = |
| 3859 | legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem); |
| 3860 | Variable *T = makeReg(Ty); |
| 3861 | _movp(T, SourceVectRM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3862 | if (Ty == IceType_v4f32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3863 | _insertps(T, ElementRM, Ctx->getConstantInt32(Index << 4)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3864 | } else { |
| 3865 | // For the pinsrb and pinsrw instructions, when the source operand is a |
| 3866 | // register, it must be a full r32 register like eax, and not ax/al/ah. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3867 | // For filetype=asm, InstX86Pinsr<TraitsType>::emit() compensates for |
| 3868 | // the use |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3869 | // of r16 and r8 by converting them through getBaseReg(), while emitIAS() |
| 3870 | // validates that the original and base register encodings are the same. |
| 3871 | if (ElementRM->getType() == IceType_i8 && |
| 3872 | llvm::isa<Variable>(ElementRM)) { |
| 3873 | // Don't use ah/bh/ch/dh for pinsrb. |
| 3874 | ElementRM = copyToReg8(ElementRM); |
| 3875 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3876 | _pinsr(T, ElementRM, Ctx->getConstantInt32(Index)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3877 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3878 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3879 | } else if (Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v4i1) { |
| 3880 | // Use shufps or movss. |
| 3881 | Variable *ElementR = nullptr; |
| 3882 | Operand *SourceVectRM = |
| 3883 | legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem); |
| 3884 | |
| 3885 | if (InVectorElementTy == IceType_f32) { |
| 3886 | // ElementR will be in an XMM register since it is floating point. |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3887 | ElementR = legalizeToReg(ElementToInsertNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3888 | } else { |
| 3889 | // Copy an integer to an XMM register. |
| 3890 | Operand *T = legalize(ElementToInsertNotLegalized, Legal_Reg | Legal_Mem); |
| 3891 | ElementR = makeReg(Ty); |
| 3892 | _movd(ElementR, T); |
| 3893 | } |
| 3894 | |
| 3895 | if (Index == 0) { |
| 3896 | Variable *T = makeReg(Ty); |
| 3897 | _movp(T, SourceVectRM); |
| 3898 | _movss(T, ElementR); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3899 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3900 | return; |
| 3901 | } |
| 3902 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3903 | // shufps treats the source and destination operands as vectors of four |
| 3904 | // doublewords. The destination's two high doublewords are selected from |
| 3905 | // the source operand and the two low doublewords are selected from the |
| 3906 | // (original value of) the destination operand. An insertelement operation |
| 3907 | // can be effected with a sequence of two shufps operations with |
| 3908 | // appropriate masks. In all cases below, Element[0] is being inserted into |
| 3909 | // SourceVectOperand. Indices are ordered from left to right. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3910 | // |
| 3911 | // insertelement into index 1 (result is stored in ElementR): |
| 3912 | // ElementR := ElementR[0, 0] SourceVectRM[0, 0] |
| 3913 | // ElementR := ElementR[3, 0] SourceVectRM[2, 3] |
| 3914 | // |
| 3915 | // insertelement into index 2 (result is stored in T): |
| 3916 | // T := SourceVectRM |
| 3917 | // ElementR := ElementR[0, 0] T[0, 3] |
| 3918 | // T := T[0, 1] ElementR[0, 3] |
| 3919 | // |
| 3920 | // insertelement into index 3 (result is stored in T): |
| 3921 | // T := SourceVectRM |
| 3922 | // ElementR := ElementR[0, 0] T[0, 2] |
| 3923 | // T := T[0, 1] ElementR[3, 0] |
| 3924 | const unsigned char Mask1[3] = {0, 192, 128}; |
| 3925 | const unsigned char Mask2[3] = {227, 196, 52}; |
| 3926 | |
| 3927 | Constant *Mask1Constant = Ctx->getConstantInt32(Mask1[Index - 1]); |
| 3928 | Constant *Mask2Constant = Ctx->getConstantInt32(Mask2[Index - 1]); |
| 3929 | |
| 3930 | if (Index == 1) { |
| 3931 | _shufps(ElementR, SourceVectRM, Mask1Constant); |
| 3932 | _shufps(ElementR, SourceVectRM, Mask2Constant); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3933 | _movp(Instr->getDest(), ElementR); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3934 | } else { |
| 3935 | Variable *T = makeReg(Ty); |
| 3936 | _movp(T, SourceVectRM); |
| 3937 | _shufps(ElementR, T, Mask1Constant); |
| 3938 | _shufps(T, ElementR, Mask2Constant); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3939 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3940 | } |
| 3941 | } else { |
| 3942 | assert(Ty == IceType_v16i8 || Ty == IceType_v16i1); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3943 | // Spill the value to a stack slot and perform the insertion in memory. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3944 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3945 | // TODO(wala): use legalize(SourceVectNotLegalized, Legal_Mem) when support |
| 3946 | // for legalizing to mem is implemented. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3947 | Variable *Slot = Func->makeVariable(Ty); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3948 | Slot->setMustNotHaveReg(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3949 | _movp(Slot, legalizeToReg(SourceVectNotLegalized)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3950 | |
| 3951 | // Compute the location of the position to insert in memory. |
| 3952 | unsigned Offset = Index * typeWidthInBytes(InVectorElementTy); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3953 | X86OperandMem *Loc = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3954 | getMemoryOperandForStackSlot(InVectorElementTy, Slot, Offset); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3955 | _store(legalizeToReg(ElementToInsertNotLegalized), Loc); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3956 | |
| 3957 | Variable *T = makeReg(Ty); |
| 3958 | _movp(T, Slot); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3959 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3960 | } |
| 3961 | } |
| 3962 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3963 | template <typename TraitsType> |
| 3964 | void TargetX86Base<TraitsType>::lowerIntrinsicCall( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3965 | const InstIntrinsicCall *Instr) { |
| 3966 | switch (Intrinsics::IntrinsicID ID = Instr->getIntrinsicInfo().ID) { |
| 3967 | case Intrinsics::AtomicCmpxchg: { |
| 3968 | if (!Intrinsics::isMemoryOrderValid( |
| 3969 | ID, getConstantMemoryOrder(Instr->getArg(3)), |
| 3970 | getConstantMemoryOrder(Instr->getArg(4)))) { |
| 3971 | Func->setError("Unexpected memory ordering for AtomicCmpxchg"); |
| 3972 | return; |
| 3973 | } |
| 3974 | Variable *DestPrev = Instr->getDest(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 3975 | Operand *PtrToMem = legalize(Instr->getArg(0)); |
| 3976 | Operand *Expected = legalize(Instr->getArg(1)); |
| 3977 | Operand *Desired = legalize(Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3978 | if (tryOptimizedCmpxchgCmpBr(DestPrev, PtrToMem, Expected, Desired)) |
| 3979 | return; |
| 3980 | lowerAtomicCmpxchg(DestPrev, PtrToMem, Expected, Desired); |
| 3981 | return; |
| 3982 | } |
| 3983 | case Intrinsics::AtomicFence: |
| 3984 | if (!Intrinsics::isMemoryOrderValid( |
| 3985 | ID, getConstantMemoryOrder(Instr->getArg(0)))) { |
| 3986 | Func->setError("Unexpected memory ordering for AtomicFence"); |
| 3987 | return; |
| 3988 | } |
| 3989 | _mfence(); |
| 3990 | return; |
| 3991 | case Intrinsics::AtomicFenceAll: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3992 | // NOTE: FenceAll should prevent and load/store from being moved across the |
| 3993 | // fence (both atomic and non-atomic). The InstX8632Mfence instruction is |
| 3994 | // currently marked coarsely as "HasSideEffects". |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3995 | _mfence(); |
| 3996 | return; |
| 3997 | case Intrinsics::AtomicIsLockFree: { |
| 3998 | // X86 is always lock free for 8/16/32/64 bit accesses. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3999 | // TODO(jvoung): Since the result is constant when given a constant byte |
| 4000 | // size, this opens up DCE opportunities. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4001 | Operand *ByteSize = Instr->getArg(0); |
| 4002 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4003 | if (auto *CI = llvm::dyn_cast<ConstantInteger32>(ByteSize)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4004 | Constant *Result; |
| 4005 | switch (CI->getValue()) { |
| 4006 | default: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4007 | // Some x86-64 processors support the cmpxchg16b instruction, which can |
| 4008 | // make 16-byte operations lock free (when used with the LOCK prefix). |
| 4009 | // However, that's not supported in 32-bit mode, so just return 0 even |
| 4010 | // for large sizes. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4011 | Result = Ctx->getConstantZero(IceType_i32); |
| 4012 | break; |
| 4013 | case 1: |
| 4014 | case 2: |
| 4015 | case 4: |
| 4016 | case 8: |
| 4017 | Result = Ctx->getConstantInt32(1); |
| 4018 | break; |
| 4019 | } |
| 4020 | _mov(Dest, Result); |
| 4021 | return; |
| 4022 | } |
| 4023 | // The PNaCl ABI requires the byte size to be a compile-time constant. |
| 4024 | Func->setError("AtomicIsLockFree byte size should be compile-time const"); |
| 4025 | return; |
| 4026 | } |
| 4027 | case Intrinsics::AtomicLoad: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4028 | // We require the memory address to be naturally aligned. Given that is the |
| 4029 | // case, then normal loads are atomic. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4030 | if (!Intrinsics::isMemoryOrderValid( |
| 4031 | ID, getConstantMemoryOrder(Instr->getArg(1)))) { |
| 4032 | Func->setError("Unexpected memory ordering for AtomicLoad"); |
| 4033 | return; |
| 4034 | } |
| 4035 | Variable *Dest = Instr->getDest(); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4036 | if (!Traits::Is64Bit) { |
| 4037 | if (auto *Dest64On32 = llvm::dyn_cast<Variable64On32>(Dest)) { |
| 4038 | // Follow what GCC does and use a movq instead of what lowerLoad() |
| 4039 | // normally does (split the load into two). Thus, this skips |
| 4040 | // load/arithmetic op folding. Load/arithmetic folding can't happen |
| 4041 | // anyway, since this is x86-32 and integer arithmetic only happens on |
| 4042 | // 32-bit quantities. |
| 4043 | Variable *T = makeReg(IceType_f64); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4044 | X86OperandMem *Addr = formMemoryOperand(Instr->getArg(0), IceType_f64); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4045 | _movq(T, Addr); |
| 4046 | // Then cast the bits back out of the XMM register to the i64 Dest. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4047 | auto *Cast = InstCast::create(Func, InstCast::Bitcast, Dest, T); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4048 | lowerCast(Cast); |
| 4049 | // Make sure that the atomic load isn't elided when unused. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4050 | Context.insert<InstFakeUse>(Dest64On32->getLo()); |
| 4051 | Context.insert<InstFakeUse>(Dest64On32->getHi()); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4052 | return; |
| 4053 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4054 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4055 | auto *Load = InstLoad::create(Func, Dest, Instr->getArg(0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4056 | lowerLoad(Load); |
| 4057 | // Make sure the atomic load isn't elided when unused, by adding a FakeUse. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4058 | // Since lowerLoad may fuse the load w/ an arithmetic instruction, insert |
| 4059 | // the FakeUse on the last-inserted instruction's dest. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4060 | Context.insert<InstFakeUse>(Context.getLastInserted()->getDest()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4061 | return; |
| 4062 | } |
| 4063 | case Intrinsics::AtomicRMW: |
| 4064 | if (!Intrinsics::isMemoryOrderValid( |
| 4065 | ID, getConstantMemoryOrder(Instr->getArg(3)))) { |
| 4066 | Func->setError("Unexpected memory ordering for AtomicRMW"); |
| 4067 | return; |
| 4068 | } |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 4069 | lowerAtomicRMW( |
| 4070 | Instr->getDest(), |
| 4071 | static_cast<uint32_t>( |
| 4072 | llvm::cast<ConstantInteger32>(Instr->getArg(0))->getValue()), |
| 4073 | Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4074 | return; |
| 4075 | case Intrinsics::AtomicStore: { |
| 4076 | if (!Intrinsics::isMemoryOrderValid( |
| 4077 | ID, getConstantMemoryOrder(Instr->getArg(2)))) { |
| 4078 | Func->setError("Unexpected memory ordering for AtomicStore"); |
| 4079 | return; |
| 4080 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4081 | // We require the memory address to be naturally aligned. Given that is the |
| 4082 | // case, then normal stores are atomic. Add a fence after the store to make |
| 4083 | // it visible. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4084 | Operand *Value = Instr->getArg(0); |
| 4085 | Operand *Ptr = Instr->getArg(1); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4086 | if (!Traits::Is64Bit && Value->getType() == IceType_i64) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4087 | // Use a movq instead of what lowerStore() normally does (split the store |
| 4088 | // into two), following what GCC does. Cast the bits from int -> to an |
| 4089 | // xmm register first. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4090 | Variable *T = makeReg(IceType_f64); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4091 | auto *Cast = InstCast::create(Func, InstCast::Bitcast, T, Value); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4092 | lowerCast(Cast); |
| 4093 | // Then store XMM w/ a movq. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4094 | X86OperandMem *Addr = formMemoryOperand(Ptr, IceType_f64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4095 | _storeq(T, Addr); |
| 4096 | _mfence(); |
| 4097 | return; |
| 4098 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4099 | auto *Store = InstStore::create(Func, Value, Ptr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4100 | lowerStore(Store); |
| 4101 | _mfence(); |
| 4102 | return; |
| 4103 | } |
| 4104 | case Intrinsics::Bswap: { |
| 4105 | Variable *Dest = Instr->getDest(); |
| 4106 | Operand *Val = Instr->getArg(0); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4107 | // In 32-bit mode, bswap only works on 32-bit arguments, and the argument |
| 4108 | // must be a register. Use rotate left for 16-bit bswap. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4109 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 4110 | Val = legalizeUndef(Val); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4111 | Variable *T_Lo = legalizeToReg(loOperand(Val)); |
| 4112 | Variable *T_Hi = legalizeToReg(hiOperand(Val)); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4113 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4114 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4115 | _bswap(T_Lo); |
| 4116 | _bswap(T_Hi); |
| 4117 | _mov(DestLo, T_Hi); |
| 4118 | _mov(DestHi, T_Lo); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4119 | } else if ((Traits::Is64Bit && Val->getType() == IceType_i64) || |
| 4120 | Val->getType() == IceType_i32) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4121 | Variable *T = legalizeToReg(Val); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4122 | _bswap(T); |
| 4123 | _mov(Dest, T); |
| 4124 | } else { |
| 4125 | assert(Val->getType() == IceType_i16); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4126 | Constant *Eight = Ctx->getConstantInt16(8); |
| 4127 | Variable *T = nullptr; |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 4128 | Val = legalize(Val); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4129 | _mov(T, Val); |
| 4130 | _rol(T, Eight); |
| 4131 | _mov(Dest, T); |
| 4132 | } |
| 4133 | return; |
| 4134 | } |
| 4135 | case Intrinsics::Ctpop: { |
| 4136 | Variable *Dest = Instr->getDest(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4137 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4138 | Operand *Val = Instr->getArg(0); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4139 | Type ValTy = Val->getType(); |
| 4140 | assert(ValTy == IceType_i32 || ValTy == IceType_i64); |
| 4141 | |
| 4142 | if (!Traits::Is64Bit) { |
| 4143 | T = Dest; |
| 4144 | } else { |
| 4145 | T = makeReg(IceType_i64); |
| 4146 | if (ValTy == IceType_i32) { |
| 4147 | // in x86-64, __popcountsi2 is not defined, so we cheat a bit by |
| 4148 | // converting it to a 64-bit value, and using ctpop_i64. _movzx should |
| 4149 | // ensure we will not have any bits set on Val's upper 32 bits. |
| 4150 | Variable *V = makeReg(IceType_i64); |
| 4151 | _movzx(V, Val); |
| 4152 | Val = V; |
| 4153 | } |
| 4154 | ValTy = IceType_i64; |
| 4155 | } |
| 4156 | |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4157 | InstCall *Call = |
| 4158 | makeHelperCall(ValTy == IceType_i32 ? RuntimeHelper::H_call_ctpop_i32 |
| 4159 | : RuntimeHelper::H_call_ctpop_i64, |
| 4160 | T, 1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4161 | Call->addArg(Val); |
| 4162 | lowerCall(Call); |
| 4163 | // The popcount helpers always return 32-bit values, while the intrinsic's |
| 4164 | // signature matches the native POPCNT instruction and fills a 64-bit reg |
| 4165 | // (in 64-bit mode). Thus, clear the upper bits of the dest just in case |
| 4166 | // the user doesn't do that in the IR. If the user does that in the IR, |
| 4167 | // then this zero'ing instruction is dead and gets optimized out. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4168 | if (!Traits::Is64Bit) { |
| 4169 | assert(T == Dest); |
| 4170 | if (Val->getType() == IceType_i64) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4171 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4172 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 4173 | _mov(DestHi, Zero); |
| 4174 | } |
| 4175 | } else { |
| 4176 | assert(Val->getType() == IceType_i64); |
| 4177 | // T is 64 bit. It needs to be copied to dest. We need to: |
| 4178 | // |
| 4179 | // T_1.32 = trunc T.64 to i32 |
| 4180 | // T_2.64 = zext T_1.32 to i64 |
| 4181 | // Dest.<<right_size>> = T_2.<<right_size>> |
| 4182 | // |
| 4183 | // which ensures the upper 32 bits will always be cleared. Just doing a |
| 4184 | // |
| 4185 | // mov Dest.32 = trunc T.32 to i32 |
| 4186 | // |
| 4187 | // is dangerous because there's a chance the compiler will optimize this |
| 4188 | // copy out. To use _movzx we need two new registers (one 32-, and |
| 4189 | // another 64-bit wide.) |
| 4190 | Variable *T_1 = makeReg(IceType_i32); |
| 4191 | _mov(T_1, T); |
| 4192 | Variable *T_2 = makeReg(IceType_i64); |
| 4193 | _movzx(T_2, T_1); |
| 4194 | _mov(Dest, T_2); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4195 | } |
| 4196 | return; |
| 4197 | } |
| 4198 | case Intrinsics::Ctlz: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4199 | // The "is zero undef" parameter is ignored and we always return a |
| 4200 | // well-defined value. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4201 | Operand *Val = legalize(Instr->getArg(0)); |
| 4202 | Operand *FirstVal; |
| 4203 | Operand *SecondVal = nullptr; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4204 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4205 | FirstVal = loOperand(Val); |
| 4206 | SecondVal = hiOperand(Val); |
| 4207 | } else { |
| 4208 | FirstVal = Val; |
| 4209 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4210 | constexpr bool IsCttz = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4211 | lowerCountZeros(IsCttz, Val->getType(), Instr->getDest(), FirstVal, |
| 4212 | SecondVal); |
| 4213 | return; |
| 4214 | } |
| 4215 | case Intrinsics::Cttz: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4216 | // The "is zero undef" parameter is ignored and we always return a |
| 4217 | // well-defined value. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4218 | Operand *Val = legalize(Instr->getArg(0)); |
| 4219 | Operand *FirstVal; |
| 4220 | Operand *SecondVal = nullptr; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4221 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4222 | FirstVal = hiOperand(Val); |
| 4223 | SecondVal = loOperand(Val); |
| 4224 | } else { |
| 4225 | FirstVal = Val; |
| 4226 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4227 | constexpr bool IsCttz = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4228 | lowerCountZeros(IsCttz, Val->getType(), Instr->getDest(), FirstVal, |
| 4229 | SecondVal); |
| 4230 | return; |
| 4231 | } |
| 4232 | case Intrinsics::Fabs: { |
| 4233 | Operand *Src = legalize(Instr->getArg(0)); |
| 4234 | Type Ty = Src->getType(); |
| 4235 | Variable *Dest = Instr->getDest(); |
| 4236 | Variable *T = makeVectorOfFabsMask(Ty); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4237 | // The pand instruction operates on an m128 memory operand, so if Src is an |
| 4238 | // f32 or f64, we need to make sure it's in a register. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4239 | if (isVectorType(Ty)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4240 | if (llvm::isa<X86OperandMem>(Src)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4241 | Src = legalizeToReg(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4242 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4243 | Src = legalizeToReg(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4244 | } |
| 4245 | _pand(T, Src); |
| 4246 | if (isVectorType(Ty)) |
| 4247 | _movp(Dest, T); |
| 4248 | else |
| 4249 | _mov(Dest, T); |
| 4250 | return; |
| 4251 | } |
| 4252 | case Intrinsics::Longjmp: { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4253 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_longjmp, nullptr, 2); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4254 | Call->addArg(Instr->getArg(0)); |
| 4255 | Call->addArg(Instr->getArg(1)); |
| 4256 | lowerCall(Call); |
| 4257 | return; |
| 4258 | } |
| 4259 | case Intrinsics::Memcpy: { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4260 | lowerMemcpy(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4261 | return; |
| 4262 | } |
| 4263 | case Intrinsics::Memmove: { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4264 | lowerMemmove(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4265 | return; |
| 4266 | } |
| 4267 | case Intrinsics::Memset: { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4268 | lowerMemset(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4269 | return; |
| 4270 | } |
| 4271 | case Intrinsics::NaClReadTP: { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4272 | if (NeedSandboxing) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4273 | Operand *Src = |
| 4274 | dispatchToConcrete(&ConcreteTarget::createNaClReadTPSrcOperand); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4275 | Variable *Dest = Instr->getDest(); |
| 4276 | Variable *T = nullptr; |
| 4277 | _mov(T, Src); |
| 4278 | _mov(Dest, T); |
| 4279 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4280 | InstCall *Call = |
| 4281 | makeHelperCall(RuntimeHelper::H_call_read_tp, Instr->getDest(), 0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4282 | lowerCall(Call); |
| 4283 | } |
| 4284 | return; |
| 4285 | } |
| 4286 | case Intrinsics::Setjmp: { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4287 | InstCall *Call = |
| 4288 | makeHelperCall(RuntimeHelper::H_call_setjmp, Instr->getDest(), 1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4289 | Call->addArg(Instr->getArg(0)); |
| 4290 | lowerCall(Call); |
| 4291 | return; |
| 4292 | } |
| 4293 | case Intrinsics::Sqrt: { |
| 4294 | Operand *Src = legalize(Instr->getArg(0)); |
| 4295 | Variable *Dest = Instr->getDest(); |
| 4296 | Variable *T = makeReg(Dest->getType()); |
| 4297 | _sqrtss(T, Src); |
| 4298 | _mov(Dest, T); |
| 4299 | return; |
| 4300 | } |
| 4301 | case Intrinsics::Stacksave: { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4302 | if (!Traits::Is64Bit || !NeedSandboxing) { |
| 4303 | Variable *esp = Func->getTarget()->getPhysicalRegister(getStackReg(), |
| 4304 | Traits::WordType); |
| 4305 | Variable *Dest = Instr->getDest(); |
| 4306 | _mov(Dest, esp); |
| 4307 | return; |
| 4308 | } |
| 4309 | Variable *esp = Func->getTarget()->getPhysicalRegister( |
| 4310 | Traits::RegisterSet::Reg_esp, IceType_i32); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4311 | Variable *Dest = Instr->getDest(); |
| 4312 | _mov(Dest, esp); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4313 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4314 | return; |
| 4315 | } |
| 4316 | case Intrinsics::Stackrestore: { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 4317 | Operand *Src = Instr->getArg(0); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4318 | _mov_sp(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4319 | return; |
| 4320 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4321 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4322 | case Intrinsics::Trap: |
| 4323 | _ud2(); |
| 4324 | return; |
| 4325 | case Intrinsics::UnknownIntrinsic: |
| 4326 | Func->setError("Should not be lowering UnknownIntrinsic"); |
| 4327 | return; |
| 4328 | } |
| 4329 | return; |
| 4330 | } |
| 4331 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4332 | template <typename TraitsType> |
| 4333 | void TargetX86Base<TraitsType>::lowerAtomicCmpxchg(Variable *DestPrev, |
| 4334 | Operand *Ptr, |
| 4335 | Operand *Expected, |
| 4336 | Operand *Desired) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4337 | Type Ty = Expected->getType(); |
| 4338 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4339 | // Reserve the pre-colored registers first, before adding any more |
| 4340 | // infinite-weight variables from formMemoryOperand's legalization. |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4341 | Variable *T_edx = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 4342 | Variable *T_eax = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 4343 | Variable *T_ecx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ecx); |
| 4344 | Variable *T_ebx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ebx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4345 | _mov(T_eax, loOperand(Expected)); |
| 4346 | _mov(T_edx, hiOperand(Expected)); |
| 4347 | _mov(T_ebx, loOperand(Desired)); |
| 4348 | _mov(T_ecx, hiOperand(Desired)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4349 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4350 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4351 | _cmpxchg8b(Addr, T_edx, T_eax, T_ecx, T_ebx, Locked); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4352 | auto *DestLo = llvm::cast<Variable>(loOperand(DestPrev)); |
| 4353 | auto *DestHi = llvm::cast<Variable>(hiOperand(DestPrev)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4354 | _mov(DestLo, T_eax); |
| 4355 | _mov(DestHi, T_edx); |
| 4356 | return; |
| 4357 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 4358 | RegNumT Eax; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4359 | switch (Ty) { |
| 4360 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4361 | llvm::report_fatal_error("Bad type for cmpxchg"); |
| 4362 | case IceType_i64: |
| 4363 | Eax = Traits::getRaxOrDie(); |
| 4364 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4365 | case IceType_i32: |
| 4366 | Eax = Traits::RegisterSet::Reg_eax; |
| 4367 | break; |
| 4368 | case IceType_i16: |
| 4369 | Eax = Traits::RegisterSet::Reg_ax; |
| 4370 | break; |
| 4371 | case IceType_i8: |
| 4372 | Eax = Traits::RegisterSet::Reg_al; |
| 4373 | break; |
| 4374 | } |
| 4375 | Variable *T_eax = makeReg(Ty, Eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4376 | _mov(T_eax, Expected); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4377 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4378 | Variable *DesiredReg = legalizeToReg(Desired); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4379 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4380 | _cmpxchg(Addr, T_eax, DesiredReg, Locked); |
| 4381 | _mov(DestPrev, T_eax); |
| 4382 | } |
| 4383 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4384 | template <typename TraitsType> |
| 4385 | bool TargetX86Base<TraitsType>::tryOptimizedCmpxchgCmpBr(Variable *Dest, |
| 4386 | Operand *PtrToMem, |
| 4387 | Operand *Expected, |
| 4388 | Operand *Desired) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 4389 | if (Func->getOptLevel() == Opt_m1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4390 | return false; |
| 4391 | // Peek ahead a few instructions and see how Dest is used. |
| 4392 | // It's very common to have: |
| 4393 | // |
| 4394 | // %x = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* ptr, i32 %expected, ...) |
| 4395 | // [%y_phi = ...] // list of phi stores |
| 4396 | // %p = icmp eq i32 %x, %expected |
| 4397 | // br i1 %p, label %l1, label %l2 |
| 4398 | // |
| 4399 | // which we can optimize into: |
| 4400 | // |
| 4401 | // %x = <cmpxchg code> |
| 4402 | // [%y_phi = ...] // list of phi stores |
| 4403 | // br eq, %l1, %l2 |
| 4404 | InstList::iterator I = Context.getCur(); |
| 4405 | // I is currently the InstIntrinsicCall. Peek past that. |
| 4406 | // This assumes that the atomic cmpxchg has not been lowered yet, |
| 4407 | // so that the instructions seen in the scan from "Cur" is simple. |
| 4408 | assert(llvm::isa<InstIntrinsicCall>(*I)); |
| 4409 | Inst *NextInst = Context.getNextInst(I); |
| 4410 | if (!NextInst) |
| 4411 | return false; |
| 4412 | // There might be phi assignments right before the compare+branch, since this |
| 4413 | // could be a backward branch for a loop. This placement of assignments is |
| 4414 | // determined by placePhiStores(). |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 4415 | CfgVector<InstAssign *> PhiAssigns; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4416 | while (auto *PhiAssign = llvm::dyn_cast<InstAssign>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4417 | if (PhiAssign->getDest() == Dest) |
| 4418 | return false; |
| 4419 | PhiAssigns.push_back(PhiAssign); |
| 4420 | NextInst = Context.getNextInst(I); |
| 4421 | if (!NextInst) |
| 4422 | return false; |
| 4423 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4424 | if (auto *NextCmp = llvm::dyn_cast<InstIcmp>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4425 | if (!(NextCmp->getCondition() == InstIcmp::Eq && |
| 4426 | ((NextCmp->getSrc(0) == Dest && NextCmp->getSrc(1) == Expected) || |
| 4427 | (NextCmp->getSrc(1) == Dest && NextCmp->getSrc(0) == Expected)))) { |
| 4428 | return false; |
| 4429 | } |
| 4430 | NextInst = Context.getNextInst(I); |
| 4431 | if (!NextInst) |
| 4432 | return false; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4433 | if (auto *NextBr = llvm::dyn_cast<InstBr>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4434 | if (!NextBr->isUnconditional() && |
| 4435 | NextCmp->getDest() == NextBr->getCondition() && |
| 4436 | NextBr->isLastUse(NextCmp->getDest())) { |
| 4437 | lowerAtomicCmpxchg(Dest, PtrToMem, Expected, Desired); |
| 4438 | for (size_t i = 0; i < PhiAssigns.size(); ++i) { |
| 4439 | // Lower the phi assignments now, before the branch (same placement |
| 4440 | // as before). |
| 4441 | InstAssign *PhiAssign = PhiAssigns[i]; |
| 4442 | PhiAssign->setDeleted(); |
| 4443 | lowerAssign(PhiAssign); |
| 4444 | Context.advanceNext(); |
| 4445 | } |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4446 | _br(Traits::Cond::Br_e, NextBr->getTargetTrue(), |
| 4447 | NextBr->getTargetFalse()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4448 | // Skip over the old compare and branch, by deleting them. |
| 4449 | NextCmp->setDeleted(); |
| 4450 | NextBr->setDeleted(); |
| 4451 | Context.advanceNext(); |
| 4452 | Context.advanceNext(); |
| 4453 | return true; |
| 4454 | } |
| 4455 | } |
| 4456 | } |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4460 | template <typename TraitsType> |
| 4461 | void TargetX86Base<TraitsType>::lowerAtomicRMW(Variable *Dest, |
| 4462 | uint32_t Operation, Operand *Ptr, |
| 4463 | Operand *Val) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4464 | bool NeedsCmpxchg = false; |
| 4465 | LowerBinOp Op_Lo = nullptr; |
| 4466 | LowerBinOp Op_Hi = nullptr; |
| 4467 | switch (Operation) { |
| 4468 | default: |
| 4469 | Func->setError("Unknown AtomicRMW operation"); |
| 4470 | return; |
| 4471 | case Intrinsics::AtomicAdd: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4472 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4473 | // All the fall-through paths must set this to true, but use this |
| 4474 | // for asserting. |
| 4475 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4476 | Op_Lo = &TargetX86Base<TraitsType>::_add; |
| 4477 | Op_Hi = &TargetX86Base<TraitsType>::_adc; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4478 | break; |
| 4479 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4480 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4481 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4482 | Variable *T = nullptr; |
| 4483 | _mov(T, Val); |
| 4484 | _xadd(Addr, T, Locked); |
| 4485 | _mov(Dest, T); |
| 4486 | return; |
| 4487 | } |
| 4488 | case Intrinsics::AtomicSub: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4489 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4490 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4491 | Op_Lo = &TargetX86Base<TraitsType>::_sub; |
| 4492 | Op_Hi = &TargetX86Base<TraitsType>::_sbb; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4493 | break; |
| 4494 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4495 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4496 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4497 | Variable *T = nullptr; |
| 4498 | _mov(T, Val); |
| 4499 | _neg(T); |
| 4500 | _xadd(Addr, T, Locked); |
| 4501 | _mov(Dest, T); |
| 4502 | return; |
| 4503 | } |
| 4504 | case Intrinsics::AtomicOr: |
| 4505 | // TODO(jvoung): If Dest is null or dead, then some of these |
| 4506 | // operations do not need an "exchange", but just a locked op. |
| 4507 | // That appears to be "worth" it for sub, or, and, and xor. |
| 4508 | // xadd is probably fine vs lock add for add, and xchg is fine |
| 4509 | // vs an atomic store. |
| 4510 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4511 | Op_Lo = &TargetX86Base<TraitsType>::_or; |
| 4512 | Op_Hi = &TargetX86Base<TraitsType>::_or; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4513 | break; |
| 4514 | case Intrinsics::AtomicAnd: |
| 4515 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4516 | Op_Lo = &TargetX86Base<TraitsType>::_and; |
| 4517 | Op_Hi = &TargetX86Base<TraitsType>::_and; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4518 | break; |
| 4519 | case Intrinsics::AtomicXor: |
| 4520 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4521 | Op_Lo = &TargetX86Base<TraitsType>::_xor; |
| 4522 | Op_Hi = &TargetX86Base<TraitsType>::_xor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4523 | break; |
| 4524 | case Intrinsics::AtomicExchange: |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4525 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4526 | NeedsCmpxchg = true; |
| 4527 | // NeedsCmpxchg, but no real Op_Lo/Op_Hi need to be done. The values |
| 4528 | // just need to be moved to the ecx and ebx registers. |
| 4529 | Op_Lo = nullptr; |
| 4530 | Op_Hi = nullptr; |
| 4531 | break; |
| 4532 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4533 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4534 | Variable *T = nullptr; |
| 4535 | _mov(T, Val); |
| 4536 | _xchg(Addr, T); |
| 4537 | _mov(Dest, T); |
| 4538 | return; |
| 4539 | } |
| 4540 | // Otherwise, we need a cmpxchg loop. |
| 4541 | (void)NeedsCmpxchg; |
| 4542 | assert(NeedsCmpxchg); |
| 4543 | expandAtomicRMWAsCmpxchg(Op_Lo, Op_Hi, Dest, Ptr, Val); |
| 4544 | } |
| 4545 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4546 | template <typename TraitsType> |
| 4547 | void TargetX86Base<TraitsType>::expandAtomicRMWAsCmpxchg(LowerBinOp Op_Lo, |
| 4548 | LowerBinOp Op_Hi, |
| 4549 | Variable *Dest, |
| 4550 | Operand *Ptr, |
| 4551 | Operand *Val) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4552 | // Expand a more complex RMW operation as a cmpxchg loop: |
| 4553 | // For 64-bit: |
| 4554 | // mov eax, [ptr] |
| 4555 | // mov edx, [ptr + 4] |
| 4556 | // .LABEL: |
| 4557 | // mov ebx, eax |
| 4558 | // <Op_Lo> ebx, <desired_adj_lo> |
| 4559 | // mov ecx, edx |
| 4560 | // <Op_Hi> ecx, <desired_adj_hi> |
| 4561 | // lock cmpxchg8b [ptr] |
| 4562 | // jne .LABEL |
| 4563 | // mov <dest_lo>, eax |
| 4564 | // mov <dest_lo>, edx |
| 4565 | // |
| 4566 | // For 32-bit: |
| 4567 | // mov eax, [ptr] |
| 4568 | // .LABEL: |
| 4569 | // mov <reg>, eax |
| 4570 | // op <reg>, [desired_adj] |
| 4571 | // lock cmpxchg [ptr], <reg> |
| 4572 | // jne .LABEL |
| 4573 | // mov <dest>, eax |
| 4574 | // |
| 4575 | // If Op_{Lo,Hi} are nullptr, then just copy the value. |
| 4576 | Val = legalize(Val); |
| 4577 | Type Ty = Val->getType(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4578 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4579 | Variable *T_edx = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 4580 | Variable *T_eax = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4581 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4582 | _mov(T_eax, loOperand(Addr)); |
| 4583 | _mov(T_edx, hiOperand(Addr)); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4584 | Variable *T_ecx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ecx); |
| 4585 | Variable *T_ebx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ebx); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4586 | InstX86Label *Label = InstX86Label::create(Func, this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4587 | const bool IsXchg8b = Op_Lo == nullptr && Op_Hi == nullptr; |
| 4588 | if (!IsXchg8b) { |
| 4589 | Context.insert(Label); |
| 4590 | _mov(T_ebx, T_eax); |
| 4591 | (this->*Op_Lo)(T_ebx, loOperand(Val)); |
| 4592 | _mov(T_ecx, T_edx); |
| 4593 | (this->*Op_Hi)(T_ecx, hiOperand(Val)); |
| 4594 | } else { |
| 4595 | // This is for xchg, which doesn't need an actual Op_Lo/Op_Hi. |
| 4596 | // It just needs the Val loaded into ebx and ecx. |
| 4597 | // That can also be done before the loop. |
| 4598 | _mov(T_ebx, loOperand(Val)); |
| 4599 | _mov(T_ecx, hiOperand(Val)); |
| 4600 | Context.insert(Label); |
| 4601 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4602 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4603 | _cmpxchg8b(Addr, T_edx, T_eax, T_ecx, T_ebx, Locked); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4604 | _br(Traits::Cond::Br_ne, Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4605 | if (!IsXchg8b) { |
| 4606 | // If Val is a variable, model the extended live range of Val through |
| 4607 | // the end of the loop, since it will be re-used by the loop. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4608 | if (auto *ValVar = llvm::dyn_cast<Variable>(Val)) { |
| 4609 | auto *ValLo = llvm::cast<Variable>(loOperand(ValVar)); |
| 4610 | auto *ValHi = llvm::cast<Variable>(hiOperand(ValVar)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4611 | Context.insert<InstFakeUse>(ValLo); |
| 4612 | Context.insert<InstFakeUse>(ValHi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } else { |
| 4615 | // For xchg, the loop is slightly smaller and ebx/ecx are used. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4616 | Context.insert<InstFakeUse>(T_ebx); |
| 4617 | Context.insert<InstFakeUse>(T_ecx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4618 | } |
| 4619 | // The address base (if any) is also reused in the loop. |
| 4620 | if (Variable *Base = Addr->getBase()) |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4621 | Context.insert<InstFakeUse>(Base); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4622 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4623 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4624 | _mov(DestLo, T_eax); |
| 4625 | _mov(DestHi, T_edx); |
| 4626 | return; |
| 4627 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4628 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 4629 | RegNumT Eax; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4630 | switch (Ty) { |
| 4631 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4632 | llvm::report_fatal_error("Bad type for atomicRMW"); |
| 4633 | case IceType_i64: |
| 4634 | Eax = Traits::getRaxOrDie(); |
| 4635 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4636 | case IceType_i32: |
| 4637 | Eax = Traits::RegisterSet::Reg_eax; |
| 4638 | break; |
| 4639 | case IceType_i16: |
| 4640 | Eax = Traits::RegisterSet::Reg_ax; |
| 4641 | break; |
| 4642 | case IceType_i8: |
| 4643 | Eax = Traits::RegisterSet::Reg_al; |
| 4644 | break; |
| 4645 | } |
| 4646 | Variable *T_eax = makeReg(Ty, Eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4647 | _mov(T_eax, Addr); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4648 | auto *Label = Context.insert<InstX86Label>(this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4649 | // We want to pick a different register for T than Eax, so don't use |
| 4650 | // _mov(T == nullptr, T_eax). |
| 4651 | Variable *T = makeReg(Ty); |
| 4652 | _mov(T, T_eax); |
| 4653 | (this->*Op_Lo)(T, Val); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4654 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4655 | _cmpxchg(Addr, T_eax, T, Locked); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4656 | _br(Traits::Cond::Br_ne, Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4657 | // If Val is a variable, model the extended live range of Val through |
| 4658 | // the end of the loop, since it will be re-used by the loop. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4659 | if (auto *ValVar = llvm::dyn_cast<Variable>(Val)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4660 | Context.insert<InstFakeUse>(ValVar); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4661 | } |
| 4662 | // The address base (if any) is also reused in the loop. |
| 4663 | if (Variable *Base = Addr->getBase()) |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4664 | Context.insert<InstFakeUse>(Base); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4665 | _mov(Dest, T_eax); |
| 4666 | } |
| 4667 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 4668 | /// Lowers count {trailing, leading} zeros intrinsic. |
| 4669 | /// |
| 4670 | /// We could do constant folding here, but that should have |
| 4671 | /// been done by the front-end/middle-end optimizations. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4672 | template <typename TraitsType> |
| 4673 | void TargetX86Base<TraitsType>::lowerCountZeros(bool Cttz, Type Ty, |
| 4674 | Variable *Dest, |
| 4675 | Operand *FirstVal, |
| 4676 | Operand *SecondVal) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4677 | // TODO(jvoung): Determine if the user CPU supports LZCNT (BMI). |
| 4678 | // Then the instructions will handle the Val == 0 case much more simply |
| 4679 | // and won't require conversion from bit position to number of zeros. |
| 4680 | // |
| 4681 | // Otherwise: |
| 4682 | // bsr IF_NOT_ZERO, Val |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4683 | // mov T_DEST, ((Ty == i32) ? 63 : 127) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4684 | // cmovne T_DEST, IF_NOT_ZERO |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4685 | // xor T_DEST, ((Ty == i32) ? 31 : 63) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4686 | // mov DEST, T_DEST |
| 4687 | // |
| 4688 | // NOTE: T_DEST must be a register because cmov requires its dest to be a |
| 4689 | // register. Also, bsf and bsr require their dest to be a register. |
| 4690 | // |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4691 | // The xor DEST, C(31|63) converts a bit position to # of leading zeroes. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4692 | // E.g., for 000... 00001100, bsr will say that the most significant bit |
| 4693 | // set is at position 3, while the number of leading zeros is 28. Xor is |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4694 | // like (M - N) for N <= M, and converts 63 to 32, and 127 to 64 (for the |
| 4695 | // all-zeros case). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4696 | // |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4697 | // X8632 only: Similar for 64-bit, but start w/ speculating that the upper 32 |
| 4698 | // bits are all zero, and compute the result for that case (checking the |
| 4699 | // lower 32 bits). Then actually compute the result for the upper bits and |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4700 | // cmov in the result from the lower computation if the earlier speculation |
| 4701 | // was correct. |
| 4702 | // |
| 4703 | // Cttz, is similar, but uses bsf instead, and doesn't require the xor |
| 4704 | // bit position conversion, and the speculation is reversed. |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4705 | |
| 4706 | // TODO(jpp): refactor this method. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4707 | assert(Ty == IceType_i32 || Ty == IceType_i64); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4708 | const Type DestTy = Traits::Is64Bit ? Dest->getType() : IceType_i32; |
| 4709 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4710 | Operand *FirstValRM = legalize(FirstVal, Legal_Mem | Legal_Reg); |
| 4711 | if (Cttz) { |
| 4712 | _bsf(T, FirstValRM); |
| 4713 | } else { |
| 4714 | _bsr(T, FirstValRM); |
| 4715 | } |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4716 | Variable *T_Dest = makeReg(DestTy); |
| 4717 | Constant *_31 = Ctx->getConstantInt32(31); |
| 4718 | Constant *_32 = Ctx->getConstantInt(DestTy, 32); |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4719 | Constant *_63 = Ctx->getConstantInt(DestTy, 63); |
| 4720 | Constant *_64 = Ctx->getConstantInt(DestTy, 64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4721 | if (Cttz) { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4722 | if (DestTy == IceType_i64) { |
| 4723 | _mov(T_Dest, _64); |
| 4724 | } else { |
| 4725 | _mov(T_Dest, _32); |
| 4726 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4727 | } else { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4728 | Constant *_127 = Ctx->getConstantInt(DestTy, 127); |
| 4729 | if (DestTy == IceType_i64) { |
| 4730 | _mov(T_Dest, _127); |
| 4731 | } else { |
| 4732 | _mov(T_Dest, _63); |
| 4733 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4734 | } |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4735 | _cmov(T_Dest, T, Traits::Cond::Br_ne); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4736 | if (!Cttz) { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4737 | if (DestTy == IceType_i64) { |
| 4738 | // Even though there's a _63 available at this point, that constant might |
| 4739 | // not be an i32, which will cause the xor emission to fail. |
| 4740 | Constant *_63 = Ctx->getConstantInt32(63); |
| 4741 | _xor(T_Dest, _63); |
| 4742 | } else { |
| 4743 | _xor(T_Dest, _31); |
| 4744 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4745 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4746 | if (Traits::Is64Bit || Ty == IceType_i32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4747 | _mov(Dest, T_Dest); |
| 4748 | return; |
| 4749 | } |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4750 | _add(T_Dest, _32); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4751 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4752 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4753 | // Will be using "test" on this, so we need a registerized variable. |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4754 | Variable *SecondVar = legalizeToReg(SecondVal); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4755 | Variable *T_Dest2 = makeReg(IceType_i32); |
| 4756 | if (Cttz) { |
| 4757 | _bsf(T_Dest2, SecondVar); |
| 4758 | } else { |
| 4759 | _bsr(T_Dest2, SecondVar); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4760 | _xor(T_Dest2, _31); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4761 | } |
| 4762 | _test(SecondVar, SecondVar); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4763 | _cmov(T_Dest2, T_Dest, Traits::Cond::Br_e); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4764 | _mov(DestLo, T_Dest2); |
| 4765 | _mov(DestHi, Ctx->getConstantZero(IceType_i32)); |
| 4766 | } |
| 4767 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4768 | template <typename TraitsType> |
| 4769 | void TargetX86Base<TraitsType>::typedLoad(Type Ty, Variable *Dest, |
| 4770 | Variable *Base, Constant *Offset) { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 4771 | // If Offset is a ConstantRelocatable in Non-SFI mode, we will need to |
| 4772 | // legalize Mem properly. |
| 4773 | if (Offset) |
| 4774 | assert(!llvm::isa<ConstantRelocatable>(Offset)); |
| 4775 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4776 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4777 | |
| 4778 | if (isVectorType(Ty)) |
| 4779 | _movp(Dest, Mem); |
| 4780 | else if (Ty == IceType_f64) |
| 4781 | _movq(Dest, Mem); |
| 4782 | else |
| 4783 | _mov(Dest, Mem); |
| 4784 | } |
| 4785 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4786 | template <typename TraitsType> |
| 4787 | void TargetX86Base<TraitsType>::typedStore(Type Ty, Variable *Value, |
| 4788 | Variable *Base, Constant *Offset) { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 4789 | // If Offset is a ConstantRelocatable in Non-SFI mode, we will need to |
| 4790 | // legalize Mem properly. |
| 4791 | if (Offset) |
| 4792 | assert(!llvm::isa<ConstantRelocatable>(Offset)); |
| 4793 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4794 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4795 | |
| 4796 | if (isVectorType(Ty)) |
| 4797 | _storep(Value, Mem); |
| 4798 | else if (Ty == IceType_f64) |
| 4799 | _storeq(Value, Mem); |
| 4800 | else |
| 4801 | _store(Value, Mem); |
| 4802 | } |
| 4803 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4804 | template <typename TraitsType> |
| 4805 | void TargetX86Base<TraitsType>::copyMemory(Type Ty, Variable *Dest, |
| 4806 | Variable *Src, int32_t OffsetAmt) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4807 | Constant *Offset = OffsetAmt ? Ctx->getConstantInt32(OffsetAmt) : nullptr; |
| 4808 | // TODO(ascull): this or add nullptr test to _movp, _movq |
| 4809 | Variable *Data = makeReg(Ty); |
| 4810 | |
| 4811 | typedLoad(Ty, Data, Src, Offset); |
| 4812 | typedStore(Ty, Data, Dest, Offset); |
| 4813 | } |
| 4814 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4815 | template <typename TraitsType> |
| 4816 | void TargetX86Base<TraitsType>::lowerMemcpy(Operand *Dest, Operand *Src, |
| 4817 | Operand *Count) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4818 | // There is a load and store for each chunk in the unroll |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4819 | constexpr uint32_t BytesPerStorep = 16; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4820 | |
| 4821 | // Check if the operands are constants |
| 4822 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4823 | const bool IsCountConst = CountConst != nullptr; |
| 4824 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4825 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4826 | if (shouldOptimizeMemIntrins() && IsCountConst && |
| 4827 | CountValue <= BytesPerStorep * Traits::MEMCPY_UNROLL_LIMIT) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4828 | // Unlikely, but nothing to do if it does happen |
| 4829 | if (CountValue == 0) |
| 4830 | return; |
| 4831 | |
| 4832 | Variable *SrcBase = legalizeToReg(Src); |
| 4833 | Variable *DestBase = legalizeToReg(Dest); |
| 4834 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4835 | // Find the largest type that can be used and use it as much as possible in |
| 4836 | // reverse order. Then handle any remainder with overlapping copies. Since |
| 4837 | // the remainder will be at the end, there will be reduced pressure on the |
| 4838 | // memory unit as the accesses to the same memory are far apart. |
| 4839 | Type Ty = largestTypeInSize(CountValue); |
| 4840 | uint32_t TyWidth = typeWidthInBytes(Ty); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4841 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4842 | uint32_t RemainingBytes = CountValue; |
| 4843 | int32_t Offset = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 4844 | while (RemainingBytes >= TyWidth) { |
| 4845 | copyMemory(Ty, DestBase, SrcBase, Offset); |
| 4846 | RemainingBytes -= TyWidth; |
| 4847 | Offset -= TyWidth; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4848 | } |
| 4849 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4850 | if (RemainingBytes == 0) |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4851 | return; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4852 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4853 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 4854 | // of overlaps in the copies. |
| 4855 | Type LeftOverTy = firstTypeThatFitsSize(RemainingBytes); |
| 4856 | Offset = CountValue - typeWidthInBytes(LeftOverTy); |
| 4857 | copyMemory(LeftOverTy, DestBase, SrcBase, Offset); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4858 | return; |
| 4859 | } |
| 4860 | |
| 4861 | // Fall back on a function call |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4862 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memcpy, nullptr, 3); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4863 | Call->addArg(Dest); |
| 4864 | Call->addArg(Src); |
| 4865 | Call->addArg(Count); |
| 4866 | lowerCall(Call); |
| 4867 | } |
| 4868 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4869 | template <typename TraitsType> |
| 4870 | void TargetX86Base<TraitsType>::lowerMemmove(Operand *Dest, Operand *Src, |
| 4871 | Operand *Count) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4872 | // There is a load and store for each chunk in the unroll |
| 4873 | constexpr uint32_t BytesPerStorep = 16; |
| 4874 | |
| 4875 | // Check if the operands are constants |
| 4876 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4877 | const bool IsCountConst = CountConst != nullptr; |
| 4878 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4879 | |
| 4880 | if (shouldOptimizeMemIntrins() && IsCountConst && |
| 4881 | CountValue <= BytesPerStorep * Traits::MEMMOVE_UNROLL_LIMIT) { |
| 4882 | // Unlikely, but nothing to do if it does happen |
| 4883 | if (CountValue == 0) |
| 4884 | return; |
| 4885 | |
| 4886 | Variable *SrcBase = legalizeToReg(Src); |
| 4887 | Variable *DestBase = legalizeToReg(Dest); |
| 4888 | |
| 4889 | std::tuple<Type, Constant *, Variable *> |
| 4890 | Moves[Traits::MEMMOVE_UNROLL_LIMIT]; |
| 4891 | Constant *Offset; |
| 4892 | Variable *Reg; |
| 4893 | |
| 4894 | // Copy the data into registers as the source and destination could overlap |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4895 | // so make sure not to clobber the memory. This also means overlapping |
| 4896 | // moves can be used as we are taking a safe snapshot of the memory. |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4897 | Type Ty = largestTypeInSize(CountValue); |
| 4898 | uint32_t TyWidth = typeWidthInBytes(Ty); |
| 4899 | |
| 4900 | uint32_t RemainingBytes = CountValue; |
| 4901 | int32_t OffsetAmt = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 4902 | size_t N = 0; |
| 4903 | while (RemainingBytes >= TyWidth) { |
| 4904 | assert(N <= Traits::MEMMOVE_UNROLL_LIMIT); |
| 4905 | Offset = Ctx->getConstantInt32(OffsetAmt); |
| 4906 | Reg = makeReg(Ty); |
| 4907 | typedLoad(Ty, Reg, SrcBase, Offset); |
| 4908 | RemainingBytes -= TyWidth; |
| 4909 | OffsetAmt -= TyWidth; |
| 4910 | Moves[N++] = std::make_tuple(Ty, Offset, Reg); |
| 4911 | } |
| 4912 | |
| 4913 | if (RemainingBytes != 0) { |
| 4914 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 4915 | // of overlaps in the copies. |
| 4916 | assert(N <= Traits::MEMMOVE_UNROLL_LIMIT); |
| 4917 | Ty = firstTypeThatFitsSize(RemainingBytes); |
| 4918 | Offset = Ctx->getConstantInt32(CountValue - typeWidthInBytes(Ty)); |
| 4919 | Reg = makeReg(Ty); |
| 4920 | typedLoad(Ty, Reg, SrcBase, Offset); |
| 4921 | Moves[N++] = std::make_tuple(Ty, Offset, Reg); |
| 4922 | } |
| 4923 | |
| 4924 | // Copy the data out into the destination memory |
| 4925 | for (size_t i = 0; i < N; ++i) { |
| 4926 | std::tie(Ty, Offset, Reg) = Moves[i]; |
| 4927 | typedStore(Ty, Reg, DestBase, Offset); |
| 4928 | } |
| 4929 | |
| 4930 | return; |
| 4931 | } |
| 4932 | |
| 4933 | // Fall back on a function call |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4934 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memmove, nullptr, 3); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4935 | Call->addArg(Dest); |
| 4936 | Call->addArg(Src); |
| 4937 | Call->addArg(Count); |
| 4938 | lowerCall(Call); |
| 4939 | } |
| 4940 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4941 | template <typename TraitsType> |
| 4942 | void TargetX86Base<TraitsType>::lowerMemset(Operand *Dest, Operand *Val, |
| 4943 | Operand *Count) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4944 | constexpr uint32_t BytesPerStorep = 16; |
| 4945 | constexpr uint32_t BytesPerStoreq = 8; |
| 4946 | constexpr uint32_t BytesPerStorei32 = 4; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4947 | assert(Val->getType() == IceType_i8); |
| 4948 | |
| 4949 | // Check if the operands are constants |
| 4950 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4951 | const auto *ValConst = llvm::dyn_cast<const ConstantInteger32>(Val); |
| 4952 | const bool IsCountConst = CountConst != nullptr; |
| 4953 | const bool IsValConst = ValConst != nullptr; |
| 4954 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4955 | const uint32_t ValValue = IsValConst ? ValConst->getValue() : 0; |
| 4956 | |
| 4957 | // Unlikely, but nothing to do if it does happen |
| 4958 | if (IsCountConst && CountValue == 0) |
| 4959 | return; |
| 4960 | |
| 4961 | // TODO(ascull): if the count is constant but val is not it would be possible |
| 4962 | // to inline by spreading the value across 4 bytes and accessing subregs e.g. |
| 4963 | // eax, ax and al. |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4964 | if (shouldOptimizeMemIntrins() && IsCountConst && IsValConst) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4965 | Variable *Base = nullptr; |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4966 | Variable *VecReg = nullptr; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4967 | const uint32_t SpreadValue = |
| 4968 | (ValValue << 24) | (ValValue << 16) | (ValValue << 8) | ValValue; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4969 | |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4970 | auto lowerSet = [this, &Base, SpreadValue, &VecReg](Type Ty, |
Jim Stichnoth | 992f91d | 2015-08-10 11:18:38 -0700 | [diff] [blame] | 4971 | uint32_t OffsetAmt) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4972 | assert(Base != nullptr); |
| 4973 | Constant *Offset = OffsetAmt ? Ctx->getConstantInt32(OffsetAmt) : nullptr; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4974 | |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4975 | // TODO(ascull): is 64-bit better with vector or scalar movq? |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4976 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4977 | if (isVectorType(Ty)) { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4978 | assert(VecReg != nullptr); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4979 | _storep(VecReg, Mem); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4980 | } else if (Ty == IceType_f64) { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4981 | assert(VecReg != nullptr); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4982 | _storeq(VecReg, Mem); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4983 | } else { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4984 | assert(Ty != IceType_i64); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4985 | _store(Ctx->getConstantInt(Ty, SpreadValue), Mem); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4986 | } |
| 4987 | }; |
| 4988 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4989 | // Find the largest type that can be used and use it as much as possible in |
| 4990 | // reverse order. Then handle any remainder with overlapping copies. Since |
| 4991 | // the remainder will be at the end, there will be reduces pressure on the |
| 4992 | // memory unit as the access to the same memory are far apart. |
| 4993 | Type Ty; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4994 | if (ValValue == 0 && CountValue >= BytesPerStoreq && |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4995 | CountValue <= BytesPerStorep * Traits::MEMCPY_UNROLL_LIMIT) { |
| 4996 | // When the value is zero it can be loaded into a vector register cheaply |
| 4997 | // using the xor trick. |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4998 | Base = legalizeToReg(Dest); |
| 4999 | VecReg = makeVectorOfZeros(IceType_v16i8); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5000 | Ty = largestTypeInSize(CountValue); |
| 5001 | } else if (CountValue <= BytesPerStorei32 * Traits::MEMCPY_UNROLL_LIMIT) { |
| 5002 | // When the value is non-zero or the count is small we can't use vector |
| 5003 | // instructions so are limited to 32-bit stores. |
| 5004 | Base = legalizeToReg(Dest); |
| 5005 | constexpr uint32_t MaxSize = 4; |
| 5006 | Ty = largestTypeInSize(CountValue, MaxSize); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5007 | } |
| 5008 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5009 | if (Base) { |
| 5010 | uint32_t TyWidth = typeWidthInBytes(Ty); |
| 5011 | |
| 5012 | uint32_t RemainingBytes = CountValue; |
| 5013 | uint32_t Offset = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 5014 | while (RemainingBytes >= TyWidth) { |
| 5015 | lowerSet(Ty, Offset); |
| 5016 | RemainingBytes -= TyWidth; |
| 5017 | Offset -= TyWidth; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5018 | } |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 5019 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5020 | if (RemainingBytes == 0) |
| 5021 | return; |
| 5022 | |
| 5023 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 5024 | // of overlaps in the copies. |
| 5025 | Type LeftOverTy = firstTypeThatFitsSize(RemainingBytes); |
| 5026 | Offset = CountValue - typeWidthInBytes(LeftOverTy); |
| 5027 | lowerSet(LeftOverTy, Offset); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5028 | return; |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | // Fall back on calling the memset function. The value operand needs to be |
| 5033 | // extended to a stack slot size because the PNaCl ABI requires arguments to |
| 5034 | // be at least 32 bits wide. |
| 5035 | Operand *ValExt; |
| 5036 | if (IsValConst) { |
| 5037 | ValExt = Ctx->getConstantInt(stackSlotType(), ValValue); |
| 5038 | } else { |
| 5039 | Variable *ValExtVar = Func->makeVariable(stackSlotType()); |
| 5040 | lowerCast(InstCast::create(Func, InstCast::Zext, ValExtVar, Val)); |
| 5041 | ValExt = ValExtVar; |
| 5042 | } |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 5043 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memset, nullptr, 3); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5044 | Call->addArg(Dest); |
| 5045 | Call->addArg(ValExt); |
| 5046 | Call->addArg(Count); |
| 5047 | lowerCall(Call); |
| 5048 | } |
| 5049 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5050 | class AddressOptimizer { |
| 5051 | AddressOptimizer() = delete; |
| 5052 | AddressOptimizer(const AddressOptimizer &) = delete; |
| 5053 | AddressOptimizer &operator=(const AddressOptimizer &) = delete; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5054 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5055 | public: |
| 5056 | explicit AddressOptimizer(const Cfg *Func) |
| 5057 | : Func(Func), VMetadata(Func->getVMetadata()) {} |
| 5058 | |
| 5059 | inline void dumpAddressOpt(const ConstantRelocatable *const Relocatable, |
| 5060 | int32_t Offset, const Variable *Base, |
| 5061 | const Variable *Index, uint16_t Shift, |
| 5062 | const Inst *Reason) const; |
| 5063 | |
| 5064 | inline const Inst *matchAssign(Variable **Var, |
| 5065 | ConstantRelocatable **Relocatable, |
| 5066 | int32_t *Offset); |
| 5067 | |
| 5068 | inline const Inst *matchCombinedBaseIndex(Variable **Base, Variable **Index, |
| 5069 | uint16_t *Shift); |
| 5070 | |
| 5071 | inline const Inst *matchShiftedIndex(Variable **Index, uint16_t *Shift); |
| 5072 | |
| 5073 | inline const Inst *matchOffsetBase(Variable **Base, |
| 5074 | ConstantRelocatable **Relocatable, |
| 5075 | int32_t *Offset); |
| 5076 | |
| 5077 | private: |
| 5078 | const Cfg *const Func; |
| 5079 | const VariablesMetadata *const VMetadata; |
| 5080 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5081 | static bool isAdd(const Inst *Instr) { |
| 5082 | if (auto *Arith = llvm::dyn_cast_or_null<const InstArithmetic>(Instr)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5083 | return (Arith->getOp() == InstArithmetic::Add); |
| 5084 | } |
| 5085 | return false; |
| 5086 | } |
| 5087 | }; |
| 5088 | |
| 5089 | void AddressOptimizer::dumpAddressOpt( |
| 5090 | const ConstantRelocatable *const Relocatable, int32_t Offset, |
| 5091 | const Variable *Base, const Variable *Index, uint16_t Shift, |
| 5092 | const Inst *Reason) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 5093 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5094 | return; |
| 5095 | if (!Func->isVerbose(IceV_AddrOpt)) |
| 5096 | return; |
| 5097 | OstreamLocker L(Func->getContext()); |
| 5098 | Ostream &Str = Func->getContext()->getStrDump(); |
| 5099 | Str << "Instruction: "; |
| 5100 | Reason->dumpDecorated(Func); |
| 5101 | Str << " results in Base="; |
| 5102 | if (Base) |
| 5103 | Base->dump(Func); |
| 5104 | else |
| 5105 | Str << "<null>"; |
| 5106 | Str << ", Index="; |
| 5107 | if (Index) |
| 5108 | Index->dump(Func); |
| 5109 | else |
| 5110 | Str << "<null>"; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5111 | Str << ", Shift=" << Shift << ", Offset=" << Offset |
| 5112 | << ", Relocatable=" << Relocatable << "\n"; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5113 | } |
| 5114 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5115 | const Inst *AddressOptimizer::matchAssign(Variable **Var, |
| 5116 | ConstantRelocatable **Relocatable, |
| 5117 | int32_t *Offset) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 5118 | // Var originates from Var=SrcVar ==> set Var:=SrcVar |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5119 | if (*Var == nullptr) |
| 5120 | return nullptr; |
| 5121 | if (const Inst *VarAssign = VMetadata->getSingleDefinition(*Var)) { |
| 5122 | assert(!VMetadata->isMultiDef(*Var)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5123 | if (llvm::isa<InstAssign>(VarAssign)) { |
| 5124 | Operand *SrcOp = VarAssign->getSrc(0); |
| 5125 | assert(SrcOp); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5126 | if (auto *SrcVar = llvm::dyn_cast<Variable>(SrcOp)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5127 | if (!VMetadata->isMultiDef(SrcVar) && |
| 5128 | // TODO: ensure SrcVar stays single-BB |
| 5129 | true) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5130 | *Var = SrcVar; |
| 5131 | return VarAssign; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5132 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5133 | } else if (auto *Const = llvm::dyn_cast<ConstantInteger32>(SrcOp)) { |
| 5134 | int32_t MoreOffset = Const->getValue(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5135 | if (Utils::WouldOverflowAdd(*Offset, MoreOffset)) |
| 5136 | return nullptr; |
| 5137 | *Var = nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5138 | Offset += MoreOffset; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5139 | return VarAssign; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5140 | } else if (auto *AddReloc = llvm::dyn_cast<ConstantRelocatable>(SrcOp)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5141 | if (*Relocatable == nullptr) { |
| 5142 | // It is always safe to fold a relocatable through assignment -- the |
| 5143 | // assignment frees a slot in the address operand that can be used to |
| 5144 | // hold the Sandbox Pointer -- if any. |
| 5145 | *Var = nullptr; |
| 5146 | *Relocatable = AddReloc; |
| 5147 | return VarAssign; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5148 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5149 | } |
| 5150 | } |
| 5151 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5152 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5155 | const Inst *AddressOptimizer::matchCombinedBaseIndex(Variable **Base, |
| 5156 | Variable **Index, |
| 5157 | uint16_t *Shift) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5158 | // Index==nullptr && Base is Base=Var1+Var2 ==> |
| 5159 | // set Base=Var1, Index=Var2, Shift=0 |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5160 | if (*Base == nullptr) |
| 5161 | return nullptr; |
| 5162 | if (*Index != nullptr) |
| 5163 | return nullptr; |
| 5164 | auto *BaseInst = VMetadata->getSingleDefinition(*Base); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5165 | if (BaseInst == nullptr) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5166 | return nullptr; |
| 5167 | assert(!VMetadata->isMultiDef(*Base)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5168 | if (BaseInst->getSrcSize() < 2) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5169 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5170 | if (auto *Var1 = llvm::dyn_cast<Variable>(BaseInst->getSrc(0))) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5171 | if (VMetadata->isMultiDef(Var1)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5172 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5173 | if (auto *Var2 = llvm::dyn_cast<Variable>(BaseInst->getSrc(1))) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5174 | if (VMetadata->isMultiDef(Var2)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5175 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5176 | if (isAdd(BaseInst) && |
| 5177 | // TODO: ensure Var1 and Var2 stay single-BB |
| 5178 | true) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5179 | *Base = Var1; |
| 5180 | *Index = Var2; |
| 5181 | *Shift = 0; // should already have been 0 |
| 5182 | return BaseInst; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5183 | } |
| 5184 | } |
| 5185 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5186 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5189 | const Inst *AddressOptimizer::matchShiftedIndex(Variable **Index, |
| 5190 | uint16_t *Shift) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5191 | // Index is Index=Var*Const && log2(Const)+Shift<=3 ==> |
| 5192 | // Index=Var, Shift+=log2(Const) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5193 | if (*Index == nullptr) |
| 5194 | return nullptr; |
| 5195 | auto *IndexInst = VMetadata->getSingleDefinition(*Index); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5196 | if (IndexInst == nullptr) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5197 | return nullptr; |
| 5198 | assert(!VMetadata->isMultiDef(*Index)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5199 | if (IndexInst->getSrcSize() < 2) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5200 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5201 | if (auto *ArithInst = llvm::dyn_cast<InstArithmetic>(IndexInst)) { |
| 5202 | if (auto *Var = llvm::dyn_cast<Variable>(ArithInst->getSrc(0))) { |
| 5203 | if (auto *Const = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5204 | llvm::dyn_cast<ConstantInteger32>(ArithInst->getSrc(1))) { |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5205 | if (VMetadata->isMultiDef(Var) || Const->getType() != IceType_i32) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5206 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5207 | switch (ArithInst->getOp()) { |
| 5208 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5209 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5210 | case InstArithmetic::Mul: { |
| 5211 | uint32_t Mult = Const->getValue(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5212 | uint32_t LogMult; |
| 5213 | switch (Mult) { |
| 5214 | case 1: |
| 5215 | LogMult = 0; |
| 5216 | break; |
| 5217 | case 2: |
| 5218 | LogMult = 1; |
| 5219 | break; |
| 5220 | case 4: |
| 5221 | LogMult = 2; |
| 5222 | break; |
| 5223 | case 8: |
| 5224 | LogMult = 3; |
| 5225 | break; |
| 5226 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5227 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5228 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5229 | if (*Shift + LogMult <= 3) { |
| 5230 | *Index = Var; |
| 5231 | *Shift += LogMult; |
| 5232 | return IndexInst; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5233 | } |
| 5234 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5235 | case InstArithmetic::Shl: { |
| 5236 | uint32_t ShiftAmount = Const->getValue(); |
| 5237 | switch (ShiftAmount) { |
| 5238 | case 0: |
| 5239 | case 1: |
| 5240 | case 2: |
| 5241 | case 3: |
| 5242 | break; |
| 5243 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5244 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5245 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5246 | if (*Shift + ShiftAmount <= 3) { |
| 5247 | *Index = Var; |
| 5248 | *Shift += ShiftAmount; |
| 5249 | return IndexInst; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5250 | } |
| 5251 | } |
| 5252 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5256 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5257 | } |
| 5258 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5259 | const Inst *AddressOptimizer::matchOffsetBase(Variable **Base, |
| 5260 | ConstantRelocatable **Relocatable, |
| 5261 | int32_t *Offset) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5262 | // Base is Base=Var+Const || Base is Base=Const+Var ==> |
| 5263 | // set Base=Var, Offset+=Const |
| 5264 | // Base is Base=Var-Const ==> |
| 5265 | // set Base=Var, Offset-=Const |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5266 | if (*Base == nullptr) { |
| 5267 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5268 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5269 | const Inst *BaseInst = VMetadata->getSingleDefinition(*Base); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5270 | if (BaseInst == nullptr) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5271 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5272 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5273 | assert(!VMetadata->isMultiDef(*Base)); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5274 | if (auto *ArithInst = llvm::dyn_cast<const InstArithmetic>(BaseInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5275 | if (ArithInst->getOp() != InstArithmetic::Add && |
| 5276 | ArithInst->getOp() != InstArithmetic::Sub) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5277 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5278 | bool IsAdd = ArithInst->getOp() == InstArithmetic::Add; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5279 | Operand *Src0 = ArithInst->getSrc(0); |
| 5280 | Operand *Src1 = ArithInst->getSrc(1); |
| 5281 | auto *Var0 = llvm::dyn_cast<Variable>(Src0); |
| 5282 | auto *Var1 = llvm::dyn_cast<Variable>(Src1); |
| 5283 | auto *Const0 = llvm::dyn_cast<ConstantInteger32>(Src0); |
| 5284 | auto *Const1 = llvm::dyn_cast<ConstantInteger32>(Src1); |
| 5285 | auto *Reloc0 = llvm::dyn_cast<ConstantRelocatable>(Src0); |
| 5286 | auto *Reloc1 = llvm::dyn_cast<ConstantRelocatable>(Src1); |
| 5287 | Variable *NewBase = nullptr; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5288 | int32_t NewOffset = *Offset; |
| 5289 | ConstantRelocatable *NewRelocatable = *Relocatable; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5290 | if (Var0 && Var1) |
| 5291 | // TODO(sehr): merge base/index splitting into here. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5292 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5293 | if (!IsAdd && Var1) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5294 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5295 | if (Var0) |
| 5296 | NewBase = Var0; |
| 5297 | else if (Var1) |
| 5298 | NewBase = Var1; |
| 5299 | // Don't know how to add/subtract two relocatables. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5300 | if ((*Relocatable && (Reloc0 || Reloc1)) || (Reloc0 && Reloc1)) |
| 5301 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5302 | // Don't know how to subtract a relocatable. |
| 5303 | if (!IsAdd && Reloc1) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5304 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5305 | // Incorporate ConstantRelocatables. |
| 5306 | if (Reloc0) |
| 5307 | NewRelocatable = Reloc0; |
| 5308 | else if (Reloc1) |
| 5309 | NewRelocatable = Reloc1; |
| 5310 | // Compute the updated constant offset. |
| 5311 | if (Const0) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 5312 | const int32_t MoreOffset = |
| 5313 | IsAdd ? Const0->getValue() : -Const0->getValue(); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5314 | if (Utils::WouldOverflowAdd(NewOffset, MoreOffset)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5315 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5316 | NewOffset += MoreOffset; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5317 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5318 | if (Const1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 5319 | const int32_t MoreOffset = |
| 5320 | IsAdd ? Const1->getValue() : -Const1->getValue(); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5321 | if (Utils::WouldOverflowAdd(NewOffset, MoreOffset)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5322 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5323 | NewOffset += MoreOffset; |
| 5324 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5325 | *Base = NewBase; |
| 5326 | *Offset = NewOffset; |
| 5327 | *Relocatable = NewRelocatable; |
| 5328 | return BaseInst; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5329 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5330 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5331 | } |
| 5332 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5333 | template <typename TypeTraits> |
| 5334 | typename TargetX86Base<TypeTraits>::X86OperandMem * |
| 5335 | TargetX86Base<TypeTraits>::computeAddressOpt(const Inst *Instr, Type MemType, |
| 5336 | Operand *Addr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5337 | Func->resetCurrentNode(); |
| 5338 | if (Func->isVerbose(IceV_AddrOpt)) { |
| 5339 | OstreamLocker L(Func->getContext()); |
| 5340 | Ostream &Str = Func->getContext()->getStrDump(); |
| 5341 | Str << "\nStarting computeAddressOpt for instruction:\n "; |
| 5342 | Instr->dumpDecorated(Func); |
| 5343 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5344 | |
| 5345 | OptAddr NewAddr; |
| 5346 | NewAddr.Base = llvm::dyn_cast<Variable>(Addr); |
| 5347 | if (NewAddr.Base == nullptr) |
| 5348 | return nullptr; |
| 5349 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 5350 | // If the Base has more than one use or is live across multiple blocks, then |
| 5351 | // don't go further. Alternatively (?), never consider a transformation that |
| 5352 | // would change a variable that is currently *not* live across basic block |
| 5353 | // boundaries into one that *is*. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5354 | if (Func->getVMetadata()->isMultiBlock( |
| 5355 | NewAddr.Base) /* || Base->getUseCount() > 1*/) |
| 5356 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5357 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5358 | AddressOptimizer AddrOpt(Func); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 5359 | const bool MockBounds = getFlags().getMockBoundsCheck(); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5360 | const Inst *Reason = nullptr; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5361 | bool AddressWasOptimized = false; |
| 5362 | // The following unnamed struct identifies the address mode formation steps |
| 5363 | // that could potentially create an invalid memory operand (i.e., no free |
| 5364 | // slots for RebasePtr.) We add all those variables to this struct so that we |
| 5365 | // can use memset() to reset all members to false. |
| 5366 | struct { |
| 5367 | bool AssignBase = false; |
| 5368 | bool AssignIndex = false; |
| 5369 | bool OffsetFromBase = false; |
| 5370 | bool OffsetFromIndex = false; |
| 5371 | bool CombinedBaseIndex = false; |
| 5372 | } Skip; |
| 5373 | // This points to the boolean in Skip that represents the last folding |
| 5374 | // performed. This is used to disable a pattern match that generated an |
| 5375 | // invalid address. Without this, the algorithm would never finish. |
| 5376 | bool *SkipLastFolding = nullptr; |
| 5377 | // NewAddrCheckpoint is used to rollback the address being formed in case an |
| 5378 | // invalid address is formed. |
| 5379 | OptAddr NewAddrCheckpoint; |
| 5380 | Reason = Instr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5381 | do { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5382 | if (SandboxingType != ST_None) { |
| 5383 | // When sandboxing, we defer the sandboxing of NewAddr to the Concrete |
| 5384 | // Target. If our optimization was overly aggressive, then we simply undo |
| 5385 | // what the previous iteration did, and set the previous pattern's skip |
| 5386 | // bit to true. |
| 5387 | if (!legalizeOptAddrForSandbox(&NewAddr)) { |
| 5388 | *SkipLastFolding = true; |
| 5389 | SkipLastFolding = nullptr; |
| 5390 | NewAddr = NewAddrCheckpoint; |
| 5391 | Reason = nullptr; |
| 5392 | } |
| 5393 | } |
| 5394 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5395 | if (Reason) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5396 | AddrOpt.dumpAddressOpt(NewAddr.Relocatable, NewAddr.Offset, NewAddr.Base, |
| 5397 | NewAddr.Index, NewAddr.Shift, Reason); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5398 | AddressWasOptimized = true; |
| 5399 | Reason = nullptr; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5400 | SkipLastFolding = nullptr; |
| 5401 | memset(&Skip, 0, sizeof(Skip)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5402 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5403 | |
| 5404 | NewAddrCheckpoint = NewAddr; |
| 5405 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5406 | // Update Base and Index to follow through assignments to definitions. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5407 | if (!Skip.AssignBase && |
| 5408 | (Reason = AddrOpt.matchAssign(&NewAddr.Base, &NewAddr.Relocatable, |
| 5409 | &NewAddr.Offset))) { |
| 5410 | SkipLastFolding = &Skip.AssignBase; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5411 | // Assignments of Base from a Relocatable or ConstantInt32 can result |
| 5412 | // in Base becoming nullptr. To avoid code duplication in this loop we |
| 5413 | // prefer that Base be non-nullptr if possible. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5414 | if ((NewAddr.Base == nullptr) && (NewAddr.Index != nullptr) && |
| 5415 | NewAddr.Shift == 0) { |
| 5416 | std::swap(NewAddr.Base, NewAddr.Index); |
| 5417 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5418 | continue; |
| 5419 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5420 | if (!Skip.AssignBase && |
| 5421 | (Reason = AddrOpt.matchAssign(&NewAddr.Index, &NewAddr.Relocatable, |
| 5422 | &NewAddr.Offset))) { |
| 5423 | SkipLastFolding = &Skip.AssignIndex; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5424 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5425 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5426 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5427 | if (!MockBounds) { |
| 5428 | // Transition from: |
| 5429 | // <Relocatable + Offset>(Base) to |
| 5430 | // <Relocatable + Offset>(Base, Index) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5431 | if (!Skip.CombinedBaseIndex && |
| 5432 | (Reason = AddrOpt.matchCombinedBaseIndex( |
| 5433 | &NewAddr.Base, &NewAddr.Index, &NewAddr.Shift))) { |
| 5434 | SkipLastFolding = &Skip.CombinedBaseIndex; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5435 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5436 | } |
| 5437 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5438 | // Recognize multiply/shift and update Shift amount. |
| 5439 | // Index becomes Index=Var<<Const && Const+Shift<=3 ==> |
| 5440 | // Index=Var, Shift+=Const |
| 5441 | // Index becomes Index=Const*Var && log2(Const)+Shift<=3 ==> |
| 5442 | // Index=Var, Shift+=log2(Const) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5443 | if ((Reason = |
| 5444 | AddrOpt.matchShiftedIndex(&NewAddr.Index, &NewAddr.Shift))) { |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5445 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5446 | } |
| 5447 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5448 | // If Shift is zero, the choice of Base and Index was purely arbitrary. |
| 5449 | // Recognize multiply/shift and set Shift amount. |
| 5450 | // Shift==0 && Base is Base=Var*Const && log2(Const)+Shift<=3 ==> |
| 5451 | // swap(Index,Base) |
| 5452 | // Similar for Base=Const*Var and Base=Var<<Const |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5453 | if (NewAddr.Shift == 0 && |
| 5454 | (Reason = AddrOpt.matchShiftedIndex(&NewAddr.Base, &NewAddr.Shift))) { |
| 5455 | std::swap(NewAddr.Base, NewAddr.Index); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5456 | continue; |
| 5457 | } |
| 5458 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5459 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5460 | // Update Offset to reflect additions/subtractions with constants and |
| 5461 | // relocatables. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5462 | // TODO: consider overflow issues with respect to Offset. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5463 | if (!Skip.OffsetFromBase && |
| 5464 | (Reason = AddrOpt.matchOffsetBase(&NewAddr.Base, &NewAddr.Relocatable, |
| 5465 | &NewAddr.Offset))) { |
| 5466 | SkipLastFolding = &Skip.OffsetFromBase; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5467 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5468 | } |
| 5469 | if (NewAddr.Shift == 0 && !Skip.OffsetFromIndex && |
| 5470 | (Reason = AddrOpt.matchOffsetBase(&NewAddr.Index, &NewAddr.Relocatable, |
| 5471 | &NewAddr.Offset))) { |
| 5472 | SkipLastFolding = &Skip.OffsetFromIndex; |
David Sehr | 69e9290 | 2015-11-04 14:46:29 -0800 | [diff] [blame] | 5473 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5474 | } |
| 5475 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5476 | // TODO(sehr, stichnot): Handle updates of Index with Shift != 0. |
| 5477 | // Index is Index=Var+Const ==> |
| 5478 | // set Index=Var, Offset+=(Const<<Shift) |
| 5479 | // Index is Index=Const+Var ==> |
| 5480 | // set Index=Var, Offset+=(Const<<Shift) |
| 5481 | // Index is Index=Var-Const ==> |
| 5482 | // set Index=Var, Offset-=(Const<<Shift) |
| 5483 | break; |
| 5484 | } while (Reason); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5485 | |
| 5486 | if (!AddressWasOptimized) { |
| 5487 | return nullptr; |
| 5488 | } |
| 5489 | |
| 5490 | // Undo any addition of RebasePtr. It will be added back when the mem |
| 5491 | // operand is sandboxed. |
| 5492 | if (NewAddr.Base == RebasePtr) { |
| 5493 | NewAddr.Base = nullptr; |
| 5494 | } |
| 5495 | |
| 5496 | if (NewAddr.Index == RebasePtr) { |
| 5497 | NewAddr.Index = nullptr; |
| 5498 | NewAddr.Shift = 0; |
| 5499 | } |
| 5500 | |
| 5501 | Constant *OffsetOp = nullptr; |
| 5502 | if (NewAddr.Relocatable == nullptr) { |
| 5503 | OffsetOp = Ctx->getConstantInt32(NewAddr.Offset); |
| 5504 | } else { |
| 5505 | OffsetOp = |
| 5506 | Ctx->getConstantSym(NewAddr.Relocatable->getOffset() + NewAddr.Offset, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 5507 | NewAddr.Relocatable->getName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5508 | } |
| 5509 | // Vanilla ICE load instructions should not use the segment registers, and |
| 5510 | // computeAddressOpt only works at the level of Variables and Constants, not |
| 5511 | // other X86OperandMem, so there should be no mention of segment |
| 5512 | // registers there either. |
| 5513 | static constexpr auto SegmentReg = |
| 5514 | X86OperandMem::SegmentRegisters::DefaultSegment; |
| 5515 | |
| 5516 | return X86OperandMem::create(Func, MemType, NewAddr.Base, OffsetOp, |
| 5517 | NewAddr.Index, NewAddr.Shift, SegmentReg); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5518 | } |
| 5519 | |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5520 | /// Add a mock bounds check on the memory address before using it as a load or |
| 5521 | /// store operand. The basic idea is that given a memory operand [reg], we |
| 5522 | /// would first add bounds-check code something like: |
| 5523 | /// |
| 5524 | /// cmp reg, <lb> |
| 5525 | /// jl out_of_line_error |
| 5526 | /// cmp reg, <ub> |
| 5527 | /// jg out_of_line_error |
| 5528 | /// |
| 5529 | /// In reality, the specific code will depend on how <lb> and <ub> are |
| 5530 | /// represented, e.g. an immediate, a global, or a function argument. |
| 5531 | /// |
| 5532 | /// As such, we need to enforce that the memory operand does not have the form |
| 5533 | /// [reg1+reg2], because then there is no simple cmp instruction that would |
| 5534 | /// suffice. However, we consider [reg+offset] to be OK because the offset is |
| 5535 | /// usually small, and so <ub> could have a safety buffer built in and then we |
| 5536 | /// could instead branch to a custom out_of_line_error that does the precise |
| 5537 | /// check and jumps back if it turns out OK. |
| 5538 | /// |
| 5539 | /// For the purpose of mocking the bounds check, we'll do something like this: |
| 5540 | /// |
| 5541 | /// cmp reg, 0 |
| 5542 | /// je label |
| 5543 | /// cmp reg, 1 |
| 5544 | /// je label |
| 5545 | /// label: |
| 5546 | /// |
| 5547 | /// Also note that we don't need to add a bounds check to a dereference of a |
| 5548 | /// simple global variable address. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5549 | template <typename TraitsType> |
| 5550 | void TargetX86Base<TraitsType>::doMockBoundsCheck(Operand *Opnd) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 5551 | if (!getFlags().getMockBoundsCheck()) |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5552 | return; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5553 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Opnd)) { |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5554 | if (Mem->getIndex()) { |
| 5555 | llvm::report_fatal_error("doMockBoundsCheck: Opnd contains index reg"); |
| 5556 | } |
| 5557 | Opnd = Mem->getBase(); |
| 5558 | } |
| 5559 | // At this point Opnd could be nullptr, or Variable, or Constant, or perhaps |
| 5560 | // something else. We only care if it is Variable. |
| 5561 | auto *Var = llvm::dyn_cast_or_null<Variable>(Opnd); |
| 5562 | if (Var == nullptr) |
| 5563 | return; |
| 5564 | // We use lowerStore() to copy out-args onto the stack. This creates a memory |
| 5565 | // operand with the stack pointer as the base register. Don't do bounds |
| 5566 | // checks on that. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 5567 | if (Var->getRegNum() == getStackReg()) |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5568 | return; |
| 5569 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5570 | auto *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5571 | _cmp(Opnd, Ctx->getConstantZero(IceType_i32)); |
| 5572 | _br(Traits::Cond::Br_e, Label); |
| 5573 | _cmp(Opnd, Ctx->getConstantInt32(1)); |
| 5574 | _br(Traits::Cond::Br_e, Label); |
| 5575 | Context.insert(Label); |
| 5576 | } |
| 5577 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5578 | template <typename TraitsType> |
| 5579 | void TargetX86Base<TraitsType>::lowerLoad(const InstLoad *Load) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 5580 | // A Load instruction can be treated the same as an Assign instruction, after |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 5581 | // the source operand is transformed into an X86OperandMem operand. Note that |
| 5582 | // the address mode optimization already creates an X86OperandMem operand, so |
| 5583 | // it doesn't need another level of transformation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5584 | Variable *DestLoad = Load->getDest(); |
| 5585 | Type Ty = DestLoad->getType(); |
| 5586 | Operand *Src0 = formMemoryOperand(Load->getSourceAddress(), Ty); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5587 | doMockBoundsCheck(Src0); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 5588 | auto *Assign = InstAssign::create(Func, DestLoad, Src0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5589 | lowerAssign(Assign); |
| 5590 | } |
| 5591 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5592 | template <typename TraitsType> |
| 5593 | void TargetX86Base<TraitsType>::doAddressOptLoad() { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5594 | Inst *Instr = Context.getCur(); |
| 5595 | Operand *Addr = Instr->getSrc(0); |
| 5596 | Variable *Dest = Instr->getDest(); |
| 5597 | if (auto *OptAddr = computeAddressOpt(Instr, Dest->getType(), Addr)) { |
| 5598 | Instr->setDeleted(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5599 | Context.insert<InstLoad>(Dest, OptAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5603 | template <typename TraitsType> |
| 5604 | void TargetX86Base<TraitsType>::randomlyInsertNop(float Probability, |
| 5605 | RandomNumberGenerator &RNG) { |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 5606 | RandomNumberGeneratorWrapper RNGW(RNG); |
| 5607 | if (RNGW.getTrueWithProbability(Probability)) { |
| 5608 | _nop(RNGW(Traits::X86_NUM_NOP_VARIANTS)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5609 | } |
| 5610 | } |
| 5611 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5612 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5613 | void TargetX86Base<TraitsType>::lowerPhi(const InstPhi * /*Instr*/) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5614 | Func->setError("Phi found in regular instruction list"); |
| 5615 | } |
| 5616 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5617 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5618 | void TargetX86Base<TraitsType>::lowerRet(const InstRet *Instr) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5619 | Variable *Reg = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5620 | if (Instr->hasRetValue()) { |
| 5621 | Operand *RetValue = legalize(Instr->getRetValue()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5622 | const Type ReturnType = RetValue->getType(); |
| 5623 | assert(isVectorType(ReturnType) || isScalarFloatingType(ReturnType) || |
| 5624 | (ReturnType == IceType_i32) || (ReturnType == IceType_i64)); |
| 5625 | Reg = moveReturnValueToRegister(RetValue, ReturnType); |
| 5626 | } |
| 5627 | // Add a ret instruction even if sandboxing is enabled, because addEpilog |
| 5628 | // explicitly looks for a ret instruction as a marker for where to insert the |
| 5629 | // frame removal instructions. |
| 5630 | _ret(Reg); |
| 5631 | // Add a fake use of esp to make sure esp stays alive for the entire |
| 5632 | // function. Otherwise post-call esp adjustments get dead-code eliminated. |
| 5633 | keepEspLiveAtExit(); |
| 5634 | } |
| 5635 | |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5636 | inline uint32_t makePshufdMask(SizeT Index0, SizeT Index1, SizeT Index2, |
| 5637 | SizeT Index3) { |
| 5638 | const SizeT Mask = (Index0 & 0x3) | ((Index1 & 0x3) << 2) | |
| 5639 | ((Index2 & 0x3) << 4) | ((Index3 & 0x3) << 6); |
| 5640 | assert(Mask < 256); |
| 5641 | return Mask; |
| 5642 | } |
| 5643 | |
| 5644 | template <typename TraitsType> |
| 5645 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_AllFromSameSrc( |
| 5646 | Variable *Src, SizeT Index0, SizeT Index1, SizeT Index2, SizeT Index3) { |
| 5647 | constexpr SizeT SrcBit = 1 << 2; |
| 5648 | assert((Index0 & SrcBit) == (Index1 & SrcBit)); |
| 5649 | assert((Index0 & SrcBit) == (Index2 & SrcBit)); |
| 5650 | assert((Index0 & SrcBit) == (Index3 & SrcBit)); |
| 5651 | (void)SrcBit; |
| 5652 | |
| 5653 | const Type SrcTy = Src->getType(); |
| 5654 | auto *T = makeReg(SrcTy); |
| 5655 | auto *SrcRM = legalize(Src, Legal_Reg | Legal_Mem); |
| 5656 | auto *Mask = |
| 5657 | Ctx->getConstantInt32(makePshufdMask(Index0, Index1, Index2, Index3)); |
| 5658 | _pshufd(T, SrcRM, Mask); |
| 5659 | return T; |
| 5660 | } |
| 5661 | |
| 5662 | template <typename TraitsType> |
| 5663 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_TwoFromSameSrc( |
| 5664 | Variable *Src0, SizeT Index0, SizeT Index1, Variable *Src1, SizeT Index2, |
| 5665 | SizeT Index3) { |
| 5666 | constexpr SizeT SrcBit = 1 << 2; |
| 5667 | assert((Index0 & SrcBit) == (Index1 & SrcBit) || (Index1 == IGNORE_INDEX)); |
| 5668 | assert((Index2 & SrcBit) == (Index3 & SrcBit) || (Index3 == IGNORE_INDEX)); |
| 5669 | (void)SrcBit; |
| 5670 | |
| 5671 | const Type SrcTy = Src0->getType(); |
| 5672 | assert(Src1->getType() == SrcTy); |
| 5673 | auto *T = makeReg(SrcTy); |
| 5674 | auto *Src0R = legalizeToReg(Src0); |
| 5675 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 5676 | auto *Mask = |
| 5677 | Ctx->getConstantInt32(makePshufdMask(Index0, Index1, Index2, Index3)); |
| 5678 | _movp(T, Src0R); |
| 5679 | _shufps(T, Src1RM, Mask); |
| 5680 | return T; |
| 5681 | } |
| 5682 | |
| 5683 | template <typename TraitsType> |
| 5684 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5685 | Variable *Src0, SizeT Index0, Variable *Src1, SizeT Index1) { |
| 5686 | return lowerShuffleVector_TwoFromSameSrc(Src0, Index0, IGNORE_INDEX, Src1, |
| 5687 | Index1, IGNORE_INDEX); |
| 5688 | } |
| 5689 | |
| 5690 | inline SizeT makeSrcSwitchMask(SizeT Index0, SizeT Index1, SizeT Index2, |
| 5691 | SizeT Index3) { |
| 5692 | constexpr SizeT SrcBit = 1 << 2; |
| 5693 | const SizeT Index0Bits = ((Index0 & SrcBit) == 0) ? 0 : (1 << 0); |
| 5694 | const SizeT Index1Bits = ((Index1 & SrcBit) == 0) ? 0 : (1 << 1); |
| 5695 | const SizeT Index2Bits = ((Index2 & SrcBit) == 0) ? 0 : (1 << 2); |
| 5696 | const SizeT Index3Bits = ((Index3 & SrcBit) == 0) ? 0 : (1 << 3); |
| 5697 | return Index0Bits | Index1Bits | Index2Bits | Index3Bits; |
| 5698 | } |
| 5699 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5700 | template <typename TraitsType> |
John Porto | de29f12 | 2016-04-26 19:16:07 -0700 | [diff] [blame] | 5701 | GlobalString TargetX86Base<TraitsType>::lowerShuffleVector_NewMaskName() { |
| 5702 | GlobalString FuncName = Func->getFunctionName(); |
| 5703 | const SizeT Id = PshufbMaskCount++; |
| 5704 | if (!BuildDefs::dump() || !FuncName.hasStdString()) { |
| 5705 | return GlobalString::createWithString( |
| 5706 | Ctx, |
| 5707 | "$PS" + std::to_string(FuncName.getID()) + "_" + std::to_string(Id)); |
| 5708 | } |
| 5709 | return GlobalString::createWithString( |
| 5710 | Ctx, "Pshufb$" + Func->getFunctionName() + "$" + std::to_string(Id)); |
| 5711 | } |
| 5712 | |
| 5713 | template <typename TraitsType> |
| 5714 | ConstantRelocatable * |
| 5715 | TargetX86Base<TraitsType>::lowerShuffleVector_CreatePshufbMask( |
| 5716 | int8_t Idx0, int8_t Idx1, int8_t Idx2, int8_t Idx3, int8_t Idx4, |
| 5717 | int8_t Idx5, int8_t Idx6, int8_t Idx7, int8_t Idx8, int8_t Idx9, |
| 5718 | int8_t Idx10, int8_t Idx11, int8_t Idx12, int8_t Idx13, int8_t Idx14, |
| 5719 | int8_t Idx15) { |
| 5720 | static constexpr uint8_t NumElements = 16; |
| 5721 | const char Initializer[NumElements] = { |
| 5722 | Idx0, Idx1, Idx2, Idx3, Idx4, Idx5, Idx6, Idx7, |
| 5723 | Idx8, Idx9, Idx10, Idx11, Idx12, Idx13, Idx14, Idx15, |
| 5724 | }; |
| 5725 | |
| 5726 | static constexpr Type V4VectorType = IceType_v4i32; |
| 5727 | const uint32_t MaskAlignment = typeWidthInBytesOnStack(V4VectorType); |
| 5728 | auto *Mask = VariableDeclaration::create(Func->getGlobalPool()); |
| 5729 | GlobalString MaskName = lowerShuffleVector_NewMaskName(); |
| 5730 | Mask->setIsConstant(true); |
| 5731 | Mask->addInitializer(VariableDeclaration::DataInitializer::create( |
| 5732 | Func->getGlobalPool(), Initializer, NumElements)); |
| 5733 | Mask->setName(MaskName); |
| 5734 | // Mask needs to be 16-byte aligned, or pshufb will seg fault. |
| 5735 | Mask->setAlignment(MaskAlignment); |
| 5736 | Func->addGlobal(Mask); |
| 5737 | |
| 5738 | constexpr RelocOffsetT Offset = 0; |
| 5739 | return llvm::cast<ConstantRelocatable>(Ctx->getConstantSym(Offset, MaskName)); |
| 5740 | } |
| 5741 | |
| 5742 | template <typename TraitsType> |
| 5743 | void TargetX86Base<TraitsType>::lowerShuffleVector_UsingPshufb( |
| 5744 | Variable *Dest, Operand *Src0, Operand *Src1, int8_t Idx0, int8_t Idx1, |
| 5745 | int8_t Idx2, int8_t Idx3, int8_t Idx4, int8_t Idx5, int8_t Idx6, |
| 5746 | int8_t Idx7, int8_t Idx8, int8_t Idx9, int8_t Idx10, int8_t Idx11, |
| 5747 | int8_t Idx12, int8_t Idx13, int8_t Idx14, int8_t Idx15) { |
| 5748 | const Type DestTy = Dest->getType(); |
| 5749 | static constexpr bool NotRebased = false; |
| 5750 | static constexpr Variable *NoBase = nullptr; |
| 5751 | // We use void for the memory operand instead of DestTy because using the |
| 5752 | // latter causes a validation failure: the X86 Inst layer complains that |
| 5753 | // vector mem operands could be under aligned. Thus, using void we avoid the |
| 5754 | // validation error. Note that the mask global declaration is aligned, so it |
| 5755 | // can be used as an XMM mem operand. |
| 5756 | static constexpr Type MaskType = IceType_void; |
| 5757 | #define IDX_IN_SRC(N, S) \ |
| 5758 | ((((N) & (1 << 4)) == (S << 4)) ? ((N)&0xf) : CLEAR_ALL_BITS) |
| 5759 | auto *Mask0M = X86OperandMem::create( |
| 5760 | Func, MaskType, NoBase, |
| 5761 | lowerShuffleVector_CreatePshufbMask( |
| 5762 | IDX_IN_SRC(Idx0, 0), IDX_IN_SRC(Idx1, 0), IDX_IN_SRC(Idx2, 0), |
| 5763 | IDX_IN_SRC(Idx3, 0), IDX_IN_SRC(Idx4, 0), IDX_IN_SRC(Idx5, 0), |
| 5764 | IDX_IN_SRC(Idx6, 0), IDX_IN_SRC(Idx7, 0), IDX_IN_SRC(Idx8, 0), |
| 5765 | IDX_IN_SRC(Idx9, 0), IDX_IN_SRC(Idx10, 0), IDX_IN_SRC(Idx11, 0), |
| 5766 | IDX_IN_SRC(Idx12, 0), IDX_IN_SRC(Idx13, 0), IDX_IN_SRC(Idx14, 0), |
| 5767 | IDX_IN_SRC(Idx15, 0)), |
| 5768 | NotRebased); |
| 5769 | auto *Mask1M = X86OperandMem::create( |
| 5770 | Func, MaskType, NoBase, |
| 5771 | lowerShuffleVector_CreatePshufbMask( |
| 5772 | IDX_IN_SRC(Idx0, 1), IDX_IN_SRC(Idx1, 1), IDX_IN_SRC(Idx2, 1), |
| 5773 | IDX_IN_SRC(Idx3, 1), IDX_IN_SRC(Idx4, 1), IDX_IN_SRC(Idx5, 1), |
| 5774 | IDX_IN_SRC(Idx6, 1), IDX_IN_SRC(Idx7, 1), IDX_IN_SRC(Idx8, 1), |
| 5775 | IDX_IN_SRC(Idx9, 1), IDX_IN_SRC(Idx10, 1), IDX_IN_SRC(Idx11, 1), |
| 5776 | IDX_IN_SRC(Idx12, 1), IDX_IN_SRC(Idx13, 1), IDX_IN_SRC(Idx14, 1), |
| 5777 | IDX_IN_SRC(Idx15, 1)), |
| 5778 | NotRebased); |
| 5779 | #undef IDX_IN_SRC |
| 5780 | auto *T0 = makeReg(DestTy); |
| 5781 | auto *T1 = makeReg(DestTy); |
| 5782 | auto *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 5783 | _movp(T0, Src0RM); |
| 5784 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 5785 | _movp(T1, Src1RM); |
| 5786 | |
| 5787 | _pshufb(T1, Mask1M); |
| 5788 | _pshufb(T0, Mask0M); |
| 5789 | _por(T1, T0); |
| 5790 | _movp(Dest, T1); |
| 5791 | } |
| 5792 | |
| 5793 | template <typename TraitsType> |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 5794 | void TargetX86Base<TraitsType>::lowerShuffleVector( |
| 5795 | const InstShuffleVector *Instr) { |
| 5796 | auto *Dest = Instr->getDest(); |
| 5797 | const Type DestTy = Dest->getType(); |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5798 | auto *Src0 = llvm::cast<Variable>(Instr->getSrc(0)); |
| 5799 | auto *Src1 = llvm::cast<Variable>(Instr->getSrc(1)); |
| 5800 | const SizeT NumElements = typeNumElements(DestTy); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 5801 | |
| 5802 | auto *T = makeReg(DestTy); |
| 5803 | |
| 5804 | switch (DestTy) { |
| 5805 | default: |
John Porto | de29f12 | 2016-04-26 19:16:07 -0700 | [diff] [blame] | 5806 | llvm::report_fatal_error("Unexpected vector type."); |
| 5807 | case IceType_v16i1: |
| 5808 | case IceType_v16i8: { |
| 5809 | if (InstructionSet < Traits::SSE4_1) { |
| 5810 | // TODO(jpp): figure out how to lower with sse2. |
| 5811 | break; |
| 5812 | } |
| 5813 | static constexpr SizeT ExpectedNumElements = 16; |
| 5814 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5815 | (void)ExpectedNumElements; |
| 5816 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5817 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5818 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5819 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5820 | const SizeT Index4 = Instr->getIndex(4)->getValue(); |
| 5821 | const SizeT Index5 = Instr->getIndex(5)->getValue(); |
| 5822 | const SizeT Index6 = Instr->getIndex(6)->getValue(); |
| 5823 | const SizeT Index7 = Instr->getIndex(7)->getValue(); |
| 5824 | const SizeT Index8 = Instr->getIndex(8)->getValue(); |
| 5825 | const SizeT Index9 = Instr->getIndex(9)->getValue(); |
| 5826 | const SizeT Index10 = Instr->getIndex(10)->getValue(); |
| 5827 | const SizeT Index11 = Instr->getIndex(11)->getValue(); |
| 5828 | const SizeT Index12 = Instr->getIndex(12)->getValue(); |
| 5829 | const SizeT Index13 = Instr->getIndex(13)->getValue(); |
| 5830 | const SizeT Index14 = Instr->getIndex(14)->getValue(); |
| 5831 | const SizeT Index15 = Instr->getIndex(15)->getValue(); |
| 5832 | lowerShuffleVector_UsingPshufb(Dest, Src0, Src1, Index0, Index1, Index2, |
| 5833 | Index3, Index4, Index5, Index6, Index7, |
| 5834 | Index8, Index9, Index10, Index11, Index12, |
| 5835 | Index13, Index14, Index15); |
| 5836 | return; |
| 5837 | } |
| 5838 | case IceType_v8i1: |
| 5839 | case IceType_v8i16: { |
| 5840 | if (InstructionSet < Traits::SSE4_1) { |
| 5841 | // TODO(jpp): figure out how to lower with sse2. |
| 5842 | break; |
| 5843 | } |
| 5844 | static constexpr SizeT ExpectedNumElements = 8; |
| 5845 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5846 | (void)ExpectedNumElements; |
| 5847 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5848 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5849 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5850 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5851 | const SizeT Index4 = Instr->getIndex(4)->getValue(); |
| 5852 | const SizeT Index5 = Instr->getIndex(5)->getValue(); |
| 5853 | const SizeT Index6 = Instr->getIndex(6)->getValue(); |
| 5854 | const SizeT Index7 = Instr->getIndex(7)->getValue(); |
| 5855 | #define TO_BYTE_INDEX(I) ((I) << 1) |
| 5856 | lowerShuffleVector_UsingPshufb( |
| 5857 | Dest, Src0, Src1, TO_BYTE_INDEX(Index0), TO_BYTE_INDEX(Index0) + 1, |
| 5858 | TO_BYTE_INDEX(Index1), TO_BYTE_INDEX(Index1) + 1, TO_BYTE_INDEX(Index2), |
| 5859 | TO_BYTE_INDEX(Index2) + 1, TO_BYTE_INDEX(Index3), |
| 5860 | TO_BYTE_INDEX(Index3) + 1, TO_BYTE_INDEX(Index4), |
| 5861 | TO_BYTE_INDEX(Index4) + 1, TO_BYTE_INDEX(Index5), |
| 5862 | TO_BYTE_INDEX(Index5) + 1, TO_BYTE_INDEX(Index6), |
| 5863 | TO_BYTE_INDEX(Index6) + 1, TO_BYTE_INDEX(Index7), |
| 5864 | TO_BYTE_INDEX(Index7) + 1); |
| 5865 | #undef TO_BYTE_INDEX |
| 5866 | return; |
| 5867 | } |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5868 | case IceType_v4i1: |
| 5869 | case IceType_v4i32: |
| 5870 | case IceType_v4f32: { |
| 5871 | static constexpr SizeT ExpectedNumElements = 4; |
| 5872 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5873 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5874 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5875 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5876 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5877 | Variable *T = nullptr; |
| 5878 | switch (makeSrcSwitchMask(Index0, Index1, Index2, Index3)) { |
| 5879 | #define CASE_SRCS_IN(S0, S1, S2, S3) \ |
| 5880 | case (((S0) << 0) | ((S1) << 1) | ((S2) << 2) | ((S3) << 3)) |
| 5881 | CASE_SRCS_IN(0, 0, 0, 0) : { |
| 5882 | T = lowerShuffleVector_AllFromSameSrc(Src0, Index0, Index1, Index2, |
| 5883 | Index3); |
| 5884 | } |
| 5885 | break; |
| 5886 | CASE_SRCS_IN(0, 0, 0, 1) : { |
| 5887 | assert(false && "Following code is untested but likely correct; test " |
| 5888 | "and remove assert."); |
| 5889 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index2, |
| 5890 | Src1, Index3); |
| 5891 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Unified, |
| 5892 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 5893 | } |
| 5894 | break; |
| 5895 | CASE_SRCS_IN(0, 0, 1, 0) : { |
| 5896 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index2, |
| 5897 | Src0, Index3); |
| 5898 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Unified, |
| 5899 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 5900 | } |
| 5901 | break; |
| 5902 | CASE_SRCS_IN(0, 0, 1, 1) : { |
| 5903 | assert(false && "Following code is untested but likely correct; test " |
| 5904 | "and remove assert."); |
| 5905 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Src1, |
| 5906 | Index2, Index3); |
| 5907 | } |
| 5908 | break; |
| 5909 | CASE_SRCS_IN(0, 1, 0, 0) : { |
| 5910 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index0, |
| 5911 | Src1, Index1); |
| 5912 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5913 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src0, Index2, Index3); |
| 5914 | } |
| 5915 | break; |
| 5916 | CASE_SRCS_IN(0, 1, 0, 1) : { |
| 5917 | if (Index0 == 0 && (Index1 - ExpectedNumElements) == 0 && Index2 == 1 && |
| 5918 | (Index3 - ExpectedNumElements) == 1) { |
| 5919 | assert(false && "Following code is untested but likely correct; test " |
| 5920 | "and remove assert."); |
| 5921 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 5922 | auto *Src0R = legalizeToReg(Src0); |
| 5923 | T = makeReg(DestTy); |
| 5924 | _movp(T, Src0R); |
| 5925 | _punpckl(T, Src1RM); |
| 5926 | } else if (Index0 == Index2 && Index1 == Index3) { |
| 5927 | assert(false && "Following code is untested but likely correct; test " |
| 5928 | "and remove assert."); |
| 5929 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5930 | Src0, Index0, Src1, Index1); |
| 5931 | T = lowerShuffleVector_AllFromSameSrc( |
| 5932 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_0, |
| 5933 | UNIFIED_INDEX_1); |
| 5934 | } else { |
| 5935 | assert(false && "Following code is untested but likely correct; test " |
| 5936 | "and remove assert."); |
| 5937 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5938 | Src0, Index0, Src1, Index1); |
| 5939 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5940 | Src0, Index2, Src1, Index3); |
| 5941 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5942 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 5943 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 5944 | } |
| 5945 | } |
| 5946 | break; |
| 5947 | CASE_SRCS_IN(0, 1, 1, 0) : { |
| 5948 | if (Index0 == Index3 && Index1 == Index2) { |
| 5949 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5950 | Src0, Index0, Src1, Index1); |
| 5951 | T = lowerShuffleVector_AllFromSameSrc( |
| 5952 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_1, |
| 5953 | UNIFIED_INDEX_0); |
| 5954 | } else { |
| 5955 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5956 | Src0, Index0, Src1, Index1); |
| 5957 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5958 | Src1, Index2, Src0, Index3); |
| 5959 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5960 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 5961 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 5962 | } |
| 5963 | } |
| 5964 | break; |
| 5965 | CASE_SRCS_IN(0, 1, 1, 1) : { |
| 5966 | assert(false && "Following code is untested but likely correct; test " |
| 5967 | "and remove assert."); |
| 5968 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index0, |
| 5969 | Src1, Index1); |
| 5970 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5971 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src1, Index2, Index3); |
| 5972 | } |
| 5973 | break; |
| 5974 | CASE_SRCS_IN(1, 0, 0, 0) : { |
| 5975 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index0, |
| 5976 | Src0, Index1); |
| 5977 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5978 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src0, Index2, Index3); |
| 5979 | } |
| 5980 | break; |
| 5981 | CASE_SRCS_IN(1, 0, 0, 1) : { |
| 5982 | if (Index0 == Index3 && Index1 == Index2) { |
| 5983 | assert(false && "Following code is untested but likely correct; test " |
| 5984 | "and remove assert."); |
| 5985 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5986 | Src1, Index0, Src0, Index1); |
| 5987 | T = lowerShuffleVector_AllFromSameSrc( |
| 5988 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_1, |
| 5989 | UNIFIED_INDEX_0); |
| 5990 | } else { |
| 5991 | assert(false && "Following code is untested but likely correct; test " |
| 5992 | "and remove assert."); |
| 5993 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5994 | Src1, Index0, Src0, Index1); |
| 5995 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5996 | Src0, Index2, Src1, Index3); |
| 5997 | T = lowerShuffleVector_TwoFromSameSrc( |
| 5998 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 5999 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6000 | } |
| 6001 | } |
| 6002 | break; |
| 6003 | CASE_SRCS_IN(1, 0, 1, 0) : { |
| 6004 | if ((Index0 - ExpectedNumElements) == 0 && Index1 == 0 && |
| 6005 | (Index2 - ExpectedNumElements) == 1 && Index3 == 1) { |
| 6006 | auto *Src1RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 6007 | auto *Src0R = legalizeToReg(Src1); |
| 6008 | T = makeReg(DestTy); |
| 6009 | _movp(T, Src0R); |
| 6010 | _punpckl(T, Src1RM); |
| 6011 | } else if (Index0 == Index2 && Index1 == Index3) { |
| 6012 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6013 | Src1, Index0, Src0, Index1); |
| 6014 | T = lowerShuffleVector_AllFromSameSrc( |
| 6015 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_0, |
| 6016 | UNIFIED_INDEX_1); |
| 6017 | } else { |
| 6018 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6019 | Src1, Index0, Src0, Index1); |
| 6020 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6021 | Src1, Index2, Src0, Index3); |
| 6022 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6023 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 6024 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6025 | } |
| 6026 | } |
| 6027 | break; |
| 6028 | CASE_SRCS_IN(1, 0, 1, 1) : { |
| 6029 | assert(false && "Following code is untested but likely correct; test " |
| 6030 | "and remove assert."); |
| 6031 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index0, |
| 6032 | Src0, Index1); |
| 6033 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6034 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src1, Index2, Index3); |
| 6035 | } |
| 6036 | break; |
| 6037 | CASE_SRCS_IN(1, 1, 0, 0) : { |
| 6038 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Src0, |
| 6039 | Index2, Index3); |
| 6040 | } |
| 6041 | break; |
| 6042 | CASE_SRCS_IN(1, 1, 0, 1) : { |
| 6043 | assert(false && "Following code is untested but likely correct; test " |
| 6044 | "and remove assert."); |
| 6045 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index2, |
| 6046 | Src1, Index3); |
| 6047 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Unified, |
| 6048 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6049 | } |
| 6050 | break; |
| 6051 | CASE_SRCS_IN(1, 1, 1, 0) : { |
| 6052 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index2, |
| 6053 | Src0, Index3); |
| 6054 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Unified, |
| 6055 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6056 | } |
| 6057 | break; |
| 6058 | CASE_SRCS_IN(1, 1, 1, 1) : { |
| 6059 | assert(false && "Following code is untested but likely correct; test " |
| 6060 | "and remove assert."); |
| 6061 | T = lowerShuffleVector_AllFromSameSrc(Src1, Index0, Index1, Index2, |
| 6062 | Index3); |
| 6063 | } |
| 6064 | break; |
| 6065 | #undef CASE_SRCS_IN |
| 6066 | } |
| 6067 | |
| 6068 | assert(T != nullptr); |
| 6069 | assert(T->getType() == DestTy); |
| 6070 | _movp(Dest, T); |
| 6071 | return; |
| 6072 | } break; |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | // Unoptimized shuffle. Perform a series of inserts and extracts. |
| 6076 | Context.insert<InstFakeDef>(T); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 6077 | const Type ElementType = typeElementType(DestTy); |
| 6078 | for (SizeT I = 0; I < Instr->getNumIndexes(); ++I) { |
| 6079 | auto *Index = Instr->getIndex(I); |
| 6080 | const SizeT Elem = Index->getValue(); |
| 6081 | auto *ExtElmt = makeReg(ElementType); |
| 6082 | if (Elem < NumElements) { |
| 6083 | lowerExtractElement( |
| 6084 | InstExtractElement::create(Func, ExtElmt, Src0, Index)); |
| 6085 | } else { |
| 6086 | lowerExtractElement(InstExtractElement::create( |
| 6087 | Func, ExtElmt, Src1, |
| 6088 | Ctx->getConstantInt32(Index->getValue() - NumElements))); |
| 6089 | } |
| 6090 | auto *NewT = makeReg(DestTy); |
| 6091 | lowerInsertElement(InstInsertElement::create(Func, NewT, T, ExtElmt, |
| 6092 | Ctx->getConstantInt32(I))); |
| 6093 | T = NewT; |
| 6094 | } |
| 6095 | _movp(Dest, T); |
| 6096 | } |
| 6097 | |
| 6098 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6099 | void TargetX86Base<TraitsType>::lowerSelect(const InstSelect *Select) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6100 | Variable *Dest = Select->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6101 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6102 | if (isVectorType(Dest->getType())) { |
| 6103 | lowerSelectVector(Select); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6104 | return; |
| 6105 | } |
| 6106 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6107 | Operand *Condition = Select->getCondition(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6108 | // Handle folding opportunities. |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6109 | if (const Inst *Producer = FoldingInfo.getProducerFor(Condition)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6110 | assert(Producer->isDeleted()); |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6111 | switch (BoolFolding<Traits>::getProducerKind(Producer)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6112 | default: |
| 6113 | break; |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6114 | case BoolFolding<Traits>::PK_Icmp32: |
| 6115 | case BoolFolding<Traits>::PK_Icmp64: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6116 | lowerIcmpAndConsumer(llvm::cast<InstIcmp>(Producer), Select); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6117 | return; |
| 6118 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6119 | case BoolFolding<Traits>::PK_Fcmp: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6120 | lowerFcmpAndConsumer(llvm::cast<InstFcmp>(Producer), Select); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6121 | return; |
| 6122 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6123 | } |
| 6124 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6125 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6126 | Operand *CmpResult = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6127 | Operand *Zero = Ctx->getConstantZero(IceType_i32); |
| 6128 | _cmp(CmpResult, Zero); |
| 6129 | Operand *SrcT = Select->getTrueOperand(); |
| 6130 | Operand *SrcF = Select->getFalseOperand(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6131 | const BrCond Cond = Traits::Cond::Br_ne; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6132 | lowerSelectMove(Dest, Cond, SrcT, SrcF); |
| 6133 | } |
| 6134 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6135 | template <typename TraitsType> |
| 6136 | void TargetX86Base<TraitsType>::lowerSelectMove(Variable *Dest, BrCond Cond, |
| 6137 | Operand *SrcT, Operand *SrcF) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6138 | Type DestTy = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6139 | if (typeWidthInBytes(DestTy) == 1 || isFloatingType(DestTy)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6140 | // The cmov instruction doesn't allow 8-bit or FP operands, so we need |
| 6141 | // explicit control flow. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6142 | // d=cmp e,f; a=d?b:c ==> cmp e,f; a=b; jne L1; a=c; L1: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6143 | auto *Label = InstX86Label::create(Func, this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6144 | SrcT = legalize(SrcT, Legal_Reg | Legal_Imm); |
| 6145 | _mov(Dest, SrcT); |
| 6146 | _br(Cond, Label); |
| 6147 | SrcF = legalize(SrcF, Legal_Reg | Legal_Imm); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6148 | _redefined(_mov(Dest, SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6149 | Context.insert(Label); |
| 6150 | return; |
| 6151 | } |
| 6152 | // mov t, SrcF; cmov_cond t, SrcT; mov dest, t |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6153 | // But if SrcT is immediate, we might be able to do better, as the cmov |
| 6154 | // instruction doesn't allow an immediate operand: |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6155 | // mov t, SrcT; cmov_!cond t, SrcF; mov dest, t |
| 6156 | if (llvm::isa<Constant>(SrcT) && !llvm::isa<Constant>(SrcF)) { |
| 6157 | std::swap(SrcT, SrcF); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6158 | Cond = InstImpl<TraitsType>::InstX86Base::getOppositeCondition(Cond); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6159 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6160 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6161 | SrcT = legalizeUndef(SrcT); |
| 6162 | SrcF = legalizeUndef(SrcF); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6163 | // Set the low portion. |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6164 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6165 | lowerSelectIntMove(DestLo, Cond, loOperand(SrcT), loOperand(SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6166 | // Set the high portion. |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6167 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6168 | lowerSelectIntMove(DestHi, Cond, hiOperand(SrcT), hiOperand(SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6169 | return; |
| 6170 | } |
| 6171 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6172 | assert(DestTy == IceType_i16 || DestTy == IceType_i32 || |
| 6173 | (Traits::Is64Bit && DestTy == IceType_i64)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6174 | lowerSelectIntMove(Dest, Cond, SrcT, SrcF); |
| 6175 | } |
| 6176 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6177 | template <typename TraitsType> |
| 6178 | void TargetX86Base<TraitsType>::lowerSelectIntMove(Variable *Dest, BrCond Cond, |
| 6179 | Operand *SrcT, |
| 6180 | Operand *SrcF) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6181 | Variable *T = nullptr; |
| 6182 | SrcF = legalize(SrcF); |
| 6183 | _mov(T, SrcF); |
| 6184 | SrcT = legalize(SrcT, Legal_Reg | Legal_Mem); |
| 6185 | _cmov(T, SrcT, Cond); |
| 6186 | _mov(Dest, T); |
| 6187 | } |
| 6188 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6189 | template <typename TraitsType> |
| 6190 | void TargetX86Base<TraitsType>::lowerMove(Variable *Dest, Operand *Src, |
| 6191 | bool IsRedefinition) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6192 | assert(Dest->getType() == Src->getType()); |
| 6193 | assert(!Dest->isRematerializable()); |
| 6194 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
| 6195 | Src = legalize(Src); |
| 6196 | Operand *SrcLo = loOperand(Src); |
| 6197 | Operand *SrcHi = hiOperand(Src); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6198 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 6199 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6200 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
| 6201 | _mov(T_Lo, SrcLo); |
| 6202 | _redefined(_mov(DestLo, T_Lo), IsRedefinition); |
| 6203 | _mov(T_Hi, SrcHi); |
| 6204 | _redefined(_mov(DestHi, T_Hi), IsRedefinition); |
| 6205 | } else { |
| 6206 | Operand *SrcLegal; |
| 6207 | if (Dest->hasReg()) { |
| 6208 | // If Dest already has a physical register, then only basic legalization |
| 6209 | // is needed, as the source operand can be a register, immediate, or |
| 6210 | // memory. |
| 6211 | SrcLegal = legalize(Src, Legal_Reg, Dest->getRegNum()); |
| 6212 | } else { |
| 6213 | // If Dest could be a stack operand, then RI must be a physical register |
| 6214 | // or a scalar integer immediate. |
| 6215 | SrcLegal = legalize(Src, Legal_Reg | Legal_Imm); |
| 6216 | } |
| 6217 | if (isVectorType(Dest->getType())) { |
| 6218 | _redefined(_movp(Dest, SrcLegal), IsRedefinition); |
| 6219 | } else { |
| 6220 | _redefined(_mov(Dest, SrcLegal), IsRedefinition); |
| 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6225 | template <typename TraitsType> |
| 6226 | bool TargetX86Base<TraitsType>::lowerOptimizeFcmpSelect( |
| 6227 | const InstFcmp *Fcmp, const InstSelect *Select) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6228 | Operand *CmpSrc0 = Fcmp->getSrc(0); |
| 6229 | Operand *CmpSrc1 = Fcmp->getSrc(1); |
| 6230 | Operand *SelectSrcT = Select->getTrueOperand(); |
| 6231 | Operand *SelectSrcF = Select->getFalseOperand(); |
| 6232 | |
| 6233 | if (CmpSrc0->getType() != SelectSrcT->getType()) |
| 6234 | return false; |
| 6235 | |
| 6236 | // TODO(sehr, stichnot): fcmp/select patterns (e,g., minsd/maxss) go here. |
| 6237 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
| 6238 | switch (Condition) { |
| 6239 | default: |
| 6240 | return false; |
| 6241 | case InstFcmp::True: |
| 6242 | case InstFcmp::False: |
| 6243 | case InstFcmp::Ogt: |
| 6244 | case InstFcmp::Olt: |
| 6245 | (void)CmpSrc0; |
| 6246 | (void)CmpSrc1; |
| 6247 | (void)SelectSrcT; |
| 6248 | (void)SelectSrcF; |
| 6249 | break; |
| 6250 | } |
| 6251 | return false; |
| 6252 | } |
| 6253 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6254 | template <typename TraitsType> |
| 6255 | void TargetX86Base<TraitsType>::lowerIcmp(const InstIcmp *Icmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6256 | Variable *Dest = Icmp->getDest(); |
| 6257 | if (isVectorType(Dest->getType())) { |
| 6258 | lowerIcmpVector(Icmp); |
| 6259 | } else { |
| 6260 | constexpr Inst *Consumer = nullptr; |
| 6261 | lowerIcmpAndConsumer(Icmp, Consumer); |
| 6262 | } |
| 6263 | } |
| 6264 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6265 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6266 | void TargetX86Base<TraitsType>::lowerSelectVector(const InstSelect *Instr) { |
| 6267 | Variable *Dest = Instr->getDest(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6268 | Type DestTy = Dest->getType(); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6269 | Operand *SrcT = Instr->getTrueOperand(); |
| 6270 | Operand *SrcF = Instr->getFalseOperand(); |
| 6271 | Operand *Condition = Instr->getCondition(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6272 | |
| 6273 | if (!isVectorType(DestTy)) |
| 6274 | llvm::report_fatal_error("Expected a vector select"); |
| 6275 | |
| 6276 | Type SrcTy = SrcT->getType(); |
| 6277 | Variable *T = makeReg(SrcTy); |
| 6278 | Operand *SrcTRM = legalize(SrcT, Legal_Reg | Legal_Mem); |
| 6279 | Operand *SrcFRM = legalize(SrcF, Legal_Reg | Legal_Mem); |
| 6280 | if (InstructionSet >= Traits::SSE4_1) { |
| 6281 | // TODO(wala): If the condition operand is a constant, use blendps or |
| 6282 | // pblendw. |
| 6283 | // |
| 6284 | // Use blendvps or pblendvb to implement select. |
| 6285 | if (SrcTy == IceType_v4i1 || SrcTy == IceType_v4i32 || |
| 6286 | SrcTy == IceType_v4f32) { |
| 6287 | Operand *ConditionRM = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6288 | Variable *xmm0 = makeReg(IceType_v4i32, Traits::RegisterSet::Reg_xmm0); |
| 6289 | _movp(xmm0, ConditionRM); |
| 6290 | _psll(xmm0, Ctx->getConstantInt8(31)); |
| 6291 | _movp(T, SrcFRM); |
| 6292 | _blendvps(T, SrcTRM, xmm0); |
| 6293 | _movp(Dest, T); |
| 6294 | } else { |
| 6295 | assert(typeNumElements(SrcTy) == 8 || typeNumElements(SrcTy) == 16); |
| 6296 | Type SignExtTy = |
| 6297 | Condition->getType() == IceType_v8i1 ? IceType_v8i16 : IceType_v16i8; |
| 6298 | Variable *xmm0 = makeReg(SignExtTy, Traits::RegisterSet::Reg_xmm0); |
| 6299 | lowerCast(InstCast::create(Func, InstCast::Sext, xmm0, Condition)); |
| 6300 | _movp(T, SrcFRM); |
| 6301 | _pblendvb(T, SrcTRM, xmm0); |
| 6302 | _movp(Dest, T); |
| 6303 | } |
| 6304 | return; |
| 6305 | } |
| 6306 | // Lower select without Traits::SSE4.1: |
| 6307 | // a=d?b:c ==> |
| 6308 | // if elementtype(d) != i1: |
| 6309 | // d=sext(d); |
| 6310 | // a=(b&d)|(c&~d); |
| 6311 | Variable *T2 = makeReg(SrcTy); |
| 6312 | // Sign extend the condition operand if applicable. |
| 6313 | if (SrcTy == IceType_v4f32) { |
| 6314 | // The sext operation takes only integer arguments. |
| 6315 | Variable *T3 = Func->makeVariable(IceType_v4i32); |
| 6316 | lowerCast(InstCast::create(Func, InstCast::Sext, T3, Condition)); |
| 6317 | _movp(T, T3); |
| 6318 | } else if (typeElementType(SrcTy) != IceType_i1) { |
| 6319 | lowerCast(InstCast::create(Func, InstCast::Sext, T, Condition)); |
| 6320 | } else { |
| 6321 | Operand *ConditionRM = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6322 | _movp(T, ConditionRM); |
| 6323 | } |
| 6324 | _movp(T2, T); |
| 6325 | _pand(T, SrcTRM); |
| 6326 | _pandn(T2, SrcFRM); |
| 6327 | _por(T, T2); |
| 6328 | _movp(Dest, T); |
| 6329 | |
| 6330 | return; |
| 6331 | } |
| 6332 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6333 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6334 | void TargetX86Base<TraitsType>::lowerStore(const InstStore *Instr) { |
| 6335 | Operand *Value = Instr->getData(); |
| 6336 | Operand *Addr = Instr->getAddr(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6337 | X86OperandMem *NewAddr = formMemoryOperand(Addr, Value->getType()); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 6338 | doMockBoundsCheck(NewAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6339 | Type Ty = NewAddr->getType(); |
| 6340 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6341 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6342 | Value = legalizeUndef(Value); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6343 | Operand *ValueHi = legalize(hiOperand(Value), Legal_Reg | Legal_Imm); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6344 | _store(ValueHi, llvm::cast<X86OperandMem>(hiOperand(NewAddr))); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 6345 | Operand *ValueLo = legalize(loOperand(Value), Legal_Reg | Legal_Imm); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6346 | _store(ValueLo, llvm::cast<X86OperandMem>(loOperand(NewAddr))); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6347 | } else if (isVectorType(Ty)) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6348 | _storep(legalizeToReg(Value), NewAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6349 | } else { |
| 6350 | Value = legalize(Value, Legal_Reg | Legal_Imm); |
| 6351 | _store(Value, NewAddr); |
| 6352 | } |
| 6353 | } |
| 6354 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6355 | template <typename TraitsType> |
| 6356 | void TargetX86Base<TraitsType>::doAddressOptStore() { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6357 | auto *Instr = llvm::cast<InstStore>(Context.getCur()); |
| 6358 | Operand *Addr = Instr->getAddr(); |
| 6359 | Operand *Data = Instr->getData(); |
| 6360 | if (auto *OptAddr = computeAddressOpt(Instr, Data->getType(), Addr)) { |
| 6361 | Instr->setDeleted(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6362 | auto *NewStore = Context.insert<InstStore>(Data, OptAddr); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6363 | if (Instr->getDest()) |
| 6364 | NewStore->setRmwBeacon(Instr->getRmwBeacon()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6368 | template <typename TraitsType> |
| 6369 | Operand *TargetX86Base<TraitsType>::lowerCmpRange(Operand *Comparison, |
| 6370 | uint64_t Min, uint64_t Max) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6371 | // TODO(ascull): 64-bit should not reach here but only because it is not |
| 6372 | // implemented yet. This should be able to handle the 64-bit case. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6373 | assert(Traits::Is64Bit || Comparison->getType() != IceType_i64); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6374 | // Subtracting 0 is a nop so don't do it |
| 6375 | if (Min != 0) { |
| 6376 | // Avoid clobbering the comparison by copying it |
| 6377 | Variable *T = nullptr; |
| 6378 | _mov(T, Comparison); |
| 6379 | _sub(T, Ctx->getConstantInt32(Min)); |
| 6380 | Comparison = T; |
| 6381 | } |
| 6382 | |
| 6383 | _cmp(Comparison, Ctx->getConstantInt32(Max - Min)); |
| 6384 | |
| 6385 | return Comparison; |
| 6386 | } |
| 6387 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6388 | template <typename TraitsType> |
| 6389 | void TargetX86Base<TraitsType>::lowerCaseCluster(const CaseCluster &Case, |
| 6390 | Operand *Comparison, |
| 6391 | bool DoneCmp, |
| 6392 | CfgNode *DefaultTarget) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6393 | switch (Case.getKind()) { |
| 6394 | case CaseCluster::JumpTable: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6395 | InstX86Label *SkipJumpTable; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6396 | |
| 6397 | Operand *RangeIndex = |
| 6398 | lowerCmpRange(Comparison, Case.getLow(), Case.getHigh()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6399 | if (DefaultTarget == nullptr) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6400 | // Skip over jump table logic if comparison not in range and no default |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6401 | SkipJumpTable = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6402 | _br(Traits::Cond::Br_a, SkipJumpTable); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6403 | } else { |
| 6404 | _br(Traits::Cond::Br_a, DefaultTarget); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6405 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6406 | |
| 6407 | InstJumpTable *JumpTable = Case.getJumpTable(); |
| 6408 | Context.insert(JumpTable); |
| 6409 | |
| 6410 | // Make sure the index is a register of the same width as the base |
| 6411 | Variable *Index; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6412 | const Type PointerType = getPointerType(); |
| 6413 | if (RangeIndex->getType() != PointerType) { |
| 6414 | Index = makeReg(PointerType); |
Jim Stichnoth | e641e92 | 2016-02-29 09:54:55 -0800 | [diff] [blame] | 6415 | if (RangeIndex->getType() == IceType_i64) { |
| 6416 | assert(Traits::Is64Bit); |
| 6417 | _mov(Index, RangeIndex); // trunc |
| 6418 | } else { |
| 6419 | _movzx(Index, RangeIndex); |
| 6420 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6421 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6422 | Index = legalizeToReg(RangeIndex); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | constexpr RelocOffsetT RelocOffset = 0; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6426 | constexpr Variable *NoBase = nullptr; |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 6427 | auto JTName = GlobalString::createWithString(Ctx, JumpTable->getName()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 6428 | Constant *Offset = Ctx->getConstantSym(RelocOffset, JTName); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6429 | uint16_t Shift = typeWidthInBytesLog2(PointerType); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6430 | constexpr auto Segment = X86OperandMem::SegmentRegisters::DefaultSegment; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6431 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6432 | Variable *Target = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6433 | if (Traits::Is64Bit && NeedSandboxing) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6434 | assert(Index != nullptr && Index->getType() == IceType_i32); |
| 6435 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6436 | auto *TargetInMemory = X86OperandMem::create(Func, PointerType, NoBase, |
| 6437 | Offset, Index, Shift, Segment); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6438 | _mov(Target, TargetInMemory); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6439 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6440 | lowerIndirectJump(Target); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6441 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6442 | if (DefaultTarget == nullptr) |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6443 | Context.insert(SkipJumpTable); |
| 6444 | return; |
| 6445 | } |
| 6446 | case CaseCluster::Range: { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6447 | if (Case.isUnitRange()) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6448 | // Single item |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6449 | if (!DoneCmp) { |
| 6450 | Constant *Value = Ctx->getConstantInt32(Case.getLow()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6451 | _cmp(Comparison, Value); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6452 | } |
| 6453 | _br(Traits::Cond::Br_e, Case.getTarget()); |
| 6454 | } else if (DoneCmp && Case.isPairRange()) { |
| 6455 | // Range of two items with first item aleady compared against |
| 6456 | _br(Traits::Cond::Br_e, Case.getTarget()); |
| 6457 | Constant *Value = Ctx->getConstantInt32(Case.getHigh()); |
| 6458 | _cmp(Comparison, Value); |
| 6459 | _br(Traits::Cond::Br_e, Case.getTarget()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6460 | } else { |
| 6461 | // Range |
| 6462 | lowerCmpRange(Comparison, Case.getLow(), Case.getHigh()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6463 | _br(Traits::Cond::Br_be, Case.getTarget()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6464 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6465 | if (DefaultTarget != nullptr) |
| 6466 | _br(DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6467 | return; |
| 6468 | } |
| 6469 | } |
| 6470 | } |
| 6471 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6472 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6473 | void TargetX86Base<TraitsType>::lowerSwitch(const InstSwitch *Instr) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6474 | // Group cases together and navigate through them with a binary search |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6475 | CaseClusterArray CaseClusters = CaseCluster::clusterizeSwitch(Func, Instr); |
| 6476 | Operand *Src0 = Instr->getComparison(); |
| 6477 | CfgNode *DefaultTarget = Instr->getLabelDefault(); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6478 | |
| 6479 | assert(CaseClusters.size() != 0); // Should always be at least one |
| 6480 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6481 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6482 | Src0 = legalize(Src0); // get Base/Index into physical registers |
| 6483 | Operand *Src0Lo = loOperand(Src0); |
| 6484 | Operand *Src0Hi = hiOperand(Src0); |
| 6485 | if (CaseClusters.back().getHigh() > UINT32_MAX) { |
| 6486 | // TODO(ascull): handle 64-bit case properly (currently naive version) |
| 6487 | // This might be handled by a higher level lowering of switches. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6488 | SizeT NumCases = Instr->getNumCases(); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6489 | if (NumCases >= 2) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6490 | Src0Lo = legalizeToReg(Src0Lo); |
| 6491 | Src0Hi = legalizeToReg(Src0Hi); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6492 | } else { |
| 6493 | Src0Lo = legalize(Src0Lo, Legal_Reg | Legal_Mem); |
| 6494 | Src0Hi = legalize(Src0Hi, Legal_Reg | Legal_Mem); |
| 6495 | } |
| 6496 | for (SizeT I = 0; I < NumCases; ++I) { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6497 | Constant *ValueLo = Ctx->getConstantInt32(Instr->getValue(I)); |
| 6498 | Constant *ValueHi = Ctx->getConstantInt32(Instr->getValue(I) >> 32); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6499 | InstX86Label *Label = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6500 | _cmp(Src0Lo, ValueLo); |
| 6501 | _br(Traits::Cond::Br_ne, Label); |
| 6502 | _cmp(Src0Hi, ValueHi); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6503 | _br(Traits::Cond::Br_e, Instr->getLabel(I)); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6504 | Context.insert(Label); |
| 6505 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6506 | _br(Instr->getLabelDefault()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6507 | return; |
| 6508 | } else { |
| 6509 | // All the values are 32-bit so just check the operand is too and then |
| 6510 | // fall through to the 32-bit implementation. This is a common case. |
| 6511 | Src0Hi = legalize(Src0Hi, Legal_Reg | Legal_Mem); |
| 6512 | Constant *Zero = Ctx->getConstantInt32(0); |
| 6513 | _cmp(Src0Hi, Zero); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6514 | _br(Traits::Cond::Br_ne, DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6515 | Src0 = Src0Lo; |
| 6516 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6517 | } |
| 6518 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6519 | // 32-bit lowering |
| 6520 | |
| 6521 | if (CaseClusters.size() == 1) { |
| 6522 | // Jump straight to default if needed. Currently a common case as jump |
| 6523 | // tables occur on their own. |
| 6524 | constexpr bool DoneCmp = false; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6525 | lowerCaseCluster(CaseClusters.front(), Src0, DoneCmp, DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6526 | return; |
| 6527 | } |
| 6528 | |
| 6529 | // Going to be using multiple times so get it in a register early |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6530 | Variable *Comparison = legalizeToReg(Src0); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6531 | |
| 6532 | // A span is over the clusters |
| 6533 | struct SearchSpan { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6534 | SearchSpan(SizeT Begin, SizeT Size, InstX86Label *Label) |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6535 | : Begin(Begin), Size(Size), Label(Label) {} |
| 6536 | |
| 6537 | SizeT Begin; |
| 6538 | SizeT Size; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6539 | InstX86Label *Label; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6540 | }; |
Andrew Scull | 8447bba | 2015-07-23 11:41:18 -0700 | [diff] [blame] | 6541 | // The stack will only grow to the height of the tree so 12 should be plenty |
| 6542 | std::stack<SearchSpan, llvm::SmallVector<SearchSpan, 12>> SearchSpanStack; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6543 | SearchSpanStack.emplace(0, CaseClusters.size(), nullptr); |
| 6544 | bool DoneCmp = false; |
| 6545 | |
| 6546 | while (!SearchSpanStack.empty()) { |
| 6547 | SearchSpan Span = SearchSpanStack.top(); |
| 6548 | SearchSpanStack.pop(); |
| 6549 | |
| 6550 | if (Span.Label != nullptr) |
| 6551 | Context.insert(Span.Label); |
| 6552 | |
| 6553 | switch (Span.Size) { |
| 6554 | case 0: |
| 6555 | llvm::report_fatal_error("Invalid SearchSpan size"); |
| 6556 | break; |
| 6557 | |
| 6558 | case 1: |
| 6559 | lowerCaseCluster(CaseClusters[Span.Begin], Comparison, DoneCmp, |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6560 | SearchSpanStack.empty() ? nullptr : DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6561 | DoneCmp = false; |
| 6562 | break; |
| 6563 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6564 | case 2: { |
| 6565 | const CaseCluster *CaseA = &CaseClusters[Span.Begin]; |
| 6566 | const CaseCluster *CaseB = &CaseClusters[Span.Begin + 1]; |
| 6567 | |
| 6568 | // Placing a range last may allow register clobbering during the range |
| 6569 | // test. That means there is no need to clone the register. If it is a |
| 6570 | // unit range the comparison may have already been done in the binary |
| 6571 | // search (DoneCmp) and so it should be placed first. If this is a range |
| 6572 | // of two items and the comparison with the low value has already been |
| 6573 | // done, comparing with the other element is cheaper than a range test. |
| 6574 | // If the low end of the range is zero then there is no subtraction and |
| 6575 | // nothing to be gained. |
| 6576 | if (!CaseA->isUnitRange() && |
| 6577 | !(CaseA->getLow() == 0 || (DoneCmp && CaseA->isPairRange()))) { |
| 6578 | std::swap(CaseA, CaseB); |
| 6579 | DoneCmp = false; |
| 6580 | } |
| 6581 | |
| 6582 | lowerCaseCluster(*CaseA, Comparison, DoneCmp); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6583 | DoneCmp = false; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6584 | lowerCaseCluster(*CaseB, Comparison, DoneCmp, |
| 6585 | SearchSpanStack.empty() ? nullptr : DefaultTarget); |
| 6586 | } break; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6587 | |
| 6588 | default: |
| 6589 | // Pick the middle item and branch b or ae |
| 6590 | SizeT PivotIndex = Span.Begin + (Span.Size / 2); |
| 6591 | const CaseCluster &Pivot = CaseClusters[PivotIndex]; |
| 6592 | Constant *Value = Ctx->getConstantInt32(Pivot.getLow()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6593 | InstX86Label *Label = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6594 | _cmp(Comparison, Value); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6595 | // TODO(ascull): does it alway have to be far? |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6596 | _br(Traits::Cond::Br_b, Label, InstX86Br::Far); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6597 | // Lower the left and (pivot+right) sides, falling through to the right |
| 6598 | SearchSpanStack.emplace(Span.Begin, Span.Size / 2, Label); |
| 6599 | SearchSpanStack.emplace(PivotIndex, Span.Size - (Span.Size / 2), nullptr); |
| 6600 | DoneCmp = true; |
| 6601 | break; |
| 6602 | } |
| 6603 | } |
| 6604 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6605 | _br(DefaultTarget); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6606 | } |
| 6607 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 6608 | /// The following pattern occurs often in lowered C and C++ code: |
| 6609 | /// |
| 6610 | /// %cmp = fcmp/icmp pred <n x ty> %src0, %src1 |
| 6611 | /// %cmp.ext = sext <n x i1> %cmp to <n x ty> |
| 6612 | /// |
| 6613 | /// We can eliminate the sext operation by copying the result of pcmpeqd, |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6614 | /// pcmpgtd, or cmpps (which produce sign extended results) to the result of the |
| 6615 | /// sext operation. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6616 | template <typename TraitsType> |
| 6617 | void TargetX86Base<TraitsType>::eliminateNextVectorSextInstruction( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6618 | Variable *SignExtendedResult) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 6619 | if (auto *NextCast = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6620 | llvm::dyn_cast_or_null<InstCast>(Context.getNextInst())) { |
| 6621 | if (NextCast->getCastKind() == InstCast::Sext && |
| 6622 | NextCast->getSrc(0) == SignExtendedResult) { |
| 6623 | NextCast->setDeleted(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6624 | _movp(NextCast->getDest(), legalizeToReg(SignExtendedResult)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6625 | // Skip over the instruction. |
| 6626 | Context.advanceNext(); |
| 6627 | } |
| 6628 | } |
| 6629 | } |
| 6630 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6631 | template <typename TraitsType> |
| 6632 | void TargetX86Base<TraitsType>::lowerUnreachable( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6633 | const InstUnreachable * /*Instr*/) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6634 | _ud2(); |
David Sehr | 21fd103 | 2015-11-13 16:32:37 -0800 | [diff] [blame] | 6635 | // Add a fake use of esp to make sure esp adjustments after the unreachable |
| 6636 | // do not get dead-code eliminated. |
| 6637 | keepEspLiveAtExit(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6638 | } |
| 6639 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6640 | template <typename TraitsType> |
Eric Holk | 67c7c41 | 2016-04-15 13:05:37 -0700 | [diff] [blame] | 6641 | void TargetX86Base<TraitsType>::lowerBreakpoint( |
| 6642 | const InstBreakpoint * /*Instr*/) { |
| 6643 | _int3(); |
| 6644 | } |
| 6645 | |
| 6646 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6647 | void TargetX86Base<TraitsType>::lowerRMW(const InstX86FakeRMW *RMW) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6648 | // If the beacon variable's live range does not end in this instruction, then |
| 6649 | // it must end in the modified Store instruction that follows. This means |
| 6650 | // that the original Store instruction is still there, either because the |
| 6651 | // value being stored is used beyond the Store instruction, or because dead |
| 6652 | // code elimination did not happen. In either case, we cancel RMW lowering |
| 6653 | // (and the caller deletes the RMW instruction). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6654 | if (!RMW->isLastUse(RMW->getBeacon())) |
| 6655 | return; |
| 6656 | Operand *Src = RMW->getData(); |
| 6657 | Type Ty = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6658 | X86OperandMem *Addr = formMemoryOperand(RMW->getAddr(), Ty); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 6659 | doMockBoundsCheck(Addr); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6660 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6661 | Src = legalizeUndef(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6662 | Operand *SrcLo = legalize(loOperand(Src), Legal_Reg | Legal_Imm); |
| 6663 | Operand *SrcHi = legalize(hiOperand(Src), Legal_Reg | Legal_Imm); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6664 | auto *AddrLo = llvm::cast<X86OperandMem>(loOperand(Addr)); |
| 6665 | auto *AddrHi = llvm::cast<X86OperandMem>(hiOperand(Addr)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6666 | switch (RMW->getOp()) { |
| 6667 | default: |
| 6668 | // TODO(stichnot): Implement other arithmetic operators. |
| 6669 | break; |
| 6670 | case InstArithmetic::Add: |
| 6671 | _add_rmw(AddrLo, SrcLo); |
| 6672 | _adc_rmw(AddrHi, SrcHi); |
| 6673 | return; |
| 6674 | case InstArithmetic::Sub: |
| 6675 | _sub_rmw(AddrLo, SrcLo); |
| 6676 | _sbb_rmw(AddrHi, SrcHi); |
| 6677 | return; |
| 6678 | case InstArithmetic::And: |
| 6679 | _and_rmw(AddrLo, SrcLo); |
| 6680 | _and_rmw(AddrHi, SrcHi); |
| 6681 | return; |
| 6682 | case InstArithmetic::Or: |
| 6683 | _or_rmw(AddrLo, SrcLo); |
| 6684 | _or_rmw(AddrHi, SrcHi); |
| 6685 | return; |
| 6686 | case InstArithmetic::Xor: |
| 6687 | _xor_rmw(AddrLo, SrcLo); |
| 6688 | _xor_rmw(AddrHi, SrcHi); |
| 6689 | return; |
| 6690 | } |
| 6691 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6692 | // x86-32: i8, i16, i32 |
| 6693 | // x86-64: i8, i16, i32, i64 |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6694 | switch (RMW->getOp()) { |
| 6695 | default: |
| 6696 | // TODO(stichnot): Implement other arithmetic operators. |
| 6697 | break; |
| 6698 | case InstArithmetic::Add: |
| 6699 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6700 | _add_rmw(Addr, Src); |
| 6701 | return; |
| 6702 | case InstArithmetic::Sub: |
| 6703 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6704 | _sub_rmw(Addr, Src); |
| 6705 | return; |
| 6706 | case InstArithmetic::And: |
| 6707 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6708 | _and_rmw(Addr, Src); |
| 6709 | return; |
| 6710 | case InstArithmetic::Or: |
| 6711 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6712 | _or_rmw(Addr, Src); |
| 6713 | return; |
| 6714 | case InstArithmetic::Xor: |
| 6715 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6716 | _xor_rmw(Addr, Src); |
| 6717 | return; |
| 6718 | } |
| 6719 | } |
| 6720 | llvm::report_fatal_error("Couldn't lower RMW instruction"); |
| 6721 | } |
| 6722 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6723 | template <typename TraitsType> |
| 6724 | void TargetX86Base<TraitsType>::lowerOther(const Inst *Instr) { |
| 6725 | if (const auto *RMW = llvm::dyn_cast<InstX86FakeRMW>(Instr)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6726 | lowerRMW(RMW); |
| 6727 | } else { |
| 6728 | TargetLowering::lowerOther(Instr); |
| 6729 | } |
| 6730 | } |
| 6731 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6732 | /// Turn an i64 Phi instruction into a pair of i32 Phi instructions, to preserve |
| 6733 | /// integrity of liveness analysis. Undef values are also turned into zeroes, |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6734 | /// since loOperand() and hiOperand() don't expect Undef input. Also, in |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6735 | /// Non-SFI mode, add a FakeUse(RebasePtr) for every pooled constant operand. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6736 | template <typename TraitsType> void TargetX86Base<TraitsType>::prelowerPhis() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 6737 | if (getFlags().getUseNonsfi()) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6738 | assert(RebasePtr); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6739 | CfgNode *Node = Context.getNode(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6740 | uint32_t RebasePtrUseCount = 0; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6741 | for (Inst &I : Node->getPhis()) { |
| 6742 | auto *Phi = llvm::dyn_cast<InstPhi>(&I); |
| 6743 | if (Phi->isDeleted()) |
| 6744 | continue; |
| 6745 | for (SizeT I = 0; I < Phi->getSrcSize(); ++I) { |
| 6746 | Operand *Src = Phi->getSrc(I); |
| 6747 | // TODO(stichnot): This over-counts for +0.0, and under-counts for other |
| 6748 | // kinds of pooling. |
| 6749 | if (llvm::isa<ConstantRelocatable>(Src) || |
| 6750 | llvm::isa<ConstantFloat>(Src) || llvm::isa<ConstantDouble>(Src)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6751 | ++RebasePtrUseCount; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6752 | } |
| 6753 | } |
| 6754 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6755 | if (RebasePtrUseCount) { |
| 6756 | Node->getInsts().push_front(InstFakeUse::create(Func, RebasePtr)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6757 | } |
| 6758 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6759 | if (Traits::Is64Bit) { |
| 6760 | // On x86-64 we don't need to prelower phis -- the architecture can handle |
| 6761 | // 64-bit integer natively. |
| 6762 | return; |
| 6763 | } |
| 6764 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6765 | // Pause constant blinding or pooling, blinding or pooling will be done later |
| 6766 | // during phi lowering assignments |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6767 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6768 | PhiLowering::prelowerPhis32Bit<TargetX86Base<TraitsType>>( |
Jan Voung | 5348369 | 2015-07-16 10:47:46 -0700 | [diff] [blame] | 6769 | this, Context.getNode(), Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6770 | } |
| 6771 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6772 | template <typename TraitsType> |
| 6773 | void TargetX86Base<TraitsType>::genTargetHelperCallFor(Inst *Instr) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6774 | uint32_t StackArgumentsSize = 0; |
| 6775 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Instr)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6776 | RuntimeHelper HelperID = RuntimeHelper::H_Num; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6777 | Variable *Dest = Arith->getDest(); |
| 6778 | Type DestTy = Dest->getType(); |
| 6779 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
| 6780 | switch (Arith->getOp()) { |
| 6781 | default: |
| 6782 | return; |
| 6783 | case InstArithmetic::Udiv: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6784 | HelperID = RuntimeHelper::H_udiv_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6785 | break; |
| 6786 | case InstArithmetic::Sdiv: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6787 | HelperID = RuntimeHelper::H_sdiv_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6788 | break; |
| 6789 | case InstArithmetic::Urem: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6790 | HelperID = RuntimeHelper::H_urem_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6791 | break; |
| 6792 | case InstArithmetic::Srem: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6793 | HelperID = RuntimeHelper::H_srem_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6794 | break; |
| 6795 | } |
| 6796 | } else if (isVectorType(DestTy)) { |
| 6797 | Variable *Dest = Arith->getDest(); |
| 6798 | Operand *Src0 = Arith->getSrc(0); |
| 6799 | Operand *Src1 = Arith->getSrc(1); |
| 6800 | switch (Arith->getOp()) { |
| 6801 | default: |
| 6802 | return; |
| 6803 | case InstArithmetic::Mul: |
| 6804 | if (DestTy == IceType_v16i8) { |
| 6805 | scalarizeArithmetic(Arith->getOp(), Dest, Src0, Src1); |
| 6806 | Arith->setDeleted(); |
| 6807 | } |
| 6808 | return; |
| 6809 | case InstArithmetic::Shl: |
| 6810 | case InstArithmetic::Lshr: |
| 6811 | case InstArithmetic::Ashr: |
| 6812 | case InstArithmetic::Udiv: |
| 6813 | case InstArithmetic::Urem: |
| 6814 | case InstArithmetic::Sdiv: |
| 6815 | case InstArithmetic::Srem: |
| 6816 | case InstArithmetic::Frem: |
| 6817 | scalarizeArithmetic(Arith->getOp(), Dest, Src0, Src1); |
| 6818 | Arith->setDeleted(); |
| 6819 | return; |
| 6820 | } |
| 6821 | } else { |
| 6822 | switch (Arith->getOp()) { |
| 6823 | default: |
| 6824 | return; |
| 6825 | case InstArithmetic::Frem: |
| 6826 | if (isFloat32Asserting32Or64(DestTy)) |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6827 | HelperID = RuntimeHelper::H_frem_f32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6828 | else |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6829 | HelperID = RuntimeHelper::H_frem_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6830 | } |
| 6831 | } |
| 6832 | constexpr SizeT MaxSrcs = 2; |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6833 | InstCall *Call = makeHelperCall(HelperID, Dest, MaxSrcs); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6834 | Call->addArg(Arith->getSrc(0)); |
| 6835 | Call->addArg(Arith->getSrc(1)); |
| 6836 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 6837 | Context.insert(Call); |
| 6838 | Arith->setDeleted(); |
| 6839 | } else if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 6840 | InstCast::OpKind CastKind = Cast->getCastKind(); |
| 6841 | Operand *Src0 = Cast->getSrc(0); |
| 6842 | const Type SrcType = Src0->getType(); |
| 6843 | Variable *Dest = Cast->getDest(); |
| 6844 | const Type DestTy = Dest->getType(); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6845 | RuntimeHelper HelperID = RuntimeHelper::H_Num; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 6846 | Variable *CallDest = Dest; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6847 | switch (CastKind) { |
| 6848 | default: |
| 6849 | return; |
| 6850 | case InstCast::Fptosi: |
| 6851 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6852 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6853 | ? RuntimeHelper::H_fptosi_f32_i64 |
| 6854 | : RuntimeHelper::H_fptosi_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6855 | } else { |
| 6856 | return; |
| 6857 | } |
| 6858 | break; |
| 6859 | case InstCast::Fptoui: |
| 6860 | if (isVectorType(DestTy)) { |
| 6861 | assert(DestTy == IceType_v4i32 && SrcType == IceType_v4f32); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6862 | HelperID = RuntimeHelper::H_fptoui_4xi32_f32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6863 | } else if (DestTy == IceType_i64 || |
| 6864 | (!Traits::Is64Bit && DestTy == IceType_i32)) { |
| 6865 | if (Traits::Is64Bit) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6866 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6867 | ? RuntimeHelper::H_fptoui_f32_i64 |
| 6868 | : RuntimeHelper::H_fptoui_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6869 | } else if (isInt32Asserting32Or64(DestTy)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6870 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6871 | ? RuntimeHelper::H_fptoui_f32_i32 |
| 6872 | : RuntimeHelper::H_fptoui_f64_i32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6873 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6874 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6875 | ? RuntimeHelper::H_fptoui_f32_i64 |
| 6876 | : RuntimeHelper::H_fptoui_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6877 | } |
| 6878 | } else { |
| 6879 | return; |
| 6880 | } |
| 6881 | break; |
| 6882 | case InstCast::Sitofp: |
| 6883 | if (!Traits::Is64Bit && SrcType == IceType_i64) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6884 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 6885 | ? RuntimeHelper::H_sitofp_i64_f32 |
| 6886 | : RuntimeHelper::H_sitofp_i64_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6887 | } else { |
| 6888 | return; |
| 6889 | } |
| 6890 | break; |
| 6891 | case InstCast::Uitofp: |
| 6892 | if (isVectorType(SrcType)) { |
| 6893 | assert(DestTy == IceType_v4f32 && SrcType == IceType_v4i32); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6894 | HelperID = RuntimeHelper::H_uitofp_4xi32_4xf32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6895 | } else if (SrcType == IceType_i64 || |
| 6896 | (!Traits::Is64Bit && SrcType == IceType_i32)) { |
| 6897 | if (isInt32Asserting32Or64(SrcType)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6898 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 6899 | ? RuntimeHelper::H_uitofp_i32_f32 |
| 6900 | : RuntimeHelper::H_uitofp_i32_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6901 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6902 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 6903 | ? RuntimeHelper::H_uitofp_i64_f32 |
| 6904 | : RuntimeHelper::H_uitofp_i64_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6905 | } |
| 6906 | } else { |
| 6907 | return; |
| 6908 | } |
| 6909 | break; |
| 6910 | case InstCast::Bitcast: { |
| 6911 | if (DestTy == Src0->getType()) |
| 6912 | return; |
| 6913 | switch (DestTy) { |
| 6914 | default: |
| 6915 | return; |
| 6916 | case IceType_i8: |
| 6917 | assert(Src0->getType() == IceType_v8i1); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6918 | HelperID = RuntimeHelper::H_bitcast_8xi1_i8; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 6919 | CallDest = Func->makeVariable(IceType_i32); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6920 | break; |
| 6921 | case IceType_i16: |
| 6922 | assert(Src0->getType() == IceType_v16i1); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6923 | HelperID = RuntimeHelper::H_bitcast_16xi1_i16; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 6924 | CallDest = Func->makeVariable(IceType_i32); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6925 | break; |
| 6926 | case IceType_v8i1: { |
| 6927 | assert(Src0->getType() == IceType_i8); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6928 | HelperID = RuntimeHelper::H_bitcast_i8_8xi1; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6929 | Variable *Src0AsI32 = Func->makeVariable(stackSlotType()); |
| 6930 | // Arguments to functions are required to be at least 32 bits wide. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 6931 | Context.insert<InstCast>(InstCast::Zext, Src0AsI32, Src0); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6932 | Src0 = Src0AsI32; |
| 6933 | } break; |
| 6934 | case IceType_v16i1: { |
| 6935 | assert(Src0->getType() == IceType_i16); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6936 | HelperID = RuntimeHelper::H_bitcast_i16_16xi1; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6937 | Variable *Src0AsI32 = Func->makeVariable(stackSlotType()); |
| 6938 | // Arguments to functions are required to be at least 32 bits wide. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 6939 | Context.insert<InstCast>(InstCast::Zext, Src0AsI32, Src0); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6940 | Src0 = Src0AsI32; |
| 6941 | } break; |
| 6942 | } |
| 6943 | } break; |
| 6944 | } |
| 6945 | constexpr SizeT MaxSrcs = 1; |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6946 | InstCall *Call = makeHelperCall(HelperID, CallDest, MaxSrcs); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6947 | Call->addArg(Src0); |
| 6948 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 6949 | Context.insert(Call); |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 6950 | // The PNaCl ABI disallows i8/i16 return types, so truncate the helper call |
| 6951 | // result to the appropriate type as necessary. |
| 6952 | if (CallDest->getType() != Dest->getType()) |
| 6953 | Context.insert<InstCast>(InstCast::Trunc, Dest, CallDest); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6954 | Cast->setDeleted(); |
| 6955 | } else if (auto *Intrinsic = llvm::dyn_cast<InstIntrinsicCall>(Instr)) { |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 6956 | CfgVector<Type> ArgTypes; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6957 | Type ReturnType = IceType_void; |
| 6958 | switch (Intrinsics::IntrinsicID ID = Intrinsic->getIntrinsicInfo().ID) { |
| 6959 | default: |
| 6960 | return; |
| 6961 | case Intrinsics::Ctpop: { |
| 6962 | Operand *Val = Intrinsic->getArg(0); |
| 6963 | Type ValTy = Val->getType(); |
| 6964 | if (ValTy == IceType_i64) |
| 6965 | ArgTypes = {IceType_i64}; |
| 6966 | else |
| 6967 | ArgTypes = {IceType_i32}; |
| 6968 | ReturnType = IceType_i32; |
| 6969 | } break; |
| 6970 | case Intrinsics::Longjmp: |
| 6971 | ArgTypes = {IceType_i32, IceType_i32}; |
| 6972 | ReturnType = IceType_void; |
| 6973 | break; |
| 6974 | case Intrinsics::Memcpy: |
| 6975 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 6976 | ReturnType = IceType_void; |
| 6977 | break; |
| 6978 | case Intrinsics::Memmove: |
| 6979 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 6980 | ReturnType = IceType_void; |
| 6981 | break; |
| 6982 | case Intrinsics::Memset: |
| 6983 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 6984 | ReturnType = IceType_void; |
| 6985 | break; |
| 6986 | case Intrinsics::NaClReadTP: |
| 6987 | ReturnType = IceType_i32; |
| 6988 | break; |
| 6989 | case Intrinsics::Setjmp: |
| 6990 | ArgTypes = {IceType_i32}; |
| 6991 | ReturnType = IceType_i32; |
| 6992 | break; |
| 6993 | } |
| 6994 | StackArgumentsSize = getCallStackArgumentsSizeBytes(ArgTypes, ReturnType); |
| 6995 | } else if (auto *Call = llvm::dyn_cast<InstCall>(Instr)) { |
| 6996 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 6997 | } else if (auto *Ret = llvm::dyn_cast<InstRet>(Instr)) { |
| 6998 | if (!Ret->hasRetValue()) |
| 6999 | return; |
| 7000 | Operand *RetValue = Ret->getRetValue(); |
| 7001 | Type ReturnType = RetValue->getType(); |
| 7002 | if (!isScalarFloatingType(ReturnType)) |
| 7003 | return; |
| 7004 | StackArgumentsSize = typeWidthInBytes(ReturnType); |
| 7005 | } else { |
| 7006 | return; |
| 7007 | } |
| 7008 | StackArgumentsSize = Traits::applyStackAlignment(StackArgumentsSize); |
| 7009 | updateMaxOutArgsSizeBytes(StackArgumentsSize); |
| 7010 | } |
| 7011 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7012 | template <typename TraitsType> |
| 7013 | uint32_t TargetX86Base<TraitsType>::getCallStackArgumentsSizeBytes( |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7014 | const CfgVector<Type> &ArgTypes, Type ReturnType) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7015 | uint32_t OutArgumentsSizeBytes = 0; |
| 7016 | uint32_t XmmArgCount = 0; |
| 7017 | uint32_t GprArgCount = 0; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7018 | for (Type Ty : ArgTypes) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7019 | // The PNaCl ABI requires the width of arguments to be at least 32 bits. |
| 7020 | assert(typeWidthInBytes(Ty) >= 4); |
| 7021 | if (isVectorType(Ty) && XmmArgCount < Traits::X86_MAX_XMM_ARGS) { |
| 7022 | ++XmmArgCount; |
Jim Stichnoth | c577727 | 2016-06-20 06:46:07 -0700 | [diff] [blame] | 7023 | } else if (isScalarFloatingType(Ty) && Traits::X86_PASS_SCALAR_FP_IN_XMM && |
| 7024 | XmmArgCount < Traits::X86_MAX_XMM_ARGS) { |
| 7025 | ++XmmArgCount; |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7026 | } else if (isScalarIntegerType(Ty) && |
| 7027 | GprArgCount < Traits::X86_MAX_GPR_ARGS) { |
| 7028 | // The 64 bit ABI allows some integers to be passed in GPRs. |
| 7029 | ++GprArgCount; |
| 7030 | } else { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7031 | if (isVectorType(Ty)) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7032 | OutArgumentsSizeBytes = |
| 7033 | Traits::applyStackAlignment(OutArgumentsSizeBytes); |
| 7034 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7035 | OutArgumentsSizeBytes += typeWidthInBytesOnStack(Ty); |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7036 | } |
| 7037 | } |
| 7038 | if (Traits::Is64Bit) |
| 7039 | return OutArgumentsSizeBytes; |
| 7040 | // The 32 bit ABI requires floating point values to be returned on the x87 FP |
| 7041 | // stack. Ensure there is enough space for the fstp/movs for floating returns. |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7042 | if (isScalarFloatingType(ReturnType)) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7043 | OutArgumentsSizeBytes = |
| 7044 | std::max(OutArgumentsSizeBytes, |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7045 | static_cast<uint32_t>(typeWidthInBytesOnStack(ReturnType))); |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7046 | } |
| 7047 | return OutArgumentsSizeBytes; |
| 7048 | } |
| 7049 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7050 | template <typename TraitsType> |
| 7051 | uint32_t TargetX86Base<TraitsType>::getCallStackArgumentsSizeBytes( |
| 7052 | const InstCall *Instr) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7053 | // Build a vector of the arguments' types. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7054 | const SizeT NumArgs = Instr->getNumArgs(); |
| 7055 | CfgVector<Type> ArgTypes; |
| 7056 | ArgTypes.reserve(NumArgs); |
| 7057 | for (SizeT i = 0; i < NumArgs; ++i) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7058 | Operand *Arg = Instr->getArg(i); |
| 7059 | ArgTypes.emplace_back(Arg->getType()); |
| 7060 | } |
| 7061 | // Compute the return type (if any); |
| 7062 | Type ReturnType = IceType_void; |
| 7063 | Variable *Dest = Instr->getDest(); |
| 7064 | if (Dest != nullptr) |
| 7065 | ReturnType = Dest->getType(); |
| 7066 | return getCallStackArgumentsSizeBytes(ArgTypes, ReturnType); |
| 7067 | } |
| 7068 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7069 | template <typename TraitsType> |
| 7070 | Variable *TargetX86Base<TraitsType>::makeZeroedRegister(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7071 | RegNumT RegNum) { |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7072 | Variable *Reg = makeReg(Ty, RegNum); |
| 7073 | switch (Ty) { |
| 7074 | case IceType_i1: |
| 7075 | case IceType_i8: |
| 7076 | case IceType_i16: |
| 7077 | case IceType_i32: |
| 7078 | case IceType_i64: |
| 7079 | // Conservatively do "mov reg, 0" to avoid modifying FLAGS. |
| 7080 | _mov(Reg, Ctx->getConstantZero(Ty)); |
| 7081 | break; |
| 7082 | case IceType_f32: |
| 7083 | case IceType_f64: |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7084 | Context.insert<InstFakeDef>(Reg); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 7085 | _xorps(Reg, Reg); |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7086 | break; |
| 7087 | default: |
| 7088 | // All vector types use the same pxor instruction. |
| 7089 | assert(isVectorType(Ty)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7090 | Context.insert<InstFakeDef>(Reg); |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7091 | _pxor(Reg, Reg); |
| 7092 | break; |
| 7093 | } |
| 7094 | return Reg; |
| 7095 | } |
| 7096 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7097 | // There is no support for loading or emitting vector constants, so the vector |
| 7098 | // values returned from makeVectorOfZeros, makeVectorOfOnes, etc. are |
| 7099 | // initialized with register operations. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7100 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7101 | // TODO(wala): Add limited support for vector constants so that complex |
| 7102 | // initialization in registers is unnecessary. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7103 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7104 | template <typename TraitsType> |
| 7105 | Variable *TargetX86Base<TraitsType>::makeVectorOfZeros(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7106 | RegNumT RegNum) { |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7107 | return makeZeroedRegister(Ty, RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7108 | } |
| 7109 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7110 | template <typename TraitsType> |
| 7111 | Variable *TargetX86Base<TraitsType>::makeVectorOfMinusOnes(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7112 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7113 | Variable *MinusOnes = makeReg(Ty, RegNum); |
| 7114 | // Insert a FakeDef so the live range of MinusOnes is not overestimated. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7115 | Context.insert<InstFakeDef>(MinusOnes); |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 7116 | if (Ty == IceType_f64) |
| 7117 | // Making a vector of minus ones of type f64 is currently only used for the |
| 7118 | // fabs intrinsic. To use the f64 type to create this mask with pcmpeqq |
| 7119 | // requires SSE 4.1. Since we're just creating a mask, pcmpeqd does the |
| 7120 | // same job and only requires SSE2. |
| 7121 | _pcmpeq(MinusOnes, MinusOnes, IceType_f32); |
| 7122 | else |
| 7123 | _pcmpeq(MinusOnes, MinusOnes); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7124 | return MinusOnes; |
| 7125 | } |
| 7126 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7127 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7128 | Variable *TargetX86Base<TraitsType>::makeVectorOfOnes(Type Ty, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7129 | Variable *Dest = makeVectorOfZeros(Ty, RegNum); |
| 7130 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 7131 | _psub(Dest, MinusOne); |
| 7132 | return Dest; |
| 7133 | } |
| 7134 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7135 | template <typename TraitsType> |
| 7136 | Variable *TargetX86Base<TraitsType>::makeVectorOfHighOrderBits(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7137 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7138 | assert(Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v8i16 || |
| 7139 | Ty == IceType_v16i8); |
| 7140 | if (Ty == IceType_v4f32 || Ty == IceType_v4i32 || Ty == IceType_v8i16) { |
| 7141 | Variable *Reg = makeVectorOfOnes(Ty, RegNum); |
| 7142 | SizeT Shift = |
| 7143 | typeWidthInBytes(typeElementType(Ty)) * Traits::X86_CHAR_BIT - 1; |
| 7144 | _psll(Reg, Ctx->getConstantInt8(Shift)); |
| 7145 | return Reg; |
| 7146 | } else { |
| 7147 | // SSE has no left shift operation for vectors of 8 bit integers. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7148 | constexpr uint32_t HIGH_ORDER_BITS_MASK = 0x80808080; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7149 | Constant *ConstantMask = Ctx->getConstantInt32(HIGH_ORDER_BITS_MASK); |
| 7150 | Variable *Reg = makeReg(Ty, RegNum); |
| 7151 | _movd(Reg, legalize(ConstantMask, Legal_Reg | Legal_Mem)); |
| 7152 | _pshufd(Reg, Reg, Ctx->getConstantZero(IceType_i8)); |
| 7153 | return Reg; |
| 7154 | } |
| 7155 | } |
| 7156 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7157 | /// Construct a mask in a register that can be and'ed with a floating-point |
| 7158 | /// value to mask off its sign bit. The value will be <4 x 0x7fffffff> for f32 |
| 7159 | /// and v4f32, and <2 x 0x7fffffffffffffff> for f64. Construct it as vector of |
| 7160 | /// ones logically right shifted one bit. |
| 7161 | // TODO(stichnot): Fix the wala |
| 7162 | // TODO: above, to represent vector constants in memory. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7163 | template <typename TraitsType> |
| 7164 | Variable *TargetX86Base<TraitsType>::makeVectorOfFabsMask(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7165 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7166 | Variable *Reg = makeVectorOfMinusOnes(Ty, RegNum); |
| 7167 | _psrl(Reg, Ctx->getConstantInt8(1)); |
| 7168 | return Reg; |
| 7169 | } |
| 7170 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7171 | template <typename TraitsType> |
| 7172 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7173 | TargetX86Base<TraitsType>::getMemoryOperandForStackSlot(Type Ty, Variable *Slot, |
| 7174 | uint32_t Offset) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7175 | // Ensure that Loc is a stack slot. |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 7176 | assert(Slot->mustNotHaveReg()); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7177 | assert(Slot->getRegNum().hasNoValue()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7178 | // Compute the location of Loc in memory. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7179 | // TODO(wala,stichnot): lea should not |
| 7180 | // be required. The address of the stack slot is known at compile time |
| 7181 | // (although not until after addProlog()). |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7182 | constexpr Type PointerType = IceType_i32; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7183 | Variable *Loc = makeReg(PointerType); |
| 7184 | _lea(Loc, Slot); |
| 7185 | Constant *ConstantOffset = Ctx->getConstantInt32(Offset); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7186 | return X86OperandMem::create(Func, Ty, Loc, ConstantOffset); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7187 | } |
| 7188 | |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 7189 | /// Lowering helper to copy a scalar integer source operand into some 8-bit GPR. |
| 7190 | /// Src is assumed to already be legalized. If the source operand is known to |
| 7191 | /// be a memory or immediate operand, a simple mov will suffice. But if the |
| 7192 | /// source operand can be a physical register, then it must first be copied into |
| 7193 | /// a physical register that is truncable to 8-bit, then truncated into a |
| 7194 | /// physical register that can receive a truncation, and finally copied into the |
| 7195 | /// result 8-bit register (which in general can be any 8-bit register). For |
| 7196 | /// example, moving %ebp into %ah may be accomplished as: |
| 7197 | /// movl %ebp, %edx |
| 7198 | /// mov_trunc %edx, %dl // this redundant assignment is ultimately elided |
| 7199 | /// movb %dl, %ah |
| 7200 | /// On the other hand, moving a memory or immediate operand into ah: |
| 7201 | /// movb 4(%ebp), %ah |
| 7202 | /// movb $my_imm, %ah |
| 7203 | /// |
| 7204 | /// Note #1. On a 64-bit target, the "movb 4(%ebp), %ah" is likely not |
| 7205 | /// encodable, so RegNum=Reg_ah should NOT be given as an argument. Instead, |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7206 | /// use RegNum=RegNumT() and then let the caller do a separate copy into |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 7207 | /// Reg_ah. |
| 7208 | /// |
| 7209 | /// Note #2. ConstantRelocatable operands are also put through this process |
| 7210 | /// (not truncated directly) because our ELF emitter does R_386_32 relocations |
| 7211 | /// but not R_386_8 relocations. |
| 7212 | /// |
| 7213 | /// Note #3. If Src is a Variable, the result will be an infinite-weight i8 |
| 7214 | /// Variable with the RCX86_IsTrunc8Rcvr register class. As such, this helper |
| 7215 | /// is a convenient way to prevent ah/bh/ch/dh from being an (invalid) argument |
| 7216 | /// to the pinsrb instruction. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7217 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7218 | Variable *TargetX86Base<TraitsType>::copyToReg8(Operand *Src, RegNumT RegNum) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 7219 | Type Ty = Src->getType(); |
| 7220 | assert(isScalarIntegerType(Ty)); |
| 7221 | assert(Ty != IceType_i1); |
| 7222 | Variable *Reg = makeReg(IceType_i8, RegNum); |
| 7223 | Reg->setRegClass(RCX86_IsTrunc8Rcvr); |
| 7224 | if (llvm::isa<Variable>(Src) || llvm::isa<ConstantRelocatable>(Src)) { |
| 7225 | Variable *SrcTruncable = makeReg(Ty); |
| 7226 | switch (Ty) { |
| 7227 | case IceType_i64: |
| 7228 | SrcTruncable->setRegClass(RCX86_Is64To8); |
| 7229 | break; |
| 7230 | case IceType_i32: |
| 7231 | SrcTruncable->setRegClass(RCX86_Is32To8); |
| 7232 | break; |
| 7233 | case IceType_i16: |
| 7234 | SrcTruncable->setRegClass(RCX86_Is16To8); |
| 7235 | break; |
| 7236 | default: |
| 7237 | // i8 - just use default register class |
| 7238 | break; |
| 7239 | } |
| 7240 | Variable *SrcRcvr = makeReg(IceType_i8); |
| 7241 | SrcRcvr->setRegClass(RCX86_IsTrunc8Rcvr); |
| 7242 | _mov(SrcTruncable, Src); |
| 7243 | _mov(SrcRcvr, SrcTruncable); |
| 7244 | Src = SrcRcvr; |
| 7245 | } |
| 7246 | _mov(Reg, Src); |
| 7247 | return Reg; |
| 7248 | } |
| 7249 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7250 | /// Helper for legalize() to emit the right code to lower an operand to a |
| 7251 | /// register of the appropriate type. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7252 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7253 | Variable *TargetX86Base<TraitsType>::copyToReg(Operand *Src, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7254 | Type Ty = Src->getType(); |
| 7255 | Variable *Reg = makeReg(Ty, RegNum); |
| 7256 | if (isVectorType(Ty)) { |
| 7257 | _movp(Reg, Src); |
| 7258 | } else { |
| 7259 | _mov(Reg, Src); |
| 7260 | } |
| 7261 | return Reg; |
| 7262 | } |
| 7263 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7264 | template <typename TraitsType> |
| 7265 | Operand *TargetX86Base<TraitsType>::legalize(Operand *From, LegalMask Allowed, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7266 | RegNumT RegNum) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7267 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7268 | const Type Ty = From->getType(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7269 | // Assert that a physical register is allowed. To date, all calls to |
| 7270 | // legalize() allow a physical register. If a physical register needs to be |
| 7271 | // explicitly disallowed, then new code will need to be written to force a |
| 7272 | // spill. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7273 | assert(Allowed & Legal_Reg); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7274 | // If we're asking for a specific physical register, make sure we're not |
| 7275 | // allowing any other operand kinds. (This could be future work, e.g. allow |
| 7276 | // the shl shift amount to be either an immediate or in ecx.) |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7277 | assert(RegNum.hasNoValue() || Allowed == Legal_Reg); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7278 | |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7279 | // Substitute with an available infinite-weight variable if possible. Only do |
| 7280 | // this when we are not asking for a specific register, and when the |
| 7281 | // substitution is not locked to a specific register, and when the types |
| 7282 | // match, in order to capture the vast majority of opportunities and avoid |
| 7283 | // corner cases in the lowering. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7284 | if (RegNum.hasNoValue()) { |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7285 | if (Variable *Subst = getContext().availabilityGet(From)) { |
| 7286 | // At this point we know there is a potential substitution available. |
| 7287 | if (Subst->mustHaveReg() && !Subst->hasReg()) { |
| 7288 | // At this point we know the substitution will have a register. |
| 7289 | if (From->getType() == Subst->getType()) { |
| 7290 | // At this point we know the substitution's register is compatible. |
| 7291 | return Subst; |
| 7292 | } |
| 7293 | } |
| 7294 | } |
| 7295 | } |
| 7296 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7297 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(From)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7298 | // Before doing anything with a Mem operand, we need to ensure that the |
| 7299 | // Base and Index components are in physical registers. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7300 | Variable *Base = Mem->getBase(); |
| 7301 | Variable *Index = Mem->getIndex(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7302 | Constant *Offset = Mem->getOffset(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7303 | Variable *RegBase = nullptr; |
| 7304 | Variable *RegIndex = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7305 | uint16_t Shift = Mem->getShift(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7306 | if (Base) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7307 | RegBase = llvm::cast<Variable>( |
| 7308 | legalize(Base, Legal_Reg | Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7309 | } |
| 7310 | if (Index) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7311 | // TODO(jpp): perhaps we should only allow Legal_Reg if |
| 7312 | // Base->isRematerializable. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7313 | RegIndex = llvm::cast<Variable>( |
| 7314 | legalize(Index, Legal_Reg | Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7315 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7316 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7317 | if (Base != RegBase || Index != RegIndex) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7318 | Mem = X86OperandMem::create(Func, Ty, RegBase, Offset, RegIndex, Shift, |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7319 | Mem->getSegmentRegister()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7320 | } |
| 7321 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7322 | // For all Memory Operands, we do randomization/pooling here. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7323 | From = randomizeOrPoolImmediate(Mem); |
| 7324 | |
| 7325 | if (!(Allowed & Legal_Mem)) { |
| 7326 | From = copyToReg(From, RegNum); |
| 7327 | } |
| 7328 | return From; |
| 7329 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7330 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7331 | if (auto *Const = llvm::dyn_cast<Constant>(From)) { |
| 7332 | if (llvm::isa<ConstantUndef>(Const)) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7333 | From = legalizeUndef(Const, RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7334 | if (isVectorType(Ty)) |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7335 | return From; |
| 7336 | Const = llvm::cast<Constant>(From); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7337 | } |
| 7338 | // There should be no constants of vector type (other than undef). |
| 7339 | assert(!isVectorType(Ty)); |
| 7340 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7341 | // If the operand is a 64 bit constant integer we need to legalize it to a |
| 7342 | // register in x86-64. |
| 7343 | if (Traits::Is64Bit) { |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 7344 | if (auto *C64 = llvm::dyn_cast<ConstantInteger64>(Const)) { |
| 7345 | if (!Utils::IsInt(32, C64->getValue())) { |
| 7346 | if (RegNum.hasValue()) { |
| 7347 | assert(Traits::getGprForType(IceType_i64, RegNum) == RegNum); |
| 7348 | } |
| 7349 | return copyToReg(Const, RegNum); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 7350 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7351 | } |
| 7352 | } |
| 7353 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7354 | // If the operand is an 32 bit constant integer, we should check whether we |
| 7355 | // need to randomize it or pool it. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 7356 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Const)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7357 | Operand *NewConst = randomizeOrPoolImmediate(C, RegNum); |
| 7358 | if (NewConst != Const) { |
| 7359 | return NewConst; |
| 7360 | } |
| 7361 | } |
| 7362 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7363 | if (auto *CR = llvm::dyn_cast<ConstantRelocatable>(Const)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7364 | // If the operand is a ConstantRelocatable, and Legal_AddrAbs is not |
| 7365 | // specified, and UseNonsfi is indicated, we need to add RebasePtr. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7366 | if (UseNonsfi && !(Allowed & Legal_AddrAbs)) { |
| 7367 | assert(Ty == IceType_i32); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7368 | Variable *NewVar = makeReg(Ty, RegNum); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7369 | auto *Mem = Traits::X86OperandMem::create(Func, Ty, nullptr, CR); |
| 7370 | // LEAs are not automatically sandboxed, thus we explicitly invoke |
| 7371 | // _sandbox_mem_reference. |
| 7372 | _lea(NewVar, _sandbox_mem_reference(Mem)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7373 | From = NewVar; |
| 7374 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7375 | } else if (isScalarFloatingType(Ty)) { |
| 7376 | // Convert a scalar floating point constant into an explicit memory |
| 7377 | // operand. |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7378 | if (auto *ConstFloat = llvm::dyn_cast<ConstantFloat>(Const)) { |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 7379 | if (Utils::isPositiveZero(ConstFloat->getValue())) |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7380 | return makeZeroedRegister(Ty, RegNum); |
| 7381 | } else if (auto *ConstDouble = llvm::dyn_cast<ConstantDouble>(Const)) { |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 7382 | if (Utils::isPositiveZero(ConstDouble->getValue())) |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7383 | return makeZeroedRegister(Ty, RegNum); |
| 7384 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7385 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7386 | auto *CFrom = llvm::cast<Constant>(From); |
| 7387 | assert(CFrom->getShouldBePooled()); |
| 7388 | Constant *Offset = Ctx->getConstantSym(0, CFrom->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7389 | auto *Mem = X86OperandMem::create(Func, Ty, nullptr, Offset); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7390 | From = Mem; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7391 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7392 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7393 | bool NeedsReg = false; |
| 7394 | if (!(Allowed & Legal_Imm) && !isScalarFloatingType(Ty)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7395 | // Immediate specifically not allowed. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7396 | NeedsReg = true; |
| 7397 | if (!(Allowed & Legal_Mem) && isScalarFloatingType(Ty)) |
| 7398 | // On x86, FP constants are lowered to mem operands. |
| 7399 | NeedsReg = true; |
| 7400 | if (NeedsReg) { |
| 7401 | From = copyToReg(From, RegNum); |
| 7402 | } |
| 7403 | return From; |
| 7404 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7405 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7406 | if (auto *Var = llvm::dyn_cast<Variable>(From)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7407 | // Check if the variable is guaranteed a physical register. This can happen |
| 7408 | // either when the variable is pre-colored or when it is assigned infinite |
| 7409 | // weight. |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 7410 | bool MustHaveRegister = (Var->hasReg() || Var->mustHaveReg()); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7411 | bool MustRematerialize = |
| 7412 | (Var->isRematerializable() && !(Allowed & Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7413 | // We need a new physical register for the operand if: |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7414 | // - Mem is not allowed and Var isn't guaranteed a physical register, or |
| 7415 | // - RegNum is required and Var->getRegNum() doesn't match, or |
| 7416 | // - Var is a rematerializable variable and rematerializable pass-through is |
| 7417 | // not allowed (in which case we need an lea instruction). |
| 7418 | if (MustRematerialize) { |
| 7419 | assert(Ty == IceType_i32); |
| 7420 | Variable *NewVar = makeReg(Ty, RegNum); |
| 7421 | // Since Var is rematerializable, the offset will be added when the lea is |
| 7422 | // emitted. |
| 7423 | constexpr Constant *NoOffset = nullptr; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7424 | auto *Mem = X86OperandMem::create(Func, Ty, Var, NoOffset); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7425 | _lea(NewVar, Mem); |
| 7426 | From = NewVar; |
| 7427 | } else if ((!(Allowed & Legal_Mem) && !MustHaveRegister) || |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7428 | (RegNum.hasValue() && RegNum != Var->getRegNum())) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7429 | From = copyToReg(From, RegNum); |
| 7430 | } |
| 7431 | return From; |
| 7432 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7433 | |
| 7434 | llvm::report_fatal_error("Unhandled operand kind in legalize()"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7435 | return From; |
| 7436 | } |
| 7437 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7438 | /// Provide a trivial wrapper to legalize() for this common usage. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7439 | template <typename TraitsType> |
| 7440 | Variable *TargetX86Base<TraitsType>::legalizeToReg(Operand *From, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7441 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7442 | return llvm::cast<Variable>(legalize(From, Legal_Reg, RegNum)); |
| 7443 | } |
| 7444 | |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7445 | /// Legalize undef values to concrete values. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7446 | template <typename TraitsType> |
| 7447 | Operand *TargetX86Base<TraitsType>::legalizeUndef(Operand *From, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7448 | RegNumT RegNum) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7449 | Type Ty = From->getType(); |
| 7450 | if (llvm::isa<ConstantUndef>(From)) { |
| 7451 | // Lower undefs to zero. Another option is to lower undefs to an |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7452 | // uninitialized register; however, using an uninitialized register results |
| 7453 | // in less predictable code. |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7454 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7455 | // If in the future the implementation is changed to lower undef values to |
| 7456 | // uninitialized registers, a FakeDef will be needed: |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7457 | // Context.insert<InstFakeDef>(Reg); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7458 | // This is in order to ensure that the live range of Reg is not |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7459 | // overestimated. If the constant being lowered is a 64 bit value, then |
| 7460 | // the result should be split and the lo and hi components will need to go |
| 7461 | // in uninitialized registers. |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7462 | if (isVectorType(Ty)) |
| 7463 | return makeVectorOfZeros(Ty, RegNum); |
| 7464 | return Ctx->getConstantZero(Ty); |
| 7465 | } |
| 7466 | return From; |
| 7467 | } |
| 7468 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7469 | /// For the cmp instruction, if Src1 is an immediate, or known to be a physical |
| 7470 | /// register, we can allow Src0 to be a memory operand. Otherwise, Src0 must be |
| 7471 | /// copied into a physical register. (Actually, either Src0 or Src1 can be |
| 7472 | /// chosen for the physical register, but unfortunately we have to commit to one |
| 7473 | /// or the other before register allocation.) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7474 | template <typename TraitsType> |
| 7475 | Operand *TargetX86Base<TraitsType>::legalizeSrc0ForCmp(Operand *Src0, |
| 7476 | Operand *Src1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7477 | bool IsSrc1ImmOrReg = false; |
| 7478 | if (llvm::isa<Constant>(Src1)) { |
| 7479 | IsSrc1ImmOrReg = true; |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7480 | } else if (auto *Var = llvm::dyn_cast<Variable>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7481 | if (Var->hasReg()) |
| 7482 | IsSrc1ImmOrReg = true; |
| 7483 | } |
| 7484 | return legalize(Src0, IsSrc1ImmOrReg ? (Legal_Reg | Legal_Mem) : Legal_Reg); |
| 7485 | } |
| 7486 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7487 | template <typename TraitsType> |
| 7488 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7489 | TargetX86Base<TraitsType>::formMemoryOperand(Operand *Opnd, Type Ty, |
| 7490 | bool DoLegalize) { |
| 7491 | auto *Mem = llvm::dyn_cast<X86OperandMem>(Opnd); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 7492 | // It may be the case that address mode optimization already creates an |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7493 | // X86OperandMem, so in that case it wouldn't need another level of |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 7494 | // transformation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7495 | if (!Mem) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 7496 | auto *Base = llvm::dyn_cast<Variable>(Opnd); |
| 7497 | auto *Offset = llvm::dyn_cast<Constant>(Opnd); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7498 | assert(Base || Offset); |
| 7499 | if (Offset) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7500 | // During memory operand building, we do not blind or pool the constant |
| 7501 | // offset, we will work on the whole memory operand later as one entity |
| 7502 | // later, this save one instruction. By turning blinding and pooling off, |
| 7503 | // we guarantee legalize(Offset) will return a Constant*. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7504 | if (!llvm::isa<ConstantRelocatable>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7505 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
| 7506 | |
| 7507 | Offset = llvm::cast<Constant>(legalize(Offset)); |
| 7508 | } |
| 7509 | |
| 7510 | assert(llvm::isa<ConstantInteger32>(Offset) || |
| 7511 | llvm::isa<ConstantRelocatable>(Offset)); |
| 7512 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7513 | // Not completely sure whether it's OK to leave IsRebased unset when |
| 7514 | // creating the mem operand. If DoLegalize is true, it will definitely be |
| 7515 | // applied during the legalize() call, but perhaps not during the |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7516 | // randomizeOrPoolImmediate() call. In any case, the emit routines will |
| 7517 | // assert that PIC legalization has been applied. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7518 | Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7519 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7520 | // Do legalization, which contains randomization/pooling or do |
| 7521 | // randomization/pooling. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7522 | return llvm::cast<X86OperandMem>(DoLegalize ? legalize(Mem) |
| 7523 | : randomizeOrPoolImmediate(Mem)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7524 | } |
| 7525 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7526 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7527 | Variable *TargetX86Base<TraitsType>::makeReg(Type Type, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7528 | // There aren't any 64-bit integer registers for x86-32. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7529 | assert(Traits::Is64Bit || Type != IceType_i64); |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 7530 | Variable *Reg = Func->makeVariable(Type); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7531 | if (RegNum.hasValue()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7532 | Reg->setRegNum(RegNum); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7533 | else |
| 7534 | Reg->setMustHaveReg(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7535 | return Reg; |
| 7536 | } |
| 7537 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7538 | template <typename TraitsType> |
| 7539 | const Type TargetX86Base<TraitsType>::TypeForSize[] = { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 7540 | IceType_i8, IceType_i16, IceType_i32, IceType_f64, IceType_v16i8}; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7541 | template <typename TraitsType> |
| 7542 | Type TargetX86Base<TraitsType>::largestTypeInSize(uint32_t Size, |
| 7543 | uint32_t MaxSize) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 7544 | assert(Size != 0); |
| 7545 | uint32_t TyIndex = llvm::findLastSet(Size, llvm::ZB_Undefined); |
| 7546 | uint32_t MaxIndex = MaxSize == NoSizeLimit |
| 7547 | ? llvm::array_lengthof(TypeForSize) - 1 |
| 7548 | : llvm::findLastSet(MaxSize, llvm::ZB_Undefined); |
| 7549 | return TypeForSize[std::min(TyIndex, MaxIndex)]; |
| 7550 | } |
| 7551 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7552 | template <typename TraitsType> |
| 7553 | Type TargetX86Base<TraitsType>::firstTypeThatFitsSize(uint32_t Size, |
| 7554 | uint32_t MaxSize) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 7555 | assert(Size != 0); |
| 7556 | uint32_t TyIndex = llvm::findLastSet(Size, llvm::ZB_Undefined); |
| 7557 | if (!llvm::isPowerOf2_32(Size)) |
| 7558 | ++TyIndex; |
| 7559 | uint32_t MaxIndex = MaxSize == NoSizeLimit |
| 7560 | ? llvm::array_lengthof(TypeForSize) - 1 |
| 7561 | : llvm::findLastSet(MaxSize, llvm::ZB_Undefined); |
| 7562 | return TypeForSize[std::min(TyIndex, MaxIndex)]; |
| 7563 | } |
| 7564 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7565 | template <typename TraitsType> void TargetX86Base<TraitsType>::postLower() { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 7566 | if (Func->getOptLevel() == Opt_m1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7567 | return; |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 7568 | markRedefinitions(); |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7569 | Context.availabilityUpdate(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7570 | } |
| 7571 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7572 | template <typename TraitsType> |
| 7573 | void TargetX86Base<TraitsType>::makeRandomRegisterPermutation( |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7574 | llvm::SmallVectorImpl<RegNumT> &Permutation, |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7575 | const SmallBitVector &ExcludeRegisters, uint64_t Salt) const { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7576 | Traits::makeRandomRegisterPermutation(Func, Permutation, ExcludeRegisters, |
| 7577 | Salt); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7578 | } |
| 7579 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7580 | template <typename TraitsType> |
| 7581 | void TargetX86Base<TraitsType>::emit(const ConstantInteger32 *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7582 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7583 | return; |
| 7584 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7585 | Str << "$" << C->getValue(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7586 | } |
| 7587 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7588 | template <typename TraitsType> |
| 7589 | void TargetX86Base<TraitsType>::emit(const ConstantInteger64 *C) const { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7590 | if (!Traits::Is64Bit) { |
| 7591 | llvm::report_fatal_error("Not expecting to emit 64-bit integers"); |
| 7592 | } else { |
| 7593 | if (!BuildDefs::dump()) |
| 7594 | return; |
| 7595 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7596 | Str << "$" << C->getValue(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7597 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7598 | } |
| 7599 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7600 | template <typename TraitsType> |
| 7601 | void TargetX86Base<TraitsType>::emit(const ConstantFloat *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7602 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7603 | return; |
| 7604 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7605 | Str << C->getLabelName(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7606 | } |
| 7607 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7608 | template <typename TraitsType> |
| 7609 | void TargetX86Base<TraitsType>::emit(const ConstantDouble *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7610 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7611 | return; |
| 7612 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7613 | Str << C->getLabelName(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7614 | } |
| 7615 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7616 | template <typename TraitsType> |
| 7617 | void TargetX86Base<TraitsType>::emit(const ConstantUndef *) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7618 | llvm::report_fatal_error("undef value encountered by emitter."); |
| 7619 | } |
| 7620 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7621 | template <class Machine> |
| 7622 | void TargetX86Base<Machine>::emit(const ConstantRelocatable *C) const { |
| 7623 | if (!BuildDefs::dump()) |
| 7624 | return; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7625 | assert(!getFlags().getUseNonsfi() || |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7626 | C->getName().toString() == GlobalOffsetTable); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7627 | Ostream &Str = Ctx->getStrEmit(); |
| 7628 | Str << "$"; |
| 7629 | emitWithoutPrefix(C); |
| 7630 | } |
| 7631 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7632 | /// Randomize or pool an Immediate. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7633 | template <typename TraitsType> |
| 7634 | Operand * |
| 7635 | TargetX86Base<TraitsType>::randomizeOrPoolImmediate(Constant *Immediate, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7636 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7637 | assert(llvm::isa<ConstantInteger32>(Immediate) || |
| 7638 | llvm::isa<ConstantRelocatable>(Immediate)); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7639 | if (getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7640 | RandomizationPoolingPaused == true) { |
| 7641 | // Immediates randomization/pooling off or paused |
| 7642 | return Immediate; |
| 7643 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7644 | |
| 7645 | if (Traits::Is64Bit && NeedSandboxing) { |
| 7646 | // Immediate randomization/pooling is currently disabled for x86-64 |
| 7647 | // sandboxing for it could generate invalid memory operands. |
| 7648 | assert(false && |
| 7649 | "Constant pooling/randomization is disabled for x8664 sandbox."); |
| 7650 | return Immediate; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7651 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7652 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7653 | if (!Immediate->shouldBeRandomizedOrPooled()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7654 | // the constant Immediate is not eligible for blinding/pooling |
| 7655 | return Immediate; |
| 7656 | } |
| 7657 | Ctx->statsUpdateRPImms(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7658 | switch (getFlags().getRandomizeAndPoolImmediatesOption()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7659 | default: |
| 7660 | llvm::report_fatal_error("Unsupported -randomize-pool-immediates option"); |
| 7661 | case RPI_Randomize: { |
| 7662 | // blind the constant |
| 7663 | // FROM: |
| 7664 | // imm |
| 7665 | // TO: |
| 7666 | // insert: mov imm+cookie, Reg |
| 7667 | // insert: lea -cookie[Reg], Reg |
| 7668 | // => Reg |
| 7669 | // If we have already assigned a phy register, we must come from |
| 7670 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse the |
| 7671 | // assigned register as this assignment is that start of its use-def |
| 7672 | // chain. So we add RegNum argument here. Note we use 'lea' instruction |
| 7673 | // instead of 'xor' to avoid affecting the flags. |
| 7674 | Variable *Reg = makeReg(IceType_i32, RegNum); |
| 7675 | auto *Integer = llvm::cast<ConstantInteger32>(Immediate); |
| 7676 | uint32_t Value = Integer->getValue(); |
| 7677 | uint32_t Cookie = Func->getConstantBlindingCookie(); |
| 7678 | _mov(Reg, Ctx->getConstantInt(IceType_i32, Cookie + Value)); |
| 7679 | Constant *Offset = Ctx->getConstantInt(IceType_i32, 0 - Cookie); |
| 7680 | _lea(Reg, X86OperandMem::create(Func, IceType_i32, Reg, Offset)); |
| 7681 | if (Immediate->getType() == IceType_i32) { |
| 7682 | return Reg; |
| 7683 | } |
| 7684 | Variable *TruncReg = makeReg(Immediate->getType(), RegNum); |
| 7685 | _mov(TruncReg, Reg); |
| 7686 | return TruncReg; |
| 7687 | } |
| 7688 | case RPI_Pool: { |
| 7689 | // pool the constant |
| 7690 | // FROM: |
| 7691 | // imm |
| 7692 | // TO: |
| 7693 | // insert: mov $label, Reg |
| 7694 | // => Reg |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7695 | assert(getFlags().getRandomizeAndPoolImmediatesOption() == RPI_Pool); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7696 | assert(Immediate->getShouldBePooled()); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7697 | // if we have already assigned a phy register, we must come from |
| 7698 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse the |
| 7699 | // assigned register as this assignment is that start of its use-def |
| 7700 | // chain. So we add RegNum argument here. |
| 7701 | Variable *Reg = makeReg(Immediate->getType(), RegNum); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7702 | constexpr RelocOffsetT Offset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7703 | Constant *Symbol = Ctx->getConstantSym(Offset, Immediate->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7704 | constexpr Variable *NoBase = nullptr; |
| 7705 | X86OperandMem *MemOperand = |
| 7706 | X86OperandMem::create(Func, Immediate->getType(), NoBase, Symbol); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7707 | _mov(Reg, MemOperand); |
| 7708 | return Reg; |
| 7709 | } |
| 7710 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7711 | } |
| 7712 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7713 | template <typename TraitsType> |
| 7714 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7715 | TargetX86Base<TraitsType>::randomizeOrPoolImmediate(X86OperandMem *MemOperand, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7716 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7717 | assert(MemOperand); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7718 | if (getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7719 | RandomizationPoolingPaused == true) { |
| 7720 | // immediates randomization/pooling is turned off |
| 7721 | return MemOperand; |
| 7722 | } |
| 7723 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7724 | if (Traits::Is64Bit && NeedSandboxing) { |
| 7725 | // Immediate randomization/pooling is currently disabled for x86-64 |
| 7726 | // sandboxing for it could generate invalid memory operands. |
| 7727 | assert(false && |
| 7728 | "Constant pooling/randomization is disabled for x8664 sandbox."); |
| 7729 | return MemOperand; |
| 7730 | } |
| 7731 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7732 | // If this memory operand is already a randomized one, we do not randomize it |
| 7733 | // again. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7734 | if (MemOperand->getRandomized()) |
| 7735 | return MemOperand; |
| 7736 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7737 | auto *C = llvm::dyn_cast_or_null<Constant>(MemOperand->getOffset()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7738 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7739 | if (C == nullptr) { |
| 7740 | return MemOperand; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7741 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7742 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7743 | if (!C->shouldBeRandomizedOrPooled()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7744 | return MemOperand; |
| 7745 | } |
| 7746 | |
| 7747 | // The offset of this mem operand should be blinded or pooled |
| 7748 | Ctx->statsUpdateRPImms(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7749 | switch (getFlags().getRandomizeAndPoolImmediatesOption()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7750 | default: |
| 7751 | llvm::report_fatal_error("Unsupported -randomize-pool-immediates option"); |
| 7752 | case RPI_Randomize: { |
| 7753 | // blind the constant offset |
| 7754 | // FROM: |
| 7755 | // offset[base, index, shift] |
| 7756 | // TO: |
| 7757 | // insert: lea offset+cookie[base], RegTemp |
| 7758 | // => -cookie[RegTemp, index, shift] |
| 7759 | uint32_t Value = |
| 7760 | llvm::dyn_cast<ConstantInteger32>(MemOperand->getOffset())->getValue(); |
| 7761 | uint32_t Cookie = Func->getConstantBlindingCookie(); |
| 7762 | Constant *Mask1 = |
| 7763 | Ctx->getConstantInt(MemOperand->getOffset()->getType(), Cookie + Value); |
| 7764 | Constant *Mask2 = |
| 7765 | Ctx->getConstantInt(MemOperand->getOffset()->getType(), 0 - Cookie); |
| 7766 | |
| 7767 | X86OperandMem *TempMemOperand = X86OperandMem::create( |
| 7768 | Func, MemOperand->getType(), MemOperand->getBase(), Mask1); |
| 7769 | // If we have already assigned a physical register, we must come from |
| 7770 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse |
| 7771 | // the assigned register as this assignment is that start of its |
| 7772 | // use-def chain. So we add RegNum argument here. |
| 7773 | Variable *RegTemp = makeReg(MemOperand->getOffset()->getType(), RegNum); |
| 7774 | _lea(RegTemp, TempMemOperand); |
| 7775 | |
| 7776 | X86OperandMem *NewMemOperand = X86OperandMem::create( |
| 7777 | Func, MemOperand->getType(), RegTemp, Mask2, MemOperand->getIndex(), |
| 7778 | MemOperand->getShift(), MemOperand->getSegmentRegister(), |
| 7779 | MemOperand->getIsRebased()); |
| 7780 | |
| 7781 | // Label this memory operand as randomized, so we won't randomize it |
| 7782 | // again in case we call legalize() multiple times on this memory |
| 7783 | // operand. |
| 7784 | NewMemOperand->setRandomized(true); |
| 7785 | return NewMemOperand; |
| 7786 | } |
| 7787 | case RPI_Pool: { |
| 7788 | // pool the constant offset |
| 7789 | // FROM: |
| 7790 | // offset[base, index, shift] |
| 7791 | // TO: |
| 7792 | // insert: mov $label, RegTemp |
| 7793 | // insert: lea [base, RegTemp], RegTemp |
| 7794 | // =>[RegTemp, index, shift] |
| 7795 | |
| 7796 | // Memory operand should never exist as source operands in phi lowering |
| 7797 | // assignments, so there is no need to reuse any registers here. For |
| 7798 | // phi lowering, we should not ask for new physical registers in |
| 7799 | // general. However, if we do meet Memory Operand during phi lowering, |
| 7800 | // we should not blind or pool the immediates for now. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7801 | if (RegNum.hasValue()) |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7802 | return MemOperand; |
| 7803 | Variable *RegTemp = makeReg(IceType_i32); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7804 | assert(MemOperand->getOffset()->getShouldBePooled()); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7805 | constexpr RelocOffsetT SymOffset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7806 | Constant *Symbol = |
| 7807 | Ctx->getConstantSym(SymOffset, MemOperand->getOffset()->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7808 | constexpr Variable *NoBase = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7809 | X86OperandMem *SymbolOperand = X86OperandMem::create( |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7810 | Func, MemOperand->getOffset()->getType(), NoBase, Symbol); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7811 | _mov(RegTemp, SymbolOperand); |
| 7812 | // If we have a base variable here, we should add the lea instruction |
| 7813 | // to add the value of the base variable to RegTemp. If there is no |
| 7814 | // base variable, we won't need this lea instruction. |
| 7815 | if (MemOperand->getBase()) { |
| 7816 | X86OperandMem *CalculateOperand = X86OperandMem::create( |
| 7817 | Func, MemOperand->getType(), MemOperand->getBase(), nullptr, RegTemp, |
| 7818 | 0, MemOperand->getSegmentRegister()); |
| 7819 | _lea(RegTemp, CalculateOperand); |
| 7820 | } |
| 7821 | X86OperandMem *NewMemOperand = X86OperandMem::create( |
| 7822 | Func, MemOperand->getType(), RegTemp, nullptr, MemOperand->getIndex(), |
| 7823 | MemOperand->getShift(), MemOperand->getSegmentRegister()); |
| 7824 | return NewMemOperand; |
| 7825 | } |
| 7826 | } |
| 7827 | } |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7828 | |
| 7829 | template <typename TraitsType> |
| 7830 | void TargetX86Base<TraitsType>::emitJumpTable( |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 7831 | const Cfg *, const InstJumpTable *JumpTable) const { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7832 | if (!BuildDefs::dump()) |
| 7833 | return; |
| 7834 | Ostream &Str = Ctx->getStrEmit(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7835 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7836 | const char *Prefix = UseNonsfi ? ".data.rel.ro." : ".rodata."; |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 7837 | Str << "\t.section\t" << Prefix << JumpTable->getSectionName() |
| 7838 | << ",\"a\",@progbits\n" |
| 7839 | "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n" |
| 7840 | << JumpTable->getName() << ":"; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7841 | |
| 7842 | // On X86 ILP32 pointers are 32-bit hence the use of .long |
| 7843 | for (SizeT I = 0; I < JumpTable->getNumTargets(); ++I) |
| 7844 | Str << "\n\t.long\t" << JumpTable->getTarget(I)->getAsmName(); |
| 7845 | Str << "\n"; |
| 7846 | } |
| 7847 | |
| 7848 | template <typename TraitsType> |
| 7849 | template <typename T> |
| 7850 | void TargetDataX86<TraitsType>::emitConstantPool(GlobalContext *Ctx) { |
| 7851 | if (!BuildDefs::dump()) |
| 7852 | return; |
| 7853 | Ostream &Str = Ctx->getStrEmit(); |
| 7854 | Type Ty = T::Ty; |
| 7855 | SizeT Align = typeAlignInBytes(Ty); |
| 7856 | ConstantList Pool = Ctx->getConstantPool(Ty); |
| 7857 | |
| 7858 | Str << "\t.section\t.rodata.cst" << Align << ",\"aM\",@progbits," << Align |
| 7859 | << "\n"; |
| 7860 | Str << "\t.align\t" << Align << "\n"; |
| 7861 | |
| 7862 | // If reorder-pooled-constants option is set to true, we need to shuffle the |
| 7863 | // constant pool before emitting it. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7864 | if (getFlags().getReorderPooledConstants() && !Pool.empty()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7865 | // Use the constant's kind value as the salt for creating random number |
| 7866 | // generator. |
| 7867 | Operand::OperandKind K = (*Pool.begin())->getKind(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7868 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7869 | RPE_PooledConstantReordering, K); |
| 7870 | RandomShuffle(Pool.begin(), Pool.end(), |
| 7871 | [&RNG](uint64_t N) { return (uint32_t)RNG.next(N); }); |
| 7872 | } |
| 7873 | |
| 7874 | for (Constant *C : Pool) { |
| 7875 | if (!C->getShouldBePooled()) |
| 7876 | continue; |
| 7877 | auto *Const = llvm::cast<typename T::IceType>(C); |
| 7878 | typename T::IceType::PrimType Value = Const->getValue(); |
| 7879 | // Use memcpy() to copy bits from Value into RawValue in a way that avoids |
| 7880 | // breaking strict-aliasing rules. |
| 7881 | typename T::PrimitiveIntType RawValue; |
| 7882 | memcpy(&RawValue, &Value, sizeof(Value)); |
| 7883 | char buf[30]; |
| 7884 | int CharsPrinted = |
| 7885 | snprintf(buf, llvm::array_lengthof(buf), T::PrintfString, RawValue); |
| 7886 | assert(CharsPrinted >= 0); |
| 7887 | assert((size_t)CharsPrinted < llvm::array_lengthof(buf)); |
| 7888 | (void)CharsPrinted; // avoid warnings if asserts are disabled |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7889 | Str << Const->getLabelName(); |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7890 | Str << ":\n\t" << T::AsmTag << "\t" << buf << "\t/* " << T::TypeName << " " |
| 7891 | << Value << " */\n"; |
| 7892 | } |
| 7893 | } |
| 7894 | |
| 7895 | template <typename TraitsType> |
| 7896 | void TargetDataX86<TraitsType>::lowerConstants() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7897 | if (getFlags().getDisableTranslation()) |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7898 | return; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7899 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7900 | case FT_Elf: { |
| 7901 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 7902 | |
| 7903 | Writer->writeConstantPool<ConstantInteger32>(IceType_i8); |
| 7904 | Writer->writeConstantPool<ConstantInteger32>(IceType_i16); |
| 7905 | Writer->writeConstantPool<ConstantInteger32>(IceType_i32); |
| 7906 | |
| 7907 | Writer->writeConstantPool<ConstantFloat>(IceType_f32); |
| 7908 | Writer->writeConstantPool<ConstantDouble>(IceType_f64); |
| 7909 | } break; |
| 7910 | case FT_Asm: |
| 7911 | case FT_Iasm: { |
| 7912 | OstreamLocker L(Ctx); |
| 7913 | |
| 7914 | emitConstantPool<PoolTypeConverter<uint8_t>>(Ctx); |
| 7915 | emitConstantPool<PoolTypeConverter<uint16_t>>(Ctx); |
| 7916 | emitConstantPool<PoolTypeConverter<uint32_t>>(Ctx); |
| 7917 | |
| 7918 | emitConstantPool<PoolTypeConverter<float>>(Ctx); |
| 7919 | emitConstantPool<PoolTypeConverter<double>>(Ctx); |
| 7920 | } break; |
| 7921 | } |
| 7922 | } |
| 7923 | |
| 7924 | template <typename TraitsType> |
| 7925 | void TargetDataX86<TraitsType>::lowerJumpTables() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7926 | const bool IsPIC = getFlags().getUseNonsfi(); |
| 7927 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7928 | case FT_Elf: { |
| 7929 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 7930 | for (const JumpTableData &JT : Ctx->getJumpTables()) |
| 7931 | Writer->writeJumpTable(JT, Traits::FK_Abs, IsPIC); |
| 7932 | } break; |
| 7933 | case FT_Asm: |
| 7934 | // Already emitted from Cfg |
| 7935 | break; |
| 7936 | case FT_Iasm: { |
| 7937 | if (!BuildDefs::dump()) |
| 7938 | return; |
| 7939 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7940 | const char *Prefix = IsPIC ? ".data.rel.ro." : ".rodata."; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7941 | for (const JumpTableData &JT : Ctx->getJumpTables()) { |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 7942 | Str << "\t.section\t" << Prefix << JT.getSectionName() |
| 7943 | << ",\"a\",@progbits\n" |
| 7944 | "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n" |
| 7945 | << JT.getName().toString() << ":"; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7946 | |
| 7947 | // On X8664 ILP32 pointers are 32-bit hence the use of .long |
| 7948 | for (intptr_t TargetOffset : JT.getTargetOffsets()) |
| 7949 | Str << "\n\t.long\t" << JT.getFunctionName() << "+" << TargetOffset; |
| 7950 | Str << "\n"; |
| 7951 | } |
| 7952 | } break; |
| 7953 | } |
| 7954 | } |
| 7955 | |
| 7956 | template <typename TraitsType> |
| 7957 | void TargetDataX86<TraitsType>::lowerGlobals( |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7958 | const VariableDeclarationList &Vars, const std::string &SectionSuffix) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7959 | const bool IsPIC = getFlags().getUseNonsfi(); |
| 7960 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7961 | case FT_Elf: { |
| 7962 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 7963 | Writer->writeDataSection(Vars, Traits::FK_Abs, SectionSuffix, IsPIC); |
| 7964 | } break; |
| 7965 | case FT_Asm: |
| 7966 | case FT_Iasm: { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7967 | OstreamLocker L(Ctx); |
| 7968 | for (const VariableDeclaration *Var : Vars) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 7969 | if (getFlags().matchTranslateOnly(Var->getName(), 0)) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7970 | emitGlobal(*Var, SectionSuffix); |
| 7971 | } |
| 7972 | } |
| 7973 | } break; |
| 7974 | } |
| 7975 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7976 | } // end of namespace X86NAMESPACE |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7977 | } // end of namespace Ice |
| 7978 | |
| 7979 | #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |