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" |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 26 | #include "IceInstX86Base.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 27 | #include "IceLiveness.h" |
| 28 | #include "IceOperand.h" |
Jan Voung | 5348369 | 2015-07-16 10:47:46 -0700 | [diff] [blame] | 29 | #include "IcePhiLoweringImpl.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 30 | #include "IceUtils.h" |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 31 | #include "IceVariableSplitting.h" |
| 32 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 34 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 35 | #include <stack> |
| 36 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 37 | namespace Ice { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 38 | namespace X86 { |
| 39 | template <typename T> struct PoolTypeConverter {}; |
| 40 | |
| 41 | template <> struct PoolTypeConverter<float> { |
| 42 | using PrimitiveIntType = uint32_t; |
| 43 | using IceType = ConstantFloat; |
| 44 | static const Type Ty = IceType_f32; |
| 45 | static const char *TypeName; |
| 46 | static const char *AsmTag; |
| 47 | static const char *PrintfString; |
| 48 | }; |
| 49 | |
| 50 | template <> struct PoolTypeConverter<double> { |
| 51 | using PrimitiveIntType = uint64_t; |
| 52 | using IceType = ConstantDouble; |
| 53 | static const Type Ty = IceType_f64; |
| 54 | static const char *TypeName; |
| 55 | static const char *AsmTag; |
| 56 | static const char *PrintfString; |
| 57 | }; |
| 58 | |
| 59 | // Add converter for int type constant pooling |
| 60 | template <> struct PoolTypeConverter<uint32_t> { |
| 61 | using PrimitiveIntType = uint32_t; |
| 62 | using IceType = ConstantInteger32; |
| 63 | static const Type Ty = IceType_i32; |
| 64 | static const char *TypeName; |
| 65 | static const char *AsmTag; |
| 66 | static const char *PrintfString; |
| 67 | }; |
| 68 | |
| 69 | // Add converter for int type constant pooling |
| 70 | template <> struct PoolTypeConverter<uint16_t> { |
| 71 | using PrimitiveIntType = uint32_t; |
| 72 | using IceType = ConstantInteger32; |
| 73 | static const Type Ty = IceType_i16; |
| 74 | static const char *TypeName; |
| 75 | static const char *AsmTag; |
| 76 | static const char *PrintfString; |
| 77 | }; |
| 78 | |
| 79 | // Add converter for int type constant pooling |
| 80 | template <> struct PoolTypeConverter<uint8_t> { |
| 81 | using PrimitiveIntType = uint32_t; |
| 82 | using IceType = ConstantInteger32; |
| 83 | static const Type Ty = IceType_i8; |
| 84 | static const char *TypeName; |
| 85 | static const char *AsmTag; |
| 86 | static const char *PrintfString; |
| 87 | }; |
| 88 | } // end of namespace X86 |
| 89 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 90 | namespace X86NAMESPACE { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 91 | |
Eric Holk | d6cf6b3 | 2016-02-17 11:09:48 -0800 | [diff] [blame] | 92 | using Utils::BoolFlagSaver; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 93 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 94 | template <typename Traits> class BoolFoldingEntry { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 95 | BoolFoldingEntry(const BoolFoldingEntry &) = delete; |
| 96 | |
| 97 | public: |
| 98 | BoolFoldingEntry() = default; |
| 99 | explicit BoolFoldingEntry(Inst *I); |
| 100 | BoolFoldingEntry &operator=(const BoolFoldingEntry &) = default; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 101 | /// Instr is the instruction producing the i1-type variable of interest. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 102 | Inst *Instr = nullptr; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 103 | /// IsComplex is the cached result of BoolFolding::hasComplexLowering(Instr). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 104 | bool IsComplex = false; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 105 | /// IsLiveOut is initialized conservatively to true, and is set to false when |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 106 | /// we encounter an instruction that ends Var's live range. We disable the |
| 107 | /// folding optimization when Var is live beyond this basic block. Note that |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 108 | /// if liveness analysis is not performed (e.g. in Om1 mode), IsLiveOut will |
| 109 | /// always be true and the folding optimization will never be performed. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 110 | bool IsLiveOut = true; |
| 111 | // 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] | 112 | // 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] | 113 | // then the folding optimization is disabled for Var. |
| 114 | uint32_t NumUses = 0; |
| 115 | }; |
| 116 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 117 | template <typename Traits> class BoolFolding { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 118 | public: |
| 119 | enum BoolFoldingProducerKind { |
| 120 | PK_None, |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 121 | // TODO(jpp): PK_Icmp32 is no longer meaningful. Rename to PK_IcmpNative. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 122 | PK_Icmp32, |
| 123 | PK_Icmp64, |
| 124 | PK_Fcmp, |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 125 | PK_Trunc, |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 126 | PK_Arith // A flag-setting arithmetic instruction. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 129 | /// 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] | 130 | /// go ahead and produce them anyway for symmetry with the |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 131 | /// BoolFoldingProducerKind. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 132 | enum BoolFoldingConsumerKind { CK_None, CK_Br, CK_Select, CK_Sext, CK_Zext }; |
| 133 | |
| 134 | private: |
| 135 | BoolFolding(const BoolFolding &) = delete; |
| 136 | BoolFolding &operator=(const BoolFolding &) = delete; |
| 137 | |
| 138 | public: |
| 139 | BoolFolding() = default; |
| 140 | static BoolFoldingProducerKind getProducerKind(const Inst *Instr); |
| 141 | static BoolFoldingConsumerKind getConsumerKind(const Inst *Instr); |
| 142 | static bool hasComplexLowering(const Inst *Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 143 | static bool isValidFolding(BoolFoldingProducerKind ProducerKind, |
| 144 | BoolFoldingConsumerKind ConsumerKind); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 145 | void init(CfgNode *Node); |
| 146 | const Inst *getProducerFor(const Operand *Opnd) const; |
| 147 | void dump(const Cfg *Func) const; |
| 148 | |
| 149 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 150 | /// Returns true if Producers contains a valid entry for the given VarNum. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 151 | bool containsValid(SizeT VarNum) const { |
| 152 | auto Element = Producers.find(VarNum); |
| 153 | return Element != Producers.end() && Element->second.Instr != nullptr; |
| 154 | } |
| 155 | void setInvalid(SizeT VarNum) { Producers[VarNum].Instr = nullptr; } |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 156 | void invalidateProducersOnStore(const Inst *Instr); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 157 | /// Producers maps Variable::Number to a BoolFoldingEntry. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 158 | CfgUnorderedMap<SizeT, BoolFoldingEntry<Traits>> Producers; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 161 | template <typename Traits> |
| 162 | BoolFoldingEntry<Traits>::BoolFoldingEntry(Inst *I) |
| 163 | : Instr(I), IsComplex(BoolFolding<Traits>::hasComplexLowering(I)) {} |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 164 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 165 | template <typename Traits> |
| 166 | typename BoolFolding<Traits>::BoolFoldingProducerKind |
| 167 | BoolFolding<Traits>::getProducerKind(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 168 | if (llvm::isa<InstIcmp>(Instr)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 169 | if (Traits::Is64Bit || Instr->getSrc(0)->getType() != IceType_i64) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 170 | return PK_Icmp32; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 171 | return PK_Icmp64; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 172 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 173 | if (llvm::isa<InstFcmp>(Instr)) |
| 174 | return PK_Fcmp; |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 175 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Instr)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 176 | if (Traits::Is64Bit || Arith->getSrc(0)->getType() != IceType_i64) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 177 | switch (Arith->getOp()) { |
| 178 | default: |
| 179 | return PK_None; |
| 180 | case InstArithmetic::And: |
| 181 | case InstArithmetic::Or: |
| 182 | return PK_Arith; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return PK_None; // TODO(stichnot): remove this |
| 187 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 188 | if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 189 | switch (Cast->getCastKind()) { |
| 190 | default: |
| 191 | return PK_None; |
| 192 | case InstCast::Trunc: |
| 193 | return PK_Trunc; |
| 194 | } |
| 195 | } |
| 196 | return PK_None; |
| 197 | } |
| 198 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 199 | template <typename Traits> |
| 200 | typename BoolFolding<Traits>::BoolFoldingConsumerKind |
| 201 | BoolFolding<Traits>::getConsumerKind(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 202 | if (llvm::isa<InstBr>(Instr)) |
| 203 | return CK_Br; |
| 204 | if (llvm::isa<InstSelect>(Instr)) |
| 205 | return CK_Select; |
| 206 | return CK_None; // TODO(stichnot): remove this |
| 207 | |
| 208 | if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 209 | switch (Cast->getCastKind()) { |
| 210 | default: |
| 211 | return CK_None; |
| 212 | case InstCast::Sext: |
| 213 | return CK_Sext; |
| 214 | case InstCast::Zext: |
| 215 | return CK_Zext; |
| 216 | } |
| 217 | } |
| 218 | return CK_None; |
| 219 | } |
| 220 | |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 221 | /// Returns true if the producing instruction has a "complex" lowering sequence. |
| 222 | /// This generally means that its lowering sequence requires more than one |
| 223 | /// conditional branch, namely 64-bit integer compares and some floating-point |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 224 | /// 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] | 225 | /// to disable the folding optimization because it minimizes branches. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 226 | template <typename Traits> |
| 227 | bool BoolFolding<Traits>::hasComplexLowering(const Inst *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 228 | switch (getProducerKind(Instr)) { |
| 229 | default: |
| 230 | return false; |
| 231 | case PK_Icmp64: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 232 | return !Traits::Is64Bit; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 233 | case PK_Fcmp: |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 234 | return Traits::TableFcmp[llvm::cast<InstFcmp>(Instr)->getCondition()].C2 != |
| 235 | Traits::Cond::Br_None; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 239 | template <typename Traits> |
| 240 | bool BoolFolding<Traits>::isValidFolding( |
| 241 | typename BoolFolding<Traits>::BoolFoldingProducerKind ProducerKind, |
| 242 | typename BoolFolding<Traits>::BoolFoldingConsumerKind ConsumerKind) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 243 | switch (ProducerKind) { |
| 244 | default: |
| 245 | return false; |
| 246 | case PK_Icmp32: |
| 247 | case PK_Icmp64: |
| 248 | case PK_Fcmp: |
| 249 | return (ConsumerKind == CK_Br) || (ConsumerKind == CK_Select); |
| 250 | case PK_Arith: |
| 251 | return ConsumerKind == CK_Br; |
| 252 | } |
| 253 | } |
| 254 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 255 | template <typename Traits> void BoolFolding<Traits>::init(CfgNode *Node) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 256 | Producers.clear(); |
| 257 | for (Inst &Instr : Node->getInsts()) { |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 258 | if (Instr.isDeleted()) |
| 259 | continue; |
| 260 | invalidateProducersOnStore(&Instr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 261 | // Check whether Instr is a valid producer. |
| 262 | Variable *Var = Instr.getDest(); |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 263 | if (Var // only consider instructions with an actual dest var |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 264 | && Var->getType() == IceType_i1 // only bool-type dest vars |
| 265 | && getProducerKind(&Instr) != PK_None) { // white-listed instructions |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 266 | Producers[Var->getIndex()] = BoolFoldingEntry<Traits>(&Instr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 267 | } |
| 268 | // Check each src variable against the map. |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 269 | FOREACH_VAR_IN_INST(Var, Instr) { |
| 270 | SizeT VarNum = Var->getIndex(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 271 | if (!containsValid(VarNum)) |
| 272 | continue; |
| 273 | // All valid consumers use Var as the first source operand |
| 274 | if (IndexOfVarOperandInInst(Var) != 0) { |
| 275 | setInvalid(VarNum); |
| 276 | continue; |
| 277 | } |
| 278 | // Consumer instructions must be white-listed |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 279 | typename BoolFolding<Traits>::BoolFoldingConsumerKind ConsumerKind = |
| 280 | getConsumerKind(&Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 281 | if (ConsumerKind == CK_None) { |
| 282 | setInvalid(VarNum); |
| 283 | continue; |
| 284 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 285 | typename BoolFolding<Traits>::BoolFoldingProducerKind ProducerKind = |
| 286 | getProducerKind(Producers[VarNum].Instr); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 287 | if (!isValidFolding(ProducerKind, ConsumerKind)) { |
| 288 | setInvalid(VarNum); |
| 289 | continue; |
| 290 | } |
| 291 | // Avoid creating multiple copies of complex producer instructions. |
| 292 | if (Producers[VarNum].IsComplex && Producers[VarNum].NumUses > 0) { |
| 293 | setInvalid(VarNum); |
| 294 | continue; |
| 295 | } |
| 296 | ++Producers[VarNum].NumUses; |
| 297 | if (Instr.isLastUse(Var)) { |
| 298 | Producers[VarNum].IsLiveOut = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | for (auto &I : Producers) { |
| 303 | // Ignore entries previously marked invalid. |
| 304 | if (I.second.Instr == nullptr) |
| 305 | continue; |
| 306 | // Disable the producer if its dest may be live beyond this block. |
| 307 | if (I.second.IsLiveOut) { |
| 308 | setInvalid(I.first); |
| 309 | continue; |
| 310 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 311 | // Mark as "dead" rather than outright deleting. This is so that other |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 312 | // peephole style optimizations during or before lowering have access to |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 313 | // this instruction in undeleted form. See for example |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 314 | // tryOptimizedCmpxchgCmpBr(). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 315 | I.second.Instr->setDead(); |
| 316 | } |
| 317 | } |
| 318 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 319 | template <typename Traits> |
| 320 | const Inst *BoolFolding<Traits>::getProducerFor(const Operand *Opnd) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 321 | auto *Var = llvm::dyn_cast<const Variable>(Opnd); |
| 322 | if (Var == nullptr) |
| 323 | return nullptr; |
| 324 | SizeT VarNum = Var->getIndex(); |
| 325 | auto Element = Producers.find(VarNum); |
| 326 | if (Element == Producers.end()) |
| 327 | return nullptr; |
| 328 | return Element->second.Instr; |
| 329 | } |
| 330 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 331 | template <typename Traits> |
| 332 | void BoolFolding<Traits>::dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 333 | if (!BuildDefs::dump() || !Func->isVerbose(IceV_Folding)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 334 | return; |
| 335 | OstreamLocker L(Func->getContext()); |
| 336 | Ostream &Str = Func->getContext()->getStrDump(); |
| 337 | for (auto &I : Producers) { |
| 338 | if (I.second.Instr == nullptr) |
| 339 | continue; |
| 340 | Str << "Found foldable producer:\n "; |
| 341 | I.second.Instr->dump(Func); |
| 342 | Str << "\n"; |
| 343 | } |
| 344 | } |
| 345 | |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 346 | /// If the given instruction has potential memory side effects (e.g. store, rmw, |
| 347 | /// or a call instruction with potential memory side effects), then we must not |
| 348 | /// allow a pre-store Producer instruction with memory operands to be folded |
| 349 | /// into a post-store Consumer instruction. If this is detected, the Producer |
| 350 | /// is invalidated. |
| 351 | /// |
| 352 | /// We use the Producer's IsLiveOut field to determine whether any potential |
| 353 | /// Consumers come after this store instruction. The IsLiveOut field is |
| 354 | /// initialized to true, and BoolFolding::init() sets IsLiveOut to false when it |
| 355 | /// sees the variable's definitive last use (indicating the variable is not in |
| 356 | /// the node's live-out set). Thus if we see here that IsLiveOut is false, we |
| 357 | /// know that there can be no consumers after the store, and therefore we know |
| 358 | /// the folding is safe despite the store instruction. |
| 359 | template <typename Traits> |
| 360 | void BoolFolding<Traits>::invalidateProducersOnStore(const Inst *Instr) { |
| 361 | if (!Instr->isMemoryWrite()) |
| 362 | return; |
| 363 | for (auto &ProducerPair : Producers) { |
| 364 | if (!ProducerPair.second.IsLiveOut) |
| 365 | continue; |
| 366 | Inst *PInst = ProducerPair.second.Instr; |
| 367 | if (PInst == nullptr) |
| 368 | continue; |
| 369 | bool HasMemOperand = false; |
| 370 | const SizeT SrcSize = PInst->getSrcSize(); |
| 371 | for (SizeT I = 0; I < SrcSize; ++I) { |
| 372 | if (llvm::isa<typename Traits::X86OperandMem>(PInst->getSrc(I))) { |
| 373 | HasMemOperand = true; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | if (!HasMemOperand) |
| 378 | continue; |
| 379 | setInvalid(ProducerPair.first); |
| 380 | } |
| 381 | } |
| 382 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 383 | template <typename TraitsType> |
| 384 | void TargetX86Base<TraitsType>::initNodeForLowering(CfgNode *Node) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 385 | FoldingInfo.init(Node); |
| 386 | FoldingInfo.dump(Func); |
| 387 | } |
| 388 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 389 | template <typename TraitsType> |
| 390 | TargetX86Base<TraitsType>::TargetX86Base(Cfg *Func) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 391 | : TargetLowering(Func), NeedSandboxing(SandboxingType == ST_NaCl) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 392 | static_assert( |
| 393 | (Traits::InstructionSet::End - Traits::InstructionSet::Begin) == |
| 394 | (TargetInstructionSet::X86InstructionSet_End - |
| 395 | TargetInstructionSet::X86InstructionSet_Begin), |
| 396 | "Traits::InstructionSet range different from TargetInstructionSet"); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 397 | if (getFlags().getTargetInstructionSet() != |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 398 | TargetInstructionSet::BaseInstructionSet) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 399 | InstructionSet = static_cast<InstructionSetEnum>( |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 400 | (getFlags().getTargetInstructionSet() - |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 401 | TargetInstructionSet::X86InstructionSet_Begin) + |
| 402 | Traits::InstructionSet::Begin); |
| 403 | } |
Jim Stichnoth | 94844f1 | 2015-11-04 16:06:16 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 406 | template <typename TraitsType> |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 407 | void TargetX86Base<TraitsType>::staticInit(GlobalContext *Ctx) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 408 | RegNumT::setLimit(Traits::RegisterSet::Reg_NUM); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 409 | Traits::initRegisterSet(getFlags(), &TypeToRegisterSet, &RegisterAliases); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 410 | for (size_t i = 0; i < TypeToRegisterSet.size(); ++i) |
| 411 | TypeToRegisterSetUnfiltered[i] = TypeToRegisterSet[i]; |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 412 | filterTypeToRegisterSet(Ctx, Traits::RegisterSet::Reg_NUM, |
| 413 | TypeToRegisterSet.data(), TypeToRegisterSet.size(), |
Jim Stichnoth | 2544d4d | 2016-01-22 13:07:46 -0800 | [diff] [blame] | 414 | Traits::getRegName, getRegClassName); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 415 | PcRelFixup = Traits::FK_PcRel; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 416 | AbsFixup = getFlags().getUseNonsfi() ? Traits::FK_Gotoff : Traits::FK_Abs; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 419 | template <typename TraitsType> |
| 420 | bool TargetX86Base<TraitsType>::shouldBePooled(const Constant *C) { |
| 421 | if (auto *ConstFloat = llvm::dyn_cast<ConstantFloat>(C)) { |
| 422 | return !Utils::isPositiveZero(ConstFloat->getValue()); |
| 423 | } |
| 424 | if (auto *ConstDouble = llvm::dyn_cast<ConstantDouble>(C)) { |
| 425 | return !Utils::isPositiveZero(ConstDouble->getValue()); |
| 426 | } |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 427 | if (getFlags().getRandomizeAndPoolImmediatesOption() != RPI_Pool) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | return C->shouldBeRandomizedOrPooled(); |
| 431 | } |
| 432 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 433 | template <typename TraitsType> void TargetX86Base<TraitsType>::translateO2() { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 434 | TimerMarker T(TimerStack::TT_O2, Func); |
| 435 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 436 | if (SandboxingType != ST_None) { |
| 437 | initRebasePtr(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 438 | } |
| 439 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 440 | genTargetHelperCalls(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 441 | Func->dump("After target helper call insertion"); |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 442 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 443 | // Merge Alloca instructions, and lay out the stack. |
| 444 | static constexpr bool SortAndCombineAllocas = true; |
| 445 | Func->processAllocas(SortAndCombineAllocas); |
| 446 | Func->dump("After Alloca processing"); |
| 447 | |
Manasij Mukherjee | f47d520 | 2016-07-12 16:59:17 -0700 | [diff] [blame] | 448 | // Run this early so it can be used to focus optimizations on potentially hot |
| 449 | // code. |
| 450 | // TODO(stichnot,ascull): currently only used for regalloc not |
| 451 | // expensive high level optimizations which could be focused on potentially |
| 452 | // hot code. |
| 453 | Func->generateLoopInfo(); |
| 454 | Func->dump("After loop analysis"); |
| 455 | if (getFlags().getLoopInvariantCodeMotion()) { |
| 456 | Func->loopInvariantCodeMotion(); |
| 457 | Func->dump("After LICM"); |
| 458 | } |
| 459 | |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 460 | if (getFlags().getEnableExperimental()) { |
| 461 | Func->localCSE(); |
| 462 | Func->dump("After Local CSE"); |
| 463 | } |
Manasij Mukherjee | 45f51a2 | 2016-06-27 16:12:37 -0700 | [diff] [blame] | 464 | if (getFlags().getEnableShortCircuit()) { |
| 465 | Func->shortCircuitJumps(); |
| 466 | Func->dump("After Short Circuiting"); |
| 467 | } |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 468 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 469 | if (!getFlags().getEnablePhiEdgeSplit()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 470 | // Lower Phi instructions. |
| 471 | Func->placePhiLoads(); |
| 472 | if (Func->hasError()) |
| 473 | return; |
| 474 | Func->placePhiStores(); |
| 475 | if (Func->hasError()) |
| 476 | return; |
| 477 | Func->deletePhis(); |
| 478 | if (Func->hasError()) |
| 479 | return; |
| 480 | Func->dump("After Phi lowering"); |
| 481 | } |
| 482 | |
| 483 | // Address mode optimization. |
| 484 | Func->getVMetadata()->init(VMK_SingleDefs); |
| 485 | Func->doAddressOpt(); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 486 | Func->materializeVectorShuffles(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 487 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 488 | // Find read-modify-write opportunities. Do this after address mode |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 489 | // optimization so that doAddressOpt() doesn't need to be applied to RMW |
| 490 | // instructions as well. |
| 491 | findRMW(); |
| 492 | Func->dump("After RMW transform"); |
| 493 | |
| 494 | // Argument lowering |
| 495 | Func->doArgLowering(); |
| 496 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 497 | // Target lowering. This requires liveness analysis for some parts of the |
| 498 | // lowering decisions, such as compare/branch fusing. If non-lightweight |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 499 | // liveness analysis is used, the instructions need to be renumbered first |
| 500 | // TODO: This renumbering should only be necessary if we're actually |
| 501 | // calculating live intervals, which we only do for register allocation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 502 | Func->renumberInstructions(); |
| 503 | if (Func->hasError()) |
| 504 | return; |
| 505 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 506 | // TODO: It should be sufficient to use the fastest liveness calculation, |
| 507 | // i.e. livenessLightweight(). However, for some reason that slows down the |
| 508 | // rest of the translation. Investigate. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 509 | Func->liveness(Liveness_Basic); |
| 510 | if (Func->hasError()) |
| 511 | return; |
| 512 | Func->dump("After x86 address mode opt"); |
| 513 | |
| 514 | // Disable constant blinding or pooling for load optimization. |
| 515 | { |
| 516 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
| 517 | doLoadOpt(); |
| 518 | } |
| 519 | Func->genCode(); |
| 520 | if (Func->hasError()) |
| 521 | return; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 522 | if (SandboxingType != ST_None) { |
| 523 | initSandbox(); |
| 524 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 525 | Func->dump("After x86 codegen"); |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 526 | splitBlockLocalVariables(Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 527 | |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 528 | // Register allocation. This requires instruction renumbering and full |
| 529 | // liveness analysis. Loops must be identified before liveness so variable |
| 530 | // use weights are correct. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 531 | Func->renumberInstructions(); |
| 532 | if (Func->hasError()) |
| 533 | return; |
| 534 | Func->liveness(Liveness_Intervals); |
| 535 | if (Func->hasError()) |
| 536 | return; |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 537 | // The post-codegen dump is done here, after liveness analysis and associated |
| 538 | // cleanup, to make the dump cleaner and more useful. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 539 | Func->dump("After initial x8632 codegen"); |
Jim Stichnoth | 2943d77 | 2016-06-21 11:22:17 -0700 | [diff] [blame] | 540 | // Validate the live range computations. The expensive validation call is |
| 541 | // deliberately only made when assertions are enabled. |
| 542 | assert(Func->validateLiveness()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 543 | Func->getVMetadata()->init(VMK_All); |
| 544 | regAlloc(RAK_Global); |
| 545 | if (Func->hasError()) |
| 546 | return; |
| 547 | Func->dump("After linear scan regalloc"); |
| 548 | |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 549 | if (getFlags().getEnablePhiEdgeSplit()) { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 550 | Func->advancedPhiLowering(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 551 | Func->dump("After advanced Phi lowering"); |
| 552 | } |
| 553 | |
| 554 | // Stack frame mapping. |
| 555 | Func->genFrame(); |
| 556 | if (Func->hasError()) |
| 557 | return; |
| 558 | Func->dump("After stack frame mapping"); |
| 559 | |
| 560 | Func->contractEmptyNodes(); |
| 561 | Func->reorderNodes(); |
| 562 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 563 | // Shuffle basic block order if -reorder-basic-blocks is enabled. |
| 564 | Func->shuffleNodes(); |
| 565 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 566 | // Branch optimization. This needs to be done just before code emission. In |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 567 | // particular, no transformations that insert or reorder CfgNodes should be |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 568 | // 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] | 569 | // to reduce the amount of work needed for searching for opportunities. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 570 | Func->doBranchOpt(); |
| 571 | Func->dump("After branch optimization"); |
| 572 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 573 | // Nop insertion if -nop-insertion is enabled. |
| 574 | Func->doNopInsertion(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 575 | |
| 576 | // Mark nodes that require sandbox alignment |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 577 | if (NeedSandboxing) { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 578 | Func->markNodesForSandboxing(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 579 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 580 | } |
| 581 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 582 | template <typename TraitsType> void TargetX86Base<TraitsType>::translateOm1() { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 583 | TimerMarker T(TimerStack::TT_Om1, Func); |
| 584 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 585 | if (SandboxingType != ST_None) { |
| 586 | initRebasePtr(); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 587 | } |
| 588 | |
John Porto | 5e0a8a7 | 2015-11-20 13:50:36 -0800 | [diff] [blame] | 589 | genTargetHelperCalls(); |
| 590 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 591 | // Do not merge Alloca instructions, and lay out the stack. |
| 592 | static constexpr bool SortAndCombineAllocas = false; |
| 593 | Func->processAllocas(SortAndCombineAllocas); |
| 594 | Func->dump("After Alloca processing"); |
| 595 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 596 | Func->placePhiLoads(); |
| 597 | if (Func->hasError()) |
| 598 | return; |
| 599 | Func->placePhiStores(); |
| 600 | if (Func->hasError()) |
| 601 | return; |
| 602 | Func->deletePhis(); |
| 603 | if (Func->hasError()) |
| 604 | return; |
| 605 | Func->dump("After Phi lowering"); |
| 606 | |
| 607 | Func->doArgLowering(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 608 | Func->genCode(); |
| 609 | if (Func->hasError()) |
| 610 | return; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 611 | if (SandboxingType != ST_None) { |
| 612 | initSandbox(); |
| 613 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 614 | Func->dump("After initial x8632 codegen"); |
| 615 | |
| 616 | regAlloc(RAK_InfOnly); |
| 617 | if (Func->hasError()) |
| 618 | return; |
| 619 | Func->dump("After regalloc of infinite-weight variables"); |
| 620 | |
| 621 | Func->genFrame(); |
| 622 | if (Func->hasError()) |
| 623 | return; |
| 624 | Func->dump("After stack frame mapping"); |
| 625 | |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 626 | // Shuffle basic block order if -reorder-basic-blocks is enabled. |
| 627 | Func->shuffleNodes(); |
| 628 | |
| 629 | // Nop insertion if -nop-insertion is enabled. |
| 630 | Func->doNopInsertion(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 631 | |
| 632 | // Mark nodes that require sandbox alignment |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 633 | if (NeedSandboxing) |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 634 | Func->markNodesForSandboxing(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 635 | } |
| 636 | |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 637 | inline bool canRMW(const InstArithmetic *Arith) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 638 | Type Ty = Arith->getDest()->getType(); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 639 | // X86 vector instructions write to a register and have no RMW option. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 640 | if (isVectorType(Ty)) |
| 641 | return false; |
| 642 | bool isI64 = Ty == IceType_i64; |
| 643 | |
| 644 | switch (Arith->getOp()) { |
| 645 | // Not handled for lack of simple lowering: |
| 646 | // shift on i64 |
| 647 | // mul, udiv, urem, sdiv, srem, frem |
| 648 | // Not handled for lack of RMW instructions: |
| 649 | // fadd, fsub, fmul, fdiv (also vector types) |
| 650 | default: |
| 651 | return false; |
| 652 | case InstArithmetic::Add: |
| 653 | case InstArithmetic::Sub: |
| 654 | case InstArithmetic::And: |
| 655 | case InstArithmetic::Or: |
| 656 | case InstArithmetic::Xor: |
| 657 | return true; |
| 658 | case InstArithmetic::Shl: |
| 659 | case InstArithmetic::Lshr: |
| 660 | case InstArithmetic::Ashr: |
| 661 | return false; // TODO(stichnot): implement |
| 662 | return !isI64; |
| 663 | } |
| 664 | } |
| 665 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 666 | template <typename TraitsType> |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 667 | bool isSameMemAddressOperand(const Operand *A, const Operand *B) { |
| 668 | if (A == B) |
| 669 | return true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 670 | if (auto *MemA = |
| 671 | llvm::dyn_cast<typename TargetX86Base<TraitsType>::X86OperandMem>( |
| 672 | A)) { |
| 673 | if (auto *MemB = |
| 674 | llvm::dyn_cast<typename TargetX86Base<TraitsType>::X86OperandMem>( |
| 675 | B)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 676 | return MemA->getBase() == MemB->getBase() && |
| 677 | MemA->getOffset() == MemB->getOffset() && |
| 678 | MemA->getIndex() == MemB->getIndex() && |
| 679 | MemA->getShift() == MemB->getShift() && |
| 680 | MemA->getSegmentRegister() == MemB->getSegmentRegister(); |
| 681 | } |
| 682 | } |
| 683 | return false; |
| 684 | } |
| 685 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 686 | template <typename TraitsType> void TargetX86Base<TraitsType>::findRMW() { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 687 | TimerMarker _(TimerStack::TT_findRMW, Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 688 | Func->dump("Before RMW"); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 689 | if (Func->isVerbose(IceV_RMW)) |
| 690 | Func->getContext()->lockStr(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 691 | for (CfgNode *Node : Func->getNodes()) { |
| 692 | // Walk through the instructions, considering each sequence of 3 |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 693 | // instructions, and look for the particular RMW pattern. Note that this |
| 694 | // search can be "broken" (false negatives) if there are intervening |
| 695 | // deleted instructions, or intervening instructions that could be safely |
| 696 | // moved out of the way to reveal an RMW pattern. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 697 | auto E = Node->getInsts().end(); |
| 698 | auto I1 = E, I2 = E, I3 = Node->getInsts().begin(); |
| 699 | for (; I3 != E; I1 = I2, I2 = I3, ++I3) { |
| 700 | // Make I3 skip over deleted instructions. |
| 701 | while (I3 != E && I3->isDeleted()) |
| 702 | ++I3; |
| 703 | if (I1 == E || I2 == E || I3 == E) |
| 704 | continue; |
| 705 | assert(!I1->isDeleted()); |
| 706 | assert(!I2->isDeleted()); |
| 707 | assert(!I3->isDeleted()); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 708 | auto *Load = llvm::dyn_cast<InstLoad>(I1); |
| 709 | auto *Arith = llvm::dyn_cast<InstArithmetic>(I2); |
| 710 | auto *Store = llvm::dyn_cast<InstStore>(I3); |
| 711 | if (!Load || !Arith || !Store) |
| 712 | continue; |
| 713 | // Look for: |
| 714 | // a = Load addr |
| 715 | // b = <op> a, other |
| 716 | // Store b, addr |
| 717 | // Change to: |
| 718 | // a = Load addr |
| 719 | // b = <op> a, other |
| 720 | // x = FakeDef |
| 721 | // RMW <op>, addr, other, x |
| 722 | // b = Store b, addr, x |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 723 | // Note that inferTwoAddress() makes sure setDestRedefined() gets called |
| 724 | // on the updated Store instruction, to avoid liveness problems later. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 725 | // |
| 726 | // With this transformation, the Store instruction acquires a Dest |
| 727 | // 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] | 728 | // more uses of "b". Variable "x" is a beacon for determining whether the |
| 729 | // Store instruction gets dead-code eliminated. If the Store instruction |
| 730 | // is eliminated, then it must be the case that the RMW instruction ends |
| 731 | // x's live range, and therefore the RMW instruction will be retained and |
| 732 | // later lowered. On the other hand, if the RMW instruction does not end |
| 733 | // x's live range, then the Store instruction must still be present, and |
| 734 | // therefore the RMW instruction is ignored during lowering because it is |
| 735 | // redundant with the Store instruction. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 736 | // |
| 737 | // Note that if "a" has further uses, the RMW transformation may still |
| 738 | // trigger, resulting in two loads and one store, which is worse than the |
| 739 | // original one load and one store. However, this is probably rare, and |
| 740 | // caching probably keeps it just as fast. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 741 | if (!isSameMemAddressOperand<TraitsType>(Load->getSourceAddress(), |
| 742 | Store->getAddr())) |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 743 | continue; |
| 744 | Operand *ArithSrcFromLoad = Arith->getSrc(0); |
| 745 | Operand *ArithSrcOther = Arith->getSrc(1); |
| 746 | if (ArithSrcFromLoad != Load->getDest()) { |
| 747 | if (!Arith->isCommutative() || ArithSrcOther != Load->getDest()) |
| 748 | continue; |
| 749 | std::swap(ArithSrcFromLoad, ArithSrcOther); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 750 | } |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 751 | if (Arith->getDest() != Store->getData()) |
| 752 | continue; |
| 753 | if (!canRMW(Arith)) |
| 754 | continue; |
| 755 | if (Func->isVerbose(IceV_RMW)) { |
| 756 | Ostream &Str = Func->getContext()->getStrDump(); |
| 757 | Str << "Found RMW in " << Func->getFunctionName() << ":\n "; |
| 758 | Load->dump(Func); |
| 759 | Str << "\n "; |
| 760 | Arith->dump(Func); |
| 761 | Str << "\n "; |
| 762 | Store->dump(Func); |
| 763 | Str << "\n"; |
| 764 | } |
| 765 | Variable *Beacon = Func->makeVariable(IceType_i32); |
| 766 | Beacon->setMustNotHaveReg(); |
| 767 | Store->setRmwBeacon(Beacon); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 768 | auto *BeaconDef = InstFakeDef::create(Func, Beacon); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 769 | Node->getInsts().insert(I3, BeaconDef); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 770 | auto *RMW = InstX86FakeRMW::create(Func, ArithSrcOther, Store->getAddr(), |
| 771 | Beacon, Arith->getOp()); |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 772 | Node->getInsts().insert(I3, RMW); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 775 | if (Func->isVerbose(IceV_RMW)) |
| 776 | Func->getContext()->unlockStr(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | // Converts a ConstantInteger32 operand into its constant value, or |
| 780 | // MemoryOrderInvalid if the operand is not a ConstantInteger32. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 781 | inline uint64_t getConstantMemoryOrder(Operand *Opnd) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 782 | if (auto *Integer = llvm::dyn_cast<ConstantInteger32>(Opnd)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 783 | return Integer->getValue(); |
| 784 | return Intrinsics::MemoryOrderInvalid; |
| 785 | } |
| 786 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 787 | /// Determines whether the dest of a Load instruction can be folded into one of |
| 788 | /// the src operands of a 2-operand instruction. This is true as long as the |
| 789 | /// load dest matches exactly one of the binary instruction's src operands. |
| 790 | /// Replaces Src0 or Src1 with LoadSrc if the answer is true. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 791 | inline bool canFoldLoadIntoBinaryInst(Operand *LoadSrc, Variable *LoadDest, |
| 792 | Operand *&Src0, Operand *&Src1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 793 | if (Src0 == LoadDest && Src1 != LoadDest) { |
| 794 | Src0 = LoadSrc; |
| 795 | return true; |
| 796 | } |
| 797 | if (Src0 != LoadDest && Src1 == LoadDest) { |
| 798 | Src1 = LoadSrc; |
| 799 | return true; |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 804 | template <typename TraitsType> void TargetX86Base<TraitsType>::doLoadOpt() { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 805 | TimerMarker _(TimerStack::TT_loadOpt, Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 806 | for (CfgNode *Node : Func->getNodes()) { |
| 807 | Context.init(Node); |
| 808 | while (!Context.atEnd()) { |
| 809 | Variable *LoadDest = nullptr; |
| 810 | Operand *LoadSrc = nullptr; |
| 811 | Inst *CurInst = Context.getCur(); |
| 812 | Inst *Next = Context.getNextInst(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 813 | // Determine whether the current instruction is a Load instruction or |
| 814 | // equivalent. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 815 | if (auto *Load = llvm::dyn_cast<InstLoad>(CurInst)) { |
| 816 | // An InstLoad always qualifies. |
| 817 | LoadDest = Load->getDest(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 818 | constexpr bool DoLegalize = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 819 | LoadSrc = formMemoryOperand(Load->getSourceAddress(), |
| 820 | LoadDest->getType(), DoLegalize); |
| 821 | } else if (auto *Intrin = llvm::dyn_cast<InstIntrinsicCall>(CurInst)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 822 | // An AtomicLoad intrinsic qualifies as long as it has a valid memory |
| 823 | // ordering, and can be implemented in a single instruction (i.e., not |
| 824 | // i64 on x86-32). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 825 | Intrinsics::IntrinsicID ID = Intrin->getIntrinsicInfo().ID; |
| 826 | if (ID == Intrinsics::AtomicLoad && |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 827 | (Traits::Is64Bit || Intrin->getDest()->getType() != IceType_i64) && |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 828 | Intrinsics::isMemoryOrderValid( |
| 829 | ID, getConstantMemoryOrder(Intrin->getArg(1)))) { |
| 830 | LoadDest = Intrin->getDest(); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 831 | constexpr bool DoLegalize = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 832 | LoadSrc = formMemoryOperand(Intrin->getArg(0), LoadDest->getType(), |
| 833 | DoLegalize); |
| 834 | } |
| 835 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 836 | // A Load instruction can be folded into the following instruction only |
| 837 | // if the following instruction ends the Load's Dest variable's live |
| 838 | // range. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 839 | if (LoadDest && Next && Next->isLastUse(LoadDest)) { |
| 840 | assert(LoadSrc); |
| 841 | Inst *NewInst = nullptr; |
| 842 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Next)) { |
| 843 | Operand *Src0 = Arith->getSrc(0); |
| 844 | Operand *Src1 = Arith->getSrc(1); |
| 845 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 846 | NewInst = InstArithmetic::create(Func, Arith->getOp(), |
| 847 | Arith->getDest(), Src0, Src1); |
| 848 | } |
| 849 | } else if (auto *Icmp = llvm::dyn_cast<InstIcmp>(Next)) { |
| 850 | Operand *Src0 = Icmp->getSrc(0); |
| 851 | Operand *Src1 = Icmp->getSrc(1); |
| 852 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 853 | NewInst = InstIcmp::create(Func, Icmp->getCondition(), |
| 854 | Icmp->getDest(), Src0, Src1); |
| 855 | } |
| 856 | } else if (auto *Fcmp = llvm::dyn_cast<InstFcmp>(Next)) { |
| 857 | Operand *Src0 = Fcmp->getSrc(0); |
| 858 | Operand *Src1 = Fcmp->getSrc(1); |
| 859 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 860 | NewInst = InstFcmp::create(Func, Fcmp->getCondition(), |
| 861 | Fcmp->getDest(), Src0, Src1); |
| 862 | } |
| 863 | } else if (auto *Select = llvm::dyn_cast<InstSelect>(Next)) { |
| 864 | Operand *Src0 = Select->getTrueOperand(); |
| 865 | Operand *Src1 = Select->getFalseOperand(); |
| 866 | if (canFoldLoadIntoBinaryInst(LoadSrc, LoadDest, Src0, Src1)) { |
| 867 | NewInst = InstSelect::create(Func, Select->getDest(), |
| 868 | Select->getCondition(), Src0, Src1); |
| 869 | } |
| 870 | } else if (auto *Cast = llvm::dyn_cast<InstCast>(Next)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 871 | // The load dest can always be folded into a Cast instruction. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 872 | auto *Src0 = llvm::dyn_cast<Variable>(Cast->getSrc(0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 873 | if (Src0 == LoadDest) { |
| 874 | NewInst = InstCast::create(Func, Cast->getCastKind(), |
| 875 | Cast->getDest(), LoadSrc); |
| 876 | } |
| 877 | } |
| 878 | if (NewInst) { |
| 879 | CurInst->setDeleted(); |
| 880 | Next->setDeleted(); |
| 881 | Context.insert(NewInst); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 882 | // Update NewInst->LiveRangesEnded so that target lowering may |
| 883 | // benefit. Also update NewInst->HasSideEffects. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 884 | NewInst->spliceLivenessInfo(Next, CurInst); |
| 885 | } |
| 886 | } |
| 887 | Context.advanceCur(); |
| 888 | Context.advanceNext(); |
| 889 | } |
| 890 | } |
| 891 | Func->dump("After load optimization"); |
| 892 | } |
| 893 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 894 | template <typename TraitsType> |
| 895 | bool TargetX86Base<TraitsType>::doBranchOpt(Inst *I, const CfgNode *NextNode) { |
| 896 | if (auto *Br = llvm::dyn_cast<InstX86Br>(I)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 897 | return Br->optimizeBranch(NextNode); |
| 898 | } |
| 899 | return false; |
| 900 | } |
| 901 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 902 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 903 | Variable *TargetX86Base<TraitsType>::getPhysicalRegister(RegNumT RegNum, |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 904 | Type Ty) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 905 | if (Ty == IceType_void) |
| 906 | Ty = IceType_i32; |
| 907 | if (PhysicalRegisters[Ty].empty()) |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 908 | PhysicalRegisters[Ty].resize(Traits::RegisterSet::Reg_NUM); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 909 | assert(unsigned(RegNum) < PhysicalRegisters[Ty].size()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 910 | Variable *Reg = PhysicalRegisters[Ty][RegNum]; |
| 911 | if (Reg == nullptr) { |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 912 | Reg = Func->makeVariable(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 913 | Reg->setRegNum(RegNum); |
| 914 | PhysicalRegisters[Ty][RegNum] = Reg; |
Jim Stichnoth | 6966055 | 2015-09-18 06:41:02 -0700 | [diff] [blame] | 915 | // Specially mark a named physical register as an "argument" so that it is |
| 916 | // considered live upon function entry. Otherwise it's possible to get |
| 917 | // liveness validation errors for saving callee-save registers. |
| 918 | Func->addImplicitArg(Reg); |
| 919 | // Don't bother tracking the live range of a named physical register. |
| 920 | Reg->setIgnoreLiveness(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 921 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 922 | assert(Traits::getGprForType(Ty, RegNum) == RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 923 | return Reg; |
| 924 | } |
| 925 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 926 | template <typename TraitsType> |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 927 | const char *TargetX86Base<TraitsType>::getRegName(RegNumT RegNum, |
| 928 | Type Ty) const { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 929 | return Traits::getRegName(Traits::getGprForType(Ty, RegNum)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 930 | } |
| 931 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 932 | template <typename TraitsType> |
| 933 | void TargetX86Base<TraitsType>::emitVariable(const Variable *Var) const { |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 934 | if (!BuildDefs::dump()) |
| 935 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 936 | Ostream &Str = Ctx->getStrEmit(); |
| 937 | if (Var->hasReg()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 938 | const bool Is64BitSandboxing = Traits::Is64Bit && NeedSandboxing; |
| 939 | const Type VarType = (Var->isRematerializable() && Is64BitSandboxing) |
| 940 | ? IceType_i64 |
| 941 | : Var->getType(); |
| 942 | Str << "%" << getRegName(Var->getRegNum(), VarType); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 943 | return; |
| 944 | } |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 945 | if (Var->mustHaveReg()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 946 | llvm::report_fatal_error("Infinite-weight Variable (" + Var->getName() + |
Jim Stichnoth | 45bec54 | 2016-02-05 10:26:09 -0800 | [diff] [blame] | 947 | ") has no register assigned - function " + |
| 948 | Func->getFunctionName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 949 | } |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 950 | const int32_t Offset = Var->getStackOffset(); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 951 | auto BaseRegNum = Var->getBaseRegNum(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 952 | if (BaseRegNum.hasNoValue()) |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 953 | BaseRegNum = getFrameOrStackReg(); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 954 | |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 955 | // Print in the form "Offset(%reg)", omitting Offset when it is 0. |
| 956 | if (getFlags().getDecorateAsm()) { |
| 957 | Str << Var->getSymbolicStackOffset(); |
| 958 | } else if (Offset != 0) { |
| 959 | Str << Offset; |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 960 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 961 | const Type FrameSPTy = Traits::WordType; |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 962 | Str << "(%" << getRegName(BaseRegNum, FrameSPTy) << ")"; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 963 | } |
| 964 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 965 | template <typename TraitsType> |
| 966 | typename TargetX86Base<TraitsType>::X86Address |
| 967 | TargetX86Base<TraitsType>::stackVarToAsmOperand(const Variable *Var) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 968 | if (Var->hasReg()) |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 969 | llvm::report_fatal_error("Stack Variable has a register assigned"); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 970 | if (Var->mustHaveReg()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 971 | llvm::report_fatal_error("Infinite-weight Variable (" + Var->getName() + |
Jim Stichnoth | 45bec54 | 2016-02-05 10:26:09 -0800 | [diff] [blame] | 972 | ") has no register assigned - function " + |
| 973 | Func->getFunctionName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 974 | } |
| 975 | int32_t Offset = Var->getStackOffset(); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 976 | auto BaseRegNum = Var->getBaseRegNum(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 977 | if (Var->getBaseRegNum().hasNoValue()) |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 978 | BaseRegNum = getFrameOrStackReg(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 979 | return X86Address(Traits::getEncodedGPR(BaseRegNum), Offset, |
| 980 | AssemblerFixup::NoFixup); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 981 | } |
| 982 | |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 983 | template <typename TraitsType> |
| 984 | void TargetX86Base<TraitsType>::addProlog(CfgNode *Node) { |
| 985 | // Stack frame layout: |
| 986 | // |
| 987 | // +------------------------+ |
| 988 | // | 1. return address | |
| 989 | // +------------------------+ |
| 990 | // | 2. preserved registers | |
| 991 | // +------------------------+ |
| 992 | // | 3. padding | |
| 993 | // +------------------------+ |
| 994 | // | 4. global spill area | |
| 995 | // +------------------------+ |
| 996 | // | 5. padding | |
| 997 | // +------------------------+ |
| 998 | // | 6. local spill area | |
| 999 | // +------------------------+ |
| 1000 | // | 7. padding | |
| 1001 | // +------------------------+ |
| 1002 | // | 8. allocas | |
| 1003 | // +------------------------+ |
| 1004 | // | 9. padding | |
| 1005 | // +------------------------+ |
| 1006 | // | 10. out args | |
| 1007 | // +------------------------+ <--- StackPointer |
| 1008 | // |
| 1009 | // The following variables record the size in bytes of the given areas: |
| 1010 | // * X86_RET_IP_SIZE_BYTES: area 1 |
| 1011 | // * PreservedRegsSizeBytes: area 2 |
| 1012 | // * SpillAreaPaddingBytes: area 3 |
| 1013 | // * GlobalsSize: area 4 |
| 1014 | // * GlobalsAndSubsequentPaddingSize: areas 4 - 5 |
| 1015 | // * LocalsSpillAreaSize: area 6 |
| 1016 | // * SpillAreaSizeBytes: areas 3 - 10 |
| 1017 | // * maxOutArgsSizeBytes(): area 10 |
| 1018 | |
| 1019 | // Determine stack frame offsets for each Variable without a register |
| 1020 | // assignment. This can be done as one variable per stack slot. Or, do |
| 1021 | // coalescing by running the register allocator again with an infinite set of |
| 1022 | // registers (as a side effect, this gives variables a second chance at |
| 1023 | // physical register assignment). |
| 1024 | // |
| 1025 | // A middle ground approach is to leverage sparsity and allocate one block of |
| 1026 | // space on the frame for globals (variables with multi-block lifetime), and |
| 1027 | // one block to share for locals (single-block lifetime). |
| 1028 | |
| 1029 | Context.init(Node); |
| 1030 | Context.setInsertPoint(Context.getCur()); |
| 1031 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1032 | SmallBitVector CalleeSaves = getRegisterSet(RegSet_CalleeSave, RegSet_None); |
| 1033 | RegsUsed = SmallBitVector(CalleeSaves.size()); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1034 | VarList SortedSpilledVariables, VariablesLinkedToSpillSlots; |
| 1035 | size_t GlobalsSize = 0; |
| 1036 | // If there is a separate locals area, this represents that area. Otherwise |
| 1037 | // it counts any variable not counted by GlobalsSize. |
| 1038 | SpillAreaSizeBytes = 0; |
| 1039 | // If there is a separate locals area, this specifies the alignment for it. |
| 1040 | uint32_t LocalsSlotsAlignmentBytes = 0; |
| 1041 | // The entire spill locations area gets aligned to largest natural alignment |
| 1042 | // of the variables that have a spill slot. |
| 1043 | uint32_t SpillAreaAlignmentBytes = 0; |
| 1044 | // A spill slot linked to a variable with a stack slot should reuse that |
| 1045 | // stack slot. |
| 1046 | std::function<bool(Variable *)> TargetVarHook = |
| 1047 | [&VariablesLinkedToSpillSlots](Variable *Var) { |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 1048 | // TODO(stichnot): Refactor this into the base class. |
| 1049 | Variable *Root = Var->getLinkedToStackRoot(); |
| 1050 | if (Root != nullptr) { |
| 1051 | assert(!Root->hasReg()); |
| 1052 | if (!Root->hasReg()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1053 | VariablesLinkedToSpillSlots.push_back(Var); |
| 1054 | return true; |
| 1055 | } |
| 1056 | } |
| 1057 | return false; |
| 1058 | }; |
| 1059 | |
| 1060 | // Compute the list of spilled variables and bounds for GlobalsSize, etc. |
| 1061 | getVarStackSlotParams(SortedSpilledVariables, RegsUsed, &GlobalsSize, |
| 1062 | &SpillAreaSizeBytes, &SpillAreaAlignmentBytes, |
| 1063 | &LocalsSlotsAlignmentBytes, TargetVarHook); |
| 1064 | uint32_t LocalsSpillAreaSize = SpillAreaSizeBytes; |
| 1065 | SpillAreaSizeBytes += GlobalsSize; |
| 1066 | |
| 1067 | // Add push instructions for preserved registers. |
| 1068 | uint32_t NumCallee = 0; |
| 1069 | size_t PreservedRegsSizeBytes = 0; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1070 | SmallBitVector Pushed(CalleeSaves.size()); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1071 | for (RegNumT i : RegNumBVIter(CalleeSaves)) { |
| 1072 | const auto Canonical = Traits::getBaseReg(i); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1073 | assert(Canonical == Traits::getBaseReg(Canonical)); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1074 | if (RegsUsed[i]) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1075 | Pushed[Canonical] = true; |
| 1076 | } |
| 1077 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1078 | for (RegNumT RegNum : RegNumBVIter(Pushed)) { |
| 1079 | assert(RegNum == Traits::getBaseReg(RegNum)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1080 | ++NumCallee; |
| 1081 | PreservedRegsSizeBytes += typeWidthInBytes(Traits::WordType); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1082 | _push_reg(getPhysicalRegister(RegNum, Traits::WordType)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1083 | } |
| 1084 | Ctx->statsUpdateRegistersSaved(NumCallee); |
| 1085 | |
| 1086 | // Generate "push frameptr; mov frameptr, stackptr" |
| 1087 | if (IsEbpBasedFrame) { |
| 1088 | assert((RegsUsed & getRegisterSet(RegSet_FramePointer, RegSet_None)) |
| 1089 | .count() == 0); |
| 1090 | PreservedRegsSizeBytes += typeWidthInBytes(Traits::WordType); |
| 1091 | _link_bp(); |
| 1092 | } |
| 1093 | |
| 1094 | // Align the variables area. SpillAreaPaddingBytes is the size of the region |
| 1095 | // after the preserved registers and before the spill areas. |
| 1096 | // LocalsSlotsPaddingBytes is the amount of padding between the globals and |
| 1097 | // locals area if they are separate. |
| 1098 | assert(SpillAreaAlignmentBytes <= Traits::X86_STACK_ALIGNMENT_BYTES); |
| 1099 | assert(LocalsSlotsAlignmentBytes <= SpillAreaAlignmentBytes); |
| 1100 | uint32_t SpillAreaPaddingBytes = 0; |
| 1101 | uint32_t LocalsSlotsPaddingBytes = 0; |
| 1102 | alignStackSpillAreas(Traits::X86_RET_IP_SIZE_BYTES + PreservedRegsSizeBytes, |
| 1103 | SpillAreaAlignmentBytes, GlobalsSize, |
| 1104 | LocalsSlotsAlignmentBytes, &SpillAreaPaddingBytes, |
| 1105 | &LocalsSlotsPaddingBytes); |
| 1106 | SpillAreaSizeBytes += SpillAreaPaddingBytes + LocalsSlotsPaddingBytes; |
| 1107 | uint32_t GlobalsAndSubsequentPaddingSize = |
| 1108 | GlobalsSize + LocalsSlotsPaddingBytes; |
| 1109 | |
| 1110 | // Functions returning scalar floating point types may need to convert values |
| 1111 | // from an in-register xmm value to the top of the x87 floating point stack. |
| 1112 | // This is done by a movp[sd] and an fld[sd]. Ensure there is enough scratch |
| 1113 | // space on the stack for this. |
| 1114 | const Type ReturnType = Func->getReturnType(); |
| 1115 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 1116 | if (isScalarFloatingType(ReturnType)) { |
| 1117 | // Avoid misaligned double-precicion load/store. |
| 1118 | NeedsStackAlignment = true; |
| 1119 | SpillAreaSizeBytes = |
| 1120 | std::max(typeWidthInBytesOnStack(ReturnType), SpillAreaSizeBytes); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | // Align esp if necessary. |
| 1125 | if (NeedsStackAlignment) { |
| 1126 | uint32_t StackOffset = |
| 1127 | Traits::X86_RET_IP_SIZE_BYTES + PreservedRegsSizeBytes; |
| 1128 | uint32_t StackSize = |
| 1129 | Traits::applyStackAlignment(StackOffset + SpillAreaSizeBytes); |
| 1130 | StackSize = Traits::applyStackAlignment(StackSize + maxOutArgsSizeBytes()); |
| 1131 | SpillAreaSizeBytes = StackSize - StackOffset; |
| 1132 | } else { |
| 1133 | SpillAreaSizeBytes += maxOutArgsSizeBytes(); |
| 1134 | } |
| 1135 | |
| 1136 | // Combine fixed allocations into SpillAreaSizeBytes if we are emitting the |
| 1137 | // fixed allocations in the prolog. |
| 1138 | if (PrologEmitsFixedAllocas) |
| 1139 | SpillAreaSizeBytes += FixedAllocaSizeBytes; |
| 1140 | if (SpillAreaSizeBytes) { |
| 1141 | // Generate "sub stackptr, SpillAreaSizeBytes" |
| 1142 | _sub_sp(Ctx->getConstantInt32(SpillAreaSizeBytes)); |
| 1143 | // If the fixed allocas are aligned more than the stack frame, align the |
| 1144 | // stack pointer accordingly. |
| 1145 | if (PrologEmitsFixedAllocas && |
| 1146 | FixedAllocaAlignBytes > Traits::X86_STACK_ALIGNMENT_BYTES) { |
| 1147 | assert(IsEbpBasedFrame); |
| 1148 | _and(getPhysicalRegister(getStackReg(), Traits::WordType), |
| 1149 | Ctx->getConstantInt32(-FixedAllocaAlignBytes)); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // Account for known-frame-offset alloca instructions that were not already |
| 1154 | // combined into the prolog. |
| 1155 | if (!PrologEmitsFixedAllocas) |
| 1156 | SpillAreaSizeBytes += FixedAllocaSizeBytes; |
| 1157 | |
| 1158 | Ctx->statsUpdateFrameBytes(SpillAreaSizeBytes); |
| 1159 | |
| 1160 | // Fill in stack offsets for stack args, and copy args into registers for |
| 1161 | // those that were register-allocated. Args are pushed right to left, so |
| 1162 | // Arg[0] is closest to the stack/frame pointer. |
| 1163 | Variable *FramePtr = |
| 1164 | getPhysicalRegister(getFrameOrStackReg(), Traits::WordType); |
| 1165 | size_t BasicFrameOffset = |
| 1166 | PreservedRegsSizeBytes + Traits::X86_RET_IP_SIZE_BYTES; |
| 1167 | if (!IsEbpBasedFrame) |
| 1168 | BasicFrameOffset += SpillAreaSizeBytes; |
| 1169 | |
| 1170 | emitGetIP(Node); |
| 1171 | |
| 1172 | const VarList &Args = Func->getArgs(); |
| 1173 | size_t InArgsSizeBytes = 0; |
| 1174 | unsigned NumXmmArgs = 0; |
| 1175 | unsigned NumGPRArgs = 0; |
| 1176 | for (Variable *Arg : Args) { |
| 1177 | // Skip arguments passed in registers. |
| 1178 | if (isVectorType(Arg->getType())) { |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1179 | if (Traits::getRegisterForXmmArgNum(NumXmmArgs).hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1180 | ++NumXmmArgs; |
| 1181 | continue; |
| 1182 | } |
| 1183 | } else if (isScalarFloatingType(Arg->getType())) { |
| 1184 | if (Traits::X86_PASS_SCALAR_FP_IN_XMM && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1185 | Traits::getRegisterForXmmArgNum(NumXmmArgs).hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1186 | ++NumXmmArgs; |
| 1187 | continue; |
| 1188 | } |
| 1189 | } else { |
| 1190 | assert(isScalarIntegerType(Arg->getType())); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1191 | if (Traits::getRegisterForGprArgNum(Traits::WordType, NumGPRArgs) |
| 1192 | .hasValue()) { |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1193 | ++NumGPRArgs; |
| 1194 | continue; |
| 1195 | } |
| 1196 | } |
| 1197 | // For esp-based frames where the allocas are done outside the prolog, the |
| 1198 | // esp value may not stabilize to its home value until after all the |
| 1199 | // fixed-size alloca instructions have executed. In this case, a stack |
| 1200 | // adjustment is needed when accessing in-args in order to copy them into |
| 1201 | // registers. |
| 1202 | size_t StackAdjBytes = 0; |
| 1203 | if (!IsEbpBasedFrame && !PrologEmitsFixedAllocas) |
| 1204 | StackAdjBytes -= FixedAllocaSizeBytes; |
| 1205 | finishArgumentLowering(Arg, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1206 | InArgsSizeBytes); |
| 1207 | } |
| 1208 | |
| 1209 | // Fill in stack offsets for locals. |
| 1210 | assignVarStackSlots(SortedSpilledVariables, SpillAreaPaddingBytes, |
| 1211 | SpillAreaSizeBytes, GlobalsAndSubsequentPaddingSize, |
| 1212 | IsEbpBasedFrame); |
| 1213 | // Assign stack offsets to variables that have been linked to spilled |
| 1214 | // variables. |
| 1215 | for (Variable *Var : VariablesLinkedToSpillSlots) { |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 1216 | const Variable *Root = Var->getLinkedToStackRoot(); |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 1217 | assert(Root != nullptr); |
| 1218 | Var->setStackOffset(Root->getStackOffset()); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1219 | } |
| 1220 | this->HasComputedFrame = true; |
| 1221 | |
| 1222 | if (BuildDefs::dump() && Func->isVerbose(IceV_Frame)) { |
| 1223 | OstreamLocker L(Func->getContext()); |
| 1224 | Ostream &Str = Func->getContext()->getStrDump(); |
| 1225 | |
| 1226 | Str << "Stack layout:\n"; |
| 1227 | uint32_t EspAdjustmentPaddingSize = |
| 1228 | SpillAreaSizeBytes - LocalsSpillAreaSize - |
| 1229 | GlobalsAndSubsequentPaddingSize - SpillAreaPaddingBytes - |
| 1230 | maxOutArgsSizeBytes(); |
| 1231 | Str << " in-args = " << InArgsSizeBytes << " bytes\n" |
| 1232 | << " return address = " << Traits::X86_RET_IP_SIZE_BYTES << " bytes\n" |
| 1233 | << " preserved registers = " << PreservedRegsSizeBytes << " bytes\n" |
| 1234 | << " spill area padding = " << SpillAreaPaddingBytes << " bytes\n" |
| 1235 | << " globals spill area = " << GlobalsSize << " bytes\n" |
| 1236 | << " globals-locals spill areas intermediate padding = " |
| 1237 | << GlobalsAndSubsequentPaddingSize - GlobalsSize << " bytes\n" |
| 1238 | << " locals spill area = " << LocalsSpillAreaSize << " bytes\n" |
| 1239 | << " esp alignment padding = " << EspAdjustmentPaddingSize |
| 1240 | << " bytes\n"; |
| 1241 | |
| 1242 | Str << "Stack details:\n" |
| 1243 | << " esp adjustment = " << SpillAreaSizeBytes << " bytes\n" |
| 1244 | << " spill area alignment = " << SpillAreaAlignmentBytes << " bytes\n" |
| 1245 | << " outgoing args size = " << maxOutArgsSizeBytes() << " bytes\n" |
| 1246 | << " locals spill area alignment = " << LocalsSlotsAlignmentBytes |
| 1247 | << " bytes\n" |
| 1248 | << " is ebp based = " << IsEbpBasedFrame << "\n"; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1252 | /// Helper function for addProlog(). |
| 1253 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1254 | /// This assumes Arg is an argument passed on the stack. This sets the frame |
| 1255 | /// offset for Arg and updates InArgsSizeBytes according to Arg's width. For an |
| 1256 | /// I64 arg that has been split into Lo and Hi components, it calls itself |
| 1257 | /// recursively on the components, taking care to handle Lo first because of the |
| 1258 | /// little-endian architecture. Lastly, this function generates an instruction |
| 1259 | /// to copy Arg into its assigned register if applicable. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1260 | template <typename TraitsType> |
| 1261 | void TargetX86Base<TraitsType>::finishArgumentLowering( |
| 1262 | Variable *Arg, Variable *FramePtr, size_t BasicFrameOffset, |
| 1263 | size_t StackAdjBytes, size_t &InArgsSizeBytes) { |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1264 | if (!Traits::Is64Bit) { |
| 1265 | if (auto *Arg64On32 = llvm::dyn_cast<Variable64On32>(Arg)) { |
| 1266 | Variable *Lo = Arg64On32->getLo(); |
| 1267 | Variable *Hi = Arg64On32->getHi(); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1268 | finishArgumentLowering(Lo, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1269 | InArgsSizeBytes); |
| 1270 | finishArgumentLowering(Hi, FramePtr, BasicFrameOffset, StackAdjBytes, |
| 1271 | InArgsSizeBytes); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1272 | return; |
| 1273 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1274 | } |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1275 | Type Ty = Arg->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1276 | if (isVectorType(Ty)) { |
| 1277 | InArgsSizeBytes = Traits::applyStackAlignment(InArgsSizeBytes); |
| 1278 | } |
| 1279 | Arg->setStackOffset(BasicFrameOffset + InArgsSizeBytes); |
| 1280 | InArgsSizeBytes += typeWidthInBytesOnStack(Ty); |
| 1281 | if (Arg->hasReg()) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1282 | assert(Ty != IceType_i64 || Traits::Is64Bit); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1283 | auto *Mem = X86OperandMem::create( |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1284 | Func, Ty, FramePtr, |
| 1285 | Ctx->getConstantInt32(Arg->getStackOffset() + StackAdjBytes)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1286 | if (isVectorType(Arg->getType())) { |
| 1287 | _movp(Arg, Mem); |
| 1288 | } else { |
| 1289 | _mov(Arg, Mem); |
| 1290 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1291 | // This argument-copying instruction uses an explicit X86OperandMem |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1292 | // operand instead of a Variable, so its fill-from-stack operation has to |
| 1293 | // be tracked separately for statistics. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1294 | Ctx->statsUpdateFills(); |
| 1295 | } |
| 1296 | } |
| 1297 | |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1298 | template <typename TraitsType> |
| 1299 | void TargetX86Base<TraitsType>::addEpilog(CfgNode *Node) { |
| 1300 | InstList &Insts = Node->getInsts(); |
| 1301 | InstList::reverse_iterator RI, E; |
| 1302 | for (RI = Insts.rbegin(), E = Insts.rend(); RI != E; ++RI) { |
| 1303 | if (llvm::isa<typename Traits::Insts::Ret>(*RI)) |
| 1304 | break; |
| 1305 | } |
| 1306 | if (RI == E) |
| 1307 | return; |
| 1308 | |
| 1309 | // Convert the reverse_iterator position into its corresponding (forward) |
| 1310 | // iterator position. |
| 1311 | InstList::iterator InsertPoint = RI.base(); |
| 1312 | --InsertPoint; |
| 1313 | Context.init(Node); |
| 1314 | Context.setInsertPoint(InsertPoint); |
| 1315 | |
| 1316 | if (IsEbpBasedFrame) { |
| 1317 | _unlink_bp(); |
| 1318 | } else { |
| 1319 | // add stackptr, SpillAreaSizeBytes |
| 1320 | if (SpillAreaSizeBytes != 0) { |
| 1321 | _add_sp(Ctx->getConstantInt32(SpillAreaSizeBytes)); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | // Add pop instructions for preserved registers. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1326 | SmallBitVector CalleeSaves = getRegisterSet(RegSet_CalleeSave, RegSet_None); |
| 1327 | SmallBitVector Popped(CalleeSaves.size()); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1328 | for (int32_t i = CalleeSaves.size() - 1; i >= 0; --i) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1329 | const auto RegNum = RegNumT::fromInt(i); |
| 1330 | if (RegNum == getFrameReg() && IsEbpBasedFrame) |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1331 | continue; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1332 | const RegNumT Canonical = Traits::getBaseReg(RegNum); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1333 | if (CalleeSaves[i] && RegsUsed[i]) { |
| 1334 | Popped[Canonical] = true; |
| 1335 | } |
| 1336 | } |
| 1337 | for (int32_t i = Popped.size() - 1; i >= 0; --i) { |
| 1338 | if (!Popped[i]) |
| 1339 | continue; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1340 | const auto RegNum = RegNumT::fromInt(i); |
| 1341 | assert(RegNum == Traits::getBaseReg(RegNum)); |
| 1342 | _pop(getPhysicalRegister(RegNum, Traits::WordType)); |
David Sehr | b9a404d | 2016-01-21 08:09:27 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | if (!NeedSandboxing) { |
| 1346 | return; |
| 1347 | } |
| 1348 | emitSandboxedReturn(); |
| 1349 | if (RI->getSrcSize()) { |
| 1350 | auto *RetValue = llvm::cast<Variable>(RI->getSrc(0)); |
| 1351 | Context.insert<InstFakeUse>(RetValue); |
| 1352 | } |
| 1353 | RI->setDeleted(); |
| 1354 | } |
| 1355 | |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 1356 | template <typename TraitsType> |
| 1357 | Inst *TargetX86Base<TraitsType>::createLoweredMove(Variable *Dest, |
| 1358 | Variable *SrcVar) { |
| 1359 | if (isVectorType(Dest->getType())) { |
| 1360 | return Traits::Insts::Movp::create(Func, Dest, SrcVar); |
| 1361 | } |
| 1362 | return Traits::Insts::Mov::create(Func, Dest, SrcVar); |
| 1363 | } |
| 1364 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1365 | template <typename TraitsType> Type TargetX86Base<TraitsType>::stackSlotType() { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1366 | return Traits::WordType; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1369 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1370 | template <typename T> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1371 | typename std::enable_if<!T::Is64Bit, Operand>::type * |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1372 | TargetX86Base<TraitsType>::loOperand(Operand *Operand) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1373 | assert(Operand->getType() == IceType_i64 || |
| 1374 | Operand->getType() == IceType_f64); |
| 1375 | if (Operand->getType() != IceType_i64 && Operand->getType() != IceType_f64) |
| 1376 | return Operand; |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1377 | if (auto *Var64On32 = llvm::dyn_cast<Variable64On32>(Operand)) |
| 1378 | return Var64On32->getLo(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1379 | if (auto *Const = llvm::dyn_cast<ConstantInteger64>(Operand)) { |
| 1380 | auto *ConstInt = llvm::dyn_cast<ConstantInteger32>( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1381 | Ctx->getConstantInt32(static_cast<int32_t>(Const->getValue()))); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1382 | // Check if we need to blind/pool the constant. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1383 | return legalize(ConstInt); |
| 1384 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1385 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Operand)) { |
| 1386 | auto *MemOperand = X86OperandMem::create( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1387 | Func, IceType_i32, Mem->getBase(), Mem->getOffset(), Mem->getIndex(), |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1388 | Mem->getShift(), Mem->getSegmentRegister(), Mem->getIsRebased()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1389 | // Test if we should randomize or pool the offset, if so randomize it or |
| 1390 | // pool it then create mem operand with the blinded/pooled constant. |
| 1391 | // Otherwise, return the mem operand as ordinary mem operand. |
| 1392 | return legalize(MemOperand); |
| 1393 | } |
| 1394 | llvm_unreachable("Unsupported operand type"); |
| 1395 | return nullptr; |
| 1396 | } |
| 1397 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1398 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1399 | template <typename T> |
| 1400 | typename std::enable_if<!T::Is64Bit, Operand>::type * |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1401 | TargetX86Base<TraitsType>::hiOperand(Operand *Operand) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1402 | assert(Operand->getType() == IceType_i64 || |
| 1403 | Operand->getType() == IceType_f64); |
| 1404 | if (Operand->getType() != IceType_i64 && Operand->getType() != IceType_f64) |
| 1405 | return Operand; |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 1406 | if (auto *Var64On32 = llvm::dyn_cast<Variable64On32>(Operand)) |
| 1407 | return Var64On32->getHi(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1408 | if (auto *Const = llvm::dyn_cast<ConstantInteger64>(Operand)) { |
| 1409 | auto *ConstInt = llvm::dyn_cast<ConstantInteger32>( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1410 | Ctx->getConstantInt32(static_cast<int32_t>(Const->getValue() >> 32))); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1411 | // Check if we need to blind/pool the constant. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1412 | return legalize(ConstInt); |
| 1413 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1414 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Operand)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1415 | Constant *Offset = Mem->getOffset(); |
| 1416 | if (Offset == nullptr) { |
| 1417 | Offset = Ctx->getConstantInt32(4); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1418 | } else if (auto *IntOffset = llvm::dyn_cast<ConstantInteger32>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1419 | Offset = Ctx->getConstantInt32(4 + IntOffset->getValue()); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 1420 | } else if (auto *SymOffset = llvm::dyn_cast<ConstantRelocatable>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1421 | assert(!Utils::WouldOverflowAdd(SymOffset->getOffset(), 4)); |
| 1422 | Offset = |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 1423 | Ctx->getConstantSym(4 + SymOffset->getOffset(), SymOffset->getName()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1424 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1425 | auto *MemOperand = X86OperandMem::create( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1426 | Func, IceType_i32, Mem->getBase(), Offset, Mem->getIndex(), |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1427 | Mem->getShift(), Mem->getSegmentRegister(), Mem->getIsRebased()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1428 | // Test if the Offset is an eligible i32 constants for randomization and |
| 1429 | // pooling. Blind/pool it if it is. Otherwise return as oridinary mem |
| 1430 | // operand. |
| 1431 | return legalize(MemOperand); |
| 1432 | } |
| 1433 | llvm_unreachable("Unsupported operand type"); |
| 1434 | return nullptr; |
| 1435 | } |
| 1436 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1437 | template <typename TraitsType> |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1438 | SmallBitVector |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1439 | TargetX86Base<TraitsType>::getRegisterSet(RegSetMask Include, |
| 1440 | RegSetMask Exclude) const { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1441 | return Traits::getRegisterSet(getFlags(), Include, Exclude); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1444 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1445 | void TargetX86Base<TraitsType>::lowerAlloca(const InstAlloca *Instr) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1446 | // Conservatively require the stack to be aligned. Some stack adjustment |
| 1447 | // operations implemented below assume that the stack is aligned before the |
| 1448 | // alloca. All the alloca code ensures that the stack alignment is preserved |
| 1449 | // after the alloca. The stack alignment restriction can be relaxed in some |
| 1450 | // cases. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1451 | NeedsStackAlignment = true; |
| 1452 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1453 | // For default align=0, set it to the real value 1, to avoid any |
| 1454 | // bit-manipulation problems below. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1455 | const uint32_t AlignmentParam = std::max(1u, Instr->getAlignInBytes()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1456 | |
| 1457 | // LLVM enforces power of 2 alignment. |
| 1458 | assert(llvm::isPowerOf2_32(AlignmentParam)); |
| 1459 | assert(llvm::isPowerOf2_32(Traits::X86_STACK_ALIGNMENT_BYTES)); |
| 1460 | |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1461 | const uint32_t Alignment = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1462 | std::max(AlignmentParam, Traits::X86_STACK_ALIGNMENT_BYTES); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1463 | const bool OverAligned = Alignment > Traits::X86_STACK_ALIGNMENT_BYTES; |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 1464 | const bool OptM1 = Func->getOptLevel() == Opt_m1; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1465 | const bool AllocaWithKnownOffset = Instr->getKnownFrameOffset(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1466 | const bool UseFramePointer = |
| 1467 | hasFramePointer() || OverAligned || !AllocaWithKnownOffset || OptM1; |
| 1468 | |
| 1469 | if (UseFramePointer) |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 1470 | setHasFramePointer(); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1471 | |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1472 | Variable *esp = getPhysicalRegister(getStackReg(), Traits::WordType); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1473 | if (OverAligned) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1474 | _and(esp, Ctx->getConstantInt32(-Alignment)); |
| 1475 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1476 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1477 | Variable *Dest = Instr->getDest(); |
| 1478 | Operand *TotalSize = legalize(Instr->getSizeInBytes()); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1479 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1480 | if (const auto *ConstantTotalSize = |
| 1481 | llvm::dyn_cast<ConstantInteger32>(TotalSize)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1482 | const uint32_t Value = |
| 1483 | Utils::applyAlignment(ConstantTotalSize->getValue(), Alignment); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1484 | if (UseFramePointer) { |
| 1485 | _sub_sp(Ctx->getConstantInt32(Value)); |
| 1486 | } else { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1487 | // If we don't need a Frame Pointer, this alloca has a known offset to the |
| 1488 | // stack pointer. We don't need adjust the stack pointer, nor assign any |
| 1489 | // value to Dest, as Dest is rematerializable. |
| 1490 | assert(Dest->isRematerializable()); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1491 | FixedAllocaSizeBytes += Value; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 1492 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 55f931f | 2015-09-23 16:33:08 -0700 | [diff] [blame] | 1493 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1494 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1495 | // Non-constant sizes need to be adjusted to the next highest multiple of |
| 1496 | // the required alignment at runtime. |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1497 | Variable *T = nullptr; |
| 1498 | if (Traits::Is64Bit && TotalSize->getType() != IceType_i64 && |
| 1499 | !NeedSandboxing) { |
| 1500 | T = makeReg(IceType_i64); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1501 | _movzx(T, TotalSize); |
| 1502 | } else { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1503 | T = makeReg(IceType_i32); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 1504 | _mov(T, TotalSize); |
| 1505 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1506 | _add(T, Ctx->getConstantInt32(Alignment - 1)); |
| 1507 | _and(T, Ctx->getConstantInt32(-Alignment)); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1508 | _sub_sp(T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1509 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1510 | // Add enough to the returned address to account for the out args area. |
| 1511 | uint32_t OutArgsSize = maxOutArgsSizeBytes(); |
| 1512 | if (OutArgsSize > 0) { |
| 1513 | Variable *T = makeReg(IceType_i32); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1514 | auto *CalculateOperand = X86OperandMem::create( |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1515 | Func, IceType_void, esp, Ctx->getConstantInt(IceType_i32, OutArgsSize)); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1516 | _lea(T, CalculateOperand); |
| 1517 | _mov(Dest, T); |
| 1518 | } else { |
| 1519 | _mov(Dest, esp); |
| 1520 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1523 | template <typename TraitsType> |
| 1524 | void TargetX86Base<TraitsType>::lowerArguments() { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1525 | const bool OptM1 = Func->getOptLevel() == Opt_m1; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1526 | VarList &Args = Func->getArgs(); |
| 1527 | unsigned NumXmmArgs = 0; |
| 1528 | bool XmmSlotsRemain = true; |
| 1529 | unsigned NumGprArgs = 0; |
| 1530 | bool GprSlotsRemain = true; |
| 1531 | |
| 1532 | Context.init(Func->getEntryNode()); |
| 1533 | Context.setInsertPoint(Context.getCur()); |
| 1534 | |
| 1535 | for (SizeT i = 0, End = Args.size(); |
| 1536 | i < End && (XmmSlotsRemain || GprSlotsRemain); ++i) { |
| 1537 | Variable *Arg = Args[i]; |
| 1538 | Type Ty = Arg->getType(); |
| 1539 | Variable *RegisterArg = nullptr; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1540 | RegNumT RegNum; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1541 | if (isVectorType(Ty)) { |
| 1542 | RegNum = Traits::getRegisterForXmmArgNum(NumXmmArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1543 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1544 | XmmSlotsRemain = false; |
| 1545 | continue; |
| 1546 | } |
| 1547 | ++NumXmmArgs; |
| 1548 | RegisterArg = Func->makeVariable(Ty); |
| 1549 | } else if (isScalarFloatingType(Ty)) { |
| 1550 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 1551 | continue; |
| 1552 | } |
| 1553 | RegNum = Traits::getRegisterForXmmArgNum(NumXmmArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1554 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1555 | XmmSlotsRemain = false; |
| 1556 | continue; |
| 1557 | } |
| 1558 | ++NumXmmArgs; |
| 1559 | RegisterArg = Func->makeVariable(Ty); |
| 1560 | } else if (isScalarIntegerType(Ty)) { |
| 1561 | RegNum = Traits::getRegisterForGprArgNum(Ty, NumGprArgs); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1562 | if (RegNum.hasNoValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1563 | GprSlotsRemain = false; |
| 1564 | continue; |
| 1565 | } |
| 1566 | ++NumGprArgs; |
| 1567 | RegisterArg = Func->makeVariable(Ty); |
| 1568 | } |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 1569 | assert(RegNum.hasValue()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1570 | assert(RegisterArg != nullptr); |
| 1571 | // Replace Arg in the argument list with the home register. Then generate |
| 1572 | // an instruction in the prolog to copy the home register to the assigned |
| 1573 | // location of Arg. |
| 1574 | if (BuildDefs::dump()) |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 1575 | RegisterArg->setName(Func, "home_reg:" + Arg->getName()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1576 | RegisterArg->setRegNum(RegNum); |
| 1577 | RegisterArg->setIsArg(); |
| 1578 | Arg->setIsArg(false); |
| 1579 | |
| 1580 | Args[i] = RegisterArg; |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1581 | // When not Om1, do the assignment through a temporary, instead of directly |
| 1582 | // from the pre-colored variable, so that a subsequent availabilityGet() |
| 1583 | // call has a chance to work. (In Om1, don't bother creating extra |
| 1584 | // instructions with extra variables to register-allocate.) |
| 1585 | if (OptM1) { |
| 1586 | Context.insert<InstAssign>(Arg, RegisterArg); |
| 1587 | } else { |
| 1588 | Variable *Tmp = makeReg(RegisterArg->getType()); |
| 1589 | Context.insert<InstAssign>(Tmp, RegisterArg); |
| 1590 | Context.insert<InstAssign>(Arg, Tmp); |
| 1591 | } |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1592 | } |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 1593 | if (!OptM1) |
| 1594 | Context.availabilityUpdate(); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1597 | /// Strength-reduce scalar integer multiplication by a constant (for i32 or |
| 1598 | /// narrower) for certain constants. The lea instruction can be used to multiply |
| 1599 | /// by 3, 5, or 9, and the lsh instruction can be used to multiply by powers of |
| 1600 | /// 2. These can be combined such that e.g. multiplying by 100 can be done as 2 |
| 1601 | /// lea-based multiplies by 5, combined with left-shifting by 2. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1602 | template <typename TraitsType> |
| 1603 | bool TargetX86Base<TraitsType>::optimizeScalarMul(Variable *Dest, Operand *Src0, |
| 1604 | int32_t Src1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1605 | // Disable this optimization for Om1 and O0, just to keep things simple |
| 1606 | // there. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 1607 | if (Func->getOptLevel() < Opt_1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1608 | return false; |
| 1609 | Type Ty = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1610 | if (Src1 == -1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1611 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1612 | _mov(T, Src0); |
| 1613 | _neg(T); |
| 1614 | _mov(Dest, T); |
| 1615 | return true; |
| 1616 | } |
| 1617 | if (Src1 == 0) { |
| 1618 | _mov(Dest, Ctx->getConstantZero(Ty)); |
| 1619 | return true; |
| 1620 | } |
| 1621 | if (Src1 == 1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1622 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1623 | _mov(T, Src0); |
| 1624 | _mov(Dest, T); |
| 1625 | return true; |
| 1626 | } |
| 1627 | // Don't bother with the edge case where Src1 == MININT. |
| 1628 | if (Src1 == -Src1) |
| 1629 | return false; |
| 1630 | const bool Src1IsNegative = Src1 < 0; |
| 1631 | if (Src1IsNegative) |
| 1632 | Src1 = -Src1; |
| 1633 | uint32_t Count9 = 0; |
| 1634 | uint32_t Count5 = 0; |
| 1635 | uint32_t Count3 = 0; |
| 1636 | uint32_t Count2 = 0; |
| 1637 | uint32_t CountOps = 0; |
| 1638 | while (Src1 > 1) { |
| 1639 | if (Src1 % 9 == 0) { |
| 1640 | ++CountOps; |
| 1641 | ++Count9; |
| 1642 | Src1 /= 9; |
| 1643 | } else if (Src1 % 5 == 0) { |
| 1644 | ++CountOps; |
| 1645 | ++Count5; |
| 1646 | Src1 /= 5; |
| 1647 | } else if (Src1 % 3 == 0) { |
| 1648 | ++CountOps; |
| 1649 | ++Count3; |
| 1650 | Src1 /= 3; |
| 1651 | } else if (Src1 % 2 == 0) { |
| 1652 | if (Count2 == 0) |
| 1653 | ++CountOps; |
| 1654 | ++Count2; |
| 1655 | Src1 /= 2; |
| 1656 | } else { |
| 1657 | return false; |
| 1658 | } |
| 1659 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1660 | // Lea optimization only works for i16 and i32 types, not i8. |
| 1661 | if (Ty != IceType_i32 && !(Traits::Is64Bit && Ty == IceType_i64) && |
| 1662 | (Count3 || Count5 || Count9)) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1663 | return false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1664 | // Limit the number of lea/shl operations for a single multiply, to a |
| 1665 | // somewhat arbitrary choice of 3. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1666 | constexpr uint32_t MaxOpsForOptimizedMul = 3; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1667 | if (CountOps > MaxOpsForOptimizedMul) |
| 1668 | return false; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1669 | Variable *T = makeReg(Traits::WordType); |
| 1670 | if (typeWidthInBytes(Src0->getType()) < typeWidthInBytes(T->getType())) { |
| 1671 | _movzx(T, Src0); |
| 1672 | } else { |
| 1673 | _mov(T, Src0); |
| 1674 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1675 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 1676 | for (uint32_t i = 0; i < Count9; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1677 | constexpr uint16_t Shift = 3; // log2(9-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1678 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1679 | } |
| 1680 | for (uint32_t i = 0; i < Count5; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1681 | constexpr uint16_t Shift = 2; // log2(5-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1682 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1683 | } |
| 1684 | for (uint32_t i = 0; i < Count3; ++i) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 1685 | constexpr uint16_t Shift = 1; // log2(3-1) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1686 | _lea(T, X86OperandMem::create(Func, IceType_void, T, Zero, T, Shift)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1687 | } |
| 1688 | if (Count2) { |
| 1689 | _shl(T, Ctx->getConstantInt(Ty, Count2)); |
| 1690 | } |
| 1691 | if (Src1IsNegative) |
| 1692 | _neg(T); |
| 1693 | _mov(Dest, T); |
| 1694 | return true; |
| 1695 | } |
| 1696 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1697 | template <typename TraitsType> |
| 1698 | void TargetX86Base<TraitsType>::lowerShift64(InstArithmetic::OpKind Op, |
| 1699 | Operand *Src0Lo, Operand *Src0Hi, |
| 1700 | Operand *Src1Lo, Variable *DestLo, |
| 1701 | Variable *DestHi) { |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1702 | // TODO: Refactor the similarities between Shl, Lshr, and Ashr. |
| 1703 | Variable *T_1 = nullptr, *T_2 = nullptr, *T_3 = nullptr; |
| 1704 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 1705 | Constant *SignExtend = Ctx->getConstantInt32(0x1f); |
| 1706 | if (auto *ConstantShiftAmount = llvm::dyn_cast<ConstantInteger32>(Src1Lo)) { |
| 1707 | uint32_t ShiftAmount = ConstantShiftAmount->getValue(); |
| 1708 | if (ShiftAmount > 32) { |
| 1709 | Constant *ReducedShift = Ctx->getConstantInt32(ShiftAmount - 32); |
| 1710 | switch (Op) { |
| 1711 | default: |
| 1712 | assert(0 && "non-shift op"); |
| 1713 | break; |
| 1714 | case InstArithmetic::Shl: { |
| 1715 | // a=b<<c ==> |
| 1716 | // t2 = b.lo |
| 1717 | // t2 = shl t2, ShiftAmount-32 |
| 1718 | // t3 = t2 |
| 1719 | // t2 = 0 |
| 1720 | _mov(T_2, Src0Lo); |
| 1721 | _shl(T_2, ReducedShift); |
| 1722 | _mov(DestHi, T_2); |
| 1723 | _mov(DestLo, Zero); |
| 1724 | } break; |
| 1725 | case InstArithmetic::Lshr: { |
| 1726 | // a=b>>c (unsigned) ==> |
| 1727 | // t2 = b.hi |
| 1728 | // t2 = shr t2, ShiftAmount-32 |
| 1729 | // a.lo = t2 |
| 1730 | // a.hi = 0 |
| 1731 | _mov(T_2, Src0Hi); |
| 1732 | _shr(T_2, ReducedShift); |
| 1733 | _mov(DestLo, T_2); |
| 1734 | _mov(DestHi, Zero); |
| 1735 | } break; |
| 1736 | case InstArithmetic::Ashr: { |
| 1737 | // a=b>>c (signed) ==> |
| 1738 | // t3 = b.hi |
| 1739 | // t3 = sar t3, 0x1f |
| 1740 | // t2 = b.hi |
| 1741 | // t2 = shrd t2, t3, ShiftAmount-32 |
| 1742 | // a.lo = t2 |
| 1743 | // a.hi = t3 |
| 1744 | _mov(T_3, Src0Hi); |
| 1745 | _sar(T_3, SignExtend); |
| 1746 | _mov(T_2, Src0Hi); |
| 1747 | _shrd(T_2, T_3, ReducedShift); |
| 1748 | _mov(DestLo, T_2); |
| 1749 | _mov(DestHi, T_3); |
| 1750 | } break; |
| 1751 | } |
| 1752 | } else if (ShiftAmount == 32) { |
| 1753 | switch (Op) { |
| 1754 | default: |
| 1755 | assert(0 && "non-shift op"); |
| 1756 | break; |
| 1757 | case InstArithmetic::Shl: { |
| 1758 | // a=b<<c ==> |
| 1759 | // t2 = b.lo |
| 1760 | // a.hi = t2 |
| 1761 | // a.lo = 0 |
| 1762 | _mov(T_2, Src0Lo); |
| 1763 | _mov(DestHi, T_2); |
| 1764 | _mov(DestLo, Zero); |
| 1765 | } break; |
| 1766 | case InstArithmetic::Lshr: { |
| 1767 | // a=b>>c (unsigned) ==> |
| 1768 | // t2 = b.hi |
| 1769 | // a.lo = t2 |
| 1770 | // a.hi = 0 |
| 1771 | _mov(T_2, Src0Hi); |
| 1772 | _mov(DestLo, T_2); |
| 1773 | _mov(DestHi, Zero); |
| 1774 | } break; |
| 1775 | case InstArithmetic::Ashr: { |
| 1776 | // a=b>>c (signed) ==> |
| 1777 | // t2 = b.hi |
| 1778 | // a.lo = t2 |
| 1779 | // t3 = b.hi |
| 1780 | // t3 = sar t3, 0x1f |
| 1781 | // a.hi = t3 |
| 1782 | _mov(T_2, Src0Hi); |
| 1783 | _mov(DestLo, T_2); |
| 1784 | _mov(T_3, Src0Hi); |
| 1785 | _sar(T_3, SignExtend); |
| 1786 | _mov(DestHi, T_3); |
| 1787 | } break; |
| 1788 | } |
| 1789 | } else { |
| 1790 | // COMMON PREFIX OF: a=b SHIFT_OP c ==> |
| 1791 | // t2 = b.lo |
| 1792 | // t3 = b.hi |
| 1793 | _mov(T_2, Src0Lo); |
| 1794 | _mov(T_3, Src0Hi); |
| 1795 | switch (Op) { |
| 1796 | default: |
| 1797 | assert(0 && "non-shift op"); |
| 1798 | break; |
| 1799 | case InstArithmetic::Shl: { |
| 1800 | // a=b<<c ==> |
| 1801 | // t3 = shld t3, t2, ShiftAmount |
| 1802 | // t2 = shl t2, ShiftAmount |
| 1803 | _shld(T_3, T_2, ConstantShiftAmount); |
| 1804 | _shl(T_2, ConstantShiftAmount); |
| 1805 | } break; |
| 1806 | case InstArithmetic::Lshr: { |
| 1807 | // a=b>>c (unsigned) ==> |
| 1808 | // t2 = shrd t2, t3, ShiftAmount |
| 1809 | // t3 = shr t3, ShiftAmount |
| 1810 | _shrd(T_2, T_3, ConstantShiftAmount); |
| 1811 | _shr(T_3, ConstantShiftAmount); |
| 1812 | } break; |
| 1813 | case InstArithmetic::Ashr: { |
| 1814 | // a=b>>c (signed) ==> |
| 1815 | // t2 = shrd t2, t3, ShiftAmount |
| 1816 | // t3 = sar t3, ShiftAmount |
| 1817 | _shrd(T_2, T_3, ConstantShiftAmount); |
| 1818 | _sar(T_3, ConstantShiftAmount); |
| 1819 | } break; |
| 1820 | } |
| 1821 | // COMMON SUFFIX OF: a=b SHIFT_OP c ==> |
| 1822 | // a.lo = t2 |
| 1823 | // a.hi = t3 |
| 1824 | _mov(DestLo, T_2); |
| 1825 | _mov(DestHi, T_3); |
| 1826 | } |
| 1827 | } else { |
| 1828 | // NON-CONSTANT CASES. |
| 1829 | Constant *BitTest = Ctx->getConstantInt32(0x20); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1830 | InstX86Label *Label = InstX86Label::create(Func, this); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1831 | // COMMON PREFIX OF: a=b SHIFT_OP c ==> |
| 1832 | // t1:ecx = c.lo & 0xff |
| 1833 | // t2 = b.lo |
| 1834 | // t3 = b.hi |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1835 | T_1 = copyToReg8(Src1Lo, Traits::RegisterSet::Reg_cl); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1836 | _mov(T_2, Src0Lo); |
| 1837 | _mov(T_3, Src0Hi); |
| 1838 | switch (Op) { |
| 1839 | default: |
| 1840 | assert(0 && "non-shift op"); |
| 1841 | break; |
| 1842 | case InstArithmetic::Shl: { |
| 1843 | // a=b<<c ==> |
| 1844 | // t3 = shld t3, t2, t1 |
| 1845 | // t2 = shl t2, t1 |
| 1846 | // test t1, 0x20 |
| 1847 | // je L1 |
| 1848 | // use(t3) |
| 1849 | // t3 = t2 |
| 1850 | // t2 = 0 |
| 1851 | _shld(T_3, T_2, T_1); |
| 1852 | _shl(T_2, T_1); |
| 1853 | _test(T_1, BitTest); |
| 1854 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1855 | // 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] | 1856 | // flow, so we need to use _redefined to avoid liveness problems. |
| 1857 | _redefined(_mov(T_3, T_2)); |
| 1858 | _redefined(_mov(T_2, Zero)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1859 | } break; |
| 1860 | case InstArithmetic::Lshr: { |
| 1861 | // a=b>>c (unsigned) ==> |
| 1862 | // t2 = shrd t2, t3, t1 |
| 1863 | // t3 = shr t3, t1 |
| 1864 | // test t1, 0x20 |
| 1865 | // je L1 |
| 1866 | // use(t2) |
| 1867 | // t2 = t3 |
| 1868 | // t3 = 0 |
| 1869 | _shrd(T_2, T_3, T_1); |
| 1870 | _shr(T_3, T_1); |
| 1871 | _test(T_1, BitTest); |
| 1872 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1873 | // 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] | 1874 | // flow, so we need to use _redefined to avoid liveness problems. |
| 1875 | _redefined(_mov(T_2, T_3)); |
| 1876 | _redefined(_mov(T_3, Zero)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1877 | } break; |
| 1878 | case InstArithmetic::Ashr: { |
| 1879 | // a=b>>c (signed) ==> |
| 1880 | // t2 = shrd t2, t3, t1 |
| 1881 | // t3 = sar t3, t1 |
| 1882 | // test t1, 0x20 |
| 1883 | // je L1 |
| 1884 | // use(t2) |
| 1885 | // t2 = t3 |
| 1886 | // t3 = sar t3, 0x1f |
| 1887 | Constant *SignExtend = Ctx->getConstantInt32(0x1f); |
| 1888 | _shrd(T_2, T_3, T_1); |
| 1889 | _sar(T_3, T_1); |
| 1890 | _test(T_1, BitTest); |
| 1891 | _br(Traits::Cond::Br_e, Label); |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1892 | // 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] | 1893 | // flow, so T_2 needs to use _redefined to avoid liveness problems. T_3 |
| 1894 | // doesn't need special treatment because it is reassigned via _sar |
| 1895 | // instead of _mov. |
| 1896 | _redefined(_mov(T_2, T_3)); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 1897 | _sar(T_3, SignExtend); |
| 1898 | } break; |
| 1899 | } |
| 1900 | // COMMON SUFFIX OF: a=b SHIFT_OP c ==> |
| 1901 | // L1: |
| 1902 | // a.lo = t2 |
| 1903 | // a.hi = t3 |
| 1904 | Context.insert(Label); |
| 1905 | _mov(DestLo, T_2); |
| 1906 | _mov(DestHi, T_3); |
| 1907 | } |
| 1908 | } |
| 1909 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1910 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1911 | void TargetX86Base<TraitsType>::lowerArithmetic(const InstArithmetic *Instr) { |
| 1912 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 1913 | if (Dest->isRematerializable()) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 1914 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 1915 | return; |
| 1916 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1917 | Type Ty = Dest->getType(); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1918 | Operand *Src0 = legalize(Instr->getSrc(0)); |
| 1919 | Operand *Src1 = legalize(Instr->getSrc(1)); |
| 1920 | if (Instr->isCommutative()) { |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1921 | uint32_t SwapCount = 0; |
| 1922 | if (!llvm::isa<Variable>(Src0) && llvm::isa<Variable>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1923 | std::swap(Src0, Src1); |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1924 | ++SwapCount; |
| 1925 | } |
| 1926 | if (llvm::isa<Constant>(Src0) && !llvm::isa<Constant>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1927 | std::swap(Src0, Src1); |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1928 | ++SwapCount; |
| 1929 | } |
| 1930 | // Improve two-address code patterns by avoiding a copy to the dest |
| 1931 | // register when one of the source operands ends its lifetime here. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1932 | if (!Instr->isLastUse(Src0) && Instr->isLastUse(Src1)) { |
David Sehr | 487bad0 | 2015-10-06 17:41:26 -0700 | [diff] [blame] | 1933 | std::swap(Src0, Src1); |
| 1934 | ++SwapCount; |
| 1935 | } |
| 1936 | assert(SwapCount <= 1); |
Karl Schimpf | a313a12 | 2015-10-08 10:40:57 -0700 | [diff] [blame] | 1937 | (void)SwapCount; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1938 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 1939 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 1940 | // These x86-32 helper-call-involved instructions are lowered in this |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1941 | // separate switch. This is because loOperand() and hiOperand() may insert |
| 1942 | // redundant instructions for constant blinding and pooling. Such redundant |
| 1943 | // instructions will fail liveness analysis under -Om1 setting. And, |
| 1944 | // actually these arguments do not need to be processed with loOperand() |
| 1945 | // and hiOperand() to be used. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1946 | switch (Instr->getOp()) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1947 | case InstArithmetic::Udiv: |
| 1948 | case InstArithmetic::Sdiv: |
| 1949 | case InstArithmetic::Urem: |
| 1950 | case InstArithmetic::Srem: |
| 1951 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1952 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1953 | default: |
| 1954 | break; |
| 1955 | } |
| 1956 | |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 1957 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 1958 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1959 | Operand *Src0Lo = loOperand(Src0); |
| 1960 | Operand *Src0Hi = hiOperand(Src0); |
| 1961 | Operand *Src1Lo = loOperand(Src1); |
| 1962 | Operand *Src1Hi = hiOperand(Src1); |
| 1963 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1964 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 1965 | case InstArithmetic::_num: |
| 1966 | llvm_unreachable("Unknown arithmetic operator"); |
| 1967 | break; |
| 1968 | case InstArithmetic::Add: |
| 1969 | _mov(T_Lo, Src0Lo); |
| 1970 | _add(T_Lo, Src1Lo); |
| 1971 | _mov(DestLo, T_Lo); |
| 1972 | _mov(T_Hi, Src0Hi); |
| 1973 | _adc(T_Hi, Src1Hi); |
| 1974 | _mov(DestHi, T_Hi); |
| 1975 | break; |
| 1976 | case InstArithmetic::And: |
| 1977 | _mov(T_Lo, Src0Lo); |
| 1978 | _and(T_Lo, Src1Lo); |
| 1979 | _mov(DestLo, T_Lo); |
| 1980 | _mov(T_Hi, Src0Hi); |
| 1981 | _and(T_Hi, Src1Hi); |
| 1982 | _mov(DestHi, T_Hi); |
| 1983 | break; |
| 1984 | case InstArithmetic::Or: |
| 1985 | _mov(T_Lo, Src0Lo); |
| 1986 | _or(T_Lo, Src1Lo); |
| 1987 | _mov(DestLo, T_Lo); |
| 1988 | _mov(T_Hi, Src0Hi); |
| 1989 | _or(T_Hi, Src1Hi); |
| 1990 | _mov(DestHi, T_Hi); |
| 1991 | break; |
| 1992 | case InstArithmetic::Xor: |
| 1993 | _mov(T_Lo, Src0Lo); |
| 1994 | _xor(T_Lo, Src1Lo); |
| 1995 | _mov(DestLo, T_Lo); |
| 1996 | _mov(T_Hi, Src0Hi); |
| 1997 | _xor(T_Hi, Src1Hi); |
| 1998 | _mov(DestHi, T_Hi); |
| 1999 | break; |
| 2000 | case InstArithmetic::Sub: |
| 2001 | _mov(T_Lo, Src0Lo); |
| 2002 | _sub(T_Lo, Src1Lo); |
| 2003 | _mov(DestLo, T_Lo); |
| 2004 | _mov(T_Hi, Src0Hi); |
| 2005 | _sbb(T_Hi, Src1Hi); |
| 2006 | _mov(DestHi, T_Hi); |
| 2007 | break; |
| 2008 | case InstArithmetic::Mul: { |
| 2009 | Variable *T_1 = nullptr, *T_2 = nullptr, *T_3 = nullptr; |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 2010 | Variable *T_4Lo = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 2011 | Variable *T_4Hi = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2012 | // gcc does the following: |
| 2013 | // a=b*c ==> |
| 2014 | // t1 = b.hi; t1 *=(imul) c.lo |
| 2015 | // t2 = c.hi; t2 *=(imul) b.lo |
| 2016 | // t3:eax = b.lo |
| 2017 | // t4.hi:edx,t4.lo:eax = t3:eax *(mul) c.lo |
| 2018 | // a.lo = t4.lo |
| 2019 | // t4.hi += t1 |
| 2020 | // t4.hi += t2 |
| 2021 | // a.hi = t4.hi |
| 2022 | // The mul instruction cannot take an immediate operand. |
| 2023 | Src1Lo = legalize(Src1Lo, Legal_Reg | Legal_Mem); |
| 2024 | _mov(T_1, Src0Hi); |
| 2025 | _imul(T_1, Src1Lo); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 2026 | _mov(T_3, Src0Lo, Traits::RegisterSet::Reg_eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2027 | _mul(T_4Lo, T_3, Src1Lo); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2028 | // The mul instruction produces two dest variables, edx:eax. We create a |
| 2029 | // fake definition of edx to account for this. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 2030 | Context.insert<InstFakeDef>(T_4Hi, T_4Lo); |
Jim Stichnoth | 28df6ba | 2016-02-05 15:43:24 -0800 | [diff] [blame] | 2031 | Context.insert<InstFakeUse>(T_4Hi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2032 | _mov(DestLo, T_4Lo); |
| 2033 | _add(T_4Hi, T_1); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 2034 | _mov(T_2, Src1Hi); |
| 2035 | _imul(T_2, Src0Lo); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2036 | _add(T_4Hi, T_2); |
| 2037 | _mov(DestHi, T_4Hi); |
| 2038 | } break; |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 2039 | case InstArithmetic::Shl: |
| 2040 | case InstArithmetic::Lshr: |
| 2041 | case InstArithmetic::Ashr: |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2042 | lowerShift64(Instr->getOp(), Src0Lo, Src0Hi, Src1Lo, DestLo, DestHi); |
David Sehr | 188eae5 | 2015-09-24 11:42:55 -0700 | [diff] [blame] | 2043 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2044 | case InstArithmetic::Fadd: |
| 2045 | case InstArithmetic::Fsub: |
| 2046 | case InstArithmetic::Fmul: |
| 2047 | case InstArithmetic::Fdiv: |
| 2048 | case InstArithmetic::Frem: |
| 2049 | llvm_unreachable("FP instruction with i64 type"); |
| 2050 | break; |
| 2051 | case InstArithmetic::Udiv: |
| 2052 | case InstArithmetic::Sdiv: |
| 2053 | case InstArithmetic::Urem: |
| 2054 | case InstArithmetic::Srem: |
| 2055 | llvm_unreachable("Call-helper-involved instruction for i64 type \ |
| 2056 | should have already been handled before"); |
| 2057 | break; |
| 2058 | } |
| 2059 | return; |
| 2060 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2061 | if (isVectorType(Ty)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2062 | // TODO: Trap on integer divide and integer modulo by zero. See: |
| 2063 | // https://code.google.com/p/nativeclient/issues/detail?id=3899 |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2064 | if (llvm::isa<X86OperandMem>(Src1)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 2065 | Src1 = legalizeToReg(Src1); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2066 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2067 | case InstArithmetic::_num: |
| 2068 | llvm_unreachable("Unknown arithmetic operator"); |
| 2069 | break; |
| 2070 | case InstArithmetic::Add: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2071 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2072 | _movp(T, Src0); |
| 2073 | _padd(T, Src1); |
| 2074 | _movp(Dest, T); |
| 2075 | } break; |
| 2076 | case InstArithmetic::And: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2077 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2078 | _movp(T, Src0); |
| 2079 | _pand(T, Src1); |
| 2080 | _movp(Dest, T); |
| 2081 | } break; |
| 2082 | case InstArithmetic::Or: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2083 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2084 | _movp(T, Src0); |
| 2085 | _por(T, Src1); |
| 2086 | _movp(Dest, T); |
| 2087 | } break; |
| 2088 | case InstArithmetic::Xor: { |
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); |
| 2091 | _pxor(T, Src1); |
| 2092 | _movp(Dest, T); |
| 2093 | } break; |
| 2094 | case InstArithmetic::Sub: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2095 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2096 | _movp(T, Src0); |
| 2097 | _psub(T, Src1); |
| 2098 | _movp(Dest, T); |
| 2099 | } break; |
| 2100 | case InstArithmetic::Mul: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2101 | bool TypesAreValidForPmull = Ty == IceType_v4i32 || Ty == IceType_v8i16; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2102 | bool InstructionSetIsValidForPmull = |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2103 | Ty == IceType_v8i16 || InstructionSet >= Traits::SSE4_1; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2104 | if (TypesAreValidForPmull && InstructionSetIsValidForPmull) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2105 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2106 | _movp(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2107 | _pmull(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2108 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2109 | } else if (Ty == IceType_v4i32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2110 | // Lowering sequence: |
| 2111 | // Note: The mask arguments have index 0 on the left. |
| 2112 | // |
| 2113 | // movups T1, Src0 |
| 2114 | // pshufd T2, Src0, {1,0,3,0} |
| 2115 | // pshufd T3, Src1, {1,0,3,0} |
| 2116 | // # T1 = {Src0[0] * Src1[0], Src0[2] * Src1[2]} |
| 2117 | // pmuludq T1, Src1 |
| 2118 | // # T2 = {Src0[1] * Src1[1], Src0[3] * Src1[3]} |
| 2119 | // pmuludq T2, T3 |
| 2120 | // # T1 = {lo(T1[0]), lo(T1[2]), lo(T2[0]), lo(T2[2])} |
| 2121 | // shufps T1, T2, {0,2,0,2} |
| 2122 | // pshufd T4, T1, {0,2,1,3} |
| 2123 | // movups Dest, T4 |
| 2124 | |
| 2125 | // Mask that directs pshufd to create a vector with entries |
| 2126 | // Src[1, 0, 3, 0] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2127 | constexpr unsigned Constant1030 = 0x31; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2128 | Constant *Mask1030 = Ctx->getConstantInt32(Constant1030); |
| 2129 | // Mask that directs shufps to create a vector with entries |
| 2130 | // Dest[0, 2], Src[0, 2] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2131 | constexpr unsigned Mask0202 = 0x88; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2132 | // Mask that directs pshufd to create a vector with entries |
| 2133 | // Src[0, 2, 1, 3] |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2134 | constexpr unsigned Mask0213 = 0xd8; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2135 | Variable *T1 = makeReg(IceType_v4i32); |
| 2136 | Variable *T2 = makeReg(IceType_v4i32); |
| 2137 | Variable *T3 = makeReg(IceType_v4i32); |
| 2138 | Variable *T4 = makeReg(IceType_v4i32); |
| 2139 | _movp(T1, Src0); |
| 2140 | _pshufd(T2, Src0, Mask1030); |
| 2141 | _pshufd(T3, Src1, Mask1030); |
| 2142 | _pmuludq(T1, Src1); |
| 2143 | _pmuludq(T2, T3); |
| 2144 | _shufps(T1, T2, Ctx->getConstantInt32(Mask0202)); |
| 2145 | _pshufd(T4, T1, Ctx->getConstantInt32(Mask0213)); |
| 2146 | _movp(Dest, T4); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2147 | } else if (Ty == IceType_v16i8) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2148 | llvm::report_fatal_error("Scalarized operation was expected"); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2149 | } else { |
| 2150 | llvm::report_fatal_error("Invalid vector multiply type"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } break; |
| 2153 | case InstArithmetic::Shl: |
| 2154 | case InstArithmetic::Lshr: |
| 2155 | case InstArithmetic::Ashr: |
| 2156 | case InstArithmetic::Udiv: |
| 2157 | case InstArithmetic::Urem: |
| 2158 | case InstArithmetic::Sdiv: |
| 2159 | case InstArithmetic::Srem: |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2160 | llvm::report_fatal_error("Scalarized operation was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2161 | break; |
| 2162 | case InstArithmetic::Fadd: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2163 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2164 | _movp(T, Src0); |
| 2165 | _addps(T, Src1); |
| 2166 | _movp(Dest, T); |
| 2167 | } break; |
| 2168 | case InstArithmetic::Fsub: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2169 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2170 | _movp(T, Src0); |
| 2171 | _subps(T, Src1); |
| 2172 | _movp(Dest, T); |
| 2173 | } break; |
| 2174 | case InstArithmetic::Fmul: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2175 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2176 | _movp(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2177 | _mulps(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2178 | _movp(Dest, T); |
| 2179 | } break; |
| 2180 | case InstArithmetic::Fdiv: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2181 | Variable *T = makeReg(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2182 | _movp(T, Src0); |
| 2183 | _divps(T, Src1); |
| 2184 | _movp(Dest, T); |
| 2185 | } break; |
| 2186 | case InstArithmetic::Frem: |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2187 | llvm::report_fatal_error("Scalarized operation was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2188 | break; |
| 2189 | } |
| 2190 | return; |
| 2191 | } |
| 2192 | Variable *T_edx = nullptr; |
| 2193 | Variable *T = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2194 | switch (Instr->getOp()) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2195 | case InstArithmetic::_num: |
| 2196 | llvm_unreachable("Unknown arithmetic operator"); |
| 2197 | break; |
| 2198 | case InstArithmetic::Add: |
| 2199 | _mov(T, Src0); |
| 2200 | _add(T, Src1); |
| 2201 | _mov(Dest, T); |
| 2202 | break; |
| 2203 | case InstArithmetic::And: |
| 2204 | _mov(T, Src0); |
| 2205 | _and(T, Src1); |
| 2206 | _mov(Dest, T); |
| 2207 | break; |
| 2208 | case InstArithmetic::Or: |
| 2209 | _mov(T, Src0); |
| 2210 | _or(T, Src1); |
| 2211 | _mov(Dest, T); |
| 2212 | break; |
| 2213 | case InstArithmetic::Xor: |
| 2214 | _mov(T, Src0); |
| 2215 | _xor(T, Src1); |
| 2216 | _mov(Dest, T); |
| 2217 | break; |
| 2218 | case InstArithmetic::Sub: |
| 2219 | _mov(T, Src0); |
| 2220 | _sub(T, Src1); |
| 2221 | _mov(Dest, T); |
| 2222 | break; |
| 2223 | case InstArithmetic::Mul: |
| 2224 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
| 2225 | if (optimizeScalarMul(Dest, Src0, C->getValue())) |
| 2226 | return; |
| 2227 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2228 | // 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] | 2229 | // be in al. |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2230 | if (isByteSizedArithType(Ty)) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2231 | _mov(T, Src0, Traits::RegisterSet::Reg_al); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2232 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2233 | _imul(T, Src0 == Src1 ? T : Src1); |
| 2234 | _mov(Dest, T); |
| 2235 | } else if (auto *ImmConst = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2236 | T = makeReg(Ty); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2237 | _imul_imm(T, Src0, ImmConst); |
| 2238 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2239 | } else { |
| 2240 | _mov(T, Src0); |
David Sehr | e11f878 | 2015-10-06 10:26:57 -0700 | [diff] [blame] | 2241 | _imul(T, Src0 == Src1 ? T : Src1); |
| 2242 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2243 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2244 | break; |
| 2245 | case InstArithmetic::Shl: |
| 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 | _shl(T, Src1); |
| 2251 | _mov(Dest, T); |
| 2252 | break; |
| 2253 | case InstArithmetic::Lshr: |
| 2254 | _mov(T, Src0); |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 2255 | if (!llvm::isa<ConstantInteger32>(Src1) && |
| 2256 | !llvm::isa<ConstantInteger64>(Src1)) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2257 | Src1 = copyToReg8(Src1, Traits::RegisterSet::Reg_cl); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2258 | _shr(T, Src1); |
| 2259 | _mov(Dest, T); |
| 2260 | break; |
| 2261 | case InstArithmetic::Ashr: |
| 2262 | _mov(T, Src0); |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 2263 | if (!llvm::isa<ConstantInteger32>(Src1) && |
| 2264 | !llvm::isa<ConstantInteger64>(Src1)) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2265 | Src1 = copyToReg8(Src1, Traits::RegisterSet::Reg_cl); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2266 | _sar(T, Src1); |
| 2267 | _mov(Dest, T); |
| 2268 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2269 | case InstArithmetic::Udiv: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2270 | // div and idiv are the few arithmetic operators that do not allow |
| 2271 | // immediates as the operand. |
| 2272 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2273 | RegNumT Eax; |
| 2274 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2275 | switch (Ty) { |
| 2276 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2277 | llvm::report_fatal_error("Bad type for udiv"); |
| 2278 | case IceType_i64: |
| 2279 | Eax = Traits::getRaxOrDie(); |
| 2280 | Edx = Traits::getRdxOrDie(); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2281 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2282 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2283 | Eax = Traits::RegisterSet::Reg_eax; |
| 2284 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2285 | break; |
| 2286 | case IceType_i16: |
| 2287 | Eax = Traits::RegisterSet::Reg_ax; |
| 2288 | Edx = Traits::RegisterSet::Reg_dx; |
| 2289 | break; |
| 2290 | case IceType_i8: |
| 2291 | Eax = Traits::RegisterSet::Reg_al; |
| 2292 | Edx = Traits::RegisterSet::Reg_ah; |
| 2293 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2294 | } |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2295 | T_edx = makeReg(Ty, Edx); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2296 | _mov(T, Src0, Eax); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 2297 | _mov(T_edx, Ctx->getConstantZero(Ty)); |
Jim Stichnoth | 017a553 | 2016-07-19 06:35:52 -0700 | [diff] [blame] | 2298 | _div(T_edx, Src1, T); |
| 2299 | _redefined(Context.insert<InstFakeDef>(T, T_edx)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2300 | _mov(Dest, T); |
| 2301 | } break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2302 | case InstArithmetic::Sdiv: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2303 | // TODO(stichnot): Enable this after doing better performance and cross |
| 2304 | // testing. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 2305 | if (false && Func->getOptLevel() >= Opt_1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2306 | // Optimize division by constant power of 2, but not for Om1 or O0, just |
| 2307 | // to keep things simple there. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2308 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2309 | const int32_t Divisor = C->getValue(); |
| 2310 | const uint32_t UDivisor = Divisor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2311 | if (Divisor > 0 && llvm::isPowerOf2_32(UDivisor)) { |
| 2312 | uint32_t LogDiv = llvm::Log2_32(UDivisor); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2313 | // LLVM does the following for dest=src/(1<<log): |
| 2314 | // t=src |
| 2315 | // sar t,typewidth-1 // -1 if src is negative, 0 if not |
| 2316 | // shr t,typewidth-log |
| 2317 | // add t,src |
| 2318 | // sar t,log |
| 2319 | // dest=t |
| 2320 | uint32_t TypeWidth = Traits::X86_CHAR_BIT * typeWidthInBytes(Ty); |
| 2321 | _mov(T, Src0); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2322 | // If for some reason we are dividing by 1, just treat it like an |
| 2323 | // assignment. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2324 | if (LogDiv > 0) { |
| 2325 | // The initial sar is unnecessary when dividing by 2. |
| 2326 | if (LogDiv > 1) |
| 2327 | _sar(T, Ctx->getConstantInt(Ty, TypeWidth - 1)); |
| 2328 | _shr(T, Ctx->getConstantInt(Ty, TypeWidth - LogDiv)); |
| 2329 | _add(T, Src0); |
| 2330 | _sar(T, Ctx->getConstantInt(Ty, LogDiv)); |
| 2331 | } |
| 2332 | _mov(Dest, T); |
| 2333 | return; |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2338 | switch (Ty) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2339 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2340 | llvm::report_fatal_error("Bad type for sdiv"); |
| 2341 | case IceType_i64: |
| 2342 | T_edx = makeReg(Ty, Traits::getRdxOrDie()); |
| 2343 | _mov(T, Src0, Traits::getRaxOrDie()); |
| 2344 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2345 | case IceType_i32: |
| 2346 | T_edx = makeReg(Ty, Traits::RegisterSet::Reg_edx); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 2347 | _mov(T, Src0, Traits::RegisterSet::Reg_eax); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2348 | break; |
| 2349 | case IceType_i16: |
| 2350 | T_edx = makeReg(Ty, Traits::RegisterSet::Reg_dx); |
| 2351 | _mov(T, Src0, Traits::RegisterSet::Reg_ax); |
| 2352 | break; |
| 2353 | case IceType_i8: |
| 2354 | T_edx = makeReg(IceType_i16, Traits::RegisterSet::Reg_ax); |
| 2355 | _mov(T, Src0, Traits::RegisterSet::Reg_al); |
| 2356 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2357 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2358 | _cbwdq(T_edx, T); |
Jim Stichnoth | 017a553 | 2016-07-19 06:35:52 -0700 | [diff] [blame] | 2359 | _idiv(T_edx, Src1, T); |
| 2360 | _redefined(Context.insert<InstFakeDef>(T, T_edx)); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2361 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2362 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2363 | case InstArithmetic::Urem: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2364 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2365 | RegNumT Eax; |
| 2366 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2367 | switch (Ty) { |
| 2368 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2369 | llvm::report_fatal_error("Bad type for urem"); |
| 2370 | case IceType_i64: |
| 2371 | Eax = Traits::getRaxOrDie(); |
| 2372 | Edx = Traits::getRdxOrDie(); |
| 2373 | break; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2374 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2375 | Eax = Traits::RegisterSet::Reg_eax; |
| 2376 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2377 | break; |
| 2378 | case IceType_i16: |
| 2379 | Eax = Traits::RegisterSet::Reg_ax; |
| 2380 | Edx = Traits::RegisterSet::Reg_dx; |
| 2381 | break; |
| 2382 | case IceType_i8: |
| 2383 | Eax = Traits::RegisterSet::Reg_al; |
| 2384 | Edx = Traits::RegisterSet::Reg_ah; |
| 2385 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2386 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2387 | T_edx = makeReg(Ty, Edx); |
| 2388 | _mov(T_edx, Ctx->getConstantZero(Ty)); |
| 2389 | _mov(T, Src0, Eax); |
Jim Stichnoth | 017a553 | 2016-07-19 06:35:52 -0700 | [diff] [blame] | 2390 | _div(T, Src1, T_edx); |
| 2391 | _redefined(Context.insert<InstFakeDef>(T_edx, T)); |
Jim Stichnoth | 2655d96 | 2016-04-21 05:38:15 -0700 | [diff] [blame] | 2392 | if (Ty == IceType_i8) { |
| 2393 | // Register ah must be moved into one of {al,bl,cl,dl} before it can be |
| 2394 | // moved into a general 8-bit register. |
| 2395 | auto *T_AhRcvr = makeReg(Ty); |
| 2396 | T_AhRcvr->setRegClass(RCX86_IsAhRcvr); |
| 2397 | _mov(T_AhRcvr, T_edx); |
| 2398 | T_edx = T_AhRcvr; |
| 2399 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2400 | _mov(Dest, T_edx); |
| 2401 | } break; |
| 2402 | case InstArithmetic::Srem: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2403 | // TODO(stichnot): Enable this after doing better performance and cross |
| 2404 | // testing. |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 2405 | if (false && Func->getOptLevel() >= Opt_1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2406 | // Optimize mod by constant power of 2, but not for Om1 or O0, just to |
| 2407 | // keep things simple there. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2408 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Src1)) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2409 | const int32_t Divisor = C->getValue(); |
| 2410 | const uint32_t UDivisor = Divisor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2411 | if (Divisor > 0 && llvm::isPowerOf2_32(UDivisor)) { |
| 2412 | uint32_t LogDiv = llvm::Log2_32(UDivisor); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2413 | // LLVM does the following for dest=src%(1<<log): |
| 2414 | // t=src |
| 2415 | // sar t,typewidth-1 // -1 if src is negative, 0 if not |
| 2416 | // shr t,typewidth-log |
| 2417 | // add t,src |
| 2418 | // and t, -(1<<log) |
| 2419 | // sub t,src |
| 2420 | // neg t |
| 2421 | // dest=t |
| 2422 | uint32_t TypeWidth = Traits::X86_CHAR_BIT * typeWidthInBytes(Ty); |
| 2423 | // If for some reason we are dividing by 1, just assign 0. |
| 2424 | if (LogDiv == 0) { |
| 2425 | _mov(Dest, Ctx->getConstantZero(Ty)); |
| 2426 | return; |
| 2427 | } |
| 2428 | _mov(T, Src0); |
| 2429 | // The initial sar is unnecessary when dividing by 2. |
| 2430 | if (LogDiv > 1) |
| 2431 | _sar(T, Ctx->getConstantInt(Ty, TypeWidth - 1)); |
| 2432 | _shr(T, Ctx->getConstantInt(Ty, TypeWidth - LogDiv)); |
| 2433 | _add(T, Src0); |
| 2434 | _and(T, Ctx->getConstantInt(Ty, -(1 << LogDiv))); |
| 2435 | _sub(T, Src0); |
| 2436 | _neg(T); |
| 2437 | _mov(Dest, T); |
| 2438 | return; |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | Src1 = legalize(Src1, Legal_Reg | Legal_Mem); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 2443 | RegNumT Eax; |
| 2444 | RegNumT Edx; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2445 | switch (Ty) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2446 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2447 | llvm::report_fatal_error("Bad type for srem"); |
| 2448 | case IceType_i64: |
| 2449 | Eax = Traits::getRaxOrDie(); |
| 2450 | Edx = Traits::getRdxOrDie(); |
| 2451 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2452 | case IceType_i32: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 2453 | Eax = Traits::RegisterSet::Reg_eax; |
| 2454 | Edx = Traits::RegisterSet::Reg_edx; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2455 | break; |
| 2456 | case IceType_i16: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2457 | Eax = Traits::RegisterSet::Reg_ax; |
| 2458 | Edx = Traits::RegisterSet::Reg_dx; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2459 | break; |
| 2460 | case IceType_i8: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2461 | Eax = Traits::RegisterSet::Reg_al; |
| 2462 | Edx = Traits::RegisterSet::Reg_ah; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 2463 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2464 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2465 | T_edx = makeReg(Ty, Edx); |
| 2466 | _mov(T, Src0, Eax); |
| 2467 | _cbwdq(T_edx, T); |
Jim Stichnoth | 017a553 | 2016-07-19 06:35:52 -0700 | [diff] [blame] | 2468 | _idiv(T, Src1, T_edx); |
| 2469 | _redefined(Context.insert<InstFakeDef>(T_edx, T)); |
Jim Stichnoth | 2655d96 | 2016-04-21 05:38:15 -0700 | [diff] [blame] | 2470 | if (Ty == IceType_i8) { |
| 2471 | // Register ah must be moved into one of {al,bl,cl,dl} before it can be |
| 2472 | // moved into a general 8-bit register. |
| 2473 | auto *T_AhRcvr = makeReg(Ty); |
| 2474 | T_AhRcvr->setRegClass(RCX86_IsAhRcvr); |
| 2475 | _mov(T_AhRcvr, T_edx); |
| 2476 | T_edx = T_AhRcvr; |
| 2477 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2478 | _mov(Dest, T_edx); |
| 2479 | } break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2480 | case InstArithmetic::Fadd: |
| 2481 | _mov(T, Src0); |
| 2482 | _addss(T, Src1); |
| 2483 | _mov(Dest, T); |
| 2484 | break; |
| 2485 | case InstArithmetic::Fsub: |
| 2486 | _mov(T, Src0); |
| 2487 | _subss(T, Src1); |
| 2488 | _mov(Dest, T); |
| 2489 | break; |
| 2490 | case InstArithmetic::Fmul: |
| 2491 | _mov(T, Src0); |
Jim Stichnoth | ebbb591 | 2015-10-05 15:12:09 -0700 | [diff] [blame] | 2492 | _mulss(T, Src0 == Src1 ? T : Src1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2493 | _mov(Dest, T); |
| 2494 | break; |
| 2495 | case InstArithmetic::Fdiv: |
| 2496 | _mov(T, Src0); |
| 2497 | _divss(T, Src1); |
| 2498 | _mov(Dest, T); |
| 2499 | break; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2500 | case InstArithmetic::Frem: |
| 2501 | llvm::report_fatal_error("Helper call was expected"); |
| 2502 | break; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2506 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2507 | void TargetX86Base<TraitsType>::lowerAssign(const InstAssign *Instr) { |
| 2508 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 2509 | if (Dest->isRematerializable()) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 2510 | Context.insert<InstFakeDef>(Dest); |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 2511 | return; |
| 2512 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2513 | Operand *Src = Instr->getSrc(0); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2514 | assert(Dest->getType() == Src->getType()); |
| 2515 | lowerMove(Dest, Src, false); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2518 | template <typename TraitsType> |
| 2519 | void TargetX86Base<TraitsType>::lowerBr(const InstBr *Br) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2520 | if (Br->isUnconditional()) { |
| 2521 | _br(Br->getTargetUnconditional()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2522 | return; |
| 2523 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2524 | Operand *Cond = Br->getCondition(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2525 | |
| 2526 | // Handle folding opportunities. |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2527 | if (const Inst *Producer = FoldingInfo.getProducerFor(Cond)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2528 | assert(Producer->isDeleted()); |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2529 | switch (BoolFolding<Traits>::getProducerKind(Producer)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2530 | default: |
| 2531 | break; |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2532 | case BoolFolding<Traits>::PK_Icmp32: |
| 2533 | case BoolFolding<Traits>::PK_Icmp64: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2534 | lowerIcmpAndConsumer(llvm::cast<InstIcmp>(Producer), Br); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2535 | return; |
| 2536 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2537 | case BoolFolding<Traits>::PK_Fcmp: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2538 | lowerFcmpAndConsumer(llvm::cast<InstFcmp>(Producer), Br); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 2539 | return; |
| 2540 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 2541 | case BoolFolding<Traits>::PK_Arith: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2542 | lowerArithAndConsumer(llvm::cast<InstArithmetic>(Producer), Br); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 2543 | return; |
| 2544 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2545 | } |
| 2546 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2547 | Operand *Src0 = legalize(Cond, Legal_Reg | Legal_Mem); |
| 2548 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 2549 | _cmp(Src0, Zero); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 2550 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2553 | // constexprMax returns a (constexpr) max(S0, S1), and it is used for defining |
| 2554 | // OperandList in lowerCall. std::max() is supposed to work, but it doesn't. |
| 2555 | inline constexpr SizeT constexprMax(SizeT S0, SizeT S1) { |
| 2556 | return S0 < S1 ? S1 : S0; |
| 2557 | } |
| 2558 | |
| 2559 | template <typename TraitsType> |
| 2560 | void TargetX86Base<TraitsType>::lowerCall(const InstCall *Instr) { |
| 2561 | // Common x86 calling convention lowering: |
| 2562 | // |
| 2563 | // * At the point before the call, the stack must be aligned to 16 bytes. |
| 2564 | // |
| 2565 | // * Non-register arguments are pushed onto the stack in right-to-left order, |
| 2566 | // such that the left-most argument ends up on the top of the stack at the |
| 2567 | // lowest memory address. |
| 2568 | // |
| 2569 | // * Stack arguments of vector type are aligned to start at the next highest |
| 2570 | // multiple of 16 bytes. Other stack arguments are aligned to the next word |
| 2571 | // size boundary (4 or 8 bytes, respectively). |
| 2572 | NeedsStackAlignment = true; |
| 2573 | |
| 2574 | using OperandList = |
| 2575 | llvm::SmallVector<Operand *, constexprMax(Traits::X86_MAX_XMM_ARGS, |
| 2576 | Traits::X86_MAX_GPR_ARGS)>; |
| 2577 | OperandList XmmArgs; |
| 2578 | CfgVector<std::pair<const Type, Operand *>> GprArgs; |
| 2579 | OperandList StackArgs, StackArgLocations; |
| 2580 | uint32_t ParameterAreaSizeBytes = 0; |
| 2581 | |
| 2582 | // Classify each argument operand according to the location where the argument |
| 2583 | // is passed. |
| 2584 | for (SizeT i = 0, NumArgs = Instr->getNumArgs(); i < NumArgs; ++i) { |
| 2585 | Operand *Arg = Instr->getArg(i); |
| 2586 | const Type Ty = Arg->getType(); |
| 2587 | // The PNaCl ABI requires the width of arguments to be at least 32 bits. |
| 2588 | assert(typeWidthInBytes(Ty) >= 4); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2589 | if (isVectorType(Ty) && |
| 2590 | Traits::getRegisterForXmmArgNum(XmmArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2591 | XmmArgs.push_back(Arg); |
| 2592 | } else if (isScalarFloatingType(Ty) && Traits::X86_PASS_SCALAR_FP_IN_XMM && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2593 | Traits::getRegisterForXmmArgNum(XmmArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2594 | XmmArgs.push_back(Arg); |
| 2595 | } else if (isScalarIntegerType(Ty) && |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 2596 | Traits::getRegisterForGprArgNum(Ty, GprArgs.size()).hasValue()) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2597 | GprArgs.emplace_back(Ty, Arg); |
| 2598 | } else { |
| 2599 | // Place on stack. |
| 2600 | StackArgs.push_back(Arg); |
| 2601 | if (isVectorType(Arg->getType())) { |
| 2602 | ParameterAreaSizeBytes = |
| 2603 | Traits::applyStackAlignment(ParameterAreaSizeBytes); |
| 2604 | } |
| 2605 | Variable *esp = getPhysicalRegister(getStackReg(), Traits::WordType); |
| 2606 | Constant *Loc = Ctx->getConstantInt32(ParameterAreaSizeBytes); |
| 2607 | StackArgLocations.push_back( |
| 2608 | Traits::X86OperandMem::create(Func, Ty, esp, Loc)); |
| 2609 | ParameterAreaSizeBytes += typeWidthInBytesOnStack(Arg->getType()); |
| 2610 | } |
| 2611 | } |
| 2612 | // Ensure there is enough space for the fstp/movs for floating returns. |
| 2613 | Variable *Dest = Instr->getDest(); |
| 2614 | const Type DestTy = Dest ? Dest->getType() : IceType_void; |
John Porto | 4ab4fbe | 2016-01-20 13:44:30 -0800 | [diff] [blame] | 2615 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2616 | if (isScalarFloatingType(DestTy)) { |
| 2617 | ParameterAreaSizeBytes = |
| 2618 | std::max(static_cast<size_t>(ParameterAreaSizeBytes), |
| 2619 | typeWidthInBytesOnStack(DestTy)); |
| 2620 | } |
| 2621 | } |
| 2622 | // Adjust the parameter area so that the stack is aligned. It is assumed that |
| 2623 | // the stack is already aligned at the start of the calling sequence. |
| 2624 | ParameterAreaSizeBytes = Traits::applyStackAlignment(ParameterAreaSizeBytes); |
| 2625 | assert(ParameterAreaSizeBytes <= maxOutArgsSizeBytes()); |
| 2626 | // Copy arguments that are passed on the stack to the appropriate stack |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2627 | // locations. We make sure legalize() is called on each argument at this |
| 2628 | // point, to allow availabilityGet() to work. |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2629 | for (SizeT i = 0, NumStackArgs = StackArgs.size(); i < NumStackArgs; ++i) { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2630 | lowerStore( |
| 2631 | InstStore::create(Func, legalize(StackArgs[i]), StackArgLocations[i])); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2632 | } |
| 2633 | // Copy arguments to be passed in registers to the appropriate registers. |
| 2634 | for (SizeT i = 0, NumXmmArgs = XmmArgs.size(); i < NumXmmArgs; ++i) { |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2635 | XmmArgs[i] = |
| 2636 | legalizeToReg(legalize(XmmArgs[i]), Traits::getRegisterForXmmArgNum(i)); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2637 | } |
| 2638 | // Materialize moves for arguments passed in GPRs. |
| 2639 | for (SizeT i = 0, NumGprArgs = GprArgs.size(); i < NumGprArgs; ++i) { |
| 2640 | const Type SignatureTy = GprArgs[i].first; |
Jim Stichnoth | e450656 | 2016-06-20 06:44:07 -0700 | [diff] [blame] | 2641 | Operand *Arg = |
| 2642 | legalize(GprArgs[i].second, Legal_Default | Legal_Rematerializable); |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2643 | GprArgs[i].second = |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2644 | legalizeToReg(Arg, Traits::getRegisterForGprArgNum(Arg->getType(), i)); |
| 2645 | assert(SignatureTy == IceType_i64 || SignatureTy == IceType_i32); |
| 2646 | assert(SignatureTy == Arg->getType()); |
| 2647 | (void)SignatureTy; |
Jim Stichnoth | f531931 | 2016-06-10 12:21:17 -0700 | [diff] [blame] | 2648 | } |
| 2649 | // Generate a FakeUse of register arguments so that they do not get dead code |
| 2650 | // eliminated as a result of the FakeKill of scratch registers after the call. |
| 2651 | // These need to be right before the call instruction. |
| 2652 | for (auto *Arg : XmmArgs) { |
| 2653 | Context.insert<InstFakeUse>(llvm::cast<Variable>(Arg)); |
| 2654 | } |
| 2655 | for (auto &ArgPair : GprArgs) { |
| 2656 | Context.insert<InstFakeUse>(llvm::cast<Variable>(ArgPair.second)); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2657 | } |
| 2658 | // Generate the call instruction. Assign its result to a temporary with high |
| 2659 | // register allocation weight. |
| 2660 | // ReturnReg doubles as ReturnRegLo as necessary. |
| 2661 | Variable *ReturnReg = nullptr; |
| 2662 | Variable *ReturnRegHi = nullptr; |
| 2663 | if (Dest) { |
| 2664 | switch (DestTy) { |
| 2665 | case IceType_NUM: |
| 2666 | case IceType_void: |
| 2667 | case IceType_i1: |
| 2668 | case IceType_i8: |
| 2669 | case IceType_i16: |
| 2670 | llvm::report_fatal_error("Invalid Call dest type"); |
| 2671 | break; |
| 2672 | case IceType_i32: |
| 2673 | ReturnReg = makeReg(DestTy, Traits::RegisterSet::Reg_eax); |
| 2674 | break; |
| 2675 | case IceType_i64: |
| 2676 | if (Traits::Is64Bit) { |
Jim Stichnoth | ee1aae8 | 2016-02-02 09:29:21 -0800 | [diff] [blame] | 2677 | ReturnReg = makeReg(IceType_i64, Traits::getRaxOrDie()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2678 | } else { |
| 2679 | ReturnReg = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 2680 | ReturnRegHi = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 2681 | } |
| 2682 | break; |
| 2683 | case IceType_f32: |
| 2684 | case IceType_f64: |
| 2685 | if (!Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2686 | // Leave ReturnReg==ReturnRegHi==nullptr, and capture the result with |
| 2687 | // the fstp instruction. |
| 2688 | break; |
| 2689 | } |
| 2690 | // Fallthrough intended. |
| 2691 | case IceType_v4i1: |
| 2692 | case IceType_v8i1: |
| 2693 | case IceType_v16i1: |
| 2694 | case IceType_v16i8: |
| 2695 | case IceType_v8i16: |
| 2696 | case IceType_v4i32: |
| 2697 | case IceType_v4f32: |
| 2698 | ReturnReg = makeReg(DestTy, Traits::RegisterSet::Reg_xmm0); |
| 2699 | break; |
| 2700 | } |
| 2701 | } |
| 2702 | // Emit the call to the function. |
| 2703 | Operand *CallTarget = |
| 2704 | legalize(Instr->getCallTarget(), Legal_Reg | Legal_Imm | Legal_AddrAbs); |
| 2705 | Inst *NewCall = emitCallToTarget(CallTarget, ReturnReg); |
| 2706 | // Keep the upper return register live on 32-bit platform. |
| 2707 | if (ReturnRegHi) |
| 2708 | Context.insert<InstFakeDef>(ReturnRegHi); |
| 2709 | // Mark the call as killing all the caller-save registers. |
| 2710 | Context.insert<InstFakeKill>(NewCall); |
| 2711 | // Handle x86-32 floating point returns. |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2712 | if (Dest != nullptr && isScalarFloatingType(DestTy) && |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2713 | !Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2714 | // Special treatment for an FP function which returns its result in st(0). |
| 2715 | // If Dest ends up being a physical xmm register, the fstp emit code will |
| 2716 | // route st(0) through the space reserved in the function argument area |
| 2717 | // we allocated. |
| 2718 | _fstp(Dest); |
| 2719 | // Create a fake use of Dest in case it actually isn't used, because st(0) |
| 2720 | // still needs to be popped. |
| 2721 | Context.insert<InstFakeUse>(Dest); |
| 2722 | } |
| 2723 | // Generate a FakeUse to keep the call live if necessary. |
| 2724 | if (Instr->hasSideEffects() && ReturnReg) { |
| 2725 | Context.insert<InstFakeUse>(ReturnReg); |
| 2726 | } |
| 2727 | // Process the return value, if any. |
| 2728 | if (Dest == nullptr) |
| 2729 | return; |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2730 | // Assign the result of the call to Dest. Route it through a temporary so |
| 2731 | // that the local register availability peephole can be subsequently used. |
| 2732 | Variable *Tmp = nullptr; |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2733 | if (isVectorType(DestTy)) { |
| 2734 | assert(ReturnReg && "Vector type requires a return register"); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2735 | Tmp = makeReg(DestTy); |
| 2736 | _movp(Tmp, ReturnReg); |
| 2737 | _movp(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2738 | } else if (isScalarFloatingType(DestTy)) { |
| 2739 | if (Traits::X86_PASS_SCALAR_FP_IN_XMM) { |
| 2740 | assert(ReturnReg && "FP type requires a return register"); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2741 | _mov(Tmp, ReturnReg); |
| 2742 | _mov(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2743 | } |
| 2744 | } else { |
| 2745 | assert(isScalarIntegerType(DestTy)); |
| 2746 | assert(ReturnReg && "Integer type requires a return register"); |
| 2747 | if (DestTy == IceType_i64 && !Traits::Is64Bit) { |
| 2748 | assert(ReturnRegHi && "64-bit type requires two return registers"); |
| 2749 | auto *Dest64On32 = llvm::cast<Variable64On32>(Dest); |
| 2750 | Variable *DestLo = Dest64On32->getLo(); |
| 2751 | Variable *DestHi = Dest64On32->getHi(); |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2752 | _mov(Tmp, ReturnReg); |
| 2753 | _mov(DestLo, Tmp); |
| 2754 | Variable *TmpHi = nullptr; |
| 2755 | _mov(TmpHi, ReturnRegHi); |
| 2756 | _mov(DestHi, TmpHi); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2757 | } else { |
Jim Stichnoth | deb5a82 | 2016-06-14 22:21:33 -0700 | [diff] [blame] | 2758 | _mov(Tmp, ReturnReg); |
| 2759 | _mov(Dest, Tmp); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2764 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2765 | void TargetX86Base<TraitsType>::lowerCast(const InstCast *Instr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2766 | // 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] | 2767 | InstCast::OpKind CastKind = Instr->getCastKind(); |
| 2768 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2769 | Type DestTy = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2770 | switch (CastKind) { |
| 2771 | default: |
| 2772 | Func->setError("Cast type not supported"); |
| 2773 | return; |
| 2774 | case InstCast::Sext: { |
| 2775 | // Src0RM is the source operand legalized to physical register or memory, |
| 2776 | // but not immediate, since the relevant x86 native instructions don't |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2777 | // allow an immediate operand. If the operand is an immediate, we could |
| 2778 | // consider computing the strength-reduced result at translation time, but |
| 2779 | // we're unlikely to see something like that in the bitcode that the |
| 2780 | // optimizer wouldn't have already taken care of. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2781 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2782 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2783 | if (DestTy == IceType_v16i8) { |
| 2784 | // onemask = materialize(1,1,...); dst = (src & onemask) > 0 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2785 | Variable *OneMask = makeVectorOfOnes(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2786 | Variable *T = makeReg(DestTy); |
| 2787 | _movp(T, Src0RM); |
| 2788 | _pand(T, OneMask); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2789 | Variable *Zeros = makeVectorOfZeros(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2790 | _pcmpgt(T, Zeros); |
| 2791 | _movp(Dest, T); |
| 2792 | } else { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 2793 | /// width = width(elty) - 1; dest = (src << width) >> width |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2794 | SizeT ShiftAmount = |
| 2795 | Traits::X86_CHAR_BIT * typeWidthInBytes(typeElementType(DestTy)) - |
| 2796 | 1; |
| 2797 | Constant *ShiftConstant = Ctx->getConstantInt8(ShiftAmount); |
| 2798 | Variable *T = makeReg(DestTy); |
| 2799 | _movp(T, Src0RM); |
| 2800 | _psll(T, ShiftConstant); |
| 2801 | _psra(T, ShiftConstant); |
| 2802 | _movp(Dest, T); |
| 2803 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2804 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2805 | // t1=movsx src; t2=t1; t2=sar t2, 31; dst.lo=t1; dst.hi=t2 |
| 2806 | Constant *Shift = Ctx->getConstantInt32(31); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 2807 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 2808 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2809 | Variable *T_Lo = makeReg(DestLo->getType()); |
| 2810 | if (Src0RM->getType() == IceType_i32) { |
| 2811 | _mov(T_Lo, Src0RM); |
| 2812 | } else if (Src0RM->getType() == IceType_i1) { |
| 2813 | _movzx(T_Lo, Src0RM); |
| 2814 | _shl(T_Lo, Shift); |
| 2815 | _sar(T_Lo, Shift); |
| 2816 | } else { |
| 2817 | _movsx(T_Lo, Src0RM); |
| 2818 | } |
| 2819 | _mov(DestLo, T_Lo); |
| 2820 | Variable *T_Hi = nullptr; |
| 2821 | _mov(T_Hi, T_Lo); |
| 2822 | if (Src0RM->getType() != IceType_i1) |
| 2823 | // For i1, the sar instruction is already done above. |
| 2824 | _sar(T_Hi, Shift); |
| 2825 | _mov(DestHi, T_Hi); |
| 2826 | } else if (Src0RM->getType() == IceType_i1) { |
| 2827 | // t1 = src |
| 2828 | // shl t1, dst_bitwidth - 1 |
| 2829 | // sar t1, dst_bitwidth - 1 |
| 2830 | // dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2831 | size_t DestBits = Traits::X86_CHAR_BIT * typeWidthInBytes(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2832 | Constant *ShiftAmount = Ctx->getConstantInt32(DestBits - 1); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2833 | Variable *T = makeReg(DestTy); |
| 2834 | if (typeWidthInBytes(DestTy) <= typeWidthInBytes(Src0RM->getType())) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2835 | _mov(T, Src0RM); |
| 2836 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2837 | // Widen the source using movsx or movzx. (It doesn't matter which one, |
| 2838 | // since the following shl/sar overwrite the bits.) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2839 | _movzx(T, Src0RM); |
| 2840 | } |
| 2841 | _shl(T, ShiftAmount); |
| 2842 | _sar(T, ShiftAmount); |
| 2843 | _mov(Dest, T); |
| 2844 | } else { |
| 2845 | // t1 = movsx src; dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2846 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2847 | _movsx(T, Src0RM); |
| 2848 | _mov(Dest, T); |
| 2849 | } |
| 2850 | break; |
| 2851 | } |
| 2852 | case InstCast::Zext: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2853 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2854 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2855 | // onemask = materialize(1,1,...); dest = onemask & src |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2856 | Variable *OneMask = makeVectorOfOnes(DestTy); |
| 2857 | Variable *T = makeReg(DestTy); |
| 2858 | _movp(T, Src0RM); |
| 2859 | _pand(T, OneMask); |
| 2860 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2861 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2862 | // t1=movzx src; dst.lo=t1; dst.hi=0 |
| 2863 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 2864 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 2865 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2866 | Variable *Tmp = makeReg(DestLo->getType()); |
| 2867 | if (Src0RM->getType() == IceType_i32) { |
| 2868 | _mov(Tmp, Src0RM); |
| 2869 | } else { |
| 2870 | _movzx(Tmp, Src0RM); |
| 2871 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2872 | _mov(DestLo, Tmp); |
| 2873 | _mov(DestHi, Zero); |
| 2874 | } else if (Src0RM->getType() == IceType_i1) { |
Jim Stichnoth | 485d077 | 2015-10-09 06:52:19 -0700 | [diff] [blame] | 2875 | // t = Src0RM; Dest = t |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2876 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2877 | if (DestTy == IceType_i8) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2878 | _mov(T, Src0RM); |
| 2879 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2880 | assert(DestTy != IceType_i1); |
| 2881 | assert(Traits::Is64Bit || DestTy != IceType_i64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2882 | // 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] | 2883 | // In x86-64 we need to widen T to 64-bits to ensure that T -- if |
| 2884 | // written to the stack (i.e., in -Om1) will be fully zero-extended. |
| 2885 | T = makeReg(DestTy == IceType_i64 ? IceType_i64 : IceType_i32); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2886 | _movzx(T, Src0RM); |
| 2887 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2888 | _mov(Dest, T); |
| 2889 | } else { |
| 2890 | // t1 = movzx src; dst = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2891 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2892 | _movzx(T, Src0RM); |
| 2893 | _mov(Dest, T); |
| 2894 | } |
| 2895 | break; |
| 2896 | } |
| 2897 | case InstCast::Trunc: { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2898 | if (isVectorType(DestTy)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2899 | // onemask = materialize(1,1,...); dst = src & onemask |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2900 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2901 | Type Src0Ty = Src0RM->getType(); |
| 2902 | Variable *OneMask = makeVectorOfOnes(Src0Ty); |
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 | _movp(T, Src0RM); |
| 2905 | _pand(T, OneMask); |
| 2906 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2907 | } else if (DestTy == IceType_i1 || DestTy == IceType_i8) { |
| 2908 | // Make sure we truncate from and into valid registers. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2909 | Operand *Src0 = legalizeUndef(Instr->getSrc(0)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2910 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) |
| 2911 | Src0 = loOperand(Src0); |
| 2912 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 2913 | Variable *T = copyToReg8(Src0RM); |
| 2914 | if (DestTy == IceType_i1) |
| 2915 | _and(T, Ctx->getConstantInt1(1)); |
| 2916 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2917 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2918 | Operand *Src0 = legalizeUndef(Instr->getSrc(0)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2919 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2920 | Src0 = loOperand(Src0); |
| 2921 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 2922 | // t1 = trunc Src0RM; Dest = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2923 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2924 | _mov(T, Src0RM); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2925 | _mov(Dest, T); |
| 2926 | } |
| 2927 | break; |
| 2928 | } |
| 2929 | case InstCast::Fptrunc: |
| 2930 | case InstCast::Fpext: { |
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 = cvt Src0RM; Dest = t1 |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2933 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2934 | _cvt(T, Src0RM, Traits::Insts::Cvt::Float2float); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2935 | _mov(Dest, T); |
| 2936 | break; |
| 2937 | } |
| 2938 | case InstCast::Fptosi: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2939 | if (isVectorType(DestTy)) { |
| 2940 | assert(DestTy == IceType_v4i32 && |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2941 | Instr->getSrc(0)->getType() == IceType_v4f32); |
| 2942 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 2943 | if (llvm::isa<X86OperandMem>(Src0RM)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 2944 | Src0RM = legalizeToReg(Src0RM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2945 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2946 | _cvt(T, Src0RM, Traits::Insts::Cvt::Tps2dq); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2947 | _movp(Dest, T); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2948 | } else if (!Traits::Is64Bit && DestTy == IceType_i64) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2949 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2950 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2951 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2952 | // t1.i32 = cvt Src0RM; t2.dest_type = t1; Dest = t2.dest_type |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2953 | Variable *T_1 = nullptr; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2954 | if (Traits::Is64Bit && DestTy == IceType_i64) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2955 | T_1 = makeReg(IceType_i64); |
| 2956 | } else { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2957 | assert(DestTy != IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2958 | T_1 = makeReg(IceType_i32); |
| 2959 | } |
| 2960 | // cvt() requires its integer argument to be a GPR. |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2961 | Variable *T_2 = makeReg(DestTy); |
| 2962 | if (isByteSizedType(DestTy)) { |
| 2963 | assert(T_1->getType() == IceType_i32); |
| 2964 | T_1->setRegClass(RCX86_Is32To8); |
| 2965 | T_2->setRegClass(RCX86_IsTrunc8Rcvr); |
| 2966 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2967 | _cvt(T_1, Src0RM, Traits::Insts::Cvt::Tss2si); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2968 | _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] | 2969 | if (DestTy == IceType_i1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2970 | _and(T_2, Ctx->getConstantInt1(1)); |
| 2971 | _mov(Dest, T_2); |
| 2972 | } |
| 2973 | break; |
| 2974 | case InstCast::Fptoui: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2975 | if (isVectorType(DestTy)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2976 | llvm::report_fatal_error("Helper call was expected"); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2977 | } else if (DestTy == IceType_i64 || |
| 2978 | (!Traits::Is64Bit && DestTy == IceType_i32)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 2979 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2980 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2981 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2982 | // t1.i32 = cvt Src0RM; t2.dest_type = t1; Dest = t2.dest_type |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2983 | assert(DestTy != IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2984 | Variable *T_1 = nullptr; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2985 | if (Traits::Is64Bit && DestTy == IceType_i32) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2986 | T_1 = makeReg(IceType_i64); |
| 2987 | } else { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2988 | assert(DestTy != IceType_i32); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 2989 | T_1 = makeReg(IceType_i32); |
| 2990 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 2991 | Variable *T_2 = makeReg(DestTy); |
| 2992 | if (isByteSizedType(DestTy)) { |
| 2993 | assert(T_1->getType() == IceType_i32); |
| 2994 | T_1->setRegClass(RCX86_Is32To8); |
| 2995 | T_2->setRegClass(RCX86_IsTrunc8Rcvr); |
| 2996 | } |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 2997 | _cvt(T_1, Src0RM, Traits::Insts::Cvt::Tss2si); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 2998 | _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] | 2999 | if (DestTy == IceType_i1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3000 | _and(T_2, Ctx->getConstantInt1(1)); |
| 3001 | _mov(Dest, T_2); |
| 3002 | } |
| 3003 | break; |
| 3004 | case InstCast::Sitofp: |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3005 | if (isVectorType(DestTy)) { |
| 3006 | assert(DestTy == IceType_v4f32 && |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3007 | Instr->getSrc(0)->getType() == IceType_v4i32); |
| 3008 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3009 | if (llvm::isa<X86OperandMem>(Src0RM)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3010 | Src0RM = legalizeToReg(Src0RM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3011 | Variable *T = makeReg(DestTy); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 3012 | _cvt(T, Src0RM, Traits::Insts::Cvt::Dq2ps); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3013 | _movp(Dest, T); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3014 | } else if (!Traits::Is64Bit && Instr->getSrc(0)->getType() == IceType_i64) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3015 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3016 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3017 | Operand *Src0RM = legalize(Instr->getSrc(0), Legal_Reg | Legal_Mem); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3018 | // Sign-extend the operand. |
| 3019 | // t1.i32 = movsx Src0RM; t2 = Cvt t1.i32; Dest = t2 |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3020 | Variable *T_1 = nullptr; |
| 3021 | if (Traits::Is64Bit && Src0RM->getType() == IceType_i64) { |
| 3022 | T_1 = makeReg(IceType_i64); |
| 3023 | } else { |
| 3024 | assert(Src0RM->getType() != IceType_i64); |
| 3025 | T_1 = makeReg(IceType_i32); |
| 3026 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3027 | Variable *T_2 = makeReg(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3028 | if (Src0RM->getType() == T_1->getType()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3029 | _mov(T_1, Src0RM); |
| 3030 | else |
| 3031 | _movsx(T_1, Src0RM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 3032 | _cvt(T_2, T_1, Traits::Insts::Cvt::Si2ss); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3033 | _mov(Dest, T_2); |
| 3034 | } |
| 3035 | break; |
| 3036 | case InstCast::Uitofp: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3037 | Operand *Src0 = Instr->getSrc(0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3038 | if (isVectorType(Src0->getType())) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3039 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3040 | } else if (Src0->getType() == IceType_i64 || |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3041 | (!Traits::Is64Bit && Src0->getType() == IceType_i32)) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3042 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3043 | } else { |
| 3044 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3045 | // Zero-extend the operand. |
| 3046 | // t1.i32 = movzx Src0RM; t2 = Cvt t1.i32; Dest = t2 |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3047 | Variable *T_1 = nullptr; |
| 3048 | if (Traits::Is64Bit && Src0RM->getType() == IceType_i32) { |
| 3049 | T_1 = makeReg(IceType_i64); |
| 3050 | } else { |
| 3051 | assert(Src0RM->getType() != IceType_i64); |
| 3052 | assert(Traits::Is64Bit || Src0RM->getType() != IceType_i32); |
| 3053 | T_1 = makeReg(IceType_i32); |
| 3054 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3055 | Variable *T_2 = makeReg(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3056 | if (Src0RM->getType() == T_1->getType()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3057 | _mov(T_1, Src0RM); |
| 3058 | else |
| 3059 | _movzx(T_1, Src0RM); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 3060 | _cvt(T_2, T_1, Traits::Insts::Cvt::Si2ss); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3061 | _mov(Dest, T_2); |
| 3062 | } |
| 3063 | break; |
| 3064 | } |
| 3065 | case InstCast::Bitcast: { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3066 | Operand *Src0 = Instr->getSrc(0); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3067 | if (DestTy == Src0->getType()) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3068 | auto *Assign = InstAssign::create(Func, Dest, Src0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3069 | lowerAssign(Assign); |
| 3070 | return; |
| 3071 | } |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3072 | switch (DestTy) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3073 | default: |
| 3074 | llvm_unreachable("Unexpected Bitcast dest type"); |
| 3075 | case IceType_i8: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3076 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3077 | } break; |
| 3078 | case IceType_i16: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3079 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3080 | } break; |
| 3081 | case IceType_i32: |
| 3082 | case IceType_f32: { |
Jim Stichnoth | a1410df | 2016-06-16 10:02:48 -0700 | [diff] [blame] | 3083 | Variable *Src0R = legalizeToReg(Src0); |
| 3084 | Variable *T = makeReg(DestTy); |
| 3085 | _movd(T, Src0R); |
| 3086 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3087 | } break; |
| 3088 | case IceType_i64: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3089 | assert(Src0->getType() == IceType_f64); |
| 3090 | if (Traits::Is64Bit) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3091 | Variable *Src0R = legalizeToReg(Src0); |
| 3092 | Variable *T = makeReg(IceType_i64); |
| 3093 | _movd(T, Src0R); |
| 3094 | _mov(Dest, T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3095 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3096 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3097 | // a.i64 = bitcast b.f64 ==> |
| 3098 | // s.f64 = spill b.f64 |
| 3099 | // t_lo.i32 = lo(s.f64) |
| 3100 | // a_lo.i32 = t_lo.i32 |
| 3101 | // t_hi.i32 = hi(s.f64) |
| 3102 | // a_hi.i32 = t_hi.i32 |
| 3103 | Operand *SpillLo, *SpillHi; |
| 3104 | if (auto *Src0Var = llvm::dyn_cast<Variable>(Src0RM)) { |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 3105 | Variable *Spill = Func->makeVariable(IceType_f64); |
| 3106 | Spill->setLinkedTo(Src0Var); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3107 | Spill->setMustNotHaveReg(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3108 | _movq(Spill, Src0RM); |
| 3109 | SpillLo = Traits::VariableSplit::create(Func, Spill, |
| 3110 | Traits::VariableSplit::Low); |
| 3111 | SpillHi = Traits::VariableSplit::create(Func, Spill, |
| 3112 | Traits::VariableSplit::High); |
| 3113 | } else { |
| 3114 | SpillLo = loOperand(Src0RM); |
| 3115 | SpillHi = hiOperand(Src0RM); |
| 3116 | } |
| 3117 | |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3118 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 3119 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3120 | Variable *T_Lo = makeReg(IceType_i32); |
| 3121 | Variable *T_Hi = makeReg(IceType_i32); |
| 3122 | |
| 3123 | _mov(T_Lo, SpillLo); |
| 3124 | _mov(DestLo, T_Lo); |
| 3125 | _mov(T_Hi, SpillHi); |
| 3126 | _mov(DestHi, T_Hi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3127 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3128 | } break; |
| 3129 | case IceType_f64: { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3130 | assert(Src0->getType() == IceType_i64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3131 | if (Traits::Is64Bit) { |
| 3132 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3133 | Variable *T = makeReg(IceType_f64); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3134 | _movd(T, Src0RM); |
| 3135 | _mov(Dest, T); |
| 3136 | } else { |
| 3137 | Src0 = legalize(Src0); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3138 | if (llvm::isa<X86OperandMem>(Src0)) { |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame^] | 3139 | Variable *T = makeReg(DestTy); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3140 | _movq(T, Src0); |
| 3141 | _movq(Dest, T); |
| 3142 | break; |
| 3143 | } |
| 3144 | // a.f64 = bitcast b.i64 ==> |
| 3145 | // t_lo.i32 = b_lo.i32 |
| 3146 | // FakeDef(s.f64) |
| 3147 | // lo(s.f64) = t_lo.i32 |
| 3148 | // t_hi.i32 = b_hi.i32 |
| 3149 | // hi(s.f64) = t_hi.i32 |
| 3150 | // a.f64 = s.f64 |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 3151 | Variable *Spill = Func->makeVariable(IceType_f64); |
| 3152 | Spill->setLinkedTo(Dest); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3153 | Spill->setMustNotHaveReg(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3154 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3155 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3156 | auto *SpillLo = Traits::VariableSplit::create( |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3157 | Func, Spill, Traits::VariableSplit::Low); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3158 | auto *SpillHi = Traits::VariableSplit::create( |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3159 | Func, Spill, Traits::VariableSplit::High); |
| 3160 | _mov(T_Lo, loOperand(Src0)); |
| 3161 | // Technically, the Spill is defined after the _store happens, but |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3162 | // SpillLo is considered a "use" of Spill so define Spill before it is |
| 3163 | // used. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3164 | Context.insert<InstFakeDef>(Spill); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3165 | _store(T_Lo, SpillLo); |
| 3166 | _mov(T_Hi, hiOperand(Src0)); |
| 3167 | _store(T_Hi, SpillHi); |
| 3168 | _movq(Dest, Spill); |
| 3169 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3170 | } break; |
| 3171 | case IceType_v8i1: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3172 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3173 | } break; |
| 3174 | case IceType_v16i1: { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 3175 | llvm::report_fatal_error("Helper call was expected"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3176 | } break; |
| 3177 | case IceType_v8i16: |
| 3178 | case IceType_v16i8: |
| 3179 | case IceType_v4i32: |
| 3180 | case IceType_v4f32: { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3181 | _movp(Dest, legalizeToReg(Src0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3182 | } break; |
| 3183 | } |
| 3184 | break; |
| 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3189 | template <typename TraitsType> |
| 3190 | void TargetX86Base<TraitsType>::lowerExtractElement( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3191 | const InstExtractElement *Instr) { |
| 3192 | Operand *SourceVectNotLegalized = Instr->getSrc(0); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3193 | auto *ElementIndex = llvm::dyn_cast<ConstantInteger32>(Instr->getSrc(1)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3194 | // Only constant indices are allowed in PNaCl IR. |
| 3195 | assert(ElementIndex); |
| 3196 | |
| 3197 | unsigned Index = ElementIndex->getValue(); |
| 3198 | Type Ty = SourceVectNotLegalized->getType(); |
| 3199 | Type ElementTy = typeElementType(Ty); |
| 3200 | Type InVectorElementTy = Traits::getInVectorElementType(Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3201 | |
| 3202 | // TODO(wala): Determine the best lowering sequences for each type. |
| 3203 | bool CanUsePextr = Ty == IceType_v8i16 || Ty == IceType_v8i1 || |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 3204 | (InstructionSet >= Traits::SSE4_1 && Ty != IceType_v4f32); |
| 3205 | Variable *ExtractedElementR = |
| 3206 | makeReg(CanUsePextr ? IceType_i32 : InVectorElementTy); |
| 3207 | if (CanUsePextr) { |
| 3208 | // Use pextrb, pextrw, or pextrd. The "b" and "w" versions clear the upper |
| 3209 | // bits of the destination register, so we represent this by always |
| 3210 | // extracting into an i32 register. The _mov into Dest below will do |
| 3211 | // truncation as necessary. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3212 | Constant *Mask = Ctx->getConstantInt32(Index); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3213 | Variable *SourceVectR = legalizeToReg(SourceVectNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3214 | _pextr(ExtractedElementR, SourceVectR, Mask); |
| 3215 | } else if (Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v4i1) { |
| 3216 | // Use pshufd and movd/movss. |
| 3217 | Variable *T = nullptr; |
| 3218 | if (Index) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3219 | // The shuffle only needs to occur if the element to be extracted is not |
| 3220 | // at the lowest index. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3221 | Constant *Mask = Ctx->getConstantInt32(Index); |
| 3222 | T = makeReg(Ty); |
| 3223 | _pshufd(T, legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem), Mask); |
| 3224 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3225 | T = legalizeToReg(SourceVectNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | if (InVectorElementTy == IceType_i32) { |
| 3229 | _movd(ExtractedElementR, T); |
| 3230 | } else { // Ty == IceType_f32 |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3231 | // TODO(wala): _movss is only used here because _mov does not allow a |
| 3232 | // vector source and a scalar destination. _mov should be able to be |
| 3233 | // used here. |
| 3234 | // _movss is a binary instruction, so the FakeDef is needed to keep the |
| 3235 | // live range analysis consistent. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3236 | Context.insert<InstFakeDef>(ExtractedElementR); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3237 | _movss(ExtractedElementR, T); |
| 3238 | } |
| 3239 | } else { |
| 3240 | assert(Ty == IceType_v16i8 || Ty == IceType_v16i1); |
| 3241 | // Spill the value to a stack slot and do the extraction in memory. |
| 3242 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3243 | // TODO(wala): use legalize(SourceVectNotLegalized, Legal_Mem) when support |
| 3244 | // for legalizing to mem is implemented. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3245 | Variable *Slot = Func->makeVariable(Ty); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3246 | Slot->setMustNotHaveReg(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3247 | _movp(Slot, legalizeToReg(SourceVectNotLegalized)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3248 | |
| 3249 | // Compute the location of the element in memory. |
| 3250 | unsigned Offset = Index * typeWidthInBytes(InVectorElementTy); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3251 | X86OperandMem *Loc = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3252 | getMemoryOperandForStackSlot(InVectorElementTy, Slot, Offset); |
| 3253 | _mov(ExtractedElementR, Loc); |
| 3254 | } |
| 3255 | |
| 3256 | if (ElementTy == IceType_i1) { |
| 3257 | // Truncate extracted integers to i1s if necessary. |
| 3258 | Variable *T = makeReg(IceType_i1); |
| 3259 | InstCast *Cast = |
| 3260 | InstCast::create(Func, InstCast::Trunc, T, ExtractedElementR); |
| 3261 | lowerCast(Cast); |
| 3262 | ExtractedElementR = T; |
| 3263 | } |
| 3264 | |
| 3265 | // Copy the element to the destination. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3266 | Variable *Dest = Instr->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3267 | _mov(Dest, ExtractedElementR); |
| 3268 | } |
| 3269 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3270 | template <typename TraitsType> |
| 3271 | void TargetX86Base<TraitsType>::lowerFcmp(const InstFcmp *Fcmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3272 | Variable *Dest = Fcmp->getDest(); |
| 3273 | |
| 3274 | if (isVectorType(Dest->getType())) { |
| 3275 | lowerFcmpVector(Fcmp); |
| 3276 | } else { |
| 3277 | constexpr Inst *Consumer = nullptr; |
| 3278 | lowerFcmpAndConsumer(Fcmp, Consumer); |
| 3279 | } |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3280 | } |
| 3281 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3282 | template <typename TraitsType> |
| 3283 | void TargetX86Base<TraitsType>::lowerFcmpAndConsumer(const InstFcmp *Fcmp, |
| 3284 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3285 | Operand *Src0 = Fcmp->getSrc(0); |
| 3286 | Operand *Src1 = Fcmp->getSrc(1); |
| 3287 | Variable *Dest = Fcmp->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3288 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3289 | if (isVectorType(Dest->getType())) |
| 3290 | llvm::report_fatal_error("Vector compare/branch cannot be folded"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3291 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3292 | if (Consumer != nullptr) { |
| 3293 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3294 | if (lowerOptimizeFcmpSelect(Fcmp, Select)) |
| 3295 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3296 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | // Lowering a = fcmp cond, b, c |
| 3300 | // ucomiss b, c /* only if C1 != Br_None */ |
| 3301 | // /* but swap b,c order if SwapOperands==true */ |
| 3302 | // mov a, <default> |
| 3303 | // j<C1> label /* only if C1 != Br_None */ |
| 3304 | // j<C2> label /* only if C2 != Br_None */ |
| 3305 | // FakeUse(a) /* only if C1 != Br_None */ |
| 3306 | // mov a, !<default> /* only if C1 != Br_None */ |
| 3307 | // label: /* only if C1 != Br_None */ |
| 3308 | // |
| 3309 | // setcc lowering when C1 != Br_None && C2 == Br_None: |
| 3310 | // ucomiss b, c /* but swap b,c order if SwapOperands==true */ |
| 3311 | // setcc a, C1 |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3312 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3313 | assert(Condition < Traits::TableFcmpSize); |
| 3314 | if (Traits::TableFcmp[Condition].SwapScalarOperands) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3315 | std::swap(Src0, Src1); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3316 | const bool HasC1 = (Traits::TableFcmp[Condition].C1 != Traits::Cond::Br_None); |
| 3317 | const bool HasC2 = (Traits::TableFcmp[Condition].C2 != Traits::Cond::Br_None); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3318 | if (HasC1) { |
| 3319 | Src0 = legalize(Src0); |
| 3320 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 3321 | Variable *T = nullptr; |
| 3322 | _mov(T, Src0); |
| 3323 | _ucomiss(T, Src1RM); |
| 3324 | if (!HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3325 | assert(Traits::TableFcmp[Condition].Default); |
| 3326 | setccOrConsumer(Traits::TableFcmp[Condition].C1, Dest, Consumer); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | } |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3330 | int32_t IntDefault = Traits::TableFcmp[Condition].Default; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3331 | if (Consumer == nullptr) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3332 | Constant *Default = Ctx->getConstantInt(Dest->getType(), IntDefault); |
| 3333 | _mov(Dest, Default); |
| 3334 | if (HasC1) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3335 | InstX86Label *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3336 | _br(Traits::TableFcmp[Condition].C1, Label); |
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, Label); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3339 | } |
| 3340 | Constant *NonDefault = Ctx->getConstantInt(Dest->getType(), !IntDefault); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3341 | _redefined(_mov(Dest, NonDefault)); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3342 | Context.insert(Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3343 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3344 | return; |
| 3345 | } |
| 3346 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3347 | CfgNode *TrueSucc = Br->getTargetTrue(); |
| 3348 | CfgNode *FalseSucc = Br->getTargetFalse(); |
| 3349 | if (IntDefault != 0) |
| 3350 | std::swap(TrueSucc, FalseSucc); |
| 3351 | if (HasC1) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3352 | _br(Traits::TableFcmp[Condition].C1, FalseSucc); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3353 | if (HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3354 | _br(Traits::TableFcmp[Condition].C2, FalseSucc); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3355 | } |
| 3356 | _br(TrueSucc); |
| 3357 | return; |
| 3358 | } |
| 3359 | _br(FalseSucc); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3360 | return; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3361 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3362 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3363 | Operand *SrcT = Select->getTrueOperand(); |
| 3364 | Operand *SrcF = Select->getFalseOperand(); |
| 3365 | Variable *SelectDest = Select->getDest(); |
| 3366 | if (IntDefault != 0) |
| 3367 | std::swap(SrcT, SrcF); |
| 3368 | lowerMove(SelectDest, SrcF, false); |
| 3369 | if (HasC1) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3370 | InstX86Label *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3371 | _br(Traits::TableFcmp[Condition].C1, Label); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3372 | if (HasC2) { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3373 | _br(Traits::TableFcmp[Condition].C2, Label); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3374 | } |
| 3375 | static constexpr bool IsRedefinition = true; |
| 3376 | lowerMove(SelectDest, SrcT, IsRedefinition); |
| 3377 | Context.insert(Label); |
| 3378 | } |
| 3379 | return; |
| 3380 | } |
| 3381 | llvm::report_fatal_error("Unexpected consumer type"); |
| 3382 | } |
| 3383 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3384 | template <typename TraitsType> |
| 3385 | void TargetX86Base<TraitsType>::lowerFcmpVector(const InstFcmp *Fcmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3386 | Operand *Src0 = Fcmp->getSrc(0); |
| 3387 | Operand *Src1 = Fcmp->getSrc(1); |
| 3388 | Variable *Dest = Fcmp->getDest(); |
| 3389 | |
| 3390 | if (!isVectorType(Dest->getType())) |
| 3391 | llvm::report_fatal_error("Expected vector compare"); |
| 3392 | |
| 3393 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3394 | assert(Condition < Traits::TableFcmpSize); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3395 | |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3396 | if (Traits::TableFcmp[Condition].SwapVectorOperands) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3397 | std::swap(Src0, Src1); |
| 3398 | |
| 3399 | Variable *T = nullptr; |
| 3400 | |
| 3401 | if (Condition == InstFcmp::True) { |
| 3402 | // makeVectorOfOnes() requires an integer vector type. |
| 3403 | T = makeVectorOfMinusOnes(IceType_v4i32); |
| 3404 | } else if (Condition == InstFcmp::False) { |
| 3405 | T = makeVectorOfZeros(Dest->getType()); |
| 3406 | } else { |
| 3407 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3408 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3409 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3410 | Src1RM = legalizeToReg(Src1RM); |
| 3411 | |
| 3412 | switch (Condition) { |
| 3413 | default: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3414 | const CmppsCond Predicate = Traits::TableFcmp[Condition].Predicate; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3415 | assert(Predicate != Traits::Cond::Cmpps_Invalid); |
| 3416 | T = makeReg(Src0RM->getType()); |
| 3417 | _movp(T, Src0RM); |
| 3418 | _cmpps(T, Src1RM, Predicate); |
| 3419 | } break; |
| 3420 | case InstFcmp::One: { |
| 3421 | // Check both unequal and ordered. |
| 3422 | T = makeReg(Src0RM->getType()); |
| 3423 | Variable *T2 = makeReg(Src0RM->getType()); |
| 3424 | _movp(T, Src0RM); |
| 3425 | _cmpps(T, Src1RM, Traits::Cond::Cmpps_neq); |
| 3426 | _movp(T2, Src0RM); |
| 3427 | _cmpps(T2, Src1RM, Traits::Cond::Cmpps_ord); |
| 3428 | _pand(T, T2); |
| 3429 | } break; |
| 3430 | case InstFcmp::Ueq: { |
| 3431 | // Check both equal or unordered. |
| 3432 | T = makeReg(Src0RM->getType()); |
| 3433 | Variable *T2 = makeReg(Src0RM->getType()); |
| 3434 | _movp(T, Src0RM); |
| 3435 | _cmpps(T, Src1RM, Traits::Cond::Cmpps_eq); |
| 3436 | _movp(T2, Src0RM); |
| 3437 | _cmpps(T2, Src1RM, Traits::Cond::Cmpps_unord); |
| 3438 | _por(T, T2); |
| 3439 | } break; |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | assert(T != nullptr); |
| 3444 | _movp(Dest, T); |
| 3445 | eliminateNextVectorSextInstruction(Dest); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3448 | inline bool isZero(const Operand *Opnd) { |
| 3449 | if (auto *C64 = llvm::dyn_cast<ConstantInteger64>(Opnd)) |
| 3450 | return C64->getValue() == 0; |
| 3451 | if (auto *C32 = llvm::dyn_cast<ConstantInteger32>(Opnd)) |
| 3452 | return C32->getValue() == 0; |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3456 | template <typename TraitsType> |
| 3457 | void TargetX86Base<TraitsType>::lowerIcmpAndConsumer(const InstIcmp *Icmp, |
| 3458 | const Inst *Consumer) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3459 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3460 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3461 | Variable *Dest = Icmp->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3462 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3463 | if (isVectorType(Dest->getType())) |
| 3464 | llvm::report_fatal_error("Vector compare/branch cannot be folded"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3465 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3466 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3467 | lowerIcmp64(Icmp, Consumer); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3468 | return; |
| 3469 | } |
| 3470 | |
| 3471 | // cmp b, c |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3472 | if (isZero(Src1)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3473 | switch (Icmp->getCondition()) { |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3474 | default: |
| 3475 | break; |
| 3476 | case InstIcmp::Uge: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3477 | movOrConsumer(true, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3478 | return; |
| 3479 | case InstIcmp::Ult: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3480 | movOrConsumer(false, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3481 | return; |
| 3482 | } |
| 3483 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3484 | Operand *Src0RM = legalizeSrc0ForCmp(Src0, Src1); |
| 3485 | _cmp(Src0RM, Src1); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3486 | setccOrConsumer(Traits::getIcmp32Mapping(Icmp->getCondition()), Dest, |
| 3487 | Consumer); |
| 3488 | } |
| 3489 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3490 | template <typename TraitsType> |
| 3491 | void TargetX86Base<TraitsType>::lowerIcmpVector(const InstIcmp *Icmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3492 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3493 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3494 | Variable *Dest = Icmp->getDest(); |
| 3495 | |
| 3496 | if (!isVectorType(Dest->getType())) |
| 3497 | llvm::report_fatal_error("Expected a vector compare"); |
| 3498 | |
| 3499 | Type Ty = Src0->getType(); |
| 3500 | // Promote i1 vectors to 128 bit integer vector types. |
| 3501 | if (typeElementType(Ty) == IceType_i1) { |
| 3502 | Type NewTy = IceType_NUM; |
| 3503 | switch (Ty) { |
| 3504 | default: |
| 3505 | llvm::report_fatal_error("unexpected type"); |
| 3506 | break; |
| 3507 | case IceType_v4i1: |
| 3508 | NewTy = IceType_v4i32; |
| 3509 | break; |
| 3510 | case IceType_v8i1: |
| 3511 | NewTy = IceType_v8i16; |
| 3512 | break; |
| 3513 | case IceType_v16i1: |
| 3514 | NewTy = IceType_v16i8; |
| 3515 | break; |
| 3516 | } |
| 3517 | Variable *NewSrc0 = Func->makeVariable(NewTy); |
| 3518 | Variable *NewSrc1 = Func->makeVariable(NewTy); |
| 3519 | lowerCast(InstCast::create(Func, InstCast::Sext, NewSrc0, Src0)); |
| 3520 | lowerCast(InstCast::create(Func, InstCast::Sext, NewSrc1, Src1)); |
| 3521 | Src0 = NewSrc0; |
| 3522 | Src1 = NewSrc1; |
| 3523 | Ty = NewTy; |
| 3524 | } |
| 3525 | |
| 3526 | InstIcmp::ICond Condition = Icmp->getCondition(); |
| 3527 | |
| 3528 | Operand *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 3529 | Operand *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 3530 | |
| 3531 | // SSE2 only has signed comparison operations. Transform unsigned inputs in |
| 3532 | // a manner that allows for the use of signed comparison operations by |
| 3533 | // flipping the high order bits. |
| 3534 | if (Condition == InstIcmp::Ugt || Condition == InstIcmp::Uge || |
| 3535 | Condition == InstIcmp::Ult || Condition == InstIcmp::Ule) { |
| 3536 | Variable *T0 = makeReg(Ty); |
| 3537 | Variable *T1 = makeReg(Ty); |
| 3538 | Variable *HighOrderBits = makeVectorOfHighOrderBits(Ty); |
| 3539 | _movp(T0, Src0RM); |
| 3540 | _pxor(T0, HighOrderBits); |
| 3541 | _movp(T1, Src1RM); |
| 3542 | _pxor(T1, HighOrderBits); |
| 3543 | Src0RM = T0; |
| 3544 | Src1RM = T1; |
| 3545 | } |
| 3546 | |
| 3547 | Variable *T = makeReg(Ty); |
| 3548 | switch (Condition) { |
| 3549 | default: |
| 3550 | llvm_unreachable("unexpected condition"); |
| 3551 | break; |
| 3552 | case InstIcmp::Eq: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3553 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3554 | Src1RM = legalizeToReg(Src1RM); |
| 3555 | _movp(T, Src0RM); |
| 3556 | _pcmpeq(T, Src1RM); |
| 3557 | } break; |
| 3558 | case InstIcmp::Ne: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3559 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3560 | Src1RM = legalizeToReg(Src1RM); |
| 3561 | _movp(T, Src0RM); |
| 3562 | _pcmpeq(T, Src1RM); |
| 3563 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3564 | _pxor(T, MinusOne); |
| 3565 | } break; |
| 3566 | case InstIcmp::Ugt: |
| 3567 | case InstIcmp::Sgt: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3568 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3569 | Src1RM = legalizeToReg(Src1RM); |
| 3570 | _movp(T, Src0RM); |
| 3571 | _pcmpgt(T, Src1RM); |
| 3572 | } break; |
| 3573 | case InstIcmp::Uge: |
| 3574 | case InstIcmp::Sge: { |
| 3575 | // !(Src1RM > Src0RM) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3576 | if (llvm::isa<X86OperandMem>(Src0RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3577 | Src0RM = legalizeToReg(Src0RM); |
| 3578 | _movp(T, Src1RM); |
| 3579 | _pcmpgt(T, Src0RM); |
| 3580 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3581 | _pxor(T, MinusOne); |
| 3582 | } break; |
| 3583 | case InstIcmp::Ult: |
| 3584 | case InstIcmp::Slt: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3585 | if (llvm::isa<X86OperandMem>(Src0RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3586 | Src0RM = legalizeToReg(Src0RM); |
| 3587 | _movp(T, Src1RM); |
| 3588 | _pcmpgt(T, Src0RM); |
| 3589 | } break; |
| 3590 | case InstIcmp::Ule: |
| 3591 | case InstIcmp::Sle: { |
| 3592 | // !(Src0RM > Src1RM) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3593 | if (llvm::isa<X86OperandMem>(Src1RM)) |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3594 | Src1RM = legalizeToReg(Src1RM); |
| 3595 | _movp(T, Src0RM); |
| 3596 | _pcmpgt(T, Src1RM); |
| 3597 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 3598 | _pxor(T, MinusOne); |
| 3599 | } break; |
| 3600 | } |
| 3601 | |
| 3602 | _movp(Dest, T); |
| 3603 | eliminateNextVectorSextInstruction(Dest); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3604 | } |
| 3605 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3606 | template <typename TraitsType> |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3607 | template <typename T> |
| 3608 | typename std::enable_if<!T::Is64Bit, void>::type |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3609 | TargetX86Base<TraitsType>::lowerIcmp64(const InstIcmp *Icmp, |
| 3610 | const Inst *Consumer) { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3611 | // 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] | 3612 | Operand *Src0 = legalize(Icmp->getSrc(0)); |
| 3613 | Operand *Src1 = legalize(Icmp->getSrc(1)); |
| 3614 | Variable *Dest = Icmp->getDest(); |
| 3615 | InstIcmp::ICond Condition = Icmp->getCondition(); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3616 | assert(Condition < Traits::TableIcmp64Size); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3617 | Operand *Src0LoRM = nullptr; |
| 3618 | Operand *Src0HiRM = nullptr; |
| 3619 | // Legalize the portions of Src0 that are going to be needed. |
| 3620 | if (isZero(Src1)) { |
| 3621 | switch (Condition) { |
| 3622 | default: |
| 3623 | llvm_unreachable("unexpected condition"); |
| 3624 | break; |
| 3625 | // These two are not optimized, so we fall through to the general case, |
| 3626 | // which needs the upper and lower halves legalized. |
| 3627 | case InstIcmp::Sgt: |
| 3628 | case InstIcmp::Sle: |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 3629 | // These four compare after performing an "or" of the high and low half, so |
| 3630 | // they need the upper and lower halves legalized. |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3631 | case InstIcmp::Eq: |
| 3632 | case InstIcmp::Ule: |
| 3633 | case InstIcmp::Ne: |
| 3634 | case InstIcmp::Ugt: |
| 3635 | Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem); |
| 3636 | // These two test only the high half's sign bit, so they need only |
| 3637 | // the upper half legalized. |
| 3638 | case InstIcmp::Sge: |
| 3639 | case InstIcmp::Slt: |
| 3640 | Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem); |
| 3641 | break; |
| 3642 | |
| 3643 | // These two move constants and hence need no legalization. |
| 3644 | case InstIcmp::Uge: |
| 3645 | case InstIcmp::Ult: |
| 3646 | break; |
| 3647 | } |
| 3648 | } else { |
| 3649 | Src0LoRM = legalize(loOperand(Src0), Legal_Reg | Legal_Mem); |
| 3650 | Src0HiRM = legalize(hiOperand(Src0), Legal_Reg | Legal_Mem); |
| 3651 | } |
| 3652 | // Optimize comparisons with zero. |
| 3653 | if (isZero(Src1)) { |
| 3654 | Constant *SignMask = Ctx->getConstantInt32(0x80000000); |
| 3655 | Variable *Temp = nullptr; |
| 3656 | switch (Condition) { |
| 3657 | default: |
| 3658 | llvm_unreachable("unexpected condition"); |
| 3659 | break; |
| 3660 | case InstIcmp::Eq: |
| 3661 | case InstIcmp::Ule: |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 3662 | // Mov Src0HiRM first, because it was legalized most recently, and will |
| 3663 | // sometimes avoid a move before the OR. |
| 3664 | _mov(Temp, Src0HiRM); |
| 3665 | _or(Temp, Src0LoRM); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3666 | Context.insert<InstFakeUse>(Temp); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3667 | setccOrConsumer(Traits::Cond::Br_e, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3668 | return; |
| 3669 | case InstIcmp::Ne: |
| 3670 | case InstIcmp::Ugt: |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 3671 | // Mov Src0HiRM first, because it was legalized most recently, and will |
| 3672 | // sometimes avoid a move before the OR. |
| 3673 | _mov(Temp, Src0HiRM); |
| 3674 | _or(Temp, Src0LoRM); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3675 | Context.insert<InstFakeUse>(Temp); |
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::Uge: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3679 | movOrConsumer(true, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3680 | return; |
| 3681 | case InstIcmp::Ult: |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3682 | movOrConsumer(false, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3683 | return; |
| 3684 | case InstIcmp::Sgt: |
| 3685 | break; |
| 3686 | case InstIcmp::Sge: |
| 3687 | _test(Src0HiRM, SignMask); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3688 | setccOrConsumer(Traits::Cond::Br_e, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3689 | return; |
| 3690 | case InstIcmp::Slt: |
| 3691 | _test(Src0HiRM, SignMask); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3692 | setccOrConsumer(Traits::Cond::Br_ne, Dest, Consumer); |
David Sehr | 5c87542 | 2015-10-15 10:38:53 -0700 | [diff] [blame] | 3693 | return; |
| 3694 | case InstIcmp::Sle: |
| 3695 | break; |
| 3696 | } |
| 3697 | } |
| 3698 | // Handle general compares. |
| 3699 | Operand *Src1LoRI = legalize(loOperand(Src1), Legal_Reg | Legal_Imm); |
| 3700 | Operand *Src1HiRI = legalize(hiOperand(Src1), Legal_Reg | Legal_Imm); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3701 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3702 | Constant *Zero = Ctx->getConstantInt(Dest->getType(), 0); |
| 3703 | Constant *One = Ctx->getConstantInt(Dest->getType(), 1); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3704 | InstX86Label *LabelFalse = InstX86Label::create(Func, this); |
| 3705 | InstX86Label *LabelTrue = InstX86Label::create(Func, this); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3706 | _mov(Dest, One); |
| 3707 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3708 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3709 | _br(Traits::TableIcmp64[Condition].C1, LabelTrue); |
| 3710 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3711 | _br(Traits::TableIcmp64[Condition].C2, LabelFalse); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3712 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3713 | _br(Traits::TableIcmp64[Condition].C3, LabelTrue); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3714 | Context.insert(LabelFalse); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3715 | _redefined(_mov(Dest, Zero)); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3716 | Context.insert(LabelTrue); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3717 | return; |
| 3718 | } |
| 3719 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3720 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3721 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3722 | _br(Traits::TableIcmp64[Condition].C1, Br->getTargetTrue()); |
| 3723 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3724 | _br(Traits::TableIcmp64[Condition].C2, Br->getTargetFalse()); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3725 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3726 | _br(Traits::TableIcmp64[Condition].C3, Br->getTargetTrue(), |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3727 | Br->getTargetFalse()); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3728 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3729 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3730 | if (auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3731 | Operand *SrcT = Select->getTrueOperand(); |
| 3732 | Operand *SrcF = Select->getFalseOperand(); |
| 3733 | Variable *SelectDest = Select->getDest(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3734 | InstX86Label *LabelFalse = InstX86Label::create(Func, this); |
| 3735 | InstX86Label *LabelTrue = InstX86Label::create(Func, this); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3736 | lowerMove(SelectDest, SrcT, false); |
| 3737 | _cmp(Src0HiRM, Src1HiRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3738 | if (Traits::TableIcmp64[Condition].C1 != Traits::Cond::Br_None) |
| 3739 | _br(Traits::TableIcmp64[Condition].C1, LabelTrue); |
| 3740 | if (Traits::TableIcmp64[Condition].C2 != Traits::Cond::Br_None) |
| 3741 | _br(Traits::TableIcmp64[Condition].C2, LabelFalse); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3742 | _cmp(Src0LoRM, Src1LoRI); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3743 | _br(Traits::TableIcmp64[Condition].C3, LabelTrue); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3744 | Context.insert(LabelFalse); |
| 3745 | static constexpr bool IsRedefinition = true; |
| 3746 | lowerMove(SelectDest, SrcF, IsRedefinition); |
| 3747 | Context.insert(LabelTrue); |
| 3748 | return; |
| 3749 | } |
| 3750 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3753 | template <typename TraitsType> |
| 3754 | void TargetX86Base<TraitsType>::setccOrConsumer(BrCond Condition, |
| 3755 | Variable *Dest, |
| 3756 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3757 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3758 | _setcc(Dest, Condition); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3759 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3760 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3761 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
| 3762 | _br(Condition, Br->getTargetTrue(), Br->getTargetFalse()); |
| 3763 | return; |
| 3764 | } |
| 3765 | if (const auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3766 | Operand *SrcT = Select->getTrueOperand(); |
| 3767 | Operand *SrcF = Select->getFalseOperand(); |
| 3768 | Variable *SelectDest = Select->getDest(); |
| 3769 | lowerSelectMove(SelectDest, Condition, SrcT, SrcF); |
| 3770 | return; |
| 3771 | } |
| 3772 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3775 | template <typename TraitsType> |
| 3776 | void TargetX86Base<TraitsType>::movOrConsumer(bool IcmpResult, Variable *Dest, |
| 3777 | const Inst *Consumer) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3778 | if (Consumer == nullptr) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3779 | _mov(Dest, Ctx->getConstantInt(Dest->getType(), (IcmpResult ? 1 : 0))); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3780 | return; |
| 3781 | } |
| 3782 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3783 | // TODO(sehr,stichnot): This could be done with a single unconditional |
| 3784 | // branch instruction, but subzero doesn't know how to handle the resulting |
| 3785 | // control flow graph changes now. Make it do so to eliminate mov and cmp. |
| 3786 | _mov(Dest, Ctx->getConstantInt(Dest->getType(), (IcmpResult ? 1 : 0))); |
| 3787 | _cmp(Dest, Ctx->getConstantInt(Dest->getType(), 0)); |
| 3788 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3789 | return; |
David Sehr | d981025 | 2015-10-16 13:23:17 -0700 | [diff] [blame] | 3790 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3791 | if (const auto *Select = llvm::dyn_cast<InstSelect>(Consumer)) { |
| 3792 | Operand *Src = nullptr; |
| 3793 | if (IcmpResult) { |
| 3794 | Src = legalize(Select->getTrueOperand(), Legal_Reg | Legal_Imm); |
| 3795 | } else { |
| 3796 | Src = legalize(Select->getFalseOperand(), Legal_Reg | Legal_Imm); |
| 3797 | } |
| 3798 | Variable *SelectDest = Select->getDest(); |
| 3799 | lowerMove(SelectDest, Src, false); |
| 3800 | return; |
| 3801 | } |
| 3802 | llvm::report_fatal_error("Unexpected consumer type"); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 3803 | } |
| 3804 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3805 | template <typename TraitsType> |
| 3806 | void TargetX86Base<TraitsType>::lowerArithAndConsumer( |
| 3807 | const InstArithmetic *Arith, const Inst *Consumer) { |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3808 | Variable *T = nullptr; |
| 3809 | Operand *Src0 = legalize(Arith->getSrc(0)); |
| 3810 | Operand *Src1 = legalize(Arith->getSrc(1)); |
| 3811 | Variable *Dest = Arith->getDest(); |
| 3812 | switch (Arith->getOp()) { |
| 3813 | default: |
| 3814 | llvm_unreachable("arithmetic operator not AND or OR"); |
| 3815 | break; |
| 3816 | case InstArithmetic::And: |
| 3817 | _mov(T, Src0); |
| 3818 | // Test cannot have an address in the second position. Since T is |
| 3819 | // guaranteed to be a register and Src1 could be a memory load, ensure |
| 3820 | // that the second argument is a register. |
| 3821 | if (llvm::isa<Constant>(Src1)) |
| 3822 | _test(T, Src1); |
| 3823 | else |
| 3824 | _test(Src1, T); |
| 3825 | break; |
| 3826 | case InstArithmetic::Or: |
| 3827 | _mov(T, Src0); |
| 3828 | _or(T, Src1); |
| 3829 | break; |
| 3830 | } |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3831 | |
| 3832 | if (Consumer == nullptr) { |
| 3833 | llvm::report_fatal_error("Expected a consumer instruction"); |
| 3834 | } |
| 3835 | if (const auto *Br = llvm::dyn_cast<InstBr>(Consumer)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 3836 | Context.insert<InstFakeUse>(T); |
| 3837 | Context.insert<InstFakeDef>(Dest); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 3838 | _br(Traits::Cond::Br_ne, Br->getTargetTrue(), Br->getTargetFalse()); |
| 3839 | return; |
| 3840 | } |
| 3841 | llvm::report_fatal_error("Unexpected consumer type"); |
David Sehr | daf096c | 2015-11-11 10:56:58 -0800 | [diff] [blame] | 3842 | } |
| 3843 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3844 | template <typename TraitsType> |
| 3845 | void TargetX86Base<TraitsType>::lowerInsertElement( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3846 | const InstInsertElement *Instr) { |
| 3847 | Operand *SourceVectNotLegalized = Instr->getSrc(0); |
| 3848 | Operand *ElementToInsertNotLegalized = Instr->getSrc(1); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 3849 | auto *ElementIndex = llvm::dyn_cast<ConstantInteger32>(Instr->getSrc(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3850 | // Only constant indices are allowed in PNaCl IR. |
| 3851 | assert(ElementIndex); |
| 3852 | unsigned Index = ElementIndex->getValue(); |
| 3853 | assert(Index < typeNumElements(SourceVectNotLegalized->getType())); |
| 3854 | |
| 3855 | Type Ty = SourceVectNotLegalized->getType(); |
| 3856 | Type ElementTy = typeElementType(Ty); |
| 3857 | Type InVectorElementTy = Traits::getInVectorElementType(Ty); |
| 3858 | |
| 3859 | if (ElementTy == IceType_i1) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3860 | // Expand the element to the appropriate size for it to be inserted in the |
| 3861 | // vector. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3862 | Variable *Expanded = Func->makeVariable(InVectorElementTy); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3863 | auto *Cast = InstCast::create(Func, InstCast::Zext, Expanded, |
| 3864 | ElementToInsertNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3865 | lowerCast(Cast); |
| 3866 | ElementToInsertNotLegalized = Expanded; |
| 3867 | } |
| 3868 | |
| 3869 | if (Ty == IceType_v8i16 || Ty == IceType_v8i1 || |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 3870 | InstructionSet >= Traits::SSE4_1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3871 | // Use insertps, pinsrb, pinsrw, or pinsrd. |
| 3872 | Operand *ElementRM = |
| 3873 | legalize(ElementToInsertNotLegalized, Legal_Reg | Legal_Mem); |
| 3874 | Operand *SourceVectRM = |
| 3875 | legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem); |
| 3876 | Variable *T = makeReg(Ty); |
| 3877 | _movp(T, SourceVectRM); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3878 | if (Ty == IceType_v4f32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3879 | _insertps(T, ElementRM, Ctx->getConstantInt32(Index << 4)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3880 | } else { |
| 3881 | // For the pinsrb and pinsrw instructions, when the source operand is a |
| 3882 | // 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] | 3883 | // For filetype=asm, InstX86Pinsr<TraitsType>::emit() compensates for |
| 3884 | // the use |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3885 | // of r16 and r8 by converting them through getBaseReg(), while emitIAS() |
| 3886 | // validates that the original and base register encodings are the same. |
| 3887 | if (ElementRM->getType() == IceType_i8 && |
| 3888 | llvm::isa<Variable>(ElementRM)) { |
| 3889 | // Don't use ah/bh/ch/dh for pinsrb. |
| 3890 | ElementRM = copyToReg8(ElementRM); |
| 3891 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3892 | _pinsr(T, ElementRM, Ctx->getConstantInt32(Index)); |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 3893 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3894 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3895 | } else if (Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v4i1) { |
| 3896 | // Use shufps or movss. |
| 3897 | Variable *ElementR = nullptr; |
| 3898 | Operand *SourceVectRM = |
| 3899 | legalize(SourceVectNotLegalized, Legal_Reg | Legal_Mem); |
| 3900 | |
| 3901 | if (InVectorElementTy == IceType_f32) { |
| 3902 | // ElementR will be in an XMM register since it is floating point. |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3903 | ElementR = legalizeToReg(ElementToInsertNotLegalized); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3904 | } else { |
| 3905 | // Copy an integer to an XMM register. |
| 3906 | Operand *T = legalize(ElementToInsertNotLegalized, Legal_Reg | Legal_Mem); |
| 3907 | ElementR = makeReg(Ty); |
| 3908 | _movd(ElementR, T); |
| 3909 | } |
| 3910 | |
| 3911 | if (Index == 0) { |
| 3912 | Variable *T = makeReg(Ty); |
| 3913 | _movp(T, SourceVectRM); |
| 3914 | _movss(T, ElementR); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3915 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3916 | return; |
| 3917 | } |
| 3918 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3919 | // shufps treats the source and destination operands as vectors of four |
| 3920 | // doublewords. The destination's two high doublewords are selected from |
| 3921 | // the source operand and the two low doublewords are selected from the |
| 3922 | // (original value of) the destination operand. An insertelement operation |
| 3923 | // can be effected with a sequence of two shufps operations with |
| 3924 | // appropriate masks. In all cases below, Element[0] is being inserted into |
| 3925 | // SourceVectOperand. Indices are ordered from left to right. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3926 | // |
| 3927 | // insertelement into index 1 (result is stored in ElementR): |
| 3928 | // ElementR := ElementR[0, 0] SourceVectRM[0, 0] |
| 3929 | // ElementR := ElementR[3, 0] SourceVectRM[2, 3] |
| 3930 | // |
| 3931 | // insertelement into index 2 (result is stored in T): |
| 3932 | // T := SourceVectRM |
| 3933 | // ElementR := ElementR[0, 0] T[0, 3] |
| 3934 | // T := T[0, 1] ElementR[0, 3] |
| 3935 | // |
| 3936 | // insertelement into index 3 (result is stored in T): |
| 3937 | // T := SourceVectRM |
| 3938 | // ElementR := ElementR[0, 0] T[0, 2] |
| 3939 | // T := T[0, 1] ElementR[3, 0] |
| 3940 | const unsigned char Mask1[3] = {0, 192, 128}; |
| 3941 | const unsigned char Mask2[3] = {227, 196, 52}; |
| 3942 | |
| 3943 | Constant *Mask1Constant = Ctx->getConstantInt32(Mask1[Index - 1]); |
| 3944 | Constant *Mask2Constant = Ctx->getConstantInt32(Mask2[Index - 1]); |
| 3945 | |
| 3946 | if (Index == 1) { |
| 3947 | _shufps(ElementR, SourceVectRM, Mask1Constant); |
| 3948 | _shufps(ElementR, SourceVectRM, Mask2Constant); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3949 | _movp(Instr->getDest(), ElementR); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3950 | } else { |
| 3951 | Variable *T = makeReg(Ty); |
| 3952 | _movp(T, SourceVectRM); |
| 3953 | _shufps(ElementR, T, Mask1Constant); |
| 3954 | _shufps(T, ElementR, Mask2Constant); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3955 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3956 | } |
| 3957 | } else { |
| 3958 | assert(Ty == IceType_v16i8 || Ty == IceType_v16i1); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3959 | // 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] | 3960 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3961 | // TODO(wala): use legalize(SourceVectNotLegalized, Legal_Mem) when support |
| 3962 | // for legalizing to mem is implemented. |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 3963 | Variable *Slot = Func->makeVariable(Ty); |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 3964 | Slot->setMustNotHaveReg(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3965 | _movp(Slot, legalizeToReg(SourceVectNotLegalized)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3966 | |
| 3967 | // Compute the location of the position to insert in memory. |
| 3968 | unsigned Offset = Index * typeWidthInBytes(InVectorElementTy); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3969 | X86OperandMem *Loc = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3970 | getMemoryOperandForStackSlot(InVectorElementTy, Slot, Offset); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 3971 | _store(legalizeToReg(ElementToInsertNotLegalized), Loc); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3972 | |
| 3973 | Variable *T = makeReg(Ty); |
| 3974 | _movp(T, Slot); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 3975 | _movp(Instr->getDest(), T); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 3979 | template <typename TraitsType> |
| 3980 | void TargetX86Base<TraitsType>::lowerIntrinsicCall( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3981 | const InstIntrinsicCall *Instr) { |
| 3982 | switch (Intrinsics::IntrinsicID ID = Instr->getIntrinsicInfo().ID) { |
| 3983 | case Intrinsics::AtomicCmpxchg: { |
| 3984 | if (!Intrinsics::isMemoryOrderValid( |
| 3985 | ID, getConstantMemoryOrder(Instr->getArg(3)), |
| 3986 | getConstantMemoryOrder(Instr->getArg(4)))) { |
| 3987 | Func->setError("Unexpected memory ordering for AtomicCmpxchg"); |
| 3988 | return; |
| 3989 | } |
| 3990 | Variable *DestPrev = Instr->getDest(); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 3991 | Operand *PtrToMem = legalize(Instr->getArg(0)); |
| 3992 | Operand *Expected = legalize(Instr->getArg(1)); |
| 3993 | Operand *Desired = legalize(Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 3994 | if (tryOptimizedCmpxchgCmpBr(DestPrev, PtrToMem, Expected, Desired)) |
| 3995 | return; |
| 3996 | lowerAtomicCmpxchg(DestPrev, PtrToMem, Expected, Desired); |
| 3997 | return; |
| 3998 | } |
| 3999 | case Intrinsics::AtomicFence: |
| 4000 | if (!Intrinsics::isMemoryOrderValid( |
| 4001 | ID, getConstantMemoryOrder(Instr->getArg(0)))) { |
| 4002 | Func->setError("Unexpected memory ordering for AtomicFence"); |
| 4003 | return; |
| 4004 | } |
| 4005 | _mfence(); |
| 4006 | return; |
| 4007 | case Intrinsics::AtomicFenceAll: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4008 | // NOTE: FenceAll should prevent and load/store from being moved across the |
| 4009 | // fence (both atomic and non-atomic). The InstX8632Mfence instruction is |
| 4010 | // currently marked coarsely as "HasSideEffects". |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4011 | _mfence(); |
| 4012 | return; |
| 4013 | case Intrinsics::AtomicIsLockFree: { |
| 4014 | // X86 is always lock free for 8/16/32/64 bit accesses. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4015 | // TODO(jvoung): Since the result is constant when given a constant byte |
| 4016 | // size, this opens up DCE opportunities. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4017 | Operand *ByteSize = Instr->getArg(0); |
| 4018 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4019 | if (auto *CI = llvm::dyn_cast<ConstantInteger32>(ByteSize)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4020 | Constant *Result; |
| 4021 | switch (CI->getValue()) { |
| 4022 | default: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4023 | // Some x86-64 processors support the cmpxchg16b instruction, which can |
| 4024 | // make 16-byte operations lock free (when used with the LOCK prefix). |
| 4025 | // However, that's not supported in 32-bit mode, so just return 0 even |
| 4026 | // for large sizes. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4027 | Result = Ctx->getConstantZero(IceType_i32); |
| 4028 | break; |
| 4029 | case 1: |
| 4030 | case 2: |
| 4031 | case 4: |
| 4032 | case 8: |
| 4033 | Result = Ctx->getConstantInt32(1); |
| 4034 | break; |
| 4035 | } |
| 4036 | _mov(Dest, Result); |
| 4037 | return; |
| 4038 | } |
| 4039 | // The PNaCl ABI requires the byte size to be a compile-time constant. |
| 4040 | Func->setError("AtomicIsLockFree byte size should be compile-time const"); |
| 4041 | return; |
| 4042 | } |
| 4043 | case Intrinsics::AtomicLoad: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4044 | // We require the memory address to be naturally aligned. Given that is the |
| 4045 | // case, then normal loads are atomic. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4046 | if (!Intrinsics::isMemoryOrderValid( |
| 4047 | ID, getConstantMemoryOrder(Instr->getArg(1)))) { |
| 4048 | Func->setError("Unexpected memory ordering for AtomicLoad"); |
| 4049 | return; |
| 4050 | } |
| 4051 | Variable *Dest = Instr->getDest(); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4052 | if (!Traits::Is64Bit) { |
| 4053 | if (auto *Dest64On32 = llvm::dyn_cast<Variable64On32>(Dest)) { |
| 4054 | // Follow what GCC does and use a movq instead of what lowerLoad() |
| 4055 | // normally does (split the load into two). Thus, this skips |
| 4056 | // load/arithmetic op folding. Load/arithmetic folding can't happen |
| 4057 | // anyway, since this is x86-32 and integer arithmetic only happens on |
| 4058 | // 32-bit quantities. |
| 4059 | Variable *T = makeReg(IceType_f64); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4060 | X86OperandMem *Addr = formMemoryOperand(Instr->getArg(0), IceType_f64); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4061 | _movq(T, Addr); |
| 4062 | // 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] | 4063 | auto *Cast = InstCast::create(Func, InstCast::Bitcast, Dest, T); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4064 | lowerCast(Cast); |
| 4065 | // Make sure that the atomic load isn't elided when unused. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4066 | Context.insert<InstFakeUse>(Dest64On32->getLo()); |
| 4067 | Context.insert<InstFakeUse>(Dest64On32->getHi()); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 4068 | return; |
| 4069 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4070 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4071 | auto *Load = InstLoad::create(Func, Dest, Instr->getArg(0)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4072 | lowerLoad(Load); |
| 4073 | // 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] | 4074 | // Since lowerLoad may fuse the load w/ an arithmetic instruction, insert |
| 4075 | // the FakeUse on the last-inserted instruction's dest. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4076 | Context.insert<InstFakeUse>(Context.getLastInserted()->getDest()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4077 | return; |
| 4078 | } |
| 4079 | case Intrinsics::AtomicRMW: |
| 4080 | if (!Intrinsics::isMemoryOrderValid( |
| 4081 | ID, getConstantMemoryOrder(Instr->getArg(3)))) { |
| 4082 | Func->setError("Unexpected memory ordering for AtomicRMW"); |
| 4083 | return; |
| 4084 | } |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 4085 | lowerAtomicRMW( |
| 4086 | Instr->getDest(), |
| 4087 | static_cast<uint32_t>( |
| 4088 | llvm::cast<ConstantInteger32>(Instr->getArg(0))->getValue()), |
| 4089 | Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4090 | return; |
| 4091 | case Intrinsics::AtomicStore: { |
| 4092 | if (!Intrinsics::isMemoryOrderValid( |
| 4093 | ID, getConstantMemoryOrder(Instr->getArg(2)))) { |
| 4094 | Func->setError("Unexpected memory ordering for AtomicStore"); |
| 4095 | return; |
| 4096 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4097 | // We require the memory address to be naturally aligned. Given that is the |
| 4098 | // case, then normal stores are atomic. Add a fence after the store to make |
| 4099 | // it visible. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4100 | Operand *Value = Instr->getArg(0); |
| 4101 | Operand *Ptr = Instr->getArg(1); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4102 | if (!Traits::Is64Bit && Value->getType() == IceType_i64) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4103 | // Use a movq instead of what lowerStore() normally does (split the store |
| 4104 | // into two), following what GCC does. Cast the bits from int -> to an |
| 4105 | // xmm register first. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4106 | Variable *T = makeReg(IceType_f64); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4107 | auto *Cast = InstCast::create(Func, InstCast::Bitcast, T, Value); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4108 | lowerCast(Cast); |
| 4109 | // Then store XMM w/ a movq. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4110 | X86OperandMem *Addr = formMemoryOperand(Ptr, IceType_f64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4111 | _storeq(T, Addr); |
| 4112 | _mfence(); |
| 4113 | return; |
| 4114 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4115 | auto *Store = InstStore::create(Func, Value, Ptr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4116 | lowerStore(Store); |
| 4117 | _mfence(); |
| 4118 | return; |
| 4119 | } |
| 4120 | case Intrinsics::Bswap: { |
| 4121 | Variable *Dest = Instr->getDest(); |
| 4122 | Operand *Val = Instr->getArg(0); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4123 | // In 32-bit mode, bswap only works on 32-bit arguments, and the argument |
| 4124 | // must be a register. Use rotate left for 16-bit bswap. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4125 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 4126 | Val = legalizeUndef(Val); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4127 | Variable *T_Lo = legalizeToReg(loOperand(Val)); |
| 4128 | Variable *T_Hi = legalizeToReg(hiOperand(Val)); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4129 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4130 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4131 | _bswap(T_Lo); |
| 4132 | _bswap(T_Hi); |
| 4133 | _mov(DestLo, T_Hi); |
| 4134 | _mov(DestHi, T_Lo); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4135 | } else if ((Traits::Is64Bit && Val->getType() == IceType_i64) || |
| 4136 | Val->getType() == IceType_i32) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4137 | Variable *T = legalizeToReg(Val); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4138 | _bswap(T); |
| 4139 | _mov(Dest, T); |
| 4140 | } else { |
| 4141 | assert(Val->getType() == IceType_i16); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4142 | Constant *Eight = Ctx->getConstantInt16(8); |
| 4143 | Variable *T = nullptr; |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 4144 | Val = legalize(Val); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4145 | _mov(T, Val); |
| 4146 | _rol(T, Eight); |
| 4147 | _mov(Dest, T); |
| 4148 | } |
| 4149 | return; |
| 4150 | } |
| 4151 | case Intrinsics::Ctpop: { |
| 4152 | Variable *Dest = Instr->getDest(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4153 | Variable *T = nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4154 | Operand *Val = Instr->getArg(0); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4155 | Type ValTy = Val->getType(); |
| 4156 | assert(ValTy == IceType_i32 || ValTy == IceType_i64); |
| 4157 | |
| 4158 | if (!Traits::Is64Bit) { |
| 4159 | T = Dest; |
| 4160 | } else { |
| 4161 | T = makeReg(IceType_i64); |
| 4162 | if (ValTy == IceType_i32) { |
| 4163 | // in x86-64, __popcountsi2 is not defined, so we cheat a bit by |
| 4164 | // converting it to a 64-bit value, and using ctpop_i64. _movzx should |
| 4165 | // ensure we will not have any bits set on Val's upper 32 bits. |
| 4166 | Variable *V = makeReg(IceType_i64); |
| 4167 | _movzx(V, Val); |
| 4168 | Val = V; |
| 4169 | } |
| 4170 | ValTy = IceType_i64; |
| 4171 | } |
| 4172 | |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4173 | InstCall *Call = |
| 4174 | makeHelperCall(ValTy == IceType_i32 ? RuntimeHelper::H_call_ctpop_i32 |
| 4175 | : RuntimeHelper::H_call_ctpop_i64, |
| 4176 | T, 1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4177 | Call->addArg(Val); |
| 4178 | lowerCall(Call); |
| 4179 | // The popcount helpers always return 32-bit values, while the intrinsic's |
| 4180 | // signature matches the native POPCNT instruction and fills a 64-bit reg |
| 4181 | // (in 64-bit mode). Thus, clear the upper bits of the dest just in case |
| 4182 | // the user doesn't do that in the IR. If the user does that in the IR, |
| 4183 | // then this zero'ing instruction is dead and gets optimized out. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4184 | if (!Traits::Is64Bit) { |
| 4185 | assert(T == Dest); |
| 4186 | if (Val->getType() == IceType_i64) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4187 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4188 | Constant *Zero = Ctx->getConstantZero(IceType_i32); |
| 4189 | _mov(DestHi, Zero); |
| 4190 | } |
| 4191 | } else { |
| 4192 | assert(Val->getType() == IceType_i64); |
| 4193 | // T is 64 bit. It needs to be copied to dest. We need to: |
| 4194 | // |
| 4195 | // T_1.32 = trunc T.64 to i32 |
| 4196 | // T_2.64 = zext T_1.32 to i64 |
| 4197 | // Dest.<<right_size>> = T_2.<<right_size>> |
| 4198 | // |
| 4199 | // which ensures the upper 32 bits will always be cleared. Just doing a |
| 4200 | // |
| 4201 | // mov Dest.32 = trunc T.32 to i32 |
| 4202 | // |
| 4203 | // is dangerous because there's a chance the compiler will optimize this |
| 4204 | // copy out. To use _movzx we need two new registers (one 32-, and |
| 4205 | // another 64-bit wide.) |
| 4206 | Variable *T_1 = makeReg(IceType_i32); |
| 4207 | _mov(T_1, T); |
| 4208 | Variable *T_2 = makeReg(IceType_i64); |
| 4209 | _movzx(T_2, T_1); |
| 4210 | _mov(Dest, T_2); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4211 | } |
| 4212 | return; |
| 4213 | } |
| 4214 | case Intrinsics::Ctlz: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4215 | // The "is zero undef" parameter is ignored and we always return a |
| 4216 | // well-defined value. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4217 | Operand *Val = legalize(Instr->getArg(0)); |
| 4218 | Operand *FirstVal; |
| 4219 | Operand *SecondVal = nullptr; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4220 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4221 | FirstVal = loOperand(Val); |
| 4222 | SecondVal = hiOperand(Val); |
| 4223 | } else { |
| 4224 | FirstVal = Val; |
| 4225 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4226 | constexpr bool IsCttz = false; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4227 | lowerCountZeros(IsCttz, Val->getType(), Instr->getDest(), FirstVal, |
| 4228 | SecondVal); |
| 4229 | return; |
| 4230 | } |
| 4231 | case Intrinsics::Cttz: { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4232 | // The "is zero undef" parameter is ignored and we always return a |
| 4233 | // well-defined value. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4234 | Operand *Val = legalize(Instr->getArg(0)); |
| 4235 | Operand *FirstVal; |
| 4236 | Operand *SecondVal = nullptr; |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4237 | if (!Traits::Is64Bit && Val->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4238 | FirstVal = hiOperand(Val); |
| 4239 | SecondVal = loOperand(Val); |
| 4240 | } else { |
| 4241 | FirstVal = Val; |
| 4242 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4243 | constexpr bool IsCttz = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4244 | lowerCountZeros(IsCttz, Val->getType(), Instr->getDest(), FirstVal, |
| 4245 | SecondVal); |
| 4246 | return; |
| 4247 | } |
| 4248 | case Intrinsics::Fabs: { |
| 4249 | Operand *Src = legalize(Instr->getArg(0)); |
| 4250 | Type Ty = Src->getType(); |
| 4251 | Variable *Dest = Instr->getDest(); |
| 4252 | Variable *T = makeVectorOfFabsMask(Ty); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4253 | // The pand instruction operates on an m128 memory operand, so if Src is an |
| 4254 | // 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] | 4255 | if (isVectorType(Ty)) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4256 | if (llvm::isa<X86OperandMem>(Src)) |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4257 | Src = legalizeToReg(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4258 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4259 | Src = legalizeToReg(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4260 | } |
| 4261 | _pand(T, Src); |
| 4262 | if (isVectorType(Ty)) |
| 4263 | _movp(Dest, T); |
| 4264 | else |
| 4265 | _mov(Dest, T); |
| 4266 | return; |
| 4267 | } |
| 4268 | case Intrinsics::Longjmp: { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4269 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_longjmp, nullptr, 2); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4270 | Call->addArg(Instr->getArg(0)); |
| 4271 | Call->addArg(Instr->getArg(1)); |
| 4272 | lowerCall(Call); |
| 4273 | return; |
| 4274 | } |
| 4275 | case Intrinsics::Memcpy: { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4276 | lowerMemcpy(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4277 | return; |
| 4278 | } |
| 4279 | case Intrinsics::Memmove: { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4280 | lowerMemmove(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4281 | return; |
| 4282 | } |
| 4283 | case Intrinsics::Memset: { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4284 | lowerMemset(Instr->getArg(0), Instr->getArg(1), Instr->getArg(2)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4285 | return; |
| 4286 | } |
| 4287 | case Intrinsics::NaClReadTP: { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4288 | if (NeedSandboxing) { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4289 | Operand *Src = |
| 4290 | dispatchToConcrete(&ConcreteTarget::createNaClReadTPSrcOperand); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4291 | Variable *Dest = Instr->getDest(); |
| 4292 | Variable *T = nullptr; |
| 4293 | _mov(T, Src); |
| 4294 | _mov(Dest, T); |
| 4295 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4296 | InstCall *Call = |
| 4297 | makeHelperCall(RuntimeHelper::H_call_read_tp, Instr->getDest(), 0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4298 | lowerCall(Call); |
| 4299 | } |
| 4300 | return; |
| 4301 | } |
| 4302 | case Intrinsics::Setjmp: { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4303 | InstCall *Call = |
| 4304 | makeHelperCall(RuntimeHelper::H_call_setjmp, Instr->getDest(), 1); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4305 | Call->addArg(Instr->getArg(0)); |
| 4306 | lowerCall(Call); |
| 4307 | return; |
| 4308 | } |
| 4309 | case Intrinsics::Sqrt: { |
| 4310 | Operand *Src = legalize(Instr->getArg(0)); |
| 4311 | Variable *Dest = Instr->getDest(); |
| 4312 | Variable *T = makeReg(Dest->getType()); |
| 4313 | _sqrtss(T, Src); |
| 4314 | _mov(Dest, T); |
| 4315 | return; |
| 4316 | } |
| 4317 | case Intrinsics::Stacksave: { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4318 | if (!Traits::Is64Bit || !NeedSandboxing) { |
| 4319 | Variable *esp = Func->getTarget()->getPhysicalRegister(getStackReg(), |
| 4320 | Traits::WordType); |
| 4321 | Variable *Dest = Instr->getDest(); |
| 4322 | _mov(Dest, esp); |
| 4323 | return; |
| 4324 | } |
| 4325 | Variable *esp = Func->getTarget()->getPhysicalRegister( |
| 4326 | Traits::RegisterSet::Reg_esp, IceType_i32); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4327 | Variable *Dest = Instr->getDest(); |
| 4328 | _mov(Dest, esp); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4329 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4330 | return; |
| 4331 | } |
| 4332 | case Intrinsics::Stackrestore: { |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 4333 | Operand *Src = Instr->getArg(0); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4334 | _mov_sp(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4335 | return; |
| 4336 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 4337 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4338 | case Intrinsics::Trap: |
| 4339 | _ud2(); |
| 4340 | return; |
| 4341 | case Intrinsics::UnknownIntrinsic: |
| 4342 | Func->setError("Should not be lowering UnknownIntrinsic"); |
| 4343 | return; |
| 4344 | } |
| 4345 | return; |
| 4346 | } |
| 4347 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4348 | template <typename TraitsType> |
| 4349 | void TargetX86Base<TraitsType>::lowerAtomicCmpxchg(Variable *DestPrev, |
| 4350 | Operand *Ptr, |
| 4351 | Operand *Expected, |
| 4352 | Operand *Desired) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4353 | Type Ty = Expected->getType(); |
| 4354 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4355 | // Reserve the pre-colored registers first, before adding any more |
| 4356 | // infinite-weight variables from formMemoryOperand's legalization. |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4357 | Variable *T_edx = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 4358 | Variable *T_eax = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
| 4359 | Variable *T_ecx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ecx); |
| 4360 | Variable *T_ebx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ebx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4361 | _mov(T_eax, loOperand(Expected)); |
| 4362 | _mov(T_edx, hiOperand(Expected)); |
| 4363 | _mov(T_ebx, loOperand(Desired)); |
| 4364 | _mov(T_ecx, hiOperand(Desired)); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4365 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4366 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4367 | _cmpxchg8b(Addr, T_edx, T_eax, T_ecx, T_ebx, Locked); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4368 | auto *DestLo = llvm::cast<Variable>(loOperand(DestPrev)); |
| 4369 | auto *DestHi = llvm::cast<Variable>(hiOperand(DestPrev)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4370 | _mov(DestLo, T_eax); |
| 4371 | _mov(DestHi, T_edx); |
| 4372 | return; |
| 4373 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 4374 | RegNumT Eax; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4375 | switch (Ty) { |
| 4376 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4377 | llvm::report_fatal_error("Bad type for cmpxchg"); |
| 4378 | case IceType_i64: |
| 4379 | Eax = Traits::getRaxOrDie(); |
| 4380 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4381 | case IceType_i32: |
| 4382 | Eax = Traits::RegisterSet::Reg_eax; |
| 4383 | break; |
| 4384 | case IceType_i16: |
| 4385 | Eax = Traits::RegisterSet::Reg_ax; |
| 4386 | break; |
| 4387 | case IceType_i8: |
| 4388 | Eax = Traits::RegisterSet::Reg_al; |
| 4389 | break; |
| 4390 | } |
| 4391 | Variable *T_eax = makeReg(Ty, Eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4392 | _mov(T_eax, Expected); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4393 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4394 | Variable *DesiredReg = legalizeToReg(Desired); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4395 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4396 | _cmpxchg(Addr, T_eax, DesiredReg, Locked); |
| 4397 | _mov(DestPrev, T_eax); |
| 4398 | } |
| 4399 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4400 | template <typename TraitsType> |
| 4401 | bool TargetX86Base<TraitsType>::tryOptimizedCmpxchgCmpBr(Variable *Dest, |
| 4402 | Operand *PtrToMem, |
| 4403 | Operand *Expected, |
| 4404 | Operand *Desired) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 4405 | if (Func->getOptLevel() == Opt_m1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4406 | return false; |
| 4407 | // Peek ahead a few instructions and see how Dest is used. |
| 4408 | // It's very common to have: |
| 4409 | // |
| 4410 | // %x = call i32 @llvm.nacl.atomic.cmpxchg.i32(i32* ptr, i32 %expected, ...) |
| 4411 | // [%y_phi = ...] // list of phi stores |
| 4412 | // %p = icmp eq i32 %x, %expected |
| 4413 | // br i1 %p, label %l1, label %l2 |
| 4414 | // |
| 4415 | // which we can optimize into: |
| 4416 | // |
| 4417 | // %x = <cmpxchg code> |
| 4418 | // [%y_phi = ...] // list of phi stores |
| 4419 | // br eq, %l1, %l2 |
| 4420 | InstList::iterator I = Context.getCur(); |
| 4421 | // I is currently the InstIntrinsicCall. Peek past that. |
| 4422 | // This assumes that the atomic cmpxchg has not been lowered yet, |
| 4423 | // so that the instructions seen in the scan from "Cur" is simple. |
| 4424 | assert(llvm::isa<InstIntrinsicCall>(*I)); |
| 4425 | Inst *NextInst = Context.getNextInst(I); |
| 4426 | if (!NextInst) |
| 4427 | return false; |
| 4428 | // There might be phi assignments right before the compare+branch, since this |
| 4429 | // could be a backward branch for a loop. This placement of assignments is |
| 4430 | // determined by placePhiStores(). |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 4431 | CfgVector<InstAssign *> PhiAssigns; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4432 | while (auto *PhiAssign = llvm::dyn_cast<InstAssign>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4433 | if (PhiAssign->getDest() == Dest) |
| 4434 | return false; |
| 4435 | PhiAssigns.push_back(PhiAssign); |
| 4436 | NextInst = Context.getNextInst(I); |
| 4437 | if (!NextInst) |
| 4438 | return false; |
| 4439 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4440 | if (auto *NextCmp = llvm::dyn_cast<InstIcmp>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4441 | if (!(NextCmp->getCondition() == InstIcmp::Eq && |
| 4442 | ((NextCmp->getSrc(0) == Dest && NextCmp->getSrc(1) == Expected) || |
| 4443 | (NextCmp->getSrc(1) == Dest && NextCmp->getSrc(0) == Expected)))) { |
| 4444 | return false; |
| 4445 | } |
| 4446 | NextInst = Context.getNextInst(I); |
| 4447 | if (!NextInst) |
| 4448 | return false; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4449 | if (auto *NextBr = llvm::dyn_cast<InstBr>(NextInst)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4450 | if (!NextBr->isUnconditional() && |
| 4451 | NextCmp->getDest() == NextBr->getCondition() && |
| 4452 | NextBr->isLastUse(NextCmp->getDest())) { |
| 4453 | lowerAtomicCmpxchg(Dest, PtrToMem, Expected, Desired); |
| 4454 | for (size_t i = 0; i < PhiAssigns.size(); ++i) { |
| 4455 | // Lower the phi assignments now, before the branch (same placement |
| 4456 | // as before). |
| 4457 | InstAssign *PhiAssign = PhiAssigns[i]; |
| 4458 | PhiAssign->setDeleted(); |
| 4459 | lowerAssign(PhiAssign); |
| 4460 | Context.advanceNext(); |
| 4461 | } |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4462 | _br(Traits::Cond::Br_e, NextBr->getTargetTrue(), |
| 4463 | NextBr->getTargetFalse()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4464 | // Skip over the old compare and branch, by deleting them. |
| 4465 | NextCmp->setDeleted(); |
| 4466 | NextBr->setDeleted(); |
| 4467 | Context.advanceNext(); |
| 4468 | Context.advanceNext(); |
| 4469 | return true; |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | return false; |
| 4474 | } |
| 4475 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4476 | template <typename TraitsType> |
| 4477 | void TargetX86Base<TraitsType>::lowerAtomicRMW(Variable *Dest, |
| 4478 | uint32_t Operation, Operand *Ptr, |
| 4479 | Operand *Val) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4480 | bool NeedsCmpxchg = false; |
| 4481 | LowerBinOp Op_Lo = nullptr; |
| 4482 | LowerBinOp Op_Hi = nullptr; |
| 4483 | switch (Operation) { |
| 4484 | default: |
| 4485 | Func->setError("Unknown AtomicRMW operation"); |
| 4486 | return; |
| 4487 | case Intrinsics::AtomicAdd: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4488 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4489 | // All the fall-through paths must set this to true, but use this |
| 4490 | // for asserting. |
| 4491 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4492 | Op_Lo = &TargetX86Base<TraitsType>::_add; |
| 4493 | Op_Hi = &TargetX86Base<TraitsType>::_adc; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4494 | break; |
| 4495 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4496 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4497 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4498 | Variable *T = nullptr; |
| 4499 | _mov(T, Val); |
| 4500 | _xadd(Addr, T, Locked); |
| 4501 | _mov(Dest, T); |
| 4502 | return; |
| 4503 | } |
| 4504 | case Intrinsics::AtomicSub: { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4505 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4506 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4507 | Op_Lo = &TargetX86Base<TraitsType>::_sub; |
| 4508 | Op_Hi = &TargetX86Base<TraitsType>::_sbb; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4509 | break; |
| 4510 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4511 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4512 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4513 | Variable *T = nullptr; |
| 4514 | _mov(T, Val); |
| 4515 | _neg(T); |
| 4516 | _xadd(Addr, T, Locked); |
| 4517 | _mov(Dest, T); |
| 4518 | return; |
| 4519 | } |
| 4520 | case Intrinsics::AtomicOr: |
| 4521 | // TODO(jvoung): If Dest is null or dead, then some of these |
| 4522 | // operations do not need an "exchange", but just a locked op. |
| 4523 | // That appears to be "worth" it for sub, or, and, and xor. |
| 4524 | // xadd is probably fine vs lock add for add, and xchg is fine |
| 4525 | // vs an atomic store. |
| 4526 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4527 | Op_Lo = &TargetX86Base<TraitsType>::_or; |
| 4528 | Op_Hi = &TargetX86Base<TraitsType>::_or; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4529 | break; |
| 4530 | case Intrinsics::AtomicAnd: |
| 4531 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4532 | Op_Lo = &TargetX86Base<TraitsType>::_and; |
| 4533 | Op_Hi = &TargetX86Base<TraitsType>::_and; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4534 | break; |
| 4535 | case Intrinsics::AtomicXor: |
| 4536 | NeedsCmpxchg = true; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4537 | Op_Lo = &TargetX86Base<TraitsType>::_xor; |
| 4538 | Op_Hi = &TargetX86Base<TraitsType>::_xor; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4539 | break; |
| 4540 | case Intrinsics::AtomicExchange: |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4541 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4542 | NeedsCmpxchg = true; |
| 4543 | // NeedsCmpxchg, but no real Op_Lo/Op_Hi need to be done. The values |
| 4544 | // just need to be moved to the ecx and ebx registers. |
| 4545 | Op_Lo = nullptr; |
| 4546 | Op_Hi = nullptr; |
| 4547 | break; |
| 4548 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4549 | X86OperandMem *Addr = formMemoryOperand(Ptr, Dest->getType()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4550 | Variable *T = nullptr; |
| 4551 | _mov(T, Val); |
| 4552 | _xchg(Addr, T); |
| 4553 | _mov(Dest, T); |
| 4554 | return; |
| 4555 | } |
| 4556 | // Otherwise, we need a cmpxchg loop. |
| 4557 | (void)NeedsCmpxchg; |
| 4558 | assert(NeedsCmpxchg); |
| 4559 | expandAtomicRMWAsCmpxchg(Op_Lo, Op_Hi, Dest, Ptr, Val); |
| 4560 | } |
| 4561 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4562 | template <typename TraitsType> |
| 4563 | void TargetX86Base<TraitsType>::expandAtomicRMWAsCmpxchg(LowerBinOp Op_Lo, |
| 4564 | LowerBinOp Op_Hi, |
| 4565 | Variable *Dest, |
| 4566 | Operand *Ptr, |
| 4567 | Operand *Val) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4568 | // Expand a more complex RMW operation as a cmpxchg loop: |
| 4569 | // For 64-bit: |
| 4570 | // mov eax, [ptr] |
| 4571 | // mov edx, [ptr + 4] |
| 4572 | // .LABEL: |
| 4573 | // mov ebx, eax |
| 4574 | // <Op_Lo> ebx, <desired_adj_lo> |
| 4575 | // mov ecx, edx |
| 4576 | // <Op_Hi> ecx, <desired_adj_hi> |
| 4577 | // lock cmpxchg8b [ptr] |
| 4578 | // jne .LABEL |
| 4579 | // mov <dest_lo>, eax |
| 4580 | // mov <dest_lo>, edx |
| 4581 | // |
| 4582 | // For 32-bit: |
| 4583 | // mov eax, [ptr] |
| 4584 | // .LABEL: |
| 4585 | // mov <reg>, eax |
| 4586 | // op <reg>, [desired_adj] |
| 4587 | // lock cmpxchg [ptr], <reg> |
| 4588 | // jne .LABEL |
| 4589 | // mov <dest>, eax |
| 4590 | // |
| 4591 | // If Op_{Lo,Hi} are nullptr, then just copy the value. |
| 4592 | Val = legalize(Val); |
| 4593 | Type Ty = Val->getType(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4594 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4595 | Variable *T_edx = makeReg(IceType_i32, Traits::RegisterSet::Reg_edx); |
| 4596 | Variable *T_eax = makeReg(IceType_i32, Traits::RegisterSet::Reg_eax); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4597 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4598 | _mov(T_eax, loOperand(Addr)); |
| 4599 | _mov(T_edx, hiOperand(Addr)); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4600 | Variable *T_ecx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ecx); |
| 4601 | Variable *T_ebx = makeReg(IceType_i32, Traits::RegisterSet::Reg_ebx); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4602 | InstX86Label *Label = InstX86Label::create(Func, this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4603 | const bool IsXchg8b = Op_Lo == nullptr && Op_Hi == nullptr; |
| 4604 | if (!IsXchg8b) { |
| 4605 | Context.insert(Label); |
| 4606 | _mov(T_ebx, T_eax); |
| 4607 | (this->*Op_Lo)(T_ebx, loOperand(Val)); |
| 4608 | _mov(T_ecx, T_edx); |
| 4609 | (this->*Op_Hi)(T_ecx, hiOperand(Val)); |
| 4610 | } else { |
| 4611 | // This is for xchg, which doesn't need an actual Op_Lo/Op_Hi. |
| 4612 | // It just needs the Val loaded into ebx and ecx. |
| 4613 | // That can also be done before the loop. |
| 4614 | _mov(T_ebx, loOperand(Val)); |
| 4615 | _mov(T_ecx, hiOperand(Val)); |
| 4616 | Context.insert(Label); |
| 4617 | } |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4618 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4619 | _cmpxchg8b(Addr, T_edx, T_eax, T_ecx, T_ebx, Locked); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4620 | _br(Traits::Cond::Br_ne, Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4621 | if (!IsXchg8b) { |
| 4622 | // If Val is a variable, model the extended live range of Val through |
| 4623 | // 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] | 4624 | if (auto *ValVar = llvm::dyn_cast<Variable>(Val)) { |
| 4625 | auto *ValLo = llvm::cast<Variable>(loOperand(ValVar)); |
| 4626 | auto *ValHi = llvm::cast<Variable>(hiOperand(ValVar)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4627 | Context.insert<InstFakeUse>(ValLo); |
| 4628 | Context.insert<InstFakeUse>(ValHi); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4629 | } |
| 4630 | } else { |
| 4631 | // For xchg, the loop is slightly smaller and ebx/ecx are used. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4632 | Context.insert<InstFakeUse>(T_ebx); |
| 4633 | Context.insert<InstFakeUse>(T_ecx); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4634 | } |
| 4635 | // The address base (if any) is also reused in the loop. |
| 4636 | if (Variable *Base = Addr->getBase()) |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4637 | Context.insert<InstFakeUse>(Base); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4638 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4639 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4640 | _mov(DestLo, T_eax); |
| 4641 | _mov(DestHi, T_edx); |
| 4642 | return; |
| 4643 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4644 | X86OperandMem *Addr = formMemoryOperand(Ptr, Ty); |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 4645 | RegNumT Eax; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4646 | switch (Ty) { |
| 4647 | default: |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4648 | llvm::report_fatal_error("Bad type for atomicRMW"); |
| 4649 | case IceType_i64: |
| 4650 | Eax = Traits::getRaxOrDie(); |
| 4651 | break; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4652 | case IceType_i32: |
| 4653 | Eax = Traits::RegisterSet::Reg_eax; |
| 4654 | break; |
| 4655 | case IceType_i16: |
| 4656 | Eax = Traits::RegisterSet::Reg_ax; |
| 4657 | break; |
| 4658 | case IceType_i8: |
| 4659 | Eax = Traits::RegisterSet::Reg_al; |
| 4660 | break; |
| 4661 | } |
| 4662 | Variable *T_eax = makeReg(Ty, Eax); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4663 | _mov(T_eax, Addr); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4664 | auto *Label = Context.insert<InstX86Label>(this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4665 | // We want to pick a different register for T than Eax, so don't use |
| 4666 | // _mov(T == nullptr, T_eax). |
| 4667 | Variable *T = makeReg(Ty); |
| 4668 | _mov(T, T_eax); |
| 4669 | (this->*Op_Lo)(T, Val); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 4670 | constexpr bool Locked = true; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4671 | _cmpxchg(Addr, T_eax, T, Locked); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4672 | _br(Traits::Cond::Br_ne, Label); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4673 | // If Val is a variable, model the extended live range of Val through |
| 4674 | // 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] | 4675 | if (auto *ValVar = llvm::dyn_cast<Variable>(Val)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4676 | Context.insert<InstFakeUse>(ValVar); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4677 | } |
| 4678 | // The address base (if any) is also reused in the loop. |
| 4679 | if (Variable *Base = Addr->getBase()) |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 4680 | Context.insert<InstFakeUse>(Base); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4681 | _mov(Dest, T_eax); |
| 4682 | } |
| 4683 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 4684 | /// Lowers count {trailing, leading} zeros intrinsic. |
| 4685 | /// |
| 4686 | /// We could do constant folding here, but that should have |
| 4687 | /// been done by the front-end/middle-end optimizations. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4688 | template <typename TraitsType> |
| 4689 | void TargetX86Base<TraitsType>::lowerCountZeros(bool Cttz, Type Ty, |
| 4690 | Variable *Dest, |
| 4691 | Operand *FirstVal, |
| 4692 | Operand *SecondVal) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4693 | // TODO(jvoung): Determine if the user CPU supports LZCNT (BMI). |
| 4694 | // Then the instructions will handle the Val == 0 case much more simply |
| 4695 | // and won't require conversion from bit position to number of zeros. |
| 4696 | // |
| 4697 | // Otherwise: |
| 4698 | // bsr IF_NOT_ZERO, Val |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4699 | // mov T_DEST, ((Ty == i32) ? 63 : 127) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4700 | // cmovne T_DEST, IF_NOT_ZERO |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4701 | // xor T_DEST, ((Ty == i32) ? 31 : 63) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4702 | // mov DEST, T_DEST |
| 4703 | // |
| 4704 | // NOTE: T_DEST must be a register because cmov requires its dest to be a |
| 4705 | // register. Also, bsf and bsr require their dest to be a register. |
| 4706 | // |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4707 | // 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] | 4708 | // E.g., for 000... 00001100, bsr will say that the most significant bit |
| 4709 | // 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] | 4710 | // like (M - N) for N <= M, and converts 63 to 32, and 127 to 64 (for the |
| 4711 | // all-zeros case). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4712 | // |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4713 | // X8632 only: Similar for 64-bit, but start w/ speculating that the upper 32 |
| 4714 | // bits are all zero, and compute the result for that case (checking the |
| 4715 | // 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] | 4716 | // cmov in the result from the lower computation if the earlier speculation |
| 4717 | // was correct. |
| 4718 | // |
| 4719 | // Cttz, is similar, but uses bsf instead, and doesn't require the xor |
| 4720 | // bit position conversion, and the speculation is reversed. |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4721 | |
| 4722 | // TODO(jpp): refactor this method. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4723 | assert(Ty == IceType_i32 || Ty == IceType_i64); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4724 | const Type DestTy = Traits::Is64Bit ? Dest->getType() : IceType_i32; |
| 4725 | Variable *T = makeReg(DestTy); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4726 | Operand *FirstValRM = legalize(FirstVal, Legal_Mem | Legal_Reg); |
| 4727 | if (Cttz) { |
| 4728 | _bsf(T, FirstValRM); |
| 4729 | } else { |
| 4730 | _bsr(T, FirstValRM); |
| 4731 | } |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4732 | Variable *T_Dest = makeReg(DestTy); |
| 4733 | Constant *_31 = Ctx->getConstantInt32(31); |
| 4734 | Constant *_32 = Ctx->getConstantInt(DestTy, 32); |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4735 | Constant *_63 = Ctx->getConstantInt(DestTy, 63); |
| 4736 | Constant *_64 = Ctx->getConstantInt(DestTy, 64); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4737 | if (Cttz) { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4738 | if (DestTy == IceType_i64) { |
| 4739 | _mov(T_Dest, _64); |
| 4740 | } else { |
| 4741 | _mov(T_Dest, _32); |
| 4742 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4743 | } else { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4744 | Constant *_127 = Ctx->getConstantInt(DestTy, 127); |
| 4745 | if (DestTy == IceType_i64) { |
| 4746 | _mov(T_Dest, _127); |
| 4747 | } else { |
| 4748 | _mov(T_Dest, _63); |
| 4749 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4750 | } |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4751 | _cmov(T_Dest, T, Traits::Cond::Br_ne); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4752 | if (!Cttz) { |
John Porto | 34d276a | 2016-01-27 06:31:53 -0800 | [diff] [blame] | 4753 | if (DestTy == IceType_i64) { |
| 4754 | // Even though there's a _63 available at this point, that constant might |
| 4755 | // not be an i32, which will cause the xor emission to fail. |
| 4756 | Constant *_63 = Ctx->getConstantInt32(63); |
| 4757 | _xor(T_Dest, _63); |
| 4758 | } else { |
| 4759 | _xor(T_Dest, _31); |
| 4760 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4761 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 4762 | if (Traits::Is64Bit || Ty == IceType_i32) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4763 | _mov(Dest, T_Dest); |
| 4764 | return; |
| 4765 | } |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4766 | _add(T_Dest, _32); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 4767 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 4768 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4769 | // Will be using "test" on this, so we need a registerized variable. |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 4770 | Variable *SecondVar = legalizeToReg(SecondVal); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4771 | Variable *T_Dest2 = makeReg(IceType_i32); |
| 4772 | if (Cttz) { |
| 4773 | _bsf(T_Dest2, SecondVar); |
| 4774 | } else { |
| 4775 | _bsr(T_Dest2, SecondVar); |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 4776 | _xor(T_Dest2, _31); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4777 | } |
| 4778 | _test(SecondVar, SecondVar); |
John Porto | 5d0acff | 2015-06-30 15:29:21 -0700 | [diff] [blame] | 4779 | _cmov(T_Dest2, T_Dest, Traits::Cond::Br_e); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 4780 | _mov(DestLo, T_Dest2); |
| 4781 | _mov(DestHi, Ctx->getConstantZero(IceType_i32)); |
| 4782 | } |
| 4783 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4784 | template <typename TraitsType> |
| 4785 | void TargetX86Base<TraitsType>::typedLoad(Type Ty, Variable *Dest, |
| 4786 | Variable *Base, Constant *Offset) { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 4787 | // If Offset is a ConstantRelocatable in Non-SFI mode, we will need to |
| 4788 | // legalize Mem properly. |
| 4789 | if (Offset) |
| 4790 | assert(!llvm::isa<ConstantRelocatable>(Offset)); |
| 4791 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4792 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4793 | |
| 4794 | if (isVectorType(Ty)) |
| 4795 | _movp(Dest, Mem); |
| 4796 | else if (Ty == IceType_f64) |
| 4797 | _movq(Dest, Mem); |
| 4798 | else |
| 4799 | _mov(Dest, Mem); |
| 4800 | } |
| 4801 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4802 | template <typename TraitsType> |
| 4803 | void TargetX86Base<TraitsType>::typedStore(Type Ty, Variable *Value, |
| 4804 | Variable *Base, Constant *Offset) { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 4805 | // If Offset is a ConstantRelocatable in Non-SFI mode, we will need to |
| 4806 | // legalize Mem properly. |
| 4807 | if (Offset) |
| 4808 | assert(!llvm::isa<ConstantRelocatable>(Offset)); |
| 4809 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4810 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4811 | |
| 4812 | if (isVectorType(Ty)) |
| 4813 | _storep(Value, Mem); |
| 4814 | else if (Ty == IceType_f64) |
| 4815 | _storeq(Value, Mem); |
| 4816 | else |
| 4817 | _store(Value, Mem); |
| 4818 | } |
| 4819 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4820 | template <typename TraitsType> |
| 4821 | void TargetX86Base<TraitsType>::copyMemory(Type Ty, Variable *Dest, |
| 4822 | Variable *Src, int32_t OffsetAmt) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4823 | Constant *Offset = OffsetAmt ? Ctx->getConstantInt32(OffsetAmt) : nullptr; |
| 4824 | // TODO(ascull): this or add nullptr test to _movp, _movq |
| 4825 | Variable *Data = makeReg(Ty); |
| 4826 | |
| 4827 | typedLoad(Ty, Data, Src, Offset); |
| 4828 | typedStore(Ty, Data, Dest, Offset); |
| 4829 | } |
| 4830 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4831 | template <typename TraitsType> |
| 4832 | void TargetX86Base<TraitsType>::lowerMemcpy(Operand *Dest, Operand *Src, |
| 4833 | Operand *Count) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4834 | // There is a load and store for each chunk in the unroll |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4835 | constexpr uint32_t BytesPerStorep = 16; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4836 | |
| 4837 | // Check if the operands are constants |
| 4838 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4839 | const bool IsCountConst = CountConst != nullptr; |
| 4840 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4841 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4842 | if (shouldOptimizeMemIntrins() && IsCountConst && |
| 4843 | CountValue <= BytesPerStorep * Traits::MEMCPY_UNROLL_LIMIT) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4844 | // Unlikely, but nothing to do if it does happen |
| 4845 | if (CountValue == 0) |
| 4846 | return; |
| 4847 | |
| 4848 | Variable *SrcBase = legalizeToReg(Src); |
| 4849 | Variable *DestBase = legalizeToReg(Dest); |
| 4850 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4851 | // Find the largest type that can be used and use it as much as possible in |
| 4852 | // reverse order. Then handle any remainder with overlapping copies. Since |
| 4853 | // the remainder will be at the end, there will be reduced pressure on the |
| 4854 | // memory unit as the accesses to the same memory are far apart. |
| 4855 | Type Ty = largestTypeInSize(CountValue); |
| 4856 | uint32_t TyWidth = typeWidthInBytes(Ty); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4857 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4858 | uint32_t RemainingBytes = CountValue; |
| 4859 | int32_t Offset = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 4860 | while (RemainingBytes >= TyWidth) { |
| 4861 | copyMemory(Ty, DestBase, SrcBase, Offset); |
| 4862 | RemainingBytes -= TyWidth; |
| 4863 | Offset -= TyWidth; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4866 | if (RemainingBytes == 0) |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4867 | return; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4868 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4869 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 4870 | // of overlaps in the copies. |
| 4871 | Type LeftOverTy = firstTypeThatFitsSize(RemainingBytes); |
| 4872 | Offset = CountValue - typeWidthInBytes(LeftOverTy); |
| 4873 | copyMemory(LeftOverTy, DestBase, SrcBase, Offset); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4874 | return; |
| 4875 | } |
| 4876 | |
| 4877 | // Fall back on a function call |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4878 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memcpy, nullptr, 3); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4879 | Call->addArg(Dest); |
| 4880 | Call->addArg(Src); |
| 4881 | Call->addArg(Count); |
| 4882 | lowerCall(Call); |
| 4883 | } |
| 4884 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4885 | template <typename TraitsType> |
| 4886 | void TargetX86Base<TraitsType>::lowerMemmove(Operand *Dest, Operand *Src, |
| 4887 | Operand *Count) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4888 | // There is a load and store for each chunk in the unroll |
| 4889 | constexpr uint32_t BytesPerStorep = 16; |
| 4890 | |
| 4891 | // Check if the operands are constants |
| 4892 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4893 | const bool IsCountConst = CountConst != nullptr; |
| 4894 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4895 | |
| 4896 | if (shouldOptimizeMemIntrins() && IsCountConst && |
| 4897 | CountValue <= BytesPerStorep * Traits::MEMMOVE_UNROLL_LIMIT) { |
| 4898 | // Unlikely, but nothing to do if it does happen |
| 4899 | if (CountValue == 0) |
| 4900 | return; |
| 4901 | |
| 4902 | Variable *SrcBase = legalizeToReg(Src); |
| 4903 | Variable *DestBase = legalizeToReg(Dest); |
| 4904 | |
| 4905 | std::tuple<Type, Constant *, Variable *> |
| 4906 | Moves[Traits::MEMMOVE_UNROLL_LIMIT]; |
| 4907 | Constant *Offset; |
| 4908 | Variable *Reg; |
| 4909 | |
| 4910 | // Copy the data into registers as the source and destination could overlap |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 4911 | // so make sure not to clobber the memory. This also means overlapping |
| 4912 | // 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] | 4913 | Type Ty = largestTypeInSize(CountValue); |
| 4914 | uint32_t TyWidth = typeWidthInBytes(Ty); |
| 4915 | |
| 4916 | uint32_t RemainingBytes = CountValue; |
| 4917 | int32_t OffsetAmt = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 4918 | size_t N = 0; |
| 4919 | while (RemainingBytes >= TyWidth) { |
| 4920 | assert(N <= Traits::MEMMOVE_UNROLL_LIMIT); |
| 4921 | Offset = Ctx->getConstantInt32(OffsetAmt); |
| 4922 | Reg = makeReg(Ty); |
| 4923 | typedLoad(Ty, Reg, SrcBase, Offset); |
| 4924 | RemainingBytes -= TyWidth; |
| 4925 | OffsetAmt -= TyWidth; |
| 4926 | Moves[N++] = std::make_tuple(Ty, Offset, Reg); |
| 4927 | } |
| 4928 | |
| 4929 | if (RemainingBytes != 0) { |
| 4930 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 4931 | // of overlaps in the copies. |
| 4932 | assert(N <= Traits::MEMMOVE_UNROLL_LIMIT); |
| 4933 | Ty = firstTypeThatFitsSize(RemainingBytes); |
| 4934 | Offset = Ctx->getConstantInt32(CountValue - typeWidthInBytes(Ty)); |
| 4935 | Reg = makeReg(Ty); |
| 4936 | typedLoad(Ty, Reg, SrcBase, Offset); |
| 4937 | Moves[N++] = std::make_tuple(Ty, Offset, Reg); |
| 4938 | } |
| 4939 | |
| 4940 | // Copy the data out into the destination memory |
| 4941 | for (size_t i = 0; i < N; ++i) { |
| 4942 | std::tie(Ty, Offset, Reg) = Moves[i]; |
| 4943 | typedStore(Ty, Reg, DestBase, Offset); |
| 4944 | } |
| 4945 | |
| 4946 | return; |
| 4947 | } |
| 4948 | |
| 4949 | // Fall back on a function call |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 4950 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memmove, nullptr, 3); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4951 | Call->addArg(Dest); |
| 4952 | Call->addArg(Src); |
| 4953 | Call->addArg(Count); |
| 4954 | lowerCall(Call); |
| 4955 | } |
| 4956 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4957 | template <typename TraitsType> |
| 4958 | void TargetX86Base<TraitsType>::lowerMemset(Operand *Dest, Operand *Val, |
| 4959 | Operand *Count) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4960 | constexpr uint32_t BytesPerStorep = 16; |
| 4961 | constexpr uint32_t BytesPerStoreq = 8; |
| 4962 | constexpr uint32_t BytesPerStorei32 = 4; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4963 | assert(Val->getType() == IceType_i8); |
| 4964 | |
| 4965 | // Check if the operands are constants |
| 4966 | const auto *CountConst = llvm::dyn_cast<const ConstantInteger32>(Count); |
| 4967 | const auto *ValConst = llvm::dyn_cast<const ConstantInteger32>(Val); |
| 4968 | const bool IsCountConst = CountConst != nullptr; |
| 4969 | const bool IsValConst = ValConst != nullptr; |
| 4970 | const uint32_t CountValue = IsCountConst ? CountConst->getValue() : 0; |
| 4971 | const uint32_t ValValue = IsValConst ? ValConst->getValue() : 0; |
| 4972 | |
| 4973 | // Unlikely, but nothing to do if it does happen |
| 4974 | if (IsCountConst && CountValue == 0) |
| 4975 | return; |
| 4976 | |
| 4977 | // TODO(ascull): if the count is constant but val is not it would be possible |
| 4978 | // to inline by spreading the value across 4 bytes and accessing subregs e.g. |
| 4979 | // eax, ax and al. |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4980 | if (shouldOptimizeMemIntrins() && IsCountConst && IsValConst) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4981 | Variable *Base = nullptr; |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4982 | Variable *VecReg = nullptr; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4983 | const uint32_t SpreadValue = |
| 4984 | (ValValue << 24) | (ValValue << 16) | (ValValue << 8) | ValValue; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4985 | |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4986 | auto lowerSet = [this, &Base, SpreadValue, &VecReg](Type Ty, |
Jim Stichnoth | 992f91d | 2015-08-10 11:18:38 -0700 | [diff] [blame] | 4987 | uint32_t OffsetAmt) { |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4988 | assert(Base != nullptr); |
| 4989 | Constant *Offset = OffsetAmt ? Ctx->getConstantInt32(OffsetAmt) : nullptr; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4990 | |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4991 | // TODO(ascull): is 64-bit better with vector or scalar movq? |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 4992 | auto *Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4993 | if (isVectorType(Ty)) { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4994 | assert(VecReg != nullptr); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4995 | _storep(VecReg, Mem); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 4996 | } else if (Ty == IceType_f64) { |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4997 | assert(VecReg != nullptr); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 4998 | _storeq(VecReg, Mem); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 4999 | } else { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 5000 | assert(Ty != IceType_i64); |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 5001 | _store(Ctx->getConstantInt(Ty, SpreadValue), Mem); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5002 | } |
| 5003 | }; |
| 5004 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5005 | // Find the largest type that can be used and use it as much as possible in |
| 5006 | // reverse order. Then handle any remainder with overlapping copies. Since |
| 5007 | // the remainder will be at the end, there will be reduces pressure on the |
| 5008 | // memory unit as the access to the same memory are far apart. |
| 5009 | Type Ty; |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 5010 | if (ValValue == 0 && CountValue >= BytesPerStoreq && |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5011 | CountValue <= BytesPerStorep * Traits::MEMCPY_UNROLL_LIMIT) { |
| 5012 | // When the value is zero it can be loaded into a vector register cheaply |
| 5013 | // using the xor trick. |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 5014 | Base = legalizeToReg(Dest); |
| 5015 | VecReg = makeVectorOfZeros(IceType_v16i8); |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5016 | Ty = largestTypeInSize(CountValue); |
| 5017 | } else if (CountValue <= BytesPerStorei32 * Traits::MEMCPY_UNROLL_LIMIT) { |
| 5018 | // When the value is non-zero or the count is small we can't use vector |
| 5019 | // instructions so are limited to 32-bit stores. |
| 5020 | Base = legalizeToReg(Dest); |
| 5021 | constexpr uint32_t MaxSize = 4; |
| 5022 | Ty = largestTypeInSize(CountValue, MaxSize); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5023 | } |
| 5024 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5025 | if (Base) { |
| 5026 | uint32_t TyWidth = typeWidthInBytes(Ty); |
| 5027 | |
| 5028 | uint32_t RemainingBytes = CountValue; |
| 5029 | uint32_t Offset = (CountValue & ~(TyWidth - 1)) - TyWidth; |
| 5030 | while (RemainingBytes >= TyWidth) { |
| 5031 | lowerSet(Ty, Offset); |
| 5032 | RemainingBytes -= TyWidth; |
| 5033 | Offset -= TyWidth; |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5034 | } |
Andrew Scull | 9df4a37 | 2015-08-07 09:19:35 -0700 | [diff] [blame] | 5035 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 5036 | if (RemainingBytes == 0) |
| 5037 | return; |
| 5038 | |
| 5039 | // Lower the remaining bytes. Adjust to larger types in order to make use |
| 5040 | // of overlaps in the copies. |
| 5041 | Type LeftOverTy = firstTypeThatFitsSize(RemainingBytes); |
| 5042 | Offset = CountValue - typeWidthInBytes(LeftOverTy); |
| 5043 | lowerSet(LeftOverTy, Offset); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5044 | return; |
| 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | // Fall back on calling the memset function. The value operand needs to be |
| 5049 | // extended to a stack slot size because the PNaCl ABI requires arguments to |
| 5050 | // be at least 32 bits wide. |
| 5051 | Operand *ValExt; |
| 5052 | if (IsValConst) { |
| 5053 | ValExt = Ctx->getConstantInt(stackSlotType(), ValValue); |
| 5054 | } else { |
| 5055 | Variable *ValExtVar = Func->makeVariable(stackSlotType()); |
| 5056 | lowerCast(InstCast::create(Func, InstCast::Zext, ValExtVar, Val)); |
| 5057 | ValExt = ValExtVar; |
| 5058 | } |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 5059 | InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memset, nullptr, 3); |
Andrew Scull | 713dbde | 2015-08-04 14:25:27 -0700 | [diff] [blame] | 5060 | Call->addArg(Dest); |
| 5061 | Call->addArg(ValExt); |
| 5062 | Call->addArg(Count); |
| 5063 | lowerCall(Call); |
| 5064 | } |
| 5065 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5066 | class AddressOptimizer { |
| 5067 | AddressOptimizer() = delete; |
| 5068 | AddressOptimizer(const AddressOptimizer &) = delete; |
| 5069 | AddressOptimizer &operator=(const AddressOptimizer &) = delete; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5070 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5071 | public: |
| 5072 | explicit AddressOptimizer(const Cfg *Func) |
| 5073 | : Func(Func), VMetadata(Func->getVMetadata()) {} |
| 5074 | |
| 5075 | inline void dumpAddressOpt(const ConstantRelocatable *const Relocatable, |
| 5076 | int32_t Offset, const Variable *Base, |
| 5077 | const Variable *Index, uint16_t Shift, |
| 5078 | const Inst *Reason) const; |
| 5079 | |
| 5080 | inline const Inst *matchAssign(Variable **Var, |
| 5081 | ConstantRelocatable **Relocatable, |
| 5082 | int32_t *Offset); |
| 5083 | |
| 5084 | inline const Inst *matchCombinedBaseIndex(Variable **Base, Variable **Index, |
| 5085 | uint16_t *Shift); |
| 5086 | |
| 5087 | inline const Inst *matchShiftedIndex(Variable **Index, uint16_t *Shift); |
| 5088 | |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5089 | inline const Inst *matchOffsetIndexOrBase(Variable **IndexOrBase, |
| 5090 | const uint16_t Shift, |
| 5091 | ConstantRelocatable **Relocatable, |
| 5092 | int32_t *Offset); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5093 | |
| 5094 | private: |
| 5095 | const Cfg *const Func; |
| 5096 | const VariablesMetadata *const VMetadata; |
| 5097 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5098 | static bool isAdd(const Inst *Instr) { |
| 5099 | if (auto *Arith = llvm::dyn_cast_or_null<const InstArithmetic>(Instr)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5100 | return (Arith->getOp() == InstArithmetic::Add); |
| 5101 | } |
| 5102 | return false; |
| 5103 | } |
| 5104 | }; |
| 5105 | |
| 5106 | void AddressOptimizer::dumpAddressOpt( |
| 5107 | const ConstantRelocatable *const Relocatable, int32_t Offset, |
| 5108 | const Variable *Base, const Variable *Index, uint16_t Shift, |
| 5109 | const Inst *Reason) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 5110 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5111 | return; |
| 5112 | if (!Func->isVerbose(IceV_AddrOpt)) |
| 5113 | return; |
| 5114 | OstreamLocker L(Func->getContext()); |
| 5115 | Ostream &Str = Func->getContext()->getStrDump(); |
| 5116 | Str << "Instruction: "; |
| 5117 | Reason->dumpDecorated(Func); |
| 5118 | Str << " results in Base="; |
| 5119 | if (Base) |
| 5120 | Base->dump(Func); |
| 5121 | else |
| 5122 | Str << "<null>"; |
| 5123 | Str << ", Index="; |
| 5124 | if (Index) |
| 5125 | Index->dump(Func); |
| 5126 | else |
| 5127 | Str << "<null>"; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5128 | Str << ", Shift=" << Shift << ", Offset=" << Offset |
| 5129 | << ", Relocatable=" << Relocatable << "\n"; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5132 | const Inst *AddressOptimizer::matchAssign(Variable **Var, |
| 5133 | ConstantRelocatable **Relocatable, |
| 5134 | int32_t *Offset) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 5135 | // Var originates from Var=SrcVar ==> set Var:=SrcVar |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5136 | if (*Var == nullptr) |
| 5137 | return nullptr; |
| 5138 | if (const Inst *VarAssign = VMetadata->getSingleDefinition(*Var)) { |
| 5139 | assert(!VMetadata->isMultiDef(*Var)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5140 | if (llvm::isa<InstAssign>(VarAssign)) { |
| 5141 | Operand *SrcOp = VarAssign->getSrc(0); |
| 5142 | assert(SrcOp); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5143 | if (auto *SrcVar = llvm::dyn_cast<Variable>(SrcOp)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5144 | if (!VMetadata->isMultiDef(SrcVar) && |
| 5145 | // TODO: ensure SrcVar stays single-BB |
| 5146 | true) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5147 | *Var = SrcVar; |
| 5148 | return VarAssign; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5149 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5150 | } else if (auto *Const = llvm::dyn_cast<ConstantInteger32>(SrcOp)) { |
| 5151 | int32_t MoreOffset = Const->getValue(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5152 | if (Utils::WouldOverflowAdd(*Offset, MoreOffset)) |
| 5153 | return nullptr; |
| 5154 | *Var = nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5155 | Offset += MoreOffset; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5156 | return VarAssign; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5157 | } else if (auto *AddReloc = llvm::dyn_cast<ConstantRelocatable>(SrcOp)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5158 | if (*Relocatable == nullptr) { |
| 5159 | // It is always safe to fold a relocatable through assignment -- the |
| 5160 | // assignment frees a slot in the address operand that can be used to |
| 5161 | // hold the Sandbox Pointer -- if any. |
| 5162 | *Var = nullptr; |
| 5163 | *Relocatable = AddReloc; |
| 5164 | return VarAssign; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5165 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5169 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5172 | const Inst *AddressOptimizer::matchCombinedBaseIndex(Variable **Base, |
| 5173 | Variable **Index, |
| 5174 | uint16_t *Shift) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5175 | // Index==nullptr && Base is Base=Var1+Var2 ==> |
| 5176 | // set Base=Var1, Index=Var2, Shift=0 |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5177 | if (*Base == nullptr) |
| 5178 | return nullptr; |
| 5179 | if (*Index != nullptr) |
| 5180 | return nullptr; |
| 5181 | auto *BaseInst = VMetadata->getSingleDefinition(*Base); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5182 | if (BaseInst == nullptr) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5183 | return nullptr; |
| 5184 | assert(!VMetadata->isMultiDef(*Base)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5185 | if (BaseInst->getSrcSize() < 2) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5186 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5187 | if (auto *Var1 = llvm::dyn_cast<Variable>(BaseInst->getSrc(0))) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5188 | if (VMetadata->isMultiDef(Var1)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5189 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5190 | if (auto *Var2 = llvm::dyn_cast<Variable>(BaseInst->getSrc(1))) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5191 | if (VMetadata->isMultiDef(Var2)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5192 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5193 | if (isAdd(BaseInst) && |
| 5194 | // TODO: ensure Var1 and Var2 stay single-BB |
| 5195 | true) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5196 | *Base = Var1; |
| 5197 | *Index = Var2; |
| 5198 | *Shift = 0; // should already have been 0 |
| 5199 | return BaseInst; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5200 | } |
| 5201 | } |
| 5202 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5203 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5206 | const Inst *AddressOptimizer::matchShiftedIndex(Variable **Index, |
| 5207 | uint16_t *Shift) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5208 | // Index is Index=Var*Const && log2(Const)+Shift<=3 ==> |
| 5209 | // Index=Var, Shift+=log2(Const) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5210 | if (*Index == nullptr) |
| 5211 | return nullptr; |
| 5212 | auto *IndexInst = VMetadata->getSingleDefinition(*Index); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5213 | if (IndexInst == nullptr) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5214 | return nullptr; |
| 5215 | assert(!VMetadata->isMultiDef(*Index)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5216 | if (IndexInst->getSrcSize() < 2) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5217 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5218 | if (auto *ArithInst = llvm::dyn_cast<InstArithmetic>(IndexInst)) { |
| 5219 | if (auto *Var = llvm::dyn_cast<Variable>(ArithInst->getSrc(0))) { |
| 5220 | if (auto *Const = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5221 | llvm::dyn_cast<ConstantInteger32>(ArithInst->getSrc(1))) { |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5222 | if (VMetadata->isMultiDef(Var) || Const->getType() != IceType_i32) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5223 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5224 | switch (ArithInst->getOp()) { |
| 5225 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5226 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5227 | case InstArithmetic::Mul: { |
| 5228 | uint32_t Mult = Const->getValue(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5229 | uint32_t LogMult; |
| 5230 | switch (Mult) { |
| 5231 | case 1: |
| 5232 | LogMult = 0; |
| 5233 | break; |
| 5234 | case 2: |
| 5235 | LogMult = 1; |
| 5236 | break; |
| 5237 | case 4: |
| 5238 | LogMult = 2; |
| 5239 | break; |
| 5240 | case 8: |
| 5241 | LogMult = 3; |
| 5242 | break; |
| 5243 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5244 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5245 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5246 | if (*Shift + LogMult <= 3) { |
| 5247 | *Index = Var; |
| 5248 | *Shift += LogMult; |
| 5249 | return IndexInst; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5250 | } |
| 5251 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5252 | case InstArithmetic::Shl: { |
| 5253 | uint32_t ShiftAmount = Const->getValue(); |
| 5254 | switch (ShiftAmount) { |
| 5255 | case 0: |
| 5256 | case 1: |
| 5257 | case 2: |
| 5258 | case 3: |
| 5259 | break; |
| 5260 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5261 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5262 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5263 | if (*Shift + ShiftAmount <= 3) { |
| 5264 | *Index = Var; |
| 5265 | *Shift += ShiftAmount; |
| 5266 | return IndexInst; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5270 | } |
| 5271 | } |
| 5272 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5273 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5274 | } |
| 5275 | |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5276 | const Inst *AddressOptimizer::matchOffsetIndexOrBase( |
| 5277 | Variable **IndexOrBase, const uint16_t Shift, |
| 5278 | ConstantRelocatable **Relocatable, int32_t *Offset) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5279 | // Base is Base=Var+Const || Base is Base=Const+Var ==> |
| 5280 | // set Base=Var, Offset+=Const |
| 5281 | // Base is Base=Var-Const ==> |
| 5282 | // set Base=Var, Offset-=Const |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5283 | // Index is Index=Var+Const ==> |
| 5284 | // set Index=Var, Offset+=(Const<<Shift) |
| 5285 | // Index is Index=Const+Var ==> |
| 5286 | // set Index=Var, Offset+=(Const<<Shift) |
| 5287 | // Index is Index=Var-Const ==> |
| 5288 | // set Index=Var, Offset-=(Const<<Shift) |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5289 | // Treat Index=Var Or Const as Index=Var + Const |
| 5290 | // when Var = Var' << N and log2(Const) <= N |
| 5291 | // or when Var = (2^M) * (2^N) and log2(Const) <= (M+N) |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5292 | |
| 5293 | if (*IndexOrBase == nullptr) { |
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 | } |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5296 | const Inst *Definition = VMetadata->getSingleDefinition(*IndexOrBase); |
| 5297 | if (Definition == nullptr) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5298 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5299 | } |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5300 | assert(!VMetadata->isMultiDef(*IndexOrBase)); |
| 5301 | if (auto *ArithInst = llvm::dyn_cast<const InstArithmetic>(Definition)) { |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5302 | switch (ArithInst->getOp()) { |
| 5303 | case InstArithmetic::Add: |
| 5304 | case InstArithmetic::Sub: |
| 5305 | case InstArithmetic::Or: |
| 5306 | break; |
| 5307 | default: |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5308 | return nullptr; |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5309 | } |
| 5310 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5311 | Operand *Src0 = ArithInst->getSrc(0); |
| 5312 | Operand *Src1 = ArithInst->getSrc(1); |
| 5313 | auto *Var0 = llvm::dyn_cast<Variable>(Src0); |
| 5314 | auto *Var1 = llvm::dyn_cast<Variable>(Src1); |
| 5315 | auto *Const0 = llvm::dyn_cast<ConstantInteger32>(Src0); |
| 5316 | auto *Const1 = llvm::dyn_cast<ConstantInteger32>(Src1); |
| 5317 | auto *Reloc0 = llvm::dyn_cast<ConstantRelocatable>(Src0); |
| 5318 | auto *Reloc1 = llvm::dyn_cast<ConstantRelocatable>(Src1); |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5319 | |
| 5320 | bool IsAdd = false; |
| 5321 | if (ArithInst->getOp() == InstArithmetic::Or) { |
| 5322 | Variable *Var = nullptr; |
| 5323 | ConstantInteger32 *Const = nullptr; |
| 5324 | if (Var0 && Const1) { |
| 5325 | Var = Var0; |
| 5326 | Const = Const1; |
| 5327 | } else if (Const0 && Var1) { |
| 5328 | Var = Var1; |
| 5329 | Const = Const0; |
| 5330 | } else { |
| 5331 | return nullptr; |
| 5332 | } |
| 5333 | auto *VarDef = |
| 5334 | llvm::dyn_cast<InstArithmetic>(VMetadata->getSingleDefinition(Var)); |
| 5335 | if (VarDef == nullptr) |
| 5336 | return nullptr; |
| 5337 | |
| 5338 | SizeT ZeroesAvailable = 0; |
| 5339 | if (VarDef->getOp() == InstArithmetic::Shl) { |
| 5340 | if (auto *ConstInt = |
| 5341 | llvm::dyn_cast<ConstantInteger32>(VarDef->getSrc(1))) { |
| 5342 | ZeroesAvailable = ConstInt->getValue(); |
| 5343 | } |
| 5344 | } else if (VarDef->getOp() == InstArithmetic::Mul) { |
| 5345 | SizeT PowerOfTwo = 0; |
Jim Stichnoth | d615c86 | 2016-06-27 16:11:03 -0700 | [diff] [blame] | 5346 | if (auto *MultConst = |
| 5347 | llvm::dyn_cast<ConstantInteger32>(VarDef->getSrc(0))) { |
| 5348 | if (llvm::isPowerOf2_32(MultConst->getValue())) { |
| 5349 | PowerOfTwo += MultConst->getValue(); |
| 5350 | } |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5351 | } |
Jim Stichnoth | d615c86 | 2016-06-27 16:11:03 -0700 | [diff] [blame] | 5352 | if (auto *MultConst = |
| 5353 | llvm::dyn_cast<ConstantInteger32>(VarDef->getSrc(1))) { |
| 5354 | if (llvm::isPowerOf2_32(MultConst->getValue())) { |
| 5355 | PowerOfTwo += MultConst->getValue(); |
| 5356 | } |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5357 | } |
| 5358 | ZeroesAvailable = llvm::Log2_32(PowerOfTwo) + 1; |
| 5359 | } |
| 5360 | SizeT ZeroesNeeded = llvm::Log2_32(Const->getValue()) + 1; |
| 5361 | if (ZeroesNeeded == 0 || ZeroesNeeded > ZeroesAvailable) |
| 5362 | return nullptr; |
| 5363 | IsAdd = true; // treat it as an add if the above conditions hold |
| 5364 | } else { |
| 5365 | IsAdd = ArithInst->getOp() == InstArithmetic::Add; |
| 5366 | } |
| 5367 | |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5368 | Variable *NewIndexOrBase = nullptr; |
| 5369 | int32_t NewOffset = 0; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5370 | ConstantRelocatable *NewRelocatable = *Relocatable; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5371 | if (Var0 && Var1) |
| 5372 | // TODO(sehr): merge base/index splitting into here. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5373 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5374 | if (!IsAdd && Var1) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5375 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5376 | if (Var0) |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5377 | NewIndexOrBase = Var0; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5378 | else if (Var1) |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5379 | NewIndexOrBase = Var1; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5380 | // Don't know how to add/subtract two relocatables. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5381 | if ((*Relocatable && (Reloc0 || Reloc1)) || (Reloc0 && Reloc1)) |
| 5382 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5383 | // Don't know how to subtract a relocatable. |
| 5384 | if (!IsAdd && Reloc1) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5385 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5386 | // Incorporate ConstantRelocatables. |
| 5387 | if (Reloc0) |
| 5388 | NewRelocatable = Reloc0; |
| 5389 | else if (Reloc1) |
| 5390 | NewRelocatable = Reloc1; |
| 5391 | // Compute the updated constant offset. |
| 5392 | if (Const0) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 5393 | const int32_t MoreOffset = |
| 5394 | IsAdd ? Const0->getValue() : -Const0->getValue(); |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5395 | if (Utils::WouldOverflowAdd(*Offset + NewOffset, MoreOffset)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5396 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5397 | NewOffset += MoreOffset; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5398 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5399 | if (Const1) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 5400 | const int32_t MoreOffset = |
| 5401 | IsAdd ? Const1->getValue() : -Const1->getValue(); |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5402 | if (Utils::WouldOverflowAdd(*Offset + NewOffset, MoreOffset)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5403 | return nullptr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5404 | NewOffset += MoreOffset; |
| 5405 | } |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5406 | if (Utils::WouldOverflowAdd(*Offset, NewOffset << Shift)) |
Manasij Mukherjee | 9dd397d | 2016-06-27 13:47:38 -0700 | [diff] [blame] | 5407 | return nullptr; |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5408 | *IndexOrBase = NewIndexOrBase; |
| 5409 | *Offset += (NewOffset << Shift); |
| 5410 | // Shift is always zero if this is called with the base |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5411 | *Relocatable = NewRelocatable; |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5412 | return Definition; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5413 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5414 | return nullptr; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5415 | } |
| 5416 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5417 | template <typename TypeTraits> |
| 5418 | typename TargetX86Base<TypeTraits>::X86OperandMem * |
| 5419 | TargetX86Base<TypeTraits>::computeAddressOpt(const Inst *Instr, Type MemType, |
| 5420 | Operand *Addr) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5421 | Func->resetCurrentNode(); |
| 5422 | if (Func->isVerbose(IceV_AddrOpt)) { |
| 5423 | OstreamLocker L(Func->getContext()); |
| 5424 | Ostream &Str = Func->getContext()->getStrDump(); |
| 5425 | Str << "\nStarting computeAddressOpt for instruction:\n "; |
| 5426 | Instr->dumpDecorated(Func); |
| 5427 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5428 | |
| 5429 | OptAddr NewAddr; |
| 5430 | NewAddr.Base = llvm::dyn_cast<Variable>(Addr); |
| 5431 | if (NewAddr.Base == nullptr) |
| 5432 | return nullptr; |
| 5433 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 5434 | // If the Base has more than one use or is live across multiple blocks, then |
| 5435 | // don't go further. Alternatively (?), never consider a transformation that |
| 5436 | // would change a variable that is currently *not* live across basic block |
| 5437 | // boundaries into one that *is*. |
Manasij Mukherjee | f47d520 | 2016-07-12 16:59:17 -0700 | [diff] [blame] | 5438 | if (!getFlags().getLoopInvariantCodeMotion()) { |
| 5439 | // Need multi block address opt when licm is enabled. |
| 5440 | // Might make sense to restrict to current node and loop header. |
| 5441 | if (Func->getVMetadata()->isMultiBlock( |
| 5442 | NewAddr.Base) /* || Base->getUseCount() > 1*/) |
| 5443 | return nullptr; |
| 5444 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5445 | AddressOptimizer AddrOpt(Func); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 5446 | const bool MockBounds = getFlags().getMockBoundsCheck(); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5447 | const Inst *Reason = nullptr; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5448 | bool AddressWasOptimized = false; |
| 5449 | // The following unnamed struct identifies the address mode formation steps |
| 5450 | // that could potentially create an invalid memory operand (i.e., no free |
| 5451 | // slots for RebasePtr.) We add all those variables to this struct so that we |
| 5452 | // can use memset() to reset all members to false. |
| 5453 | struct { |
| 5454 | bool AssignBase = false; |
| 5455 | bool AssignIndex = false; |
| 5456 | bool OffsetFromBase = false; |
| 5457 | bool OffsetFromIndex = false; |
| 5458 | bool CombinedBaseIndex = false; |
| 5459 | } Skip; |
| 5460 | // This points to the boolean in Skip that represents the last folding |
| 5461 | // performed. This is used to disable a pattern match that generated an |
| 5462 | // invalid address. Without this, the algorithm would never finish. |
| 5463 | bool *SkipLastFolding = nullptr; |
| 5464 | // NewAddrCheckpoint is used to rollback the address being formed in case an |
| 5465 | // invalid address is formed. |
| 5466 | OptAddr NewAddrCheckpoint; |
| 5467 | Reason = Instr; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5468 | do { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5469 | if (SandboxingType != ST_None) { |
| 5470 | // When sandboxing, we defer the sandboxing of NewAddr to the Concrete |
| 5471 | // Target. If our optimization was overly aggressive, then we simply undo |
| 5472 | // what the previous iteration did, and set the previous pattern's skip |
| 5473 | // bit to true. |
| 5474 | if (!legalizeOptAddrForSandbox(&NewAddr)) { |
| 5475 | *SkipLastFolding = true; |
| 5476 | SkipLastFolding = nullptr; |
| 5477 | NewAddr = NewAddrCheckpoint; |
| 5478 | Reason = nullptr; |
| 5479 | } |
| 5480 | } |
| 5481 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5482 | if (Reason) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5483 | AddrOpt.dumpAddressOpt(NewAddr.Relocatable, NewAddr.Offset, NewAddr.Base, |
| 5484 | NewAddr.Index, NewAddr.Shift, Reason); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5485 | AddressWasOptimized = true; |
| 5486 | Reason = nullptr; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5487 | SkipLastFolding = nullptr; |
| 5488 | memset(&Skip, 0, sizeof(Skip)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5489 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5490 | |
| 5491 | NewAddrCheckpoint = NewAddr; |
| 5492 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5493 | // Update Base and Index to follow through assignments to definitions. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5494 | if (!Skip.AssignBase && |
| 5495 | (Reason = AddrOpt.matchAssign(&NewAddr.Base, &NewAddr.Relocatable, |
| 5496 | &NewAddr.Offset))) { |
| 5497 | SkipLastFolding = &Skip.AssignBase; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5498 | // Assignments of Base from a Relocatable or ConstantInt32 can result |
| 5499 | // in Base becoming nullptr. To avoid code duplication in this loop we |
| 5500 | // prefer that Base be non-nullptr if possible. |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5501 | if ((NewAddr.Base == nullptr) && (NewAddr.Index != nullptr) && |
| 5502 | NewAddr.Shift == 0) { |
| 5503 | std::swap(NewAddr.Base, NewAddr.Index); |
| 5504 | } |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5505 | continue; |
| 5506 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5507 | if (!Skip.AssignBase && |
| 5508 | (Reason = AddrOpt.matchAssign(&NewAddr.Index, &NewAddr.Relocatable, |
| 5509 | &NewAddr.Offset))) { |
| 5510 | SkipLastFolding = &Skip.AssignIndex; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5511 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5512 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5513 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5514 | if (!MockBounds) { |
| 5515 | // Transition from: |
| 5516 | // <Relocatable + Offset>(Base) to |
| 5517 | // <Relocatable + Offset>(Base, Index) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5518 | if (!Skip.CombinedBaseIndex && |
| 5519 | (Reason = AddrOpt.matchCombinedBaseIndex( |
| 5520 | &NewAddr.Base, &NewAddr.Index, &NewAddr.Shift))) { |
| 5521 | SkipLastFolding = &Skip.CombinedBaseIndex; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5522 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5523 | } |
| 5524 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5525 | // Recognize multiply/shift and update Shift amount. |
| 5526 | // Index becomes Index=Var<<Const && Const+Shift<=3 ==> |
| 5527 | // Index=Var, Shift+=Const |
| 5528 | // Index becomes Index=Const*Var && log2(Const)+Shift<=3 ==> |
| 5529 | // Index=Var, Shift+=log2(Const) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5530 | if ((Reason = |
| 5531 | AddrOpt.matchShiftedIndex(&NewAddr.Index, &NewAddr.Shift))) { |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5532 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5533 | } |
| 5534 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5535 | // If Shift is zero, the choice of Base and Index was purely arbitrary. |
| 5536 | // Recognize multiply/shift and set Shift amount. |
| 5537 | // Shift==0 && Base is Base=Var*Const && log2(Const)+Shift<=3 ==> |
| 5538 | // swap(Index,Base) |
| 5539 | // Similar for Base=Const*Var and Base=Var<<Const |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5540 | if (NewAddr.Shift == 0 && |
| 5541 | (Reason = AddrOpt.matchShiftedIndex(&NewAddr.Base, &NewAddr.Shift))) { |
| 5542 | std::swap(NewAddr.Base, NewAddr.Index); |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5543 | continue; |
| 5544 | } |
| 5545 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5546 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5547 | // Update Offset to reflect additions/subtractions with constants and |
| 5548 | // relocatables. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5549 | // TODO: consider overflow issues with respect to Offset. |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5550 | if (!Skip.OffsetFromBase && (Reason = AddrOpt.matchOffsetIndexOrBase( |
| 5551 | &NewAddr.Base, /*Shift =*/0, |
| 5552 | &NewAddr.Relocatable, &NewAddr.Offset))) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5553 | SkipLastFolding = &Skip.OffsetFromBase; |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5554 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5555 | } |
Manasij Mukherjee | 2e4b960 | 2016-06-21 14:22:43 -0700 | [diff] [blame] | 5556 | if (!Skip.OffsetFromIndex && (Reason = AddrOpt.matchOffsetIndexOrBase( |
| 5557 | &NewAddr.Index, NewAddr.Shift, |
| 5558 | &NewAddr.Relocatable, &NewAddr.Offset))) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5559 | SkipLastFolding = &Skip.OffsetFromIndex; |
David Sehr | 69e9290 | 2015-11-04 14:46:29 -0800 | [diff] [blame] | 5560 | continue; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5561 | } |
| 5562 | |
David Sehr | aa0b1a1 | 2015-10-27 16:55:40 -0700 | [diff] [blame] | 5563 | break; |
| 5564 | } while (Reason); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5565 | |
| 5566 | if (!AddressWasOptimized) { |
| 5567 | return nullptr; |
| 5568 | } |
| 5569 | |
| 5570 | // Undo any addition of RebasePtr. It will be added back when the mem |
| 5571 | // operand is sandboxed. |
| 5572 | if (NewAddr.Base == RebasePtr) { |
| 5573 | NewAddr.Base = nullptr; |
| 5574 | } |
| 5575 | |
| 5576 | if (NewAddr.Index == RebasePtr) { |
| 5577 | NewAddr.Index = nullptr; |
| 5578 | NewAddr.Shift = 0; |
| 5579 | } |
| 5580 | |
| 5581 | Constant *OffsetOp = nullptr; |
| 5582 | if (NewAddr.Relocatable == nullptr) { |
| 5583 | OffsetOp = Ctx->getConstantInt32(NewAddr.Offset); |
| 5584 | } else { |
| 5585 | OffsetOp = |
| 5586 | Ctx->getConstantSym(NewAddr.Relocatable->getOffset() + NewAddr.Offset, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 5587 | NewAddr.Relocatable->getName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5588 | } |
| 5589 | // Vanilla ICE load instructions should not use the segment registers, and |
| 5590 | // computeAddressOpt only works at the level of Variables and Constants, not |
| 5591 | // other X86OperandMem, so there should be no mention of segment |
| 5592 | // registers there either. |
| 5593 | static constexpr auto SegmentReg = |
| 5594 | X86OperandMem::SegmentRegisters::DefaultSegment; |
| 5595 | |
| 5596 | return X86OperandMem::create(Func, MemType, NewAddr.Base, OffsetOp, |
| 5597 | NewAddr.Index, NewAddr.Shift, SegmentReg); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5598 | } |
| 5599 | |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5600 | /// Add a mock bounds check on the memory address before using it as a load or |
| 5601 | /// store operand. The basic idea is that given a memory operand [reg], we |
| 5602 | /// would first add bounds-check code something like: |
| 5603 | /// |
| 5604 | /// cmp reg, <lb> |
| 5605 | /// jl out_of_line_error |
| 5606 | /// cmp reg, <ub> |
| 5607 | /// jg out_of_line_error |
| 5608 | /// |
| 5609 | /// In reality, the specific code will depend on how <lb> and <ub> are |
| 5610 | /// represented, e.g. an immediate, a global, or a function argument. |
| 5611 | /// |
| 5612 | /// As such, we need to enforce that the memory operand does not have the form |
| 5613 | /// [reg1+reg2], because then there is no simple cmp instruction that would |
| 5614 | /// suffice. However, we consider [reg+offset] to be OK because the offset is |
| 5615 | /// usually small, and so <ub> could have a safety buffer built in and then we |
| 5616 | /// could instead branch to a custom out_of_line_error that does the precise |
| 5617 | /// check and jumps back if it turns out OK. |
| 5618 | /// |
| 5619 | /// For the purpose of mocking the bounds check, we'll do something like this: |
| 5620 | /// |
| 5621 | /// cmp reg, 0 |
| 5622 | /// je label |
| 5623 | /// cmp reg, 1 |
| 5624 | /// je label |
| 5625 | /// label: |
| 5626 | /// |
| 5627 | /// Also note that we don't need to add a bounds check to a dereference of a |
| 5628 | /// simple global variable address. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5629 | template <typename TraitsType> |
| 5630 | void TargetX86Base<TraitsType>::doMockBoundsCheck(Operand *Opnd) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 5631 | if (!getFlags().getMockBoundsCheck()) |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5632 | return; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5633 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(Opnd)) { |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5634 | if (Mem->getIndex()) { |
| 5635 | llvm::report_fatal_error("doMockBoundsCheck: Opnd contains index reg"); |
| 5636 | } |
| 5637 | Opnd = Mem->getBase(); |
| 5638 | } |
| 5639 | // At this point Opnd could be nullptr, or Variable, or Constant, or perhaps |
| 5640 | // something else. We only care if it is Variable. |
| 5641 | auto *Var = llvm::dyn_cast_or_null<Variable>(Opnd); |
| 5642 | if (Var == nullptr) |
| 5643 | return; |
| 5644 | // We use lowerStore() to copy out-args onto the stack. This creates a memory |
| 5645 | // operand with the stack pointer as the base register. Don't do bounds |
| 5646 | // checks on that. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 5647 | if (Var->getRegNum() == getStackReg()) |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5648 | return; |
| 5649 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5650 | auto *Label = InstX86Label::create(Func, this); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5651 | _cmp(Opnd, Ctx->getConstantZero(IceType_i32)); |
| 5652 | _br(Traits::Cond::Br_e, Label); |
| 5653 | _cmp(Opnd, Ctx->getConstantInt32(1)); |
| 5654 | _br(Traits::Cond::Br_e, Label); |
| 5655 | Context.insert(Label); |
| 5656 | } |
| 5657 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5658 | template <typename TraitsType> |
| 5659 | void TargetX86Base<TraitsType>::lowerLoad(const InstLoad *Load) { |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 5660 | // 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] | 5661 | // the source operand is transformed into an X86OperandMem operand. Note that |
| 5662 | // the address mode optimization already creates an X86OperandMem operand, so |
| 5663 | // it doesn't need another level of transformation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5664 | Variable *DestLoad = Load->getDest(); |
| 5665 | Type Ty = DestLoad->getType(); |
| 5666 | Operand *Src0 = formMemoryOperand(Load->getSourceAddress(), Ty); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 5667 | doMockBoundsCheck(Src0); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 5668 | auto *Assign = InstAssign::create(Func, DestLoad, Src0); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5669 | lowerAssign(Assign); |
| 5670 | } |
| 5671 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5672 | template <typename TraitsType> |
Manasij Mukherjee | 0c70417 | 2016-07-21 12:40:24 -0700 | [diff] [blame] | 5673 | void TargetX86Base<TraitsType>::doAddressOptOther() { |
| 5674 | // Inverts some Icmp instructions which helps doAddressOptLoad later. |
| 5675 | // TODO(manasijm): Refactor to unify the conditions for Var0 and Var1 |
| 5676 | Inst *Instr = Context.getCur(); |
| 5677 | auto *VMetadata = Func->getVMetadata(); |
| 5678 | if (auto *Icmp = llvm::dyn_cast<InstIcmp>(Instr)) { |
| 5679 | if (llvm::isa<Constant>(Icmp->getSrc(0)) || |
| 5680 | llvm::isa<Constant>(Icmp->getSrc(1))) |
| 5681 | return; |
| 5682 | auto *Var0 = llvm::dyn_cast<Variable>(Icmp->getSrc(0)); |
| 5683 | if (Var0 == nullptr) |
| 5684 | return; |
| 5685 | if (!VMetadata->isTracked(Var0)) |
| 5686 | return; |
| 5687 | auto *Op0Def = VMetadata->getFirstDefinitionSingleBlock(Var0); |
| 5688 | if (Op0Def == nullptr || !llvm::isa<InstLoad>(Op0Def)) |
| 5689 | return; |
| 5690 | if (VMetadata->getLocalUseNode(Var0) != Context.getNode()) |
| 5691 | return; |
| 5692 | |
| 5693 | auto *Var1 = llvm::dyn_cast<Variable>(Icmp->getSrc(1)); |
| 5694 | if (Var1 != nullptr && VMetadata->isTracked(Var1)) { |
| 5695 | auto *Op1Def = VMetadata->getFirstDefinitionSingleBlock(Var1); |
| 5696 | if (Op1Def != nullptr && !VMetadata->isMultiBlock(Var1) && |
| 5697 | llvm::isa<InstLoad>(Op1Def)) { |
| 5698 | return; // Both are loads |
| 5699 | } |
| 5700 | } |
| 5701 | Icmp->reverseConditionAndOperands(); |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5706 | void TargetX86Base<TraitsType>::doAddressOptLoad() { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5707 | Inst *Instr = Context.getCur(); |
| 5708 | Operand *Addr = Instr->getSrc(0); |
| 5709 | Variable *Dest = Instr->getDest(); |
| 5710 | if (auto *OptAddr = computeAddressOpt(Instr, Dest->getType(), Addr)) { |
| 5711 | Instr->setDeleted(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 5712 | Context.insert<InstLoad>(Dest, OptAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5713 | } |
| 5714 | } |
| 5715 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5716 | template <typename TraitsType> |
| 5717 | void TargetX86Base<TraitsType>::randomlyInsertNop(float Probability, |
| 5718 | RandomNumberGenerator &RNG) { |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 5719 | RandomNumberGeneratorWrapper RNGW(RNG); |
| 5720 | if (RNGW.getTrueWithProbability(Probability)) { |
| 5721 | _nop(RNGW(Traits::X86_NUM_NOP_VARIANTS)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5722 | } |
| 5723 | } |
| 5724 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5725 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5726 | void TargetX86Base<TraitsType>::lowerPhi(const InstPhi * /*Instr*/) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 5727 | Func->setError("Phi found in regular instruction list"); |
| 5728 | } |
| 5729 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 5730 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5731 | void TargetX86Base<TraitsType>::lowerRet(const InstRet *Instr) { |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5732 | Variable *Reg = nullptr; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 5733 | if (Instr->hasRetValue()) { |
| 5734 | Operand *RetValue = legalize(Instr->getRetValue()); |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5735 | const Type ReturnType = RetValue->getType(); |
| 5736 | assert(isVectorType(ReturnType) || isScalarFloatingType(ReturnType) || |
| 5737 | (ReturnType == IceType_i32) || (ReturnType == IceType_i64)); |
| 5738 | Reg = moveReturnValueToRegister(RetValue, ReturnType); |
| 5739 | } |
| 5740 | // Add a ret instruction even if sandboxing is enabled, because addEpilog |
| 5741 | // explicitly looks for a ret instruction as a marker for where to insert the |
| 5742 | // frame removal instructions. |
| 5743 | _ret(Reg); |
| 5744 | // Add a fake use of esp to make sure esp stays alive for the entire |
| 5745 | // function. Otherwise post-call esp adjustments get dead-code eliminated. |
| 5746 | keepEspLiveAtExit(); |
| 5747 | } |
| 5748 | |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5749 | inline uint32_t makePshufdMask(SizeT Index0, SizeT Index1, SizeT Index2, |
| 5750 | SizeT Index3) { |
| 5751 | const SizeT Mask = (Index0 & 0x3) | ((Index1 & 0x3) << 2) | |
| 5752 | ((Index2 & 0x3) << 4) | ((Index3 & 0x3) << 6); |
| 5753 | assert(Mask < 256); |
| 5754 | return Mask; |
| 5755 | } |
| 5756 | |
| 5757 | template <typename TraitsType> |
| 5758 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_AllFromSameSrc( |
| 5759 | Variable *Src, SizeT Index0, SizeT Index1, SizeT Index2, SizeT Index3) { |
| 5760 | constexpr SizeT SrcBit = 1 << 2; |
| 5761 | assert((Index0 & SrcBit) == (Index1 & SrcBit)); |
| 5762 | assert((Index0 & SrcBit) == (Index2 & SrcBit)); |
| 5763 | assert((Index0 & SrcBit) == (Index3 & SrcBit)); |
| 5764 | (void)SrcBit; |
| 5765 | |
| 5766 | const Type SrcTy = Src->getType(); |
| 5767 | auto *T = makeReg(SrcTy); |
| 5768 | auto *SrcRM = legalize(Src, Legal_Reg | Legal_Mem); |
| 5769 | auto *Mask = |
| 5770 | Ctx->getConstantInt32(makePshufdMask(Index0, Index1, Index2, Index3)); |
| 5771 | _pshufd(T, SrcRM, Mask); |
| 5772 | return T; |
| 5773 | } |
| 5774 | |
| 5775 | template <typename TraitsType> |
| 5776 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_TwoFromSameSrc( |
| 5777 | Variable *Src0, SizeT Index0, SizeT Index1, Variable *Src1, SizeT Index2, |
| 5778 | SizeT Index3) { |
| 5779 | constexpr SizeT SrcBit = 1 << 2; |
| 5780 | assert((Index0 & SrcBit) == (Index1 & SrcBit) || (Index1 == IGNORE_INDEX)); |
| 5781 | assert((Index2 & SrcBit) == (Index3 & SrcBit) || (Index3 == IGNORE_INDEX)); |
| 5782 | (void)SrcBit; |
| 5783 | |
| 5784 | const Type SrcTy = Src0->getType(); |
| 5785 | assert(Src1->getType() == SrcTy); |
| 5786 | auto *T = makeReg(SrcTy); |
| 5787 | auto *Src0R = legalizeToReg(Src0); |
| 5788 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 5789 | auto *Mask = |
| 5790 | Ctx->getConstantInt32(makePshufdMask(Index0, Index1, Index2, Index3)); |
| 5791 | _movp(T, Src0R); |
| 5792 | _shufps(T, Src1RM, Mask); |
| 5793 | return T; |
| 5794 | } |
| 5795 | |
| 5796 | template <typename TraitsType> |
| 5797 | Variable *TargetX86Base<TraitsType>::lowerShuffleVector_UnifyFromDifferentSrcs( |
| 5798 | Variable *Src0, SizeT Index0, Variable *Src1, SizeT Index1) { |
| 5799 | return lowerShuffleVector_TwoFromSameSrc(Src0, Index0, IGNORE_INDEX, Src1, |
| 5800 | Index1, IGNORE_INDEX); |
| 5801 | } |
| 5802 | |
| 5803 | inline SizeT makeSrcSwitchMask(SizeT Index0, SizeT Index1, SizeT Index2, |
| 5804 | SizeT Index3) { |
| 5805 | constexpr SizeT SrcBit = 1 << 2; |
| 5806 | const SizeT Index0Bits = ((Index0 & SrcBit) == 0) ? 0 : (1 << 0); |
| 5807 | const SizeT Index1Bits = ((Index1 & SrcBit) == 0) ? 0 : (1 << 1); |
| 5808 | const SizeT Index2Bits = ((Index2 & SrcBit) == 0) ? 0 : (1 << 2); |
| 5809 | const SizeT Index3Bits = ((Index3 & SrcBit) == 0) ? 0 : (1 << 3); |
| 5810 | return Index0Bits | Index1Bits | Index2Bits | Index3Bits; |
| 5811 | } |
| 5812 | |
David Sehr | 0c68bef | 2016-01-20 10:00:23 -0800 | [diff] [blame] | 5813 | template <typename TraitsType> |
John Porto | de29f12 | 2016-04-26 19:16:07 -0700 | [diff] [blame] | 5814 | GlobalString TargetX86Base<TraitsType>::lowerShuffleVector_NewMaskName() { |
| 5815 | GlobalString FuncName = Func->getFunctionName(); |
| 5816 | const SizeT Id = PshufbMaskCount++; |
| 5817 | if (!BuildDefs::dump() || !FuncName.hasStdString()) { |
| 5818 | return GlobalString::createWithString( |
| 5819 | Ctx, |
| 5820 | "$PS" + std::to_string(FuncName.getID()) + "_" + std::to_string(Id)); |
| 5821 | } |
| 5822 | return GlobalString::createWithString( |
| 5823 | Ctx, "Pshufb$" + Func->getFunctionName() + "$" + std::to_string(Id)); |
| 5824 | } |
| 5825 | |
| 5826 | template <typename TraitsType> |
| 5827 | ConstantRelocatable * |
| 5828 | TargetX86Base<TraitsType>::lowerShuffleVector_CreatePshufbMask( |
| 5829 | int8_t Idx0, int8_t Idx1, int8_t Idx2, int8_t Idx3, int8_t Idx4, |
| 5830 | int8_t Idx5, int8_t Idx6, int8_t Idx7, int8_t Idx8, int8_t Idx9, |
| 5831 | int8_t Idx10, int8_t Idx11, int8_t Idx12, int8_t Idx13, int8_t Idx14, |
| 5832 | int8_t Idx15) { |
| 5833 | static constexpr uint8_t NumElements = 16; |
| 5834 | const char Initializer[NumElements] = { |
| 5835 | Idx0, Idx1, Idx2, Idx3, Idx4, Idx5, Idx6, Idx7, |
| 5836 | Idx8, Idx9, Idx10, Idx11, Idx12, Idx13, Idx14, Idx15, |
| 5837 | }; |
| 5838 | |
| 5839 | static constexpr Type V4VectorType = IceType_v4i32; |
| 5840 | const uint32_t MaskAlignment = typeWidthInBytesOnStack(V4VectorType); |
| 5841 | auto *Mask = VariableDeclaration::create(Func->getGlobalPool()); |
| 5842 | GlobalString MaskName = lowerShuffleVector_NewMaskName(); |
| 5843 | Mask->setIsConstant(true); |
| 5844 | Mask->addInitializer(VariableDeclaration::DataInitializer::create( |
| 5845 | Func->getGlobalPool(), Initializer, NumElements)); |
| 5846 | Mask->setName(MaskName); |
| 5847 | // Mask needs to be 16-byte aligned, or pshufb will seg fault. |
| 5848 | Mask->setAlignment(MaskAlignment); |
| 5849 | Func->addGlobal(Mask); |
| 5850 | |
| 5851 | constexpr RelocOffsetT Offset = 0; |
| 5852 | return llvm::cast<ConstantRelocatable>(Ctx->getConstantSym(Offset, MaskName)); |
| 5853 | } |
| 5854 | |
| 5855 | template <typename TraitsType> |
| 5856 | void TargetX86Base<TraitsType>::lowerShuffleVector_UsingPshufb( |
| 5857 | Variable *Dest, Operand *Src0, Operand *Src1, int8_t Idx0, int8_t Idx1, |
| 5858 | int8_t Idx2, int8_t Idx3, int8_t Idx4, int8_t Idx5, int8_t Idx6, |
| 5859 | int8_t Idx7, int8_t Idx8, int8_t Idx9, int8_t Idx10, int8_t Idx11, |
| 5860 | int8_t Idx12, int8_t Idx13, int8_t Idx14, int8_t Idx15) { |
| 5861 | const Type DestTy = Dest->getType(); |
| 5862 | static constexpr bool NotRebased = false; |
| 5863 | static constexpr Variable *NoBase = nullptr; |
| 5864 | // We use void for the memory operand instead of DestTy because using the |
| 5865 | // latter causes a validation failure: the X86 Inst layer complains that |
| 5866 | // vector mem operands could be under aligned. Thus, using void we avoid the |
| 5867 | // validation error. Note that the mask global declaration is aligned, so it |
| 5868 | // can be used as an XMM mem operand. |
| 5869 | static constexpr Type MaskType = IceType_void; |
| 5870 | #define IDX_IN_SRC(N, S) \ |
| 5871 | ((((N) & (1 << 4)) == (S << 4)) ? ((N)&0xf) : CLEAR_ALL_BITS) |
| 5872 | auto *Mask0M = X86OperandMem::create( |
| 5873 | Func, MaskType, NoBase, |
| 5874 | lowerShuffleVector_CreatePshufbMask( |
| 5875 | IDX_IN_SRC(Idx0, 0), IDX_IN_SRC(Idx1, 0), IDX_IN_SRC(Idx2, 0), |
| 5876 | IDX_IN_SRC(Idx3, 0), IDX_IN_SRC(Idx4, 0), IDX_IN_SRC(Idx5, 0), |
| 5877 | IDX_IN_SRC(Idx6, 0), IDX_IN_SRC(Idx7, 0), IDX_IN_SRC(Idx8, 0), |
| 5878 | IDX_IN_SRC(Idx9, 0), IDX_IN_SRC(Idx10, 0), IDX_IN_SRC(Idx11, 0), |
| 5879 | IDX_IN_SRC(Idx12, 0), IDX_IN_SRC(Idx13, 0), IDX_IN_SRC(Idx14, 0), |
| 5880 | IDX_IN_SRC(Idx15, 0)), |
| 5881 | NotRebased); |
| 5882 | auto *Mask1M = X86OperandMem::create( |
| 5883 | Func, MaskType, NoBase, |
| 5884 | lowerShuffleVector_CreatePshufbMask( |
| 5885 | IDX_IN_SRC(Idx0, 1), IDX_IN_SRC(Idx1, 1), IDX_IN_SRC(Idx2, 1), |
| 5886 | IDX_IN_SRC(Idx3, 1), IDX_IN_SRC(Idx4, 1), IDX_IN_SRC(Idx5, 1), |
| 5887 | IDX_IN_SRC(Idx6, 1), IDX_IN_SRC(Idx7, 1), IDX_IN_SRC(Idx8, 1), |
| 5888 | IDX_IN_SRC(Idx9, 1), IDX_IN_SRC(Idx10, 1), IDX_IN_SRC(Idx11, 1), |
| 5889 | IDX_IN_SRC(Idx12, 1), IDX_IN_SRC(Idx13, 1), IDX_IN_SRC(Idx14, 1), |
| 5890 | IDX_IN_SRC(Idx15, 1)), |
| 5891 | NotRebased); |
| 5892 | #undef IDX_IN_SRC |
| 5893 | auto *T0 = makeReg(DestTy); |
| 5894 | auto *T1 = makeReg(DestTy); |
| 5895 | auto *Src0RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 5896 | _movp(T0, Src0RM); |
| 5897 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 5898 | _movp(T1, Src1RM); |
| 5899 | |
| 5900 | _pshufb(T1, Mask1M); |
| 5901 | _pshufb(T0, Mask0M); |
| 5902 | _por(T1, T0); |
| 5903 | _movp(Dest, T1); |
| 5904 | } |
| 5905 | |
| 5906 | template <typename TraitsType> |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 5907 | void TargetX86Base<TraitsType>::lowerShuffleVector( |
| 5908 | const InstShuffleVector *Instr) { |
| 5909 | auto *Dest = Instr->getDest(); |
| 5910 | const Type DestTy = Dest->getType(); |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5911 | auto *Src0 = llvm::cast<Variable>(Instr->getSrc(0)); |
| 5912 | auto *Src1 = llvm::cast<Variable>(Instr->getSrc(1)); |
| 5913 | const SizeT NumElements = typeNumElements(DestTy); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 5914 | |
| 5915 | auto *T = makeReg(DestTy); |
| 5916 | |
| 5917 | switch (DestTy) { |
| 5918 | default: |
John Porto | de29f12 | 2016-04-26 19:16:07 -0700 | [diff] [blame] | 5919 | llvm::report_fatal_error("Unexpected vector type."); |
| 5920 | case IceType_v16i1: |
| 5921 | case IceType_v16i8: { |
| 5922 | if (InstructionSet < Traits::SSE4_1) { |
| 5923 | // TODO(jpp): figure out how to lower with sse2. |
| 5924 | break; |
| 5925 | } |
| 5926 | static constexpr SizeT ExpectedNumElements = 16; |
| 5927 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5928 | (void)ExpectedNumElements; |
| 5929 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5930 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5931 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5932 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5933 | const SizeT Index4 = Instr->getIndex(4)->getValue(); |
| 5934 | const SizeT Index5 = Instr->getIndex(5)->getValue(); |
| 5935 | const SizeT Index6 = Instr->getIndex(6)->getValue(); |
| 5936 | const SizeT Index7 = Instr->getIndex(7)->getValue(); |
| 5937 | const SizeT Index8 = Instr->getIndex(8)->getValue(); |
| 5938 | const SizeT Index9 = Instr->getIndex(9)->getValue(); |
| 5939 | const SizeT Index10 = Instr->getIndex(10)->getValue(); |
| 5940 | const SizeT Index11 = Instr->getIndex(11)->getValue(); |
| 5941 | const SizeT Index12 = Instr->getIndex(12)->getValue(); |
| 5942 | const SizeT Index13 = Instr->getIndex(13)->getValue(); |
| 5943 | const SizeT Index14 = Instr->getIndex(14)->getValue(); |
| 5944 | const SizeT Index15 = Instr->getIndex(15)->getValue(); |
| 5945 | lowerShuffleVector_UsingPshufb(Dest, Src0, Src1, Index0, Index1, Index2, |
| 5946 | Index3, Index4, Index5, Index6, Index7, |
| 5947 | Index8, Index9, Index10, Index11, Index12, |
| 5948 | Index13, Index14, Index15); |
| 5949 | return; |
| 5950 | } |
| 5951 | case IceType_v8i1: |
| 5952 | case IceType_v8i16: { |
| 5953 | if (InstructionSet < Traits::SSE4_1) { |
| 5954 | // TODO(jpp): figure out how to lower with sse2. |
| 5955 | break; |
| 5956 | } |
| 5957 | static constexpr SizeT ExpectedNumElements = 8; |
| 5958 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5959 | (void)ExpectedNumElements; |
| 5960 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5961 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5962 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5963 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5964 | const SizeT Index4 = Instr->getIndex(4)->getValue(); |
| 5965 | const SizeT Index5 = Instr->getIndex(5)->getValue(); |
| 5966 | const SizeT Index6 = Instr->getIndex(6)->getValue(); |
| 5967 | const SizeT Index7 = Instr->getIndex(7)->getValue(); |
| 5968 | #define TO_BYTE_INDEX(I) ((I) << 1) |
| 5969 | lowerShuffleVector_UsingPshufb( |
| 5970 | Dest, Src0, Src1, TO_BYTE_INDEX(Index0), TO_BYTE_INDEX(Index0) + 1, |
| 5971 | TO_BYTE_INDEX(Index1), TO_BYTE_INDEX(Index1) + 1, TO_BYTE_INDEX(Index2), |
| 5972 | TO_BYTE_INDEX(Index2) + 1, TO_BYTE_INDEX(Index3), |
| 5973 | TO_BYTE_INDEX(Index3) + 1, TO_BYTE_INDEX(Index4), |
| 5974 | TO_BYTE_INDEX(Index4) + 1, TO_BYTE_INDEX(Index5), |
| 5975 | TO_BYTE_INDEX(Index5) + 1, TO_BYTE_INDEX(Index6), |
| 5976 | TO_BYTE_INDEX(Index6) + 1, TO_BYTE_INDEX(Index7), |
| 5977 | TO_BYTE_INDEX(Index7) + 1); |
| 5978 | #undef TO_BYTE_INDEX |
| 5979 | return; |
| 5980 | } |
John Porto | ae15f0f | 2016-04-26 04:26:33 -0700 | [diff] [blame] | 5981 | case IceType_v4i1: |
| 5982 | case IceType_v4i32: |
| 5983 | case IceType_v4f32: { |
| 5984 | static constexpr SizeT ExpectedNumElements = 4; |
| 5985 | assert(ExpectedNumElements == Instr->getNumIndexes()); |
| 5986 | const SizeT Index0 = Instr->getIndex(0)->getValue(); |
| 5987 | const SizeT Index1 = Instr->getIndex(1)->getValue(); |
| 5988 | const SizeT Index2 = Instr->getIndex(2)->getValue(); |
| 5989 | const SizeT Index3 = Instr->getIndex(3)->getValue(); |
| 5990 | Variable *T = nullptr; |
| 5991 | switch (makeSrcSwitchMask(Index0, Index1, Index2, Index3)) { |
| 5992 | #define CASE_SRCS_IN(S0, S1, S2, S3) \ |
| 5993 | case (((S0) << 0) | ((S1) << 1) | ((S2) << 2) | ((S3) << 3)) |
| 5994 | CASE_SRCS_IN(0, 0, 0, 0) : { |
| 5995 | T = lowerShuffleVector_AllFromSameSrc(Src0, Index0, Index1, Index2, |
| 5996 | Index3); |
| 5997 | } |
| 5998 | break; |
| 5999 | CASE_SRCS_IN(0, 0, 0, 1) : { |
| 6000 | assert(false && "Following code is untested but likely correct; test " |
| 6001 | "and remove assert."); |
| 6002 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index2, |
| 6003 | Src1, Index3); |
| 6004 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Unified, |
| 6005 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6006 | } |
| 6007 | break; |
| 6008 | CASE_SRCS_IN(0, 0, 1, 0) : { |
| 6009 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index2, |
| 6010 | Src0, Index3); |
| 6011 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Unified, |
| 6012 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6013 | } |
| 6014 | break; |
| 6015 | CASE_SRCS_IN(0, 0, 1, 1) : { |
| 6016 | assert(false && "Following code is untested but likely correct; test " |
| 6017 | "and remove assert."); |
| 6018 | T = lowerShuffleVector_TwoFromSameSrc(Src0, Index0, Index1, Src1, |
| 6019 | Index2, Index3); |
| 6020 | } |
| 6021 | break; |
| 6022 | CASE_SRCS_IN(0, 1, 0, 0) : { |
| 6023 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index0, |
| 6024 | Src1, Index1); |
| 6025 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6026 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src0, Index2, Index3); |
| 6027 | } |
| 6028 | break; |
| 6029 | CASE_SRCS_IN(0, 1, 0, 1) : { |
| 6030 | if (Index0 == 0 && (Index1 - ExpectedNumElements) == 0 && Index2 == 1 && |
| 6031 | (Index3 - ExpectedNumElements) == 1) { |
| 6032 | assert(false && "Following code is untested but likely correct; test " |
| 6033 | "and remove assert."); |
| 6034 | auto *Src1RM = legalize(Src1, Legal_Reg | Legal_Mem); |
| 6035 | auto *Src0R = legalizeToReg(Src0); |
| 6036 | T = makeReg(DestTy); |
| 6037 | _movp(T, Src0R); |
| 6038 | _punpckl(T, Src1RM); |
| 6039 | } else if (Index0 == Index2 && Index1 == Index3) { |
| 6040 | assert(false && "Following code is untested but likely correct; test " |
| 6041 | "and remove assert."); |
| 6042 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6043 | Src0, Index0, Src1, Index1); |
| 6044 | T = lowerShuffleVector_AllFromSameSrc( |
| 6045 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_0, |
| 6046 | UNIFIED_INDEX_1); |
| 6047 | } else { |
| 6048 | assert(false && "Following code is untested but likely correct; test " |
| 6049 | "and remove assert."); |
| 6050 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6051 | Src0, Index0, Src1, Index1); |
| 6052 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6053 | Src0, Index2, Src1, Index3); |
| 6054 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6055 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 6056 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6057 | } |
| 6058 | } |
| 6059 | break; |
| 6060 | CASE_SRCS_IN(0, 1, 1, 0) : { |
| 6061 | if (Index0 == Index3 && Index1 == Index2) { |
| 6062 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6063 | Src0, Index0, Src1, Index1); |
| 6064 | T = lowerShuffleVector_AllFromSameSrc( |
| 6065 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_1, |
| 6066 | UNIFIED_INDEX_0); |
| 6067 | } else { |
| 6068 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6069 | Src0, Index0, Src1, Index1); |
| 6070 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6071 | Src1, Index2, Src0, Index3); |
| 6072 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6073 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 6074 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6075 | } |
| 6076 | } |
| 6077 | break; |
| 6078 | CASE_SRCS_IN(0, 1, 1, 1) : { |
| 6079 | assert(false && "Following code is untested but likely correct; test " |
| 6080 | "and remove assert."); |
| 6081 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index0, |
| 6082 | Src1, Index1); |
| 6083 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6084 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src1, Index2, Index3); |
| 6085 | } |
| 6086 | break; |
| 6087 | CASE_SRCS_IN(1, 0, 0, 0) : { |
| 6088 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index0, |
| 6089 | Src0, Index1); |
| 6090 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6091 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src0, Index2, Index3); |
| 6092 | } |
| 6093 | break; |
| 6094 | CASE_SRCS_IN(1, 0, 0, 1) : { |
| 6095 | if (Index0 == Index3 && Index1 == Index2) { |
| 6096 | assert(false && "Following code is untested but likely correct; test " |
| 6097 | "and remove assert."); |
| 6098 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6099 | Src1, Index0, Src0, Index1); |
| 6100 | T = lowerShuffleVector_AllFromSameSrc( |
| 6101 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_1, |
| 6102 | UNIFIED_INDEX_0); |
| 6103 | } else { |
| 6104 | assert(false && "Following code is untested but likely correct; test " |
| 6105 | "and remove assert."); |
| 6106 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6107 | Src1, Index0, Src0, Index1); |
| 6108 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6109 | Src0, Index2, Src1, Index3); |
| 6110 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6111 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 6112 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6113 | } |
| 6114 | } |
| 6115 | break; |
| 6116 | CASE_SRCS_IN(1, 0, 1, 0) : { |
| 6117 | if ((Index0 - ExpectedNumElements) == 0 && Index1 == 0 && |
| 6118 | (Index2 - ExpectedNumElements) == 1 && Index3 == 1) { |
| 6119 | auto *Src1RM = legalize(Src0, Legal_Reg | Legal_Mem); |
| 6120 | auto *Src0R = legalizeToReg(Src1); |
| 6121 | T = makeReg(DestTy); |
| 6122 | _movp(T, Src0R); |
| 6123 | _punpckl(T, Src1RM); |
| 6124 | } else if (Index0 == Index2 && Index1 == Index3) { |
| 6125 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6126 | Src1, Index0, Src0, Index1); |
| 6127 | T = lowerShuffleVector_AllFromSameSrc( |
| 6128 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, UNIFIED_INDEX_0, |
| 6129 | UNIFIED_INDEX_1); |
| 6130 | } else { |
| 6131 | auto *Unified0 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6132 | Src1, Index0, Src0, Index1); |
| 6133 | auto *Unified1 = lowerShuffleVector_UnifyFromDifferentSrcs( |
| 6134 | Src1, Index2, Src0, Index3); |
| 6135 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6136 | Unified0, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Unified1, |
| 6137 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6138 | } |
| 6139 | } |
| 6140 | break; |
| 6141 | CASE_SRCS_IN(1, 0, 1, 1) : { |
| 6142 | assert(false && "Following code is untested but likely correct; test " |
| 6143 | "and remove assert."); |
| 6144 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index0, |
| 6145 | Src0, Index1); |
| 6146 | T = lowerShuffleVector_TwoFromSameSrc( |
| 6147 | Unified, UNIFIED_INDEX_0, UNIFIED_INDEX_1, Src1, Index2, Index3); |
| 6148 | } |
| 6149 | break; |
| 6150 | CASE_SRCS_IN(1, 1, 0, 0) : { |
| 6151 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Src0, |
| 6152 | Index2, Index3); |
| 6153 | } |
| 6154 | break; |
| 6155 | CASE_SRCS_IN(1, 1, 0, 1) : { |
| 6156 | assert(false && "Following code is untested but likely correct; test " |
| 6157 | "and remove assert."); |
| 6158 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src0, Index2, |
| 6159 | Src1, Index3); |
| 6160 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Unified, |
| 6161 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6162 | } |
| 6163 | break; |
| 6164 | CASE_SRCS_IN(1, 1, 1, 0) : { |
| 6165 | auto *Unified = lowerShuffleVector_UnifyFromDifferentSrcs(Src1, Index2, |
| 6166 | Src0, Index3); |
| 6167 | T = lowerShuffleVector_TwoFromSameSrc(Src1, Index0, Index1, Unified, |
| 6168 | UNIFIED_INDEX_0, UNIFIED_INDEX_1); |
| 6169 | } |
| 6170 | break; |
| 6171 | CASE_SRCS_IN(1, 1, 1, 1) : { |
| 6172 | assert(false && "Following code is untested but likely correct; test " |
| 6173 | "and remove assert."); |
| 6174 | T = lowerShuffleVector_AllFromSameSrc(Src1, Index0, Index1, Index2, |
| 6175 | Index3); |
| 6176 | } |
| 6177 | break; |
| 6178 | #undef CASE_SRCS_IN |
| 6179 | } |
| 6180 | |
| 6181 | assert(T != nullptr); |
| 6182 | assert(T->getType() == DestTy); |
| 6183 | _movp(Dest, T); |
| 6184 | return; |
| 6185 | } break; |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | // Unoptimized shuffle. Perform a series of inserts and extracts. |
| 6189 | Context.insert<InstFakeDef>(T); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 6190 | const Type ElementType = typeElementType(DestTy); |
| 6191 | for (SizeT I = 0; I < Instr->getNumIndexes(); ++I) { |
| 6192 | auto *Index = Instr->getIndex(I); |
| 6193 | const SizeT Elem = Index->getValue(); |
| 6194 | auto *ExtElmt = makeReg(ElementType); |
| 6195 | if (Elem < NumElements) { |
| 6196 | lowerExtractElement( |
| 6197 | InstExtractElement::create(Func, ExtElmt, Src0, Index)); |
| 6198 | } else { |
| 6199 | lowerExtractElement(InstExtractElement::create( |
| 6200 | Func, ExtElmt, Src1, |
| 6201 | Ctx->getConstantInt32(Index->getValue() - NumElements))); |
| 6202 | } |
| 6203 | auto *NewT = makeReg(DestTy); |
| 6204 | lowerInsertElement(InstInsertElement::create(Func, NewT, T, ExtElmt, |
| 6205 | Ctx->getConstantInt32(I))); |
| 6206 | T = NewT; |
| 6207 | } |
| 6208 | _movp(Dest, T); |
| 6209 | } |
| 6210 | |
| 6211 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6212 | void TargetX86Base<TraitsType>::lowerSelect(const InstSelect *Select) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6213 | Variable *Dest = Select->getDest(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6214 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6215 | if (isVectorType(Dest->getType())) { |
| 6216 | lowerSelectVector(Select); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6217 | return; |
| 6218 | } |
| 6219 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6220 | Operand *Condition = Select->getCondition(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6221 | // Handle folding opportunities. |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6222 | if (const Inst *Producer = FoldingInfo.getProducerFor(Condition)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6223 | assert(Producer->isDeleted()); |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6224 | switch (BoolFolding<Traits>::getProducerKind(Producer)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6225 | default: |
| 6226 | break; |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6227 | case BoolFolding<Traits>::PK_Icmp32: |
| 6228 | case BoolFolding<Traits>::PK_Icmp64: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6229 | lowerIcmpAndConsumer(llvm::cast<InstIcmp>(Producer), Select); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6230 | return; |
| 6231 | } |
Jim Stichnoth | caeaa27 | 2016-01-10 12:53:44 -0800 | [diff] [blame] | 6232 | case BoolFolding<Traits>::PK_Fcmp: { |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6233 | lowerFcmpAndConsumer(llvm::cast<InstFcmp>(Producer), Select); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6234 | return; |
| 6235 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6236 | } |
| 6237 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6238 | |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6239 | Operand *CmpResult = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6240 | Operand *Zero = Ctx->getConstantZero(IceType_i32); |
| 6241 | _cmp(CmpResult, Zero); |
| 6242 | Operand *SrcT = Select->getTrueOperand(); |
| 6243 | Operand *SrcF = Select->getFalseOperand(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6244 | const BrCond Cond = Traits::Cond::Br_ne; |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6245 | lowerSelectMove(Dest, Cond, SrcT, SrcF); |
| 6246 | } |
| 6247 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6248 | template <typename TraitsType> |
| 6249 | void TargetX86Base<TraitsType>::lowerSelectMove(Variable *Dest, BrCond Cond, |
| 6250 | Operand *SrcT, Operand *SrcF) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6251 | Type DestTy = Dest->getType(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6252 | if (typeWidthInBytes(DestTy) == 1 || isFloatingType(DestTy)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6253 | // The cmov instruction doesn't allow 8-bit or FP operands, so we need |
| 6254 | // explicit control flow. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6255 | // 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] | 6256 | auto *Label = InstX86Label::create(Func, this); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6257 | SrcT = legalize(SrcT, Legal_Reg | Legal_Imm); |
| 6258 | _mov(Dest, SrcT); |
| 6259 | _br(Cond, Label); |
| 6260 | SrcF = legalize(SrcF, Legal_Reg | Legal_Imm); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6261 | _redefined(_mov(Dest, SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6262 | Context.insert(Label); |
| 6263 | return; |
| 6264 | } |
| 6265 | // mov t, SrcF; cmov_cond t, SrcT; mov dest, t |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6266 | // But if SrcT is immediate, we might be able to do better, as the cmov |
| 6267 | // instruction doesn't allow an immediate operand: |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6268 | // mov t, SrcT; cmov_!cond t, SrcF; mov dest, t |
| 6269 | if (llvm::isa<Constant>(SrcT) && !llvm::isa<Constant>(SrcF)) { |
| 6270 | std::swap(SrcT, SrcF); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6271 | Cond = InstImpl<TraitsType>::InstX86Base::getOppositeCondition(Cond); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6272 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6273 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6274 | SrcT = legalizeUndef(SrcT); |
| 6275 | SrcF = legalizeUndef(SrcF); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6276 | // Set the low portion. |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6277 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6278 | lowerSelectIntMove(DestLo, Cond, loOperand(SrcT), loOperand(SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6279 | // Set the high portion. |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6280 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6281 | lowerSelectIntMove(DestHi, Cond, hiOperand(SrcT), hiOperand(SrcF)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6282 | return; |
| 6283 | } |
| 6284 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6285 | assert(DestTy == IceType_i16 || DestTy == IceType_i32 || |
| 6286 | (Traits::Is64Bit && DestTy == IceType_i64)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6287 | lowerSelectIntMove(Dest, Cond, SrcT, SrcF); |
| 6288 | } |
| 6289 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6290 | template <typename TraitsType> |
| 6291 | void TargetX86Base<TraitsType>::lowerSelectIntMove(Variable *Dest, BrCond Cond, |
| 6292 | Operand *SrcT, |
| 6293 | Operand *SrcF) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6294 | Variable *T = nullptr; |
| 6295 | SrcF = legalize(SrcF); |
| 6296 | _mov(T, SrcF); |
| 6297 | SrcT = legalize(SrcT, Legal_Reg | Legal_Mem); |
| 6298 | _cmov(T, SrcT, Cond); |
| 6299 | _mov(Dest, T); |
| 6300 | } |
| 6301 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6302 | template <typename TraitsType> |
| 6303 | void TargetX86Base<TraitsType>::lowerMove(Variable *Dest, Operand *Src, |
| 6304 | bool IsRedefinition) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6305 | assert(Dest->getType() == Src->getType()); |
| 6306 | assert(!Dest->isRematerializable()); |
| 6307 | if (!Traits::Is64Bit && Dest->getType() == IceType_i64) { |
| 6308 | Src = legalize(Src); |
| 6309 | Operand *SrcLo = loOperand(Src); |
| 6310 | Operand *SrcHi = hiOperand(Src); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6311 | auto *DestLo = llvm::cast<Variable>(loOperand(Dest)); |
| 6312 | auto *DestHi = llvm::cast<Variable>(hiOperand(Dest)); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6313 | Variable *T_Lo = nullptr, *T_Hi = nullptr; |
| 6314 | _mov(T_Lo, SrcLo); |
| 6315 | _redefined(_mov(DestLo, T_Lo), IsRedefinition); |
| 6316 | _mov(T_Hi, SrcHi); |
| 6317 | _redefined(_mov(DestHi, T_Hi), IsRedefinition); |
| 6318 | } else { |
| 6319 | Operand *SrcLegal; |
| 6320 | if (Dest->hasReg()) { |
| 6321 | // If Dest already has a physical register, then only basic legalization |
| 6322 | // is needed, as the source operand can be a register, immediate, or |
| 6323 | // memory. |
| 6324 | SrcLegal = legalize(Src, Legal_Reg, Dest->getRegNum()); |
| 6325 | } else { |
| 6326 | // If Dest could be a stack operand, then RI must be a physical register |
| 6327 | // or a scalar integer immediate. |
| 6328 | SrcLegal = legalize(Src, Legal_Reg | Legal_Imm); |
| 6329 | } |
| 6330 | if (isVectorType(Dest->getType())) { |
| 6331 | _redefined(_movp(Dest, SrcLegal), IsRedefinition); |
| 6332 | } else { |
| 6333 | _redefined(_mov(Dest, SrcLegal), IsRedefinition); |
| 6334 | } |
| 6335 | } |
| 6336 | } |
| 6337 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6338 | template <typename TraitsType> |
| 6339 | bool TargetX86Base<TraitsType>::lowerOptimizeFcmpSelect( |
| 6340 | const InstFcmp *Fcmp, const InstSelect *Select) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6341 | Operand *CmpSrc0 = Fcmp->getSrc(0); |
| 6342 | Operand *CmpSrc1 = Fcmp->getSrc(1); |
| 6343 | Operand *SelectSrcT = Select->getTrueOperand(); |
| 6344 | Operand *SelectSrcF = Select->getFalseOperand(); |
| 6345 | |
| 6346 | if (CmpSrc0->getType() != SelectSrcT->getType()) |
| 6347 | return false; |
| 6348 | |
| 6349 | // TODO(sehr, stichnot): fcmp/select patterns (e,g., minsd/maxss) go here. |
| 6350 | InstFcmp::FCond Condition = Fcmp->getCondition(); |
| 6351 | switch (Condition) { |
| 6352 | default: |
| 6353 | return false; |
| 6354 | case InstFcmp::True: |
| 6355 | case InstFcmp::False: |
| 6356 | case InstFcmp::Ogt: |
| 6357 | case InstFcmp::Olt: |
| 6358 | (void)CmpSrc0; |
| 6359 | (void)CmpSrc1; |
| 6360 | (void)SelectSrcT; |
| 6361 | (void)SelectSrcF; |
| 6362 | break; |
| 6363 | } |
| 6364 | return false; |
| 6365 | } |
| 6366 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6367 | template <typename TraitsType> |
| 6368 | void TargetX86Base<TraitsType>::lowerIcmp(const InstIcmp *Icmp) { |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6369 | Variable *Dest = Icmp->getDest(); |
| 6370 | if (isVectorType(Dest->getType())) { |
| 6371 | lowerIcmpVector(Icmp); |
| 6372 | } else { |
| 6373 | constexpr Inst *Consumer = nullptr; |
| 6374 | lowerIcmpAndConsumer(Icmp, Consumer); |
| 6375 | } |
| 6376 | } |
| 6377 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6378 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6379 | void TargetX86Base<TraitsType>::lowerSelectVector(const InstSelect *Instr) { |
| 6380 | Variable *Dest = Instr->getDest(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6381 | Type DestTy = Dest->getType(); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6382 | Operand *SrcT = Instr->getTrueOperand(); |
| 6383 | Operand *SrcF = Instr->getFalseOperand(); |
| 6384 | Operand *Condition = Instr->getCondition(); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 6385 | |
| 6386 | if (!isVectorType(DestTy)) |
| 6387 | llvm::report_fatal_error("Expected a vector select"); |
| 6388 | |
| 6389 | Type SrcTy = SrcT->getType(); |
| 6390 | Variable *T = makeReg(SrcTy); |
| 6391 | Operand *SrcTRM = legalize(SrcT, Legal_Reg | Legal_Mem); |
| 6392 | Operand *SrcFRM = legalize(SrcF, Legal_Reg | Legal_Mem); |
| 6393 | if (InstructionSet >= Traits::SSE4_1) { |
| 6394 | // TODO(wala): If the condition operand is a constant, use blendps or |
| 6395 | // pblendw. |
| 6396 | // |
| 6397 | // Use blendvps or pblendvb to implement select. |
| 6398 | if (SrcTy == IceType_v4i1 || SrcTy == IceType_v4i32 || |
| 6399 | SrcTy == IceType_v4f32) { |
| 6400 | Operand *ConditionRM = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6401 | Variable *xmm0 = makeReg(IceType_v4i32, Traits::RegisterSet::Reg_xmm0); |
| 6402 | _movp(xmm0, ConditionRM); |
| 6403 | _psll(xmm0, Ctx->getConstantInt8(31)); |
| 6404 | _movp(T, SrcFRM); |
| 6405 | _blendvps(T, SrcTRM, xmm0); |
| 6406 | _movp(Dest, T); |
| 6407 | } else { |
| 6408 | assert(typeNumElements(SrcTy) == 8 || typeNumElements(SrcTy) == 16); |
| 6409 | Type SignExtTy = |
| 6410 | Condition->getType() == IceType_v8i1 ? IceType_v8i16 : IceType_v16i8; |
| 6411 | Variable *xmm0 = makeReg(SignExtTy, Traits::RegisterSet::Reg_xmm0); |
| 6412 | lowerCast(InstCast::create(Func, InstCast::Sext, xmm0, Condition)); |
| 6413 | _movp(T, SrcFRM); |
| 6414 | _pblendvb(T, SrcTRM, xmm0); |
| 6415 | _movp(Dest, T); |
| 6416 | } |
| 6417 | return; |
| 6418 | } |
| 6419 | // Lower select without Traits::SSE4.1: |
| 6420 | // a=d?b:c ==> |
| 6421 | // if elementtype(d) != i1: |
| 6422 | // d=sext(d); |
| 6423 | // a=(b&d)|(c&~d); |
| 6424 | Variable *T2 = makeReg(SrcTy); |
| 6425 | // Sign extend the condition operand if applicable. |
| 6426 | if (SrcTy == IceType_v4f32) { |
| 6427 | // The sext operation takes only integer arguments. |
| 6428 | Variable *T3 = Func->makeVariable(IceType_v4i32); |
| 6429 | lowerCast(InstCast::create(Func, InstCast::Sext, T3, Condition)); |
| 6430 | _movp(T, T3); |
| 6431 | } else if (typeElementType(SrcTy) != IceType_i1) { |
| 6432 | lowerCast(InstCast::create(Func, InstCast::Sext, T, Condition)); |
| 6433 | } else { |
| 6434 | Operand *ConditionRM = legalize(Condition, Legal_Reg | Legal_Mem); |
| 6435 | _movp(T, ConditionRM); |
| 6436 | } |
| 6437 | _movp(T2, T); |
| 6438 | _pand(T, SrcTRM); |
| 6439 | _pandn(T2, SrcFRM); |
| 6440 | _por(T, T2); |
| 6441 | _movp(Dest, T); |
| 6442 | |
| 6443 | return; |
| 6444 | } |
| 6445 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6446 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6447 | void TargetX86Base<TraitsType>::lowerStore(const InstStore *Instr) { |
| 6448 | Operand *Value = Instr->getData(); |
| 6449 | Operand *Addr = Instr->getAddr(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6450 | X86OperandMem *NewAddr = formMemoryOperand(Addr, Value->getType()); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 6451 | doMockBoundsCheck(NewAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6452 | Type Ty = NewAddr->getType(); |
| 6453 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6454 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6455 | Value = legalizeUndef(Value); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6456 | Operand *ValueHi = legalize(hiOperand(Value), Legal_Reg | Legal_Imm); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6457 | _store(ValueHi, llvm::cast<X86OperandMem>(hiOperand(NewAddr))); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 6458 | Operand *ValueLo = legalize(loOperand(Value), Legal_Reg | Legal_Imm); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6459 | _store(ValueLo, llvm::cast<X86OperandMem>(loOperand(NewAddr))); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6460 | } else if (isVectorType(Ty)) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6461 | _storep(legalizeToReg(Value), NewAddr); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6462 | } else { |
| 6463 | Value = legalize(Value, Legal_Reg | Legal_Imm); |
| 6464 | _store(Value, NewAddr); |
| 6465 | } |
| 6466 | } |
| 6467 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6468 | template <typename TraitsType> |
| 6469 | void TargetX86Base<TraitsType>::doAddressOptStore() { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6470 | auto *Instr = llvm::cast<InstStore>(Context.getCur()); |
| 6471 | Operand *Addr = Instr->getAddr(); |
| 6472 | Operand *Data = Instr->getData(); |
| 6473 | if (auto *OptAddr = computeAddressOpt(Instr, Data->getType(), Addr)) { |
| 6474 | Instr->setDeleted(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6475 | auto *NewStore = Context.insert<InstStore>(Data, OptAddr); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6476 | if (Instr->getDest()) |
| 6477 | NewStore->setRmwBeacon(Instr->getRmwBeacon()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6478 | } |
| 6479 | } |
| 6480 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6481 | template <typename TraitsType> |
| 6482 | Operand *TargetX86Base<TraitsType>::lowerCmpRange(Operand *Comparison, |
| 6483 | uint64_t Min, uint64_t Max) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6484 | // TODO(ascull): 64-bit should not reach here but only because it is not |
| 6485 | // implemented yet. This should be able to handle the 64-bit case. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6486 | assert(Traits::Is64Bit || Comparison->getType() != IceType_i64); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6487 | // Subtracting 0 is a nop so don't do it |
| 6488 | if (Min != 0) { |
| 6489 | // Avoid clobbering the comparison by copying it |
| 6490 | Variable *T = nullptr; |
| 6491 | _mov(T, Comparison); |
| 6492 | _sub(T, Ctx->getConstantInt32(Min)); |
| 6493 | Comparison = T; |
| 6494 | } |
| 6495 | |
| 6496 | _cmp(Comparison, Ctx->getConstantInt32(Max - Min)); |
| 6497 | |
| 6498 | return Comparison; |
| 6499 | } |
| 6500 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6501 | template <typename TraitsType> |
| 6502 | void TargetX86Base<TraitsType>::lowerCaseCluster(const CaseCluster &Case, |
| 6503 | Operand *Comparison, |
| 6504 | bool DoneCmp, |
| 6505 | CfgNode *DefaultTarget) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6506 | switch (Case.getKind()) { |
| 6507 | case CaseCluster::JumpTable: { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6508 | InstX86Label *SkipJumpTable; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6509 | |
| 6510 | Operand *RangeIndex = |
| 6511 | lowerCmpRange(Comparison, Case.getLow(), Case.getHigh()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6512 | if (DefaultTarget == nullptr) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6513 | // 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] | 6514 | SkipJumpTable = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6515 | _br(Traits::Cond::Br_a, SkipJumpTable); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6516 | } else { |
| 6517 | _br(Traits::Cond::Br_a, DefaultTarget); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6518 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6519 | |
| 6520 | InstJumpTable *JumpTable = Case.getJumpTable(); |
| 6521 | Context.insert(JumpTable); |
| 6522 | |
| 6523 | // Make sure the index is a register of the same width as the base |
| 6524 | Variable *Index; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6525 | const Type PointerType = getPointerType(); |
| 6526 | if (RangeIndex->getType() != PointerType) { |
| 6527 | Index = makeReg(PointerType); |
Jim Stichnoth | e641e92 | 2016-02-29 09:54:55 -0800 | [diff] [blame] | 6528 | if (RangeIndex->getType() == IceType_i64) { |
| 6529 | assert(Traits::Is64Bit); |
| 6530 | _mov(Index, RangeIndex); // trunc |
| 6531 | } else { |
| 6532 | _movzx(Index, RangeIndex); |
| 6533 | } |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6534 | } else { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6535 | Index = legalizeToReg(RangeIndex); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6536 | } |
| 6537 | |
| 6538 | constexpr RelocOffsetT RelocOffset = 0; |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6539 | constexpr Variable *NoBase = nullptr; |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 6540 | auto JTName = GlobalString::createWithString(Ctx, JumpTable->getName()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 6541 | Constant *Offset = Ctx->getConstantSym(RelocOffset, JTName); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6542 | uint16_t Shift = typeWidthInBytesLog2(PointerType); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6543 | constexpr auto Segment = X86OperandMem::SegmentRegisters::DefaultSegment; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6544 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6545 | Variable *Target = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6546 | if (Traits::Is64Bit && NeedSandboxing) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6547 | assert(Index != nullptr && Index->getType() == IceType_i32); |
| 6548 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6549 | auto *TargetInMemory = X86OperandMem::create(Func, PointerType, NoBase, |
| 6550 | Offset, Index, Shift, Segment); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6551 | _mov(Target, TargetInMemory); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 6552 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6553 | lowerIndirectJump(Target); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6554 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6555 | if (DefaultTarget == nullptr) |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6556 | Context.insert(SkipJumpTable); |
| 6557 | return; |
| 6558 | } |
| 6559 | case CaseCluster::Range: { |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6560 | if (Case.isUnitRange()) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6561 | // Single item |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6562 | if (!DoneCmp) { |
| 6563 | Constant *Value = Ctx->getConstantInt32(Case.getLow()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6564 | _cmp(Comparison, Value); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6565 | } |
| 6566 | _br(Traits::Cond::Br_e, Case.getTarget()); |
| 6567 | } else if (DoneCmp && Case.isPairRange()) { |
| 6568 | // Range of two items with first item aleady compared against |
| 6569 | _br(Traits::Cond::Br_e, Case.getTarget()); |
| 6570 | Constant *Value = Ctx->getConstantInt32(Case.getHigh()); |
| 6571 | _cmp(Comparison, Value); |
| 6572 | _br(Traits::Cond::Br_e, Case.getTarget()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6573 | } else { |
| 6574 | // Range |
| 6575 | lowerCmpRange(Comparison, Case.getLow(), Case.getHigh()); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6576 | _br(Traits::Cond::Br_be, Case.getTarget()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6577 | } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6578 | if (DefaultTarget != nullptr) |
| 6579 | _br(DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6580 | return; |
| 6581 | } |
| 6582 | } |
| 6583 | } |
| 6584 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6585 | template <typename TraitsType> |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6586 | void TargetX86Base<TraitsType>::lowerSwitch(const InstSwitch *Instr) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6587 | // Group cases together and navigate through them with a binary search |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6588 | CaseClusterArray CaseClusters = CaseCluster::clusterizeSwitch(Func, Instr); |
| 6589 | Operand *Src0 = Instr->getComparison(); |
| 6590 | CfgNode *DefaultTarget = Instr->getLabelDefault(); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6591 | |
| 6592 | assert(CaseClusters.size() != 0); // Should always be at least one |
| 6593 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6594 | if (!Traits::Is64Bit && Src0->getType() == IceType_i64) { |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6595 | Src0 = legalize(Src0); // get Base/Index into physical registers |
| 6596 | Operand *Src0Lo = loOperand(Src0); |
| 6597 | Operand *Src0Hi = hiOperand(Src0); |
| 6598 | if (CaseClusters.back().getHigh() > UINT32_MAX) { |
| 6599 | // TODO(ascull): handle 64-bit case properly (currently naive version) |
| 6600 | // This might be handled by a higher level lowering of switches. |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6601 | SizeT NumCases = Instr->getNumCases(); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6602 | if (NumCases >= 2) { |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6603 | Src0Lo = legalizeToReg(Src0Lo); |
| 6604 | Src0Hi = legalizeToReg(Src0Hi); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6605 | } else { |
| 6606 | Src0Lo = legalize(Src0Lo, Legal_Reg | Legal_Mem); |
| 6607 | Src0Hi = legalize(Src0Hi, Legal_Reg | Legal_Mem); |
| 6608 | } |
| 6609 | for (SizeT I = 0; I < NumCases; ++I) { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6610 | Constant *ValueLo = Ctx->getConstantInt32(Instr->getValue(I)); |
| 6611 | Constant *ValueHi = Ctx->getConstantInt32(Instr->getValue(I) >> 32); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6612 | InstX86Label *Label = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6613 | _cmp(Src0Lo, ValueLo); |
| 6614 | _br(Traits::Cond::Br_ne, Label); |
| 6615 | _cmp(Src0Hi, ValueHi); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6616 | _br(Traits::Cond::Br_e, Instr->getLabel(I)); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6617 | Context.insert(Label); |
| 6618 | } |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6619 | _br(Instr->getLabelDefault()); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6620 | return; |
| 6621 | } else { |
| 6622 | // All the values are 32-bit so just check the operand is too and then |
| 6623 | // fall through to the 32-bit implementation. This is a common case. |
| 6624 | Src0Hi = legalize(Src0Hi, Legal_Reg | Legal_Mem); |
| 6625 | Constant *Zero = Ctx->getConstantInt32(0); |
| 6626 | _cmp(Src0Hi, Zero); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6627 | _br(Traits::Cond::Br_ne, DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6628 | Src0 = Src0Lo; |
| 6629 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6630 | } |
| 6631 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6632 | // 32-bit lowering |
| 6633 | |
| 6634 | if (CaseClusters.size() == 1) { |
| 6635 | // Jump straight to default if needed. Currently a common case as jump |
| 6636 | // tables occur on their own. |
| 6637 | constexpr bool DoneCmp = false; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6638 | lowerCaseCluster(CaseClusters.front(), Src0, DoneCmp, DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6639 | return; |
| 6640 | } |
| 6641 | |
| 6642 | // 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] | 6643 | Variable *Comparison = legalizeToReg(Src0); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6644 | |
| 6645 | // A span is over the clusters |
| 6646 | struct SearchSpan { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6647 | SearchSpan(SizeT Begin, SizeT Size, InstX86Label *Label) |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6648 | : Begin(Begin), Size(Size), Label(Label) {} |
| 6649 | |
| 6650 | SizeT Begin; |
| 6651 | SizeT Size; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6652 | InstX86Label *Label; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6653 | }; |
Andrew Scull | 8447bba | 2015-07-23 11:41:18 -0700 | [diff] [blame] | 6654 | // The stack will only grow to the height of the tree so 12 should be plenty |
| 6655 | std::stack<SearchSpan, llvm::SmallVector<SearchSpan, 12>> SearchSpanStack; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6656 | SearchSpanStack.emplace(0, CaseClusters.size(), nullptr); |
| 6657 | bool DoneCmp = false; |
| 6658 | |
| 6659 | while (!SearchSpanStack.empty()) { |
| 6660 | SearchSpan Span = SearchSpanStack.top(); |
| 6661 | SearchSpanStack.pop(); |
| 6662 | |
| 6663 | if (Span.Label != nullptr) |
| 6664 | Context.insert(Span.Label); |
| 6665 | |
| 6666 | switch (Span.Size) { |
| 6667 | case 0: |
| 6668 | llvm::report_fatal_error("Invalid SearchSpan size"); |
| 6669 | break; |
| 6670 | |
| 6671 | case 1: |
| 6672 | lowerCaseCluster(CaseClusters[Span.Begin], Comparison, DoneCmp, |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6673 | SearchSpanStack.empty() ? nullptr : DefaultTarget); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6674 | DoneCmp = false; |
| 6675 | break; |
| 6676 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6677 | case 2: { |
| 6678 | const CaseCluster *CaseA = &CaseClusters[Span.Begin]; |
| 6679 | const CaseCluster *CaseB = &CaseClusters[Span.Begin + 1]; |
| 6680 | |
| 6681 | // Placing a range last may allow register clobbering during the range |
| 6682 | // test. That means there is no need to clone the register. If it is a |
| 6683 | // unit range the comparison may have already been done in the binary |
| 6684 | // search (DoneCmp) and so it should be placed first. If this is a range |
| 6685 | // of two items and the comparison with the low value has already been |
| 6686 | // done, comparing with the other element is cheaper than a range test. |
| 6687 | // If the low end of the range is zero then there is no subtraction and |
| 6688 | // nothing to be gained. |
| 6689 | if (!CaseA->isUnitRange() && |
| 6690 | !(CaseA->getLow() == 0 || (DoneCmp && CaseA->isPairRange()))) { |
| 6691 | std::swap(CaseA, CaseB); |
| 6692 | DoneCmp = false; |
| 6693 | } |
| 6694 | |
| 6695 | lowerCaseCluster(*CaseA, Comparison, DoneCmp); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6696 | DoneCmp = false; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6697 | lowerCaseCluster(*CaseB, Comparison, DoneCmp, |
| 6698 | SearchSpanStack.empty() ? nullptr : DefaultTarget); |
| 6699 | } break; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6700 | |
| 6701 | default: |
| 6702 | // Pick the middle item and branch b or ae |
| 6703 | SizeT PivotIndex = Span.Begin + (Span.Size / 2); |
| 6704 | const CaseCluster &Pivot = CaseClusters[PivotIndex]; |
| 6705 | Constant *Value = Ctx->getConstantInt32(Pivot.getLow()); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6706 | InstX86Label *Label = InstX86Label::create(Func, this); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6707 | _cmp(Comparison, Value); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6708 | // TODO(ascull): does it alway have to be far? |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6709 | _br(Traits::Cond::Br_b, Label, InstX86Br::Far); |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 6710 | // Lower the left and (pivot+right) sides, falling through to the right |
| 6711 | SearchSpanStack.emplace(Span.Begin, Span.Size / 2, Label); |
| 6712 | SearchSpanStack.emplace(PivotIndex, Span.Size - (Span.Size / 2), nullptr); |
| 6713 | DoneCmp = true; |
| 6714 | break; |
| 6715 | } |
| 6716 | } |
| 6717 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 6718 | _br(DefaultTarget); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6719 | } |
| 6720 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 6721 | /// The following pattern occurs often in lowered C and C++ code: |
| 6722 | /// |
| 6723 | /// %cmp = fcmp/icmp pred <n x ty> %src0, %src1 |
| 6724 | /// %cmp.ext = sext <n x i1> %cmp to <n x ty> |
| 6725 | /// |
| 6726 | /// We can eliminate the sext operation by copying the result of pcmpeqd, |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6727 | /// pcmpgtd, or cmpps (which produce sign extended results) to the result of the |
| 6728 | /// sext operation. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6729 | template <typename TraitsType> |
| 6730 | void TargetX86Base<TraitsType>::eliminateNextVectorSextInstruction( |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6731 | Variable *SignExtendedResult) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 6732 | if (auto *NextCast = |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6733 | llvm::dyn_cast_or_null<InstCast>(Context.getNextInst())) { |
| 6734 | if (NextCast->getCastKind() == InstCast::Sext && |
| 6735 | NextCast->getSrc(0) == SignExtendedResult) { |
| 6736 | NextCast->setDeleted(); |
Andrew Scull | 97f460d | 2015-07-21 10:07:42 -0700 | [diff] [blame] | 6737 | _movp(NextCast->getDest(), legalizeToReg(SignExtendedResult)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6738 | // Skip over the instruction. |
| 6739 | Context.advanceNext(); |
| 6740 | } |
| 6741 | } |
| 6742 | } |
| 6743 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6744 | template <typename TraitsType> |
| 6745 | void TargetX86Base<TraitsType>::lowerUnreachable( |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 6746 | const InstUnreachable * /*Instr*/) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6747 | _ud2(); |
David Sehr | 21fd103 | 2015-11-13 16:32:37 -0800 | [diff] [blame] | 6748 | // Add a fake use of esp to make sure esp adjustments after the unreachable |
| 6749 | // do not get dead-code eliminated. |
| 6750 | keepEspLiveAtExit(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6751 | } |
| 6752 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6753 | template <typename TraitsType> |
Eric Holk | 67c7c41 | 2016-04-15 13:05:37 -0700 | [diff] [blame] | 6754 | void TargetX86Base<TraitsType>::lowerBreakpoint( |
| 6755 | const InstBreakpoint * /*Instr*/) { |
| 6756 | _int3(); |
| 6757 | } |
| 6758 | |
| 6759 | template <typename TraitsType> |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6760 | void TargetX86Base<TraitsType>::lowerRMW(const InstX86FakeRMW *RMW) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6761 | // If the beacon variable's live range does not end in this instruction, then |
| 6762 | // it must end in the modified Store instruction that follows. This means |
| 6763 | // that the original Store instruction is still there, either because the |
| 6764 | // value being stored is used beyond the Store instruction, or because dead |
| 6765 | // code elimination did not happen. In either case, we cancel RMW lowering |
| 6766 | // (and the caller deletes the RMW instruction). |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6767 | if (!RMW->isLastUse(RMW->getBeacon())) |
| 6768 | return; |
| 6769 | Operand *Src = RMW->getData(); |
| 6770 | Type Ty = Src->getType(); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6771 | X86OperandMem *Addr = formMemoryOperand(RMW->getAddr(), Ty); |
Jim Stichnoth | ad2989b | 2015-09-15 10:21:42 -0700 | [diff] [blame] | 6772 | doMockBoundsCheck(Addr); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6773 | if (!Traits::Is64Bit && Ty == IceType_i64) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 6774 | Src = legalizeUndef(Src); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6775 | Operand *SrcLo = legalize(loOperand(Src), Legal_Reg | Legal_Imm); |
| 6776 | Operand *SrcHi = legalize(hiOperand(Src), Legal_Reg | Legal_Imm); |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 6777 | auto *AddrLo = llvm::cast<X86OperandMem>(loOperand(Addr)); |
| 6778 | auto *AddrHi = llvm::cast<X86OperandMem>(hiOperand(Addr)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6779 | switch (RMW->getOp()) { |
| 6780 | default: |
| 6781 | // TODO(stichnot): Implement other arithmetic operators. |
| 6782 | break; |
| 6783 | case InstArithmetic::Add: |
| 6784 | _add_rmw(AddrLo, SrcLo); |
| 6785 | _adc_rmw(AddrHi, SrcHi); |
| 6786 | return; |
| 6787 | case InstArithmetic::Sub: |
| 6788 | _sub_rmw(AddrLo, SrcLo); |
| 6789 | _sbb_rmw(AddrHi, SrcHi); |
| 6790 | return; |
| 6791 | case InstArithmetic::And: |
| 6792 | _and_rmw(AddrLo, SrcLo); |
| 6793 | _and_rmw(AddrHi, SrcHi); |
| 6794 | return; |
| 6795 | case InstArithmetic::Or: |
| 6796 | _or_rmw(AddrLo, SrcLo); |
| 6797 | _or_rmw(AddrHi, SrcHi); |
| 6798 | return; |
| 6799 | case InstArithmetic::Xor: |
| 6800 | _xor_rmw(AddrLo, SrcLo); |
| 6801 | _xor_rmw(AddrHi, SrcHi); |
| 6802 | return; |
| 6803 | } |
| 6804 | } else { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6805 | // x86-32: i8, i16, i32 |
| 6806 | // x86-64: i8, i16, i32, i64 |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6807 | switch (RMW->getOp()) { |
| 6808 | default: |
| 6809 | // TODO(stichnot): Implement other arithmetic operators. |
| 6810 | break; |
| 6811 | case InstArithmetic::Add: |
| 6812 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6813 | _add_rmw(Addr, Src); |
| 6814 | return; |
| 6815 | case InstArithmetic::Sub: |
| 6816 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6817 | _sub_rmw(Addr, Src); |
| 6818 | return; |
| 6819 | case InstArithmetic::And: |
| 6820 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6821 | _and_rmw(Addr, Src); |
| 6822 | return; |
| 6823 | case InstArithmetic::Or: |
| 6824 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6825 | _or_rmw(Addr, Src); |
| 6826 | return; |
| 6827 | case InstArithmetic::Xor: |
| 6828 | Src = legalize(Src, Legal_Reg | Legal_Imm); |
| 6829 | _xor_rmw(Addr, Src); |
| 6830 | return; |
| 6831 | } |
| 6832 | } |
| 6833 | llvm::report_fatal_error("Couldn't lower RMW instruction"); |
| 6834 | } |
| 6835 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6836 | template <typename TraitsType> |
| 6837 | void TargetX86Base<TraitsType>::lowerOther(const Inst *Instr) { |
| 6838 | if (const auto *RMW = llvm::dyn_cast<InstX86FakeRMW>(Instr)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6839 | lowerRMW(RMW); |
| 6840 | } else { |
| 6841 | TargetLowering::lowerOther(Instr); |
| 6842 | } |
| 6843 | } |
| 6844 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6845 | /// Turn an i64 Phi instruction into a pair of i32 Phi instructions, to preserve |
| 6846 | /// integrity of liveness analysis. Undef values are also turned into zeroes, |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6847 | /// since loOperand() and hiOperand() don't expect Undef input. Also, in |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6848 | /// Non-SFI mode, add a FakeUse(RebasePtr) for every pooled constant operand. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6849 | template <typename TraitsType> void TargetX86Base<TraitsType>::prelowerPhis() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 6850 | if (getFlags().getUseNonsfi()) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6851 | assert(RebasePtr); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6852 | CfgNode *Node = Context.getNode(); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6853 | uint32_t RebasePtrUseCount = 0; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6854 | for (Inst &I : Node->getPhis()) { |
| 6855 | auto *Phi = llvm::dyn_cast<InstPhi>(&I); |
| 6856 | if (Phi->isDeleted()) |
| 6857 | continue; |
| 6858 | for (SizeT I = 0; I < Phi->getSrcSize(); ++I) { |
| 6859 | Operand *Src = Phi->getSrc(I); |
| 6860 | // TODO(stichnot): This over-counts for +0.0, and under-counts for other |
| 6861 | // kinds of pooling. |
| 6862 | if (llvm::isa<ConstantRelocatable>(Src) || |
| 6863 | llvm::isa<ConstantFloat>(Src) || llvm::isa<ConstantDouble>(Src)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6864 | ++RebasePtrUseCount; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6865 | } |
| 6866 | } |
| 6867 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 6868 | if (RebasePtrUseCount) { |
| 6869 | Node->getInsts().push_front(InstFakeUse::create(Func, RebasePtr)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 6870 | } |
| 6871 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 6872 | if (Traits::Is64Bit) { |
| 6873 | // On x86-64 we don't need to prelower phis -- the architecture can handle |
| 6874 | // 64-bit integer natively. |
| 6875 | return; |
| 6876 | } |
| 6877 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 6878 | // Pause constant blinding or pooling, blinding or pooling will be done later |
| 6879 | // during phi lowering assignments |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6880 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6881 | PhiLowering::prelowerPhis32Bit<TargetX86Base<TraitsType>>( |
Jan Voung | 5348369 | 2015-07-16 10:47:46 -0700 | [diff] [blame] | 6882 | this, Context.getNode(), Func); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 6883 | } |
| 6884 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 6885 | template <typename TraitsType> |
| 6886 | void TargetX86Base<TraitsType>::genTargetHelperCallFor(Inst *Instr) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6887 | uint32_t StackArgumentsSize = 0; |
| 6888 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(Instr)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6889 | RuntimeHelper HelperID = RuntimeHelper::H_Num; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6890 | Variable *Dest = Arith->getDest(); |
| 6891 | Type DestTy = Dest->getType(); |
| 6892 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
| 6893 | switch (Arith->getOp()) { |
| 6894 | default: |
| 6895 | return; |
| 6896 | case InstArithmetic::Udiv: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6897 | HelperID = RuntimeHelper::H_udiv_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6898 | break; |
| 6899 | case InstArithmetic::Sdiv: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6900 | HelperID = RuntimeHelper::H_sdiv_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6901 | break; |
| 6902 | case InstArithmetic::Urem: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6903 | HelperID = RuntimeHelper::H_urem_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6904 | break; |
| 6905 | case InstArithmetic::Srem: |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6906 | HelperID = RuntimeHelper::H_srem_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6907 | break; |
| 6908 | } |
| 6909 | } else if (isVectorType(DestTy)) { |
| 6910 | Variable *Dest = Arith->getDest(); |
| 6911 | Operand *Src0 = Arith->getSrc(0); |
| 6912 | Operand *Src1 = Arith->getSrc(1); |
| 6913 | switch (Arith->getOp()) { |
| 6914 | default: |
| 6915 | return; |
| 6916 | case InstArithmetic::Mul: |
| 6917 | if (DestTy == IceType_v16i8) { |
| 6918 | scalarizeArithmetic(Arith->getOp(), Dest, Src0, Src1); |
| 6919 | Arith->setDeleted(); |
| 6920 | } |
| 6921 | return; |
| 6922 | case InstArithmetic::Shl: |
| 6923 | case InstArithmetic::Lshr: |
| 6924 | case InstArithmetic::Ashr: |
| 6925 | case InstArithmetic::Udiv: |
| 6926 | case InstArithmetic::Urem: |
| 6927 | case InstArithmetic::Sdiv: |
| 6928 | case InstArithmetic::Srem: |
| 6929 | case InstArithmetic::Frem: |
| 6930 | scalarizeArithmetic(Arith->getOp(), Dest, Src0, Src1); |
| 6931 | Arith->setDeleted(); |
| 6932 | return; |
| 6933 | } |
| 6934 | } else { |
| 6935 | switch (Arith->getOp()) { |
| 6936 | default: |
| 6937 | return; |
| 6938 | case InstArithmetic::Frem: |
| 6939 | if (isFloat32Asserting32Or64(DestTy)) |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6940 | HelperID = RuntimeHelper::H_frem_f32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6941 | else |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6942 | HelperID = RuntimeHelper::H_frem_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6943 | } |
| 6944 | } |
| 6945 | constexpr SizeT MaxSrcs = 2; |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6946 | InstCall *Call = makeHelperCall(HelperID, Dest, MaxSrcs); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6947 | Call->addArg(Arith->getSrc(0)); |
| 6948 | Call->addArg(Arith->getSrc(1)); |
| 6949 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 6950 | Context.insert(Call); |
| 6951 | Arith->setDeleted(); |
| 6952 | } else if (auto *Cast = llvm::dyn_cast<InstCast>(Instr)) { |
| 6953 | InstCast::OpKind CastKind = Cast->getCastKind(); |
| 6954 | Operand *Src0 = Cast->getSrc(0); |
| 6955 | const Type SrcType = Src0->getType(); |
| 6956 | Variable *Dest = Cast->getDest(); |
| 6957 | const Type DestTy = Dest->getType(); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6958 | RuntimeHelper HelperID = RuntimeHelper::H_Num; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 6959 | Variable *CallDest = Dest; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6960 | switch (CastKind) { |
| 6961 | default: |
| 6962 | return; |
| 6963 | case InstCast::Fptosi: |
| 6964 | if (!Traits::Is64Bit && DestTy == IceType_i64) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6965 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6966 | ? RuntimeHelper::H_fptosi_f32_i64 |
| 6967 | : RuntimeHelper::H_fptosi_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6968 | } else { |
| 6969 | return; |
| 6970 | } |
| 6971 | break; |
| 6972 | case InstCast::Fptoui: |
| 6973 | if (isVectorType(DestTy)) { |
| 6974 | assert(DestTy == IceType_v4i32 && SrcType == IceType_v4f32); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6975 | HelperID = RuntimeHelper::H_fptoui_4xi32_f32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6976 | } else if (DestTy == IceType_i64 || |
| 6977 | (!Traits::Is64Bit && DestTy == IceType_i32)) { |
| 6978 | if (Traits::Is64Bit) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6979 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6980 | ? RuntimeHelper::H_fptoui_f32_i64 |
| 6981 | : RuntimeHelper::H_fptoui_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6982 | } else if (isInt32Asserting32Or64(DestTy)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6983 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6984 | ? RuntimeHelper::H_fptoui_f32_i32 |
| 6985 | : RuntimeHelper::H_fptoui_f64_i32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6986 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6987 | HelperID = isFloat32Asserting32Or64(SrcType) |
| 6988 | ? RuntimeHelper::H_fptoui_f32_i64 |
| 6989 | : RuntimeHelper::H_fptoui_f64_i64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 6990 | } |
| 6991 | } else { |
| 6992 | return; |
| 6993 | } |
| 6994 | break; |
| 6995 | case InstCast::Sitofp: |
| 6996 | if (!Traits::Is64Bit && SrcType == IceType_i64) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 6997 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 6998 | ? RuntimeHelper::H_sitofp_i64_f32 |
| 6999 | : RuntimeHelper::H_sitofp_i64_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7000 | } else { |
| 7001 | return; |
| 7002 | } |
| 7003 | break; |
| 7004 | case InstCast::Uitofp: |
| 7005 | if (isVectorType(SrcType)) { |
| 7006 | assert(DestTy == IceType_v4f32 && SrcType == IceType_v4i32); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7007 | HelperID = RuntimeHelper::H_uitofp_4xi32_4xf32; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7008 | } else if (SrcType == IceType_i64 || |
| 7009 | (!Traits::Is64Bit && SrcType == IceType_i32)) { |
| 7010 | if (isInt32Asserting32Or64(SrcType)) { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7011 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 7012 | ? RuntimeHelper::H_uitofp_i32_f32 |
| 7013 | : RuntimeHelper::H_uitofp_i32_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7014 | } else { |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7015 | HelperID = isFloat32Asserting32Or64(DestTy) |
| 7016 | ? RuntimeHelper::H_uitofp_i64_f32 |
| 7017 | : RuntimeHelper::H_uitofp_i64_f64; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7018 | } |
| 7019 | } else { |
| 7020 | return; |
| 7021 | } |
| 7022 | break; |
| 7023 | case InstCast::Bitcast: { |
| 7024 | if (DestTy == Src0->getType()) |
| 7025 | return; |
| 7026 | switch (DestTy) { |
| 7027 | default: |
| 7028 | return; |
| 7029 | case IceType_i8: |
| 7030 | assert(Src0->getType() == IceType_v8i1); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7031 | HelperID = RuntimeHelper::H_bitcast_8xi1_i8; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 7032 | CallDest = Func->makeVariable(IceType_i32); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7033 | break; |
| 7034 | case IceType_i16: |
| 7035 | assert(Src0->getType() == IceType_v16i1); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7036 | HelperID = RuntimeHelper::H_bitcast_16xi1_i16; |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 7037 | CallDest = Func->makeVariable(IceType_i32); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7038 | break; |
| 7039 | case IceType_v8i1: { |
| 7040 | assert(Src0->getType() == IceType_i8); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7041 | HelperID = RuntimeHelper::H_bitcast_i8_8xi1; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7042 | Variable *Src0AsI32 = Func->makeVariable(stackSlotType()); |
| 7043 | // Arguments to functions are required to be at least 32 bits wide. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7044 | Context.insert<InstCast>(InstCast::Zext, Src0AsI32, Src0); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7045 | Src0 = Src0AsI32; |
| 7046 | } break; |
| 7047 | case IceType_v16i1: { |
| 7048 | assert(Src0->getType() == IceType_i16); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7049 | HelperID = RuntimeHelper::H_bitcast_i16_16xi1; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7050 | Variable *Src0AsI32 = Func->makeVariable(stackSlotType()); |
| 7051 | // Arguments to functions are required to be at least 32 bits wide. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7052 | Context.insert<InstCast>(InstCast::Zext, Src0AsI32, Src0); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7053 | Src0 = Src0AsI32; |
| 7054 | } break; |
| 7055 | } |
| 7056 | } break; |
| 7057 | } |
| 7058 | constexpr SizeT MaxSrcs = 1; |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 7059 | InstCall *Call = makeHelperCall(HelperID, CallDest, MaxSrcs); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7060 | Call->addArg(Src0); |
| 7061 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 7062 | Context.insert(Call); |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 7063 | // The PNaCl ABI disallows i8/i16 return types, so truncate the helper call |
| 7064 | // result to the appropriate type as necessary. |
| 7065 | if (CallDest->getType() != Dest->getType()) |
| 7066 | Context.insert<InstCast>(InstCast::Trunc, Dest, CallDest); |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7067 | Cast->setDeleted(); |
| 7068 | } else if (auto *Intrinsic = llvm::dyn_cast<InstIntrinsicCall>(Instr)) { |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7069 | CfgVector<Type> ArgTypes; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7070 | Type ReturnType = IceType_void; |
| 7071 | switch (Intrinsics::IntrinsicID ID = Intrinsic->getIntrinsicInfo().ID) { |
| 7072 | default: |
| 7073 | return; |
| 7074 | case Intrinsics::Ctpop: { |
| 7075 | Operand *Val = Intrinsic->getArg(0); |
| 7076 | Type ValTy = Val->getType(); |
| 7077 | if (ValTy == IceType_i64) |
| 7078 | ArgTypes = {IceType_i64}; |
| 7079 | else |
| 7080 | ArgTypes = {IceType_i32}; |
| 7081 | ReturnType = IceType_i32; |
| 7082 | } break; |
| 7083 | case Intrinsics::Longjmp: |
| 7084 | ArgTypes = {IceType_i32, IceType_i32}; |
| 7085 | ReturnType = IceType_void; |
| 7086 | break; |
| 7087 | case Intrinsics::Memcpy: |
| 7088 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 7089 | ReturnType = IceType_void; |
| 7090 | break; |
| 7091 | case Intrinsics::Memmove: |
| 7092 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 7093 | ReturnType = IceType_void; |
| 7094 | break; |
| 7095 | case Intrinsics::Memset: |
| 7096 | ArgTypes = {IceType_i32, IceType_i32, IceType_i32}; |
| 7097 | ReturnType = IceType_void; |
| 7098 | break; |
| 7099 | case Intrinsics::NaClReadTP: |
| 7100 | ReturnType = IceType_i32; |
| 7101 | break; |
| 7102 | case Intrinsics::Setjmp: |
| 7103 | ArgTypes = {IceType_i32}; |
| 7104 | ReturnType = IceType_i32; |
| 7105 | break; |
| 7106 | } |
| 7107 | StackArgumentsSize = getCallStackArgumentsSizeBytes(ArgTypes, ReturnType); |
| 7108 | } else if (auto *Call = llvm::dyn_cast<InstCall>(Instr)) { |
| 7109 | StackArgumentsSize = getCallStackArgumentsSizeBytes(Call); |
| 7110 | } else if (auto *Ret = llvm::dyn_cast<InstRet>(Instr)) { |
| 7111 | if (!Ret->hasRetValue()) |
| 7112 | return; |
| 7113 | Operand *RetValue = Ret->getRetValue(); |
| 7114 | Type ReturnType = RetValue->getType(); |
| 7115 | if (!isScalarFloatingType(ReturnType)) |
| 7116 | return; |
| 7117 | StackArgumentsSize = typeWidthInBytes(ReturnType); |
| 7118 | } else { |
| 7119 | return; |
| 7120 | } |
| 7121 | StackArgumentsSize = Traits::applyStackAlignment(StackArgumentsSize); |
| 7122 | updateMaxOutArgsSizeBytes(StackArgumentsSize); |
| 7123 | } |
| 7124 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7125 | template <typename TraitsType> |
| 7126 | uint32_t TargetX86Base<TraitsType>::getCallStackArgumentsSizeBytes( |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7127 | const CfgVector<Type> &ArgTypes, Type ReturnType) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7128 | uint32_t OutArgumentsSizeBytes = 0; |
| 7129 | uint32_t XmmArgCount = 0; |
| 7130 | uint32_t GprArgCount = 0; |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7131 | for (Type Ty : ArgTypes) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7132 | // The PNaCl ABI requires the width of arguments to be at least 32 bits. |
| 7133 | assert(typeWidthInBytes(Ty) >= 4); |
| 7134 | if (isVectorType(Ty) && XmmArgCount < Traits::X86_MAX_XMM_ARGS) { |
| 7135 | ++XmmArgCount; |
Jim Stichnoth | c577727 | 2016-06-20 06:46:07 -0700 | [diff] [blame] | 7136 | } else if (isScalarFloatingType(Ty) && Traits::X86_PASS_SCALAR_FP_IN_XMM && |
| 7137 | XmmArgCount < Traits::X86_MAX_XMM_ARGS) { |
| 7138 | ++XmmArgCount; |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7139 | } else if (isScalarIntegerType(Ty) && |
| 7140 | GprArgCount < Traits::X86_MAX_GPR_ARGS) { |
| 7141 | // The 64 bit ABI allows some integers to be passed in GPRs. |
| 7142 | ++GprArgCount; |
| 7143 | } else { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7144 | if (isVectorType(Ty)) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7145 | OutArgumentsSizeBytes = |
| 7146 | Traits::applyStackAlignment(OutArgumentsSizeBytes); |
| 7147 | } |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7148 | OutArgumentsSizeBytes += typeWidthInBytesOnStack(Ty); |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7149 | } |
| 7150 | } |
| 7151 | if (Traits::Is64Bit) |
| 7152 | return OutArgumentsSizeBytes; |
| 7153 | // The 32 bit ABI requires floating point values to be returned on the x87 FP |
| 7154 | // 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] | 7155 | if (isScalarFloatingType(ReturnType)) { |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7156 | OutArgumentsSizeBytes = |
| 7157 | std::max(OutArgumentsSizeBytes, |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7158 | static_cast<uint32_t>(typeWidthInBytesOnStack(ReturnType))); |
David Sehr | 4163b9f | 2015-11-20 21:09:31 -0800 | [diff] [blame] | 7159 | } |
| 7160 | return OutArgumentsSizeBytes; |
| 7161 | } |
| 7162 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7163 | template <typename TraitsType> |
| 7164 | uint32_t TargetX86Base<TraitsType>::getCallStackArgumentsSizeBytes( |
| 7165 | const InstCall *Instr) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7166 | // Build a vector of the arguments' types. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7167 | const SizeT NumArgs = Instr->getNumArgs(); |
| 7168 | CfgVector<Type> ArgTypes; |
| 7169 | ArgTypes.reserve(NumArgs); |
| 7170 | for (SizeT i = 0; i < NumArgs; ++i) { |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 7171 | Operand *Arg = Instr->getArg(i); |
| 7172 | ArgTypes.emplace_back(Arg->getType()); |
| 7173 | } |
| 7174 | // Compute the return type (if any); |
| 7175 | Type ReturnType = IceType_void; |
| 7176 | Variable *Dest = Instr->getDest(); |
| 7177 | if (Dest != nullptr) |
| 7178 | ReturnType = Dest->getType(); |
| 7179 | return getCallStackArgumentsSizeBytes(ArgTypes, ReturnType); |
| 7180 | } |
| 7181 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7182 | template <typename TraitsType> |
| 7183 | Variable *TargetX86Base<TraitsType>::makeZeroedRegister(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7184 | RegNumT RegNum) { |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7185 | Variable *Reg = makeReg(Ty, RegNum); |
| 7186 | switch (Ty) { |
| 7187 | case IceType_i1: |
| 7188 | case IceType_i8: |
| 7189 | case IceType_i16: |
| 7190 | case IceType_i32: |
| 7191 | case IceType_i64: |
| 7192 | // Conservatively do "mov reg, 0" to avoid modifying FLAGS. |
| 7193 | _mov(Reg, Ctx->getConstantZero(Ty)); |
| 7194 | break; |
| 7195 | case IceType_f32: |
| 7196 | case IceType_f64: |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7197 | Context.insert<InstFakeDef>(Reg); |
David Sehr | e398428 | 2015-12-15 17:34:55 -0800 | [diff] [blame] | 7198 | _xorps(Reg, Reg); |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7199 | break; |
| 7200 | default: |
| 7201 | // All vector types use the same pxor instruction. |
| 7202 | assert(isVectorType(Ty)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7203 | Context.insert<InstFakeDef>(Reg); |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7204 | _pxor(Reg, Reg); |
| 7205 | break; |
| 7206 | } |
| 7207 | return Reg; |
| 7208 | } |
| 7209 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7210 | // There is no support for loading or emitting vector constants, so the vector |
| 7211 | // values returned from makeVectorOfZeros, makeVectorOfOnes, etc. are |
| 7212 | // initialized with register operations. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7213 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7214 | // TODO(wala): Add limited support for vector constants so that complex |
| 7215 | // initialization in registers is unnecessary. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7216 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7217 | template <typename TraitsType> |
| 7218 | Variable *TargetX86Base<TraitsType>::makeVectorOfZeros(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7219 | RegNumT RegNum) { |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7220 | return makeZeroedRegister(Ty, RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7221 | } |
| 7222 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7223 | template <typename TraitsType> |
| 7224 | Variable *TargetX86Base<TraitsType>::makeVectorOfMinusOnes(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7225 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7226 | Variable *MinusOnes = makeReg(Ty, RegNum); |
| 7227 | // Insert a FakeDef so the live range of MinusOnes is not overestimated. |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7228 | Context.insert<InstFakeDef>(MinusOnes); |
David Sehr | b19d39c | 2016-01-13 14:17:37 -0800 | [diff] [blame] | 7229 | if (Ty == IceType_f64) |
| 7230 | // Making a vector of minus ones of type f64 is currently only used for the |
| 7231 | // fabs intrinsic. To use the f64 type to create this mask with pcmpeqq |
| 7232 | // requires SSE 4.1. Since we're just creating a mask, pcmpeqd does the |
| 7233 | // same job and only requires SSE2. |
| 7234 | _pcmpeq(MinusOnes, MinusOnes, IceType_f32); |
| 7235 | else |
| 7236 | _pcmpeq(MinusOnes, MinusOnes); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7237 | return MinusOnes; |
| 7238 | } |
| 7239 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7240 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7241 | Variable *TargetX86Base<TraitsType>::makeVectorOfOnes(Type Ty, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7242 | Variable *Dest = makeVectorOfZeros(Ty, RegNum); |
| 7243 | Variable *MinusOne = makeVectorOfMinusOnes(Ty); |
| 7244 | _psub(Dest, MinusOne); |
| 7245 | return Dest; |
| 7246 | } |
| 7247 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7248 | template <typename TraitsType> |
| 7249 | Variable *TargetX86Base<TraitsType>::makeVectorOfHighOrderBits(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7250 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7251 | assert(Ty == IceType_v4i32 || Ty == IceType_v4f32 || Ty == IceType_v8i16 || |
| 7252 | Ty == IceType_v16i8); |
| 7253 | if (Ty == IceType_v4f32 || Ty == IceType_v4i32 || Ty == IceType_v8i16) { |
| 7254 | Variable *Reg = makeVectorOfOnes(Ty, RegNum); |
| 7255 | SizeT Shift = |
| 7256 | typeWidthInBytes(typeElementType(Ty)) * Traits::X86_CHAR_BIT - 1; |
| 7257 | _psll(Reg, Ctx->getConstantInt8(Shift)); |
| 7258 | return Reg; |
| 7259 | } else { |
| 7260 | // SSE has no left shift operation for vectors of 8 bit integers. |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7261 | constexpr uint32_t HIGH_ORDER_BITS_MASK = 0x80808080; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7262 | Constant *ConstantMask = Ctx->getConstantInt32(HIGH_ORDER_BITS_MASK); |
| 7263 | Variable *Reg = makeReg(Ty, RegNum); |
| 7264 | _movd(Reg, legalize(ConstantMask, Legal_Reg | Legal_Mem)); |
| 7265 | _pshufd(Reg, Reg, Ctx->getConstantZero(IceType_i8)); |
| 7266 | return Reg; |
| 7267 | } |
| 7268 | } |
| 7269 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7270 | /// Construct a mask in a register that can be and'ed with a floating-point |
| 7271 | /// value to mask off its sign bit. The value will be <4 x 0x7fffffff> for f32 |
| 7272 | /// and v4f32, and <2 x 0x7fffffffffffffff> for f64. Construct it as vector of |
| 7273 | /// ones logically right shifted one bit. |
| 7274 | // TODO(stichnot): Fix the wala |
| 7275 | // TODO: above, to represent vector constants in memory. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7276 | template <typename TraitsType> |
| 7277 | Variable *TargetX86Base<TraitsType>::makeVectorOfFabsMask(Type Ty, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7278 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7279 | Variable *Reg = makeVectorOfMinusOnes(Ty, RegNum); |
| 7280 | _psrl(Reg, Ctx->getConstantInt8(1)); |
| 7281 | return Reg; |
| 7282 | } |
| 7283 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7284 | template <typename TraitsType> |
| 7285 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7286 | TargetX86Base<TraitsType>::getMemoryOperandForStackSlot(Type Ty, Variable *Slot, |
| 7287 | uint32_t Offset) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7288 | // Ensure that Loc is a stack slot. |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 7289 | assert(Slot->mustNotHaveReg()); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7290 | assert(Slot->getRegNum().hasNoValue()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7291 | // Compute the location of Loc in memory. |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7292 | // TODO(wala,stichnot): lea should not |
| 7293 | // be required. The address of the stack slot is known at compile time |
| 7294 | // (although not until after addProlog()). |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7295 | constexpr Type PointerType = IceType_i32; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7296 | Variable *Loc = makeReg(PointerType); |
| 7297 | _lea(Loc, Slot); |
| 7298 | Constant *ConstantOffset = Ctx->getConstantInt32(Offset); |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7299 | return X86OperandMem::create(Func, Ty, Loc, ConstantOffset); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7300 | } |
| 7301 | |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 7302 | /// Lowering helper to copy a scalar integer source operand into some 8-bit GPR. |
| 7303 | /// Src is assumed to already be legalized. If the source operand is known to |
| 7304 | /// be a memory or immediate operand, a simple mov will suffice. But if the |
| 7305 | /// source operand can be a physical register, then it must first be copied into |
| 7306 | /// a physical register that is truncable to 8-bit, then truncated into a |
| 7307 | /// physical register that can receive a truncation, and finally copied into the |
| 7308 | /// result 8-bit register (which in general can be any 8-bit register). For |
| 7309 | /// example, moving %ebp into %ah may be accomplished as: |
| 7310 | /// movl %ebp, %edx |
| 7311 | /// mov_trunc %edx, %dl // this redundant assignment is ultimately elided |
| 7312 | /// movb %dl, %ah |
| 7313 | /// On the other hand, moving a memory or immediate operand into ah: |
| 7314 | /// movb 4(%ebp), %ah |
| 7315 | /// movb $my_imm, %ah |
| 7316 | /// |
| 7317 | /// Note #1. On a 64-bit target, the "movb 4(%ebp), %ah" is likely not |
| 7318 | /// 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] | 7319 | /// 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] | 7320 | /// Reg_ah. |
| 7321 | /// |
| 7322 | /// Note #2. ConstantRelocatable operands are also put through this process |
| 7323 | /// (not truncated directly) because our ELF emitter does R_386_32 relocations |
| 7324 | /// but not R_386_8 relocations. |
| 7325 | /// |
| 7326 | /// Note #3. If Src is a Variable, the result will be an infinite-weight i8 |
| 7327 | /// Variable with the RCX86_IsTrunc8Rcvr register class. As such, this helper |
| 7328 | /// is a convenient way to prevent ah/bh/ch/dh from being an (invalid) argument |
| 7329 | /// to the pinsrb instruction. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7330 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7331 | Variable *TargetX86Base<TraitsType>::copyToReg8(Operand *Src, RegNumT RegNum) { |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 7332 | Type Ty = Src->getType(); |
| 7333 | assert(isScalarIntegerType(Ty)); |
| 7334 | assert(Ty != IceType_i1); |
| 7335 | Variable *Reg = makeReg(IceType_i8, RegNum); |
| 7336 | Reg->setRegClass(RCX86_IsTrunc8Rcvr); |
| 7337 | if (llvm::isa<Variable>(Src) || llvm::isa<ConstantRelocatable>(Src)) { |
| 7338 | Variable *SrcTruncable = makeReg(Ty); |
| 7339 | switch (Ty) { |
| 7340 | case IceType_i64: |
| 7341 | SrcTruncable->setRegClass(RCX86_Is64To8); |
| 7342 | break; |
| 7343 | case IceType_i32: |
| 7344 | SrcTruncable->setRegClass(RCX86_Is32To8); |
| 7345 | break; |
| 7346 | case IceType_i16: |
| 7347 | SrcTruncable->setRegClass(RCX86_Is16To8); |
| 7348 | break; |
| 7349 | default: |
| 7350 | // i8 - just use default register class |
| 7351 | break; |
| 7352 | } |
| 7353 | Variable *SrcRcvr = makeReg(IceType_i8); |
| 7354 | SrcRcvr->setRegClass(RCX86_IsTrunc8Rcvr); |
| 7355 | _mov(SrcTruncable, Src); |
| 7356 | _mov(SrcRcvr, SrcTruncable); |
| 7357 | Src = SrcRcvr; |
| 7358 | } |
| 7359 | _mov(Reg, Src); |
| 7360 | return Reg; |
| 7361 | } |
| 7362 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7363 | /// Helper for legalize() to emit the right code to lower an operand to a |
| 7364 | /// register of the appropriate type. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7365 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7366 | Variable *TargetX86Base<TraitsType>::copyToReg(Operand *Src, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7367 | Type Ty = Src->getType(); |
| 7368 | Variable *Reg = makeReg(Ty, RegNum); |
| 7369 | if (isVectorType(Ty)) { |
| 7370 | _movp(Reg, Src); |
| 7371 | } else { |
| 7372 | _mov(Reg, Src); |
| 7373 | } |
| 7374 | return Reg; |
| 7375 | } |
| 7376 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7377 | template <typename TraitsType> |
| 7378 | Operand *TargetX86Base<TraitsType>::legalize(Operand *From, LegalMask Allowed, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7379 | RegNumT RegNum) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7380 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7381 | const Type Ty = From->getType(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7382 | // Assert that a physical register is allowed. To date, all calls to |
| 7383 | // legalize() allow a physical register. If a physical register needs to be |
| 7384 | // explicitly disallowed, then new code will need to be written to force a |
| 7385 | // spill. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7386 | assert(Allowed & Legal_Reg); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7387 | // If we're asking for a specific physical register, make sure we're not |
| 7388 | // allowing any other operand kinds. (This could be future work, e.g. allow |
| 7389 | // the shl shift amount to be either an immediate or in ecx.) |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7390 | assert(RegNum.hasNoValue() || Allowed == Legal_Reg); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7391 | |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7392 | // Substitute with an available infinite-weight variable if possible. Only do |
| 7393 | // this when we are not asking for a specific register, and when the |
| 7394 | // substitution is not locked to a specific register, and when the types |
| 7395 | // match, in order to capture the vast majority of opportunities and avoid |
| 7396 | // corner cases in the lowering. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7397 | if (RegNum.hasNoValue()) { |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7398 | if (Variable *Subst = getContext().availabilityGet(From)) { |
| 7399 | // At this point we know there is a potential substitution available. |
| 7400 | if (Subst->mustHaveReg() && !Subst->hasReg()) { |
| 7401 | // At this point we know the substitution will have a register. |
| 7402 | if (From->getType() == Subst->getType()) { |
| 7403 | // At this point we know the substitution's register is compatible. |
| 7404 | return Subst; |
| 7405 | } |
| 7406 | } |
| 7407 | } |
| 7408 | } |
| 7409 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7410 | if (auto *Mem = llvm::dyn_cast<X86OperandMem>(From)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7411 | // Before doing anything with a Mem operand, we need to ensure that the |
| 7412 | // Base and Index components are in physical registers. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7413 | Variable *Base = Mem->getBase(); |
| 7414 | Variable *Index = Mem->getIndex(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7415 | Constant *Offset = Mem->getOffset(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7416 | Variable *RegBase = nullptr; |
| 7417 | Variable *RegIndex = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7418 | uint16_t Shift = Mem->getShift(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7419 | if (Base) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7420 | RegBase = llvm::cast<Variable>( |
| 7421 | legalize(Base, Legal_Reg | Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7422 | } |
| 7423 | if (Index) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7424 | // TODO(jpp): perhaps we should only allow Legal_Reg if |
| 7425 | // Base->isRematerializable. |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7426 | RegIndex = llvm::cast<Variable>( |
| 7427 | legalize(Index, Legal_Reg | Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7428 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7429 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7430 | if (Base != RegBase || Index != RegIndex) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7431 | Mem = X86OperandMem::create(Func, Ty, RegBase, Offset, RegIndex, Shift, |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7432 | Mem->getSegmentRegister()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7433 | } |
| 7434 | |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7435 | // For all Memory Operands, we do randomization/pooling here. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7436 | From = randomizeOrPoolImmediate(Mem); |
| 7437 | |
| 7438 | if (!(Allowed & Legal_Mem)) { |
| 7439 | From = copyToReg(From, RegNum); |
| 7440 | } |
| 7441 | return From; |
| 7442 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7443 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7444 | if (auto *Const = llvm::dyn_cast<Constant>(From)) { |
| 7445 | if (llvm::isa<ConstantUndef>(Const)) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7446 | From = legalizeUndef(Const, RegNum); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7447 | if (isVectorType(Ty)) |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7448 | return From; |
| 7449 | Const = llvm::cast<Constant>(From); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7450 | } |
| 7451 | // There should be no constants of vector type (other than undef). |
| 7452 | assert(!isVectorType(Ty)); |
| 7453 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7454 | // If the operand is a 64 bit constant integer we need to legalize it to a |
| 7455 | // register in x86-64. |
| 7456 | if (Traits::Is64Bit) { |
Jim Stichnoth | 9c2c093 | 2016-06-14 07:27:22 -0700 | [diff] [blame] | 7457 | if (auto *C64 = llvm::dyn_cast<ConstantInteger64>(Const)) { |
| 7458 | if (!Utils::IsInt(32, C64->getValue())) { |
| 7459 | if (RegNum.hasValue()) { |
| 7460 | assert(Traits::getGprForType(IceType_i64, RegNum) == RegNum); |
| 7461 | } |
| 7462 | return copyToReg(Const, RegNum); |
John Porto | 008f4ce | 2015-12-24 13:22:18 -0800 | [diff] [blame] | 7463 | } |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7464 | } |
| 7465 | } |
| 7466 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7467 | // If the operand is an 32 bit constant integer, we should check whether we |
| 7468 | // need to randomize it or pool it. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 7469 | if (auto *C = llvm::dyn_cast<ConstantInteger32>(Const)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7470 | Operand *NewConst = randomizeOrPoolImmediate(C, RegNum); |
| 7471 | if (NewConst != Const) { |
| 7472 | return NewConst; |
| 7473 | } |
| 7474 | } |
| 7475 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7476 | if (auto *CR = llvm::dyn_cast<ConstantRelocatable>(Const)) { |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7477 | // If the operand is a ConstantRelocatable, and Legal_AddrAbs is not |
| 7478 | // specified, and UseNonsfi is indicated, we need to add RebasePtr. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7479 | if (UseNonsfi && !(Allowed & Legal_AddrAbs)) { |
| 7480 | assert(Ty == IceType_i32); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7481 | Variable *NewVar = makeReg(Ty, RegNum); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7482 | auto *Mem = Traits::X86OperandMem::create(Func, Ty, nullptr, CR); |
| 7483 | // LEAs are not automatically sandboxed, thus we explicitly invoke |
| 7484 | // _sandbox_mem_reference. |
| 7485 | _lea(NewVar, _sandbox_mem_reference(Mem)); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7486 | From = NewVar; |
| 7487 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7488 | } else if (isScalarFloatingType(Ty)) { |
| 7489 | // Convert a scalar floating point constant into an explicit memory |
| 7490 | // operand. |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7491 | if (auto *ConstFloat = llvm::dyn_cast<ConstantFloat>(Const)) { |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 7492 | if (Utils::isPositiveZero(ConstFloat->getValue())) |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7493 | return makeZeroedRegister(Ty, RegNum); |
| 7494 | } else if (auto *ConstDouble = llvm::dyn_cast<ConstantDouble>(Const)) { |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 7495 | if (Utils::isPositiveZero(ConstDouble->getValue())) |
Jim Stichnoth | 9916566 | 2015-11-13 14:20:40 -0800 | [diff] [blame] | 7496 | return makeZeroedRegister(Ty, RegNum); |
| 7497 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7498 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7499 | auto *CFrom = llvm::cast<Constant>(From); |
| 7500 | assert(CFrom->getShouldBePooled()); |
| 7501 | Constant *Offset = Ctx->getConstantSym(0, CFrom->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7502 | auto *Mem = X86OperandMem::create(Func, Ty, nullptr, Offset); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7503 | From = Mem; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7504 | } |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7505 | |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7506 | bool NeedsReg = false; |
| 7507 | if (!(Allowed & Legal_Imm) && !isScalarFloatingType(Ty)) |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7508 | // Immediate specifically not allowed. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7509 | NeedsReg = true; |
| 7510 | if (!(Allowed & Legal_Mem) && isScalarFloatingType(Ty)) |
| 7511 | // On x86, FP constants are lowered to mem operands. |
| 7512 | NeedsReg = true; |
| 7513 | if (NeedsReg) { |
| 7514 | From = copyToReg(From, RegNum); |
| 7515 | } |
| 7516 | return From; |
| 7517 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7518 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 7519 | if (auto *Var = llvm::dyn_cast<Variable>(From)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7520 | // Check if the variable is guaranteed a physical register. This can happen |
| 7521 | // either when the variable is pre-colored or when it is assigned infinite |
| 7522 | // weight. |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 7523 | bool MustHaveRegister = (Var->hasReg() || Var->mustHaveReg()); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7524 | bool MustRematerialize = |
| 7525 | (Var->isRematerializable() && !(Allowed & Legal_Rematerializable)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7526 | // We need a new physical register for the operand if: |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7527 | // - Mem is not allowed and Var isn't guaranteed a physical register, or |
| 7528 | // - RegNum is required and Var->getRegNum() doesn't match, or |
| 7529 | // - Var is a rematerializable variable and rematerializable pass-through is |
| 7530 | // not allowed (in which case we need an lea instruction). |
| 7531 | if (MustRematerialize) { |
| 7532 | assert(Ty == IceType_i32); |
| 7533 | Variable *NewVar = makeReg(Ty, RegNum); |
| 7534 | // Since Var is rematerializable, the offset will be added when the lea is |
| 7535 | // emitted. |
| 7536 | constexpr Constant *NoOffset = nullptr; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7537 | auto *Mem = X86OperandMem::create(Func, Ty, Var, NoOffset); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 7538 | _lea(NewVar, Mem); |
| 7539 | From = NewVar; |
| 7540 | } else if ((!(Allowed & Legal_Mem) && !MustHaveRegister) || |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7541 | (RegNum.hasValue() && RegNum != Var->getRegNum())) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7542 | From = copyToReg(From, RegNum); |
| 7543 | } |
| 7544 | return From; |
| 7545 | } |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7546 | |
| 7547 | llvm::report_fatal_error("Unhandled operand kind in legalize()"); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7548 | return From; |
| 7549 | } |
| 7550 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7551 | /// Provide a trivial wrapper to legalize() for this common usage. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7552 | template <typename TraitsType> |
| 7553 | Variable *TargetX86Base<TraitsType>::legalizeToReg(Operand *From, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7554 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7555 | return llvm::cast<Variable>(legalize(From, Legal_Reg, RegNum)); |
| 7556 | } |
| 7557 | |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7558 | /// Legalize undef values to concrete values. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7559 | template <typename TraitsType> |
| 7560 | Operand *TargetX86Base<TraitsType>::legalizeUndef(Operand *From, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7561 | RegNumT RegNum) { |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7562 | Type Ty = From->getType(); |
| 7563 | if (llvm::isa<ConstantUndef>(From)) { |
| 7564 | // Lower undefs to zero. Another option is to lower undefs to an |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7565 | // uninitialized register; however, using an uninitialized register results |
| 7566 | // in less predictable code. |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7567 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7568 | // If in the future the implementation is changed to lower undef values to |
| 7569 | // uninitialized registers, a FakeDef will be needed: |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 7570 | // Context.insert<InstFakeDef>(Reg); |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7571 | // 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] | 7572 | // overestimated. If the constant being lowered is a 64 bit value, then |
| 7573 | // the result should be split and the lo and hi components will need to go |
| 7574 | // in uninitialized registers. |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7575 | if (isVectorType(Ty)) |
| 7576 | return makeVectorOfZeros(Ty, RegNum); |
| 7577 | return Ctx->getConstantZero(Ty); |
| 7578 | } |
| 7579 | return From; |
| 7580 | } |
| 7581 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7582 | /// For the cmp instruction, if Src1 is an immediate, or known to be a physical |
| 7583 | /// register, we can allow Src0 to be a memory operand. Otherwise, Src0 must be |
| 7584 | /// copied into a physical register. (Actually, either Src0 or Src1 can be |
| 7585 | /// chosen for the physical register, but unfortunately we have to commit to one |
| 7586 | /// or the other before register allocation.) |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7587 | template <typename TraitsType> |
| 7588 | Operand *TargetX86Base<TraitsType>::legalizeSrc0ForCmp(Operand *Src0, |
| 7589 | Operand *Src1) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7590 | bool IsSrc1ImmOrReg = false; |
| 7591 | if (llvm::isa<Constant>(Src1)) { |
| 7592 | IsSrc1ImmOrReg = true; |
Jan Voung | fbdd244 | 2015-07-15 12:36:20 -0700 | [diff] [blame] | 7593 | } else if (auto *Var = llvm::dyn_cast<Variable>(Src1)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7594 | if (Var->hasReg()) |
| 7595 | IsSrc1ImmOrReg = true; |
| 7596 | } |
| 7597 | return legalize(Src0, IsSrc1ImmOrReg ? (Legal_Reg | Legal_Mem) : Legal_Reg); |
| 7598 | } |
| 7599 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7600 | template <typename TraitsType> |
| 7601 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7602 | TargetX86Base<TraitsType>::formMemoryOperand(Operand *Opnd, Type Ty, |
| 7603 | bool DoLegalize) { |
| 7604 | auto *Mem = llvm::dyn_cast<X86OperandMem>(Opnd); |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 7605 | // It may be the case that address mode optimization already creates an |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7606 | // X86OperandMem, so in that case it wouldn't need another level of |
John Porto | 921856d | 2015-07-07 11:56:26 -0700 | [diff] [blame] | 7607 | // transformation. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7608 | if (!Mem) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 7609 | auto *Base = llvm::dyn_cast<Variable>(Opnd); |
| 7610 | auto *Offset = llvm::dyn_cast<Constant>(Opnd); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7611 | assert(Base || Offset); |
| 7612 | if (Offset) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7613 | // During memory operand building, we do not blind or pool the constant |
| 7614 | // offset, we will work on the whole memory operand later as one entity |
| 7615 | // later, this save one instruction. By turning blinding and pooling off, |
| 7616 | // we guarantee legalize(Offset) will return a Constant*. |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7617 | if (!llvm::isa<ConstantRelocatable>(Offset)) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7618 | BoolFlagSaver B(RandomizationPoolingPaused, true); |
| 7619 | |
| 7620 | Offset = llvm::cast<Constant>(legalize(Offset)); |
| 7621 | } |
| 7622 | |
| 7623 | assert(llvm::isa<ConstantInteger32>(Offset) || |
| 7624 | llvm::isa<ConstantRelocatable>(Offset)); |
| 7625 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7626 | // Not completely sure whether it's OK to leave IsRebased unset when |
| 7627 | // creating the mem operand. If DoLegalize is true, it will definitely be |
| 7628 | // applied during the legalize() call, but perhaps not during the |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7629 | // randomizeOrPoolImmediate() call. In any case, the emit routines will |
| 7630 | // assert that PIC legalization has been applied. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7631 | Mem = X86OperandMem::create(Func, Ty, Base, Offset); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7632 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7633 | // Do legalization, which contains randomization/pooling or do |
| 7634 | // randomization/pooling. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7635 | return llvm::cast<X86OperandMem>(DoLegalize ? legalize(Mem) |
| 7636 | : randomizeOrPoolImmediate(Mem)); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7637 | } |
| 7638 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7639 | template <typename TraitsType> |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7640 | Variable *TargetX86Base<TraitsType>::makeReg(Type Type, RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7641 | // There aren't any 64-bit integer registers for x86-32. |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7642 | assert(Traits::Is64Bit || Type != IceType_i64); |
John Porto | 5aeed95 | 2015-07-21 13:39:09 -0700 | [diff] [blame] | 7643 | Variable *Reg = Func->makeVariable(Type); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7644 | if (RegNum.hasValue()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7645 | Reg->setRegNum(RegNum); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7646 | else |
| 7647 | Reg->setMustHaveReg(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7648 | return Reg; |
| 7649 | } |
| 7650 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7651 | template <typename TraitsType> |
| 7652 | const Type TargetX86Base<TraitsType>::TypeForSize[] = { |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 7653 | IceType_i8, IceType_i16, IceType_i32, IceType_f64, IceType_v16i8}; |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7654 | template <typename TraitsType> |
| 7655 | Type TargetX86Base<TraitsType>::largestTypeInSize(uint32_t Size, |
| 7656 | uint32_t MaxSize) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 7657 | assert(Size != 0); |
| 7658 | uint32_t TyIndex = llvm::findLastSet(Size, llvm::ZB_Undefined); |
| 7659 | uint32_t MaxIndex = MaxSize == NoSizeLimit |
| 7660 | ? llvm::array_lengthof(TypeForSize) - 1 |
| 7661 | : llvm::findLastSet(MaxSize, llvm::ZB_Undefined); |
| 7662 | return TypeForSize[std::min(TyIndex, MaxIndex)]; |
| 7663 | } |
| 7664 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7665 | template <typename TraitsType> |
| 7666 | Type TargetX86Base<TraitsType>::firstTypeThatFitsSize(uint32_t Size, |
| 7667 | uint32_t MaxSize) { |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 7668 | assert(Size != 0); |
| 7669 | uint32_t TyIndex = llvm::findLastSet(Size, llvm::ZB_Undefined); |
| 7670 | if (!llvm::isPowerOf2_32(Size)) |
| 7671 | ++TyIndex; |
| 7672 | uint32_t MaxIndex = MaxSize == NoSizeLimit |
| 7673 | ? llvm::array_lengthof(TypeForSize) - 1 |
| 7674 | : llvm::findLastSet(MaxSize, llvm::ZB_Undefined); |
| 7675 | return TypeForSize[std::min(TyIndex, MaxIndex)]; |
| 7676 | } |
| 7677 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7678 | template <typename TraitsType> void TargetX86Base<TraitsType>::postLower() { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 7679 | if (Func->getOptLevel() == Opt_m1) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7680 | return; |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 7681 | markRedefinitions(); |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 7682 | Context.availabilityUpdate(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7683 | } |
| 7684 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7685 | template <typename TraitsType> |
| 7686 | void TargetX86Base<TraitsType>::makeRandomRegisterPermutation( |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7687 | llvm::SmallVectorImpl<RegNumT> &Permutation, |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 7688 | const SmallBitVector &ExcludeRegisters, uint64_t Salt) const { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7689 | Traits::makeRandomRegisterPermutation(Func, Permutation, ExcludeRegisters, |
| 7690 | Salt); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7691 | } |
| 7692 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7693 | template <typename TraitsType> |
| 7694 | void TargetX86Base<TraitsType>::emit(const ConstantInteger32 *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7695 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7696 | return; |
| 7697 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7698 | Str << "$" << C->getValue(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7699 | } |
| 7700 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7701 | template <typename TraitsType> |
| 7702 | void TargetX86Base<TraitsType>::emit(const ConstantInteger64 *C) const { |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 7703 | if (!Traits::Is64Bit) { |
| 7704 | llvm::report_fatal_error("Not expecting to emit 64-bit integers"); |
| 7705 | } else { |
| 7706 | if (!BuildDefs::dump()) |
| 7707 | return; |
| 7708 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7709 | Str << "$" << C->getValue(); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 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 | void TargetX86Base<TraitsType>::emit(const ConstantFloat *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7715 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7716 | return; |
| 7717 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7718 | Str << C->getLabelName(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7719 | } |
| 7720 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7721 | template <typename TraitsType> |
| 7722 | void TargetX86Base<TraitsType>::emit(const ConstantDouble *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 7723 | if (!BuildDefs::dump()) |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7724 | return; |
| 7725 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7726 | Str << C->getLabelName(); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7727 | } |
| 7728 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7729 | template <typename TraitsType> |
| 7730 | void TargetX86Base<TraitsType>::emit(const ConstantUndef *) const { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7731 | llvm::report_fatal_error("undef value encountered by emitter."); |
| 7732 | } |
| 7733 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7734 | template <class Machine> |
| 7735 | void TargetX86Base<Machine>::emit(const ConstantRelocatable *C) const { |
| 7736 | if (!BuildDefs::dump()) |
| 7737 | return; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7738 | assert(!getFlags().getUseNonsfi() || |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7739 | C->getName().toString() == GlobalOffsetTable); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 7740 | Ostream &Str = Ctx->getStrEmit(); |
| 7741 | Str << "$"; |
| 7742 | emitWithoutPrefix(C); |
| 7743 | } |
| 7744 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 7745 | /// Randomize or pool an Immediate. |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7746 | template <typename TraitsType> |
| 7747 | Operand * |
| 7748 | TargetX86Base<TraitsType>::randomizeOrPoolImmediate(Constant *Immediate, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7749 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7750 | assert(llvm::isa<ConstantInteger32>(Immediate) || |
| 7751 | llvm::isa<ConstantRelocatable>(Immediate)); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7752 | if (getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7753 | RandomizationPoolingPaused == true) { |
| 7754 | // Immediates randomization/pooling off or paused |
| 7755 | return Immediate; |
| 7756 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7757 | |
| 7758 | if (Traits::Is64Bit && NeedSandboxing) { |
| 7759 | // Immediate randomization/pooling is currently disabled for x86-64 |
| 7760 | // sandboxing for it could generate invalid memory operands. |
| 7761 | assert(false && |
| 7762 | "Constant pooling/randomization is disabled for x8664 sandbox."); |
| 7763 | return Immediate; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7764 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7765 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7766 | if (!Immediate->shouldBeRandomizedOrPooled()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7767 | // the constant Immediate is not eligible for blinding/pooling |
| 7768 | return Immediate; |
| 7769 | } |
| 7770 | Ctx->statsUpdateRPImms(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7771 | switch (getFlags().getRandomizeAndPoolImmediatesOption()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7772 | default: |
| 7773 | llvm::report_fatal_error("Unsupported -randomize-pool-immediates option"); |
| 7774 | case RPI_Randomize: { |
| 7775 | // blind the constant |
| 7776 | // FROM: |
| 7777 | // imm |
| 7778 | // TO: |
| 7779 | // insert: mov imm+cookie, Reg |
| 7780 | // insert: lea -cookie[Reg], Reg |
| 7781 | // => Reg |
| 7782 | // If we have already assigned a phy register, we must come from |
| 7783 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse the |
| 7784 | // assigned register as this assignment is that start of its use-def |
| 7785 | // chain. So we add RegNum argument here. Note we use 'lea' instruction |
| 7786 | // instead of 'xor' to avoid affecting the flags. |
| 7787 | Variable *Reg = makeReg(IceType_i32, RegNum); |
| 7788 | auto *Integer = llvm::cast<ConstantInteger32>(Immediate); |
| 7789 | uint32_t Value = Integer->getValue(); |
| 7790 | uint32_t Cookie = Func->getConstantBlindingCookie(); |
| 7791 | _mov(Reg, Ctx->getConstantInt(IceType_i32, Cookie + Value)); |
| 7792 | Constant *Offset = Ctx->getConstantInt(IceType_i32, 0 - Cookie); |
| 7793 | _lea(Reg, X86OperandMem::create(Func, IceType_i32, Reg, Offset)); |
| 7794 | if (Immediate->getType() == IceType_i32) { |
| 7795 | return Reg; |
| 7796 | } |
| 7797 | Variable *TruncReg = makeReg(Immediate->getType(), RegNum); |
| 7798 | _mov(TruncReg, Reg); |
| 7799 | return TruncReg; |
| 7800 | } |
| 7801 | case RPI_Pool: { |
| 7802 | // pool the constant |
| 7803 | // FROM: |
| 7804 | // imm |
| 7805 | // TO: |
| 7806 | // insert: mov $label, Reg |
| 7807 | // => Reg |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7808 | assert(getFlags().getRandomizeAndPoolImmediatesOption() == RPI_Pool); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7809 | assert(Immediate->getShouldBePooled()); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7810 | // if we have already assigned a phy register, we must come from |
| 7811 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse the |
| 7812 | // assigned register as this assignment is that start of its use-def |
| 7813 | // chain. So we add RegNum argument here. |
| 7814 | Variable *Reg = makeReg(Immediate->getType(), RegNum); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7815 | constexpr RelocOffsetT Offset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7816 | Constant *Symbol = Ctx->getConstantSym(Offset, Immediate->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7817 | constexpr Variable *NoBase = nullptr; |
| 7818 | X86OperandMem *MemOperand = |
| 7819 | X86OperandMem::create(Func, Immediate->getType(), NoBase, Symbol); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7820 | _mov(Reg, MemOperand); |
| 7821 | return Reg; |
| 7822 | } |
| 7823 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7824 | } |
| 7825 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 7826 | template <typename TraitsType> |
| 7827 | typename TargetX86Base<TraitsType>::X86OperandMem * |
| 7828 | TargetX86Base<TraitsType>::randomizeOrPoolImmediate(X86OperandMem *MemOperand, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 7829 | RegNumT RegNum) { |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7830 | assert(MemOperand); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7831 | if (getFlags().getRandomizeAndPoolImmediatesOption() == RPI_None || |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7832 | RandomizationPoolingPaused == true) { |
| 7833 | // immediates randomization/pooling is turned off |
| 7834 | return MemOperand; |
| 7835 | } |
| 7836 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7837 | if (Traits::Is64Bit && NeedSandboxing) { |
| 7838 | // Immediate randomization/pooling is currently disabled for x86-64 |
| 7839 | // sandboxing for it could generate invalid memory operands. |
| 7840 | assert(false && |
| 7841 | "Constant pooling/randomization is disabled for x8664 sandbox."); |
| 7842 | return MemOperand; |
| 7843 | } |
| 7844 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 7845 | // If this memory operand is already a randomized one, we do not randomize it |
| 7846 | // again. |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7847 | if (MemOperand->getRandomized()) |
| 7848 | return MemOperand; |
| 7849 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7850 | auto *C = llvm::dyn_cast_or_null<Constant>(MemOperand->getOffset()); |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7851 | |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7852 | if (C == nullptr) { |
| 7853 | return MemOperand; |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7854 | } |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 7855 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7856 | if (!C->shouldBeRandomizedOrPooled()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7857 | return MemOperand; |
| 7858 | } |
| 7859 | |
| 7860 | // The offset of this mem operand should be blinded or pooled |
| 7861 | Ctx->statsUpdateRPImms(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7862 | switch (getFlags().getRandomizeAndPoolImmediatesOption()) { |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7863 | default: |
| 7864 | llvm::report_fatal_error("Unsupported -randomize-pool-immediates option"); |
| 7865 | case RPI_Randomize: { |
| 7866 | // blind the constant offset |
| 7867 | // FROM: |
| 7868 | // offset[base, index, shift] |
| 7869 | // TO: |
| 7870 | // insert: lea offset+cookie[base], RegTemp |
| 7871 | // => -cookie[RegTemp, index, shift] |
| 7872 | uint32_t Value = |
| 7873 | llvm::dyn_cast<ConstantInteger32>(MemOperand->getOffset())->getValue(); |
| 7874 | uint32_t Cookie = Func->getConstantBlindingCookie(); |
| 7875 | Constant *Mask1 = |
| 7876 | Ctx->getConstantInt(MemOperand->getOffset()->getType(), Cookie + Value); |
| 7877 | Constant *Mask2 = |
| 7878 | Ctx->getConstantInt(MemOperand->getOffset()->getType(), 0 - Cookie); |
| 7879 | |
| 7880 | X86OperandMem *TempMemOperand = X86OperandMem::create( |
| 7881 | Func, MemOperand->getType(), MemOperand->getBase(), Mask1); |
| 7882 | // If we have already assigned a physical register, we must come from |
| 7883 | // advancedPhiLowering()=>lowerAssign(). In this case we should reuse |
| 7884 | // the assigned register as this assignment is that start of its |
| 7885 | // use-def chain. So we add RegNum argument here. |
| 7886 | Variable *RegTemp = makeReg(MemOperand->getOffset()->getType(), RegNum); |
| 7887 | _lea(RegTemp, TempMemOperand); |
| 7888 | |
| 7889 | X86OperandMem *NewMemOperand = X86OperandMem::create( |
| 7890 | Func, MemOperand->getType(), RegTemp, Mask2, MemOperand->getIndex(), |
| 7891 | MemOperand->getShift(), MemOperand->getSegmentRegister(), |
| 7892 | MemOperand->getIsRebased()); |
| 7893 | |
| 7894 | // Label this memory operand as randomized, so we won't randomize it |
| 7895 | // again in case we call legalize() multiple times on this memory |
| 7896 | // operand. |
| 7897 | NewMemOperand->setRandomized(true); |
| 7898 | return NewMemOperand; |
| 7899 | } |
| 7900 | case RPI_Pool: { |
| 7901 | // pool the constant offset |
| 7902 | // FROM: |
| 7903 | // offset[base, index, shift] |
| 7904 | // TO: |
| 7905 | // insert: mov $label, RegTemp |
| 7906 | // insert: lea [base, RegTemp], RegTemp |
| 7907 | // =>[RegTemp, index, shift] |
| 7908 | |
| 7909 | // Memory operand should never exist as source operands in phi lowering |
| 7910 | // assignments, so there is no need to reuse any registers here. For |
| 7911 | // phi lowering, we should not ask for new physical registers in |
| 7912 | // general. However, if we do meet Memory Operand during phi lowering, |
| 7913 | // we should not blind or pool the immediates for now. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 7914 | if (RegNum.hasValue()) |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7915 | return MemOperand; |
| 7916 | Variable *RegTemp = makeReg(IceType_i32); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7917 | assert(MemOperand->getOffset()->getShouldBePooled()); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7918 | constexpr RelocOffsetT SymOffset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7919 | Constant *Symbol = |
| 7920 | Ctx->getConstantSym(SymOffset, MemOperand->getOffset()->getLabelName()); |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7921 | constexpr Variable *NoBase = nullptr; |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7922 | X86OperandMem *SymbolOperand = X86OperandMem::create( |
John Porto | ac2388c | 2016-01-22 07:10:56 -0800 | [diff] [blame] | 7923 | Func, MemOperand->getOffset()->getType(), NoBase, Symbol); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 7924 | _mov(RegTemp, SymbolOperand); |
| 7925 | // If we have a base variable here, we should add the lea instruction |
| 7926 | // to add the value of the base variable to RegTemp. If there is no |
| 7927 | // base variable, we won't need this lea instruction. |
| 7928 | if (MemOperand->getBase()) { |
| 7929 | X86OperandMem *CalculateOperand = X86OperandMem::create( |
| 7930 | Func, MemOperand->getType(), MemOperand->getBase(), nullptr, RegTemp, |
| 7931 | 0, MemOperand->getSegmentRegister()); |
| 7932 | _lea(RegTemp, CalculateOperand); |
| 7933 | } |
| 7934 | X86OperandMem *NewMemOperand = X86OperandMem::create( |
| 7935 | Func, MemOperand->getType(), RegTemp, nullptr, MemOperand->getIndex(), |
| 7936 | MemOperand->getShift(), MemOperand->getSegmentRegister()); |
| 7937 | return NewMemOperand; |
| 7938 | } |
| 7939 | } |
| 7940 | } |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7941 | |
| 7942 | template <typename TraitsType> |
| 7943 | void TargetX86Base<TraitsType>::emitJumpTable( |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 7944 | const Cfg *, const InstJumpTable *JumpTable) const { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7945 | if (!BuildDefs::dump()) |
| 7946 | return; |
| 7947 | Ostream &Str = Ctx->getStrEmit(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7948 | const bool UseNonsfi = getFlags().getUseNonsfi(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 7949 | const char *Prefix = UseNonsfi ? ".data.rel.ro." : ".rodata."; |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 7950 | Str << "\t.section\t" << Prefix << JumpTable->getSectionName() |
| 7951 | << ",\"a\",@progbits\n" |
| 7952 | "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n" |
| 7953 | << JumpTable->getName() << ":"; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7954 | |
| 7955 | // On X86 ILP32 pointers are 32-bit hence the use of .long |
| 7956 | for (SizeT I = 0; I < JumpTable->getNumTargets(); ++I) |
| 7957 | Str << "\n\t.long\t" << JumpTable->getTarget(I)->getAsmName(); |
| 7958 | Str << "\n"; |
| 7959 | } |
| 7960 | |
| 7961 | template <typename TraitsType> |
| 7962 | template <typename T> |
| 7963 | void TargetDataX86<TraitsType>::emitConstantPool(GlobalContext *Ctx) { |
| 7964 | if (!BuildDefs::dump()) |
| 7965 | return; |
| 7966 | Ostream &Str = Ctx->getStrEmit(); |
| 7967 | Type Ty = T::Ty; |
| 7968 | SizeT Align = typeAlignInBytes(Ty); |
| 7969 | ConstantList Pool = Ctx->getConstantPool(Ty); |
| 7970 | |
| 7971 | Str << "\t.section\t.rodata.cst" << Align << ",\"aM\",@progbits," << Align |
| 7972 | << "\n"; |
| 7973 | Str << "\t.align\t" << Align << "\n"; |
| 7974 | |
| 7975 | // If reorder-pooled-constants option is set to true, we need to shuffle the |
| 7976 | // constant pool before emitting it. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7977 | if (getFlags().getReorderPooledConstants() && !Pool.empty()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7978 | // Use the constant's kind value as the salt for creating random number |
| 7979 | // generator. |
| 7980 | Operand::OperandKind K = (*Pool.begin())->getKind(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 7981 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 7982 | RPE_PooledConstantReordering, K); |
| 7983 | RandomShuffle(Pool.begin(), Pool.end(), |
| 7984 | [&RNG](uint64_t N) { return (uint32_t)RNG.next(N); }); |
| 7985 | } |
| 7986 | |
| 7987 | for (Constant *C : Pool) { |
| 7988 | if (!C->getShouldBePooled()) |
| 7989 | continue; |
| 7990 | auto *Const = llvm::cast<typename T::IceType>(C); |
| 7991 | typename T::IceType::PrimType Value = Const->getValue(); |
| 7992 | // Use memcpy() to copy bits from Value into RawValue in a way that avoids |
| 7993 | // breaking strict-aliasing rules. |
| 7994 | typename T::PrimitiveIntType RawValue; |
| 7995 | memcpy(&RawValue, &Value, sizeof(Value)); |
| 7996 | char buf[30]; |
| 7997 | int CharsPrinted = |
| 7998 | snprintf(buf, llvm::array_lengthof(buf), T::PrintfString, RawValue); |
| 7999 | assert(CharsPrinted >= 0); |
| 8000 | assert((size_t)CharsPrinted < llvm::array_lengthof(buf)); |
| 8001 | (void)CharsPrinted; // avoid warnings if asserts are disabled |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 8002 | Str << Const->getLabelName(); |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8003 | Str << ":\n\t" << T::AsmTag << "\t" << buf << "\t/* " << T::TypeName << " " |
| 8004 | << Value << " */\n"; |
| 8005 | } |
| 8006 | } |
| 8007 | |
| 8008 | template <typename TraitsType> |
| 8009 | void TargetDataX86<TraitsType>::lowerConstants() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 8010 | if (getFlags().getDisableTranslation()) |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8011 | return; |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 8012 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8013 | case FT_Elf: { |
| 8014 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 8015 | |
| 8016 | Writer->writeConstantPool<ConstantInteger32>(IceType_i8); |
| 8017 | Writer->writeConstantPool<ConstantInteger32>(IceType_i16); |
| 8018 | Writer->writeConstantPool<ConstantInteger32>(IceType_i32); |
| 8019 | |
| 8020 | Writer->writeConstantPool<ConstantFloat>(IceType_f32); |
| 8021 | Writer->writeConstantPool<ConstantDouble>(IceType_f64); |
| 8022 | } break; |
| 8023 | case FT_Asm: |
| 8024 | case FT_Iasm: { |
| 8025 | OstreamLocker L(Ctx); |
| 8026 | |
| 8027 | emitConstantPool<PoolTypeConverter<uint8_t>>(Ctx); |
| 8028 | emitConstantPool<PoolTypeConverter<uint16_t>>(Ctx); |
| 8029 | emitConstantPool<PoolTypeConverter<uint32_t>>(Ctx); |
| 8030 | |
| 8031 | emitConstantPool<PoolTypeConverter<float>>(Ctx); |
| 8032 | emitConstantPool<PoolTypeConverter<double>>(Ctx); |
| 8033 | } break; |
| 8034 | } |
| 8035 | } |
| 8036 | |
| 8037 | template <typename TraitsType> |
| 8038 | void TargetDataX86<TraitsType>::lowerJumpTables() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 8039 | const bool IsPIC = getFlags().getUseNonsfi(); |
| 8040 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8041 | case FT_Elf: { |
| 8042 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 8043 | for (const JumpTableData &JT : Ctx->getJumpTables()) |
| 8044 | Writer->writeJumpTable(JT, Traits::FK_Abs, IsPIC); |
| 8045 | } break; |
| 8046 | case FT_Asm: |
| 8047 | // Already emitted from Cfg |
| 8048 | break; |
| 8049 | case FT_Iasm: { |
| 8050 | if (!BuildDefs::dump()) |
| 8051 | return; |
| 8052 | Ostream &Str = Ctx->getStrEmit(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 8053 | const char *Prefix = IsPIC ? ".data.rel.ro." : ".rodata."; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8054 | for (const JumpTableData &JT : Ctx->getJumpTables()) { |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 8055 | Str << "\t.section\t" << Prefix << JT.getSectionName() |
| 8056 | << ",\"a\",@progbits\n" |
| 8057 | "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n" |
| 8058 | << JT.getName().toString() << ":"; |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8059 | |
| 8060 | // On X8664 ILP32 pointers are 32-bit hence the use of .long |
| 8061 | for (intptr_t TargetOffset : JT.getTargetOffsets()) |
| 8062 | Str << "\n\t.long\t" << JT.getFunctionName() << "+" << TargetOffset; |
| 8063 | Str << "\n"; |
| 8064 | } |
| 8065 | } break; |
| 8066 | } |
| 8067 | } |
| 8068 | |
| 8069 | template <typename TraitsType> |
| 8070 | void TargetDataX86<TraitsType>::lowerGlobals( |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 8071 | const VariableDeclarationList &Vars, const std::string &SectionSuffix) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 8072 | const bool IsPIC = getFlags().getUseNonsfi(); |
| 8073 | switch (getFlags().getOutFileType()) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8074 | case FT_Elf: { |
| 8075 | ELFObjectWriter *Writer = Ctx->getObjectWriter(); |
| 8076 | Writer->writeDataSection(Vars, Traits::FK_Abs, SectionSuffix, IsPIC); |
| 8077 | } break; |
| 8078 | case FT_Asm: |
| 8079 | case FT_Iasm: { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8080 | OstreamLocker L(Ctx); |
| 8081 | for (const VariableDeclaration *Var : Vars) { |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 8082 | if (getFlags().matchTranslateOnly(Var->getName(), 0)) { |
David Sehr | 6b80cf1 | 2016-01-21 23:16:58 -0800 | [diff] [blame] | 8083 | emitGlobal(*Var, SectionSuffix); |
| 8084 | } |
| 8085 | } |
| 8086 | } break; |
| 8087 | } |
| 8088 | } |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 8089 | } // end of namespace X86NAMESPACE |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 8090 | } // end of namespace Ice |
| 8091 | |
| 8092 | #endif // SUBZERO_SRC_ICETARGETLOWERINGX86BASEIMPL_H |