Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceTargetLoweringARM32.h - ARM32 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 Declares the TargetLoweringARM32 class, which implements the |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 12 | /// TargetLowering interface for the ARM 32-bit architecture. |
| 13 | /// |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef SUBZERO_SRC_ICETARGETLOWERINGARM32_H |
| 17 | #define SUBZERO_SRC_ICETARGETLOWERINGARM32_H |
| 18 | |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 19 | #include "IceAssemblerARM32.h" |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 20 | #include "IceDefs.h" |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 21 | #include "IceInstARM32.h" |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 22 | #include "IceRegistersARM32.h" |
| 23 | #include "IceTargetLowering.h" |
| 24 | |
John Porto | a1cdd57 | 2016-03-01 15:19:29 -0800 | [diff] [blame] | 25 | #include <utility> |
| 26 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 27 | namespace Ice { |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 28 | namespace ARM32 { |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 29 | |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 30 | // Class encapsulating ARM cpu features / instruction set. |
| 31 | class TargetARM32Features { |
| 32 | TargetARM32Features() = delete; |
| 33 | TargetARM32Features(const TargetARM32Features &) = delete; |
| 34 | TargetARM32Features &operator=(const TargetARM32Features &) = delete; |
| 35 | |
| 36 | public: |
| 37 | explicit TargetARM32Features(const ClFlags &Flags); |
| 38 | |
| 39 | enum ARM32InstructionSet { |
| 40 | Begin, |
| 41 | // Neon is the PNaCl baseline instruction set. |
| 42 | Neon = Begin, |
| 43 | HWDivArm, // HW divide in ARM mode (not just Thumb mode). |
| 44 | End |
| 45 | }; |
| 46 | |
| 47 | bool hasFeature(ARM32InstructionSet I) const { return I <= InstructionSet; } |
| 48 | |
| 49 | private: |
| 50 | ARM32InstructionSet InstructionSet = ARM32InstructionSet::Begin; |
| 51 | }; |
| 52 | |
| 53 | // The target lowering logic for ARM32. |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 54 | class TargetARM32 : public TargetLowering { |
| 55 | TargetARM32() = delete; |
| 56 | TargetARM32(const TargetARM32 &) = delete; |
| 57 | TargetARM32 &operator=(const TargetARM32 &) = delete; |
| 58 | |
| 59 | public: |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 60 | static void staticInit(GlobalContext *Ctx); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 61 | |
| 62 | static bool shouldBePooled(const Constant *C) { |
| 63 | if (auto *ConstDouble = llvm::dyn_cast<ConstantDouble>(C)) { |
| 64 | return !Utils::isPositiveZero(ConstDouble->getValue()); |
| 65 | } |
| 66 | if (llvm::isa<ConstantFloat>(C)) |
| 67 | return true; |
| 68 | return false; |
| 69 | } |
| 70 | |
Nicolas Capens | 32f9cce | 2016-10-19 01:24:27 -0400 | [diff] [blame] | 71 | static ::Ice::Type getPointerType() { return ::Ice::IceType_i32; } |
| 72 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 73 | // TODO(jvoung): return a unique_ptr. |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 74 | static std::unique_ptr<::Ice::TargetLowering> create(Cfg *Func) { |
| 75 | return makeUnique<TargetARM32>(Func); |
| 76 | } |
| 77 | |
| 78 | std::unique_ptr<::Ice::Assembler> createAssembler() const override { |
Nicolas Capens | 9534228 | 2021-07-06 12:45:02 -0400 | [diff] [blame] | 79 | return makeUnique<ARM32::AssemblerARM32>(); |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 80 | } |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 81 | |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 82 | void initNodeForLowering(CfgNode *Node) override { |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 83 | Computations.forgetProducers(); |
| 84 | Computations.recordProducers(Node); |
| 85 | Computations.dump(Func); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 88 | void translateOm1() override; |
| 89 | void translateO2() override; |
| 90 | bool doBranchOpt(Inst *I, const CfgNode *NextNode) override; |
| 91 | |
| 92 | SizeT getNumRegisters() const override { return RegARM32::Reg_NUM; } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 93 | Variable *getPhysicalRegister(RegNumT RegNum, |
| 94 | Type Ty = IceType_void) override; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 95 | const char *getRegName(RegNumT RegNum, Type Ty) const override; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 96 | SmallBitVector getRegisterSet(RegSetMask Include, |
| 97 | RegSetMask Exclude) const override; |
| 98 | const SmallBitVector & |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 99 | getRegistersForVariable(const Variable *Var) const override { |
| 100 | RegClass RC = Var->getRegClass(); |
Eric Holk | 658bae2 | 2016-02-08 15:22:18 -0800 | [diff] [blame] | 101 | switch (RC) { |
| 102 | default: |
| 103 | assert(RC < RC_Target); |
| 104 | return TypeToRegisterSet[RC]; |
Nicolas Capens | 846da56 | 2021-07-06 10:55:20 -0400 | [diff] [blame] | 105 | case (RegClass)RegARM32::RCARM32_QtoS: |
Eric Holk | 658bae2 | 2016-02-08 15:22:18 -0800 | [diff] [blame] | 106 | return TypeToRegisterSet[RC]; |
| 107 | } |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 108 | } |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 109 | const SmallBitVector & |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 110 | getAllRegistersForVariable(const Variable *Var) const override { |
| 111 | RegClass RC = Var->getRegClass(); |
Eric Holk | 658bae2 | 2016-02-08 15:22:18 -0800 | [diff] [blame] | 112 | assert((RegARM32::RegClassARM32)RC < RegARM32::RCARM32_NUM); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 113 | return TypeToRegisterSetUnfiltered[RC]; |
| 114 | } |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 115 | const SmallBitVector &getAliasesForRegister(RegNumT Reg) const override { |
John Porto | bb0a5fe | 2015-09-04 11:23:41 -0700 | [diff] [blame] | 116 | return RegisterAliases[Reg]; |
| 117 | } |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 118 | bool hasFramePointer() const override { return UsesFramePointer; } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 119 | void setHasFramePointer() override { UsesFramePointer = true; } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 120 | RegNumT getStackReg() const override { return RegARM32::Reg_sp; } |
| 121 | RegNumT getFrameReg() const override { return RegARM32::Reg_fp; } |
| 122 | RegNumT getFrameOrStackReg() const override { |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 123 | return UsesFramePointer ? getFrameReg() : getStackReg(); |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 124 | } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 125 | RegNumT getReservedTmpReg() const { return RegARM32::Reg_ip; } |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 126 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 127 | size_t typeWidthInBytesOnStack(Type Ty) const override { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 128 | // Round up to the next multiple of 4 bytes. In particular, i1, i8, and i16 |
| 129 | // are rounded up to 4 bytes. |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 130 | return (typeWidthInBytes(Ty) + 3) & ~3; |
| 131 | } |
David Sehr | e39d0ca | 2015-11-06 11:25:41 -0800 | [diff] [blame] | 132 | uint32_t getStackAlignment() const override; |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 133 | void reserveFixedAllocaArea(size_t Size, size_t Align) override { |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 134 | FixedAllocaSizeBytes = Size; |
| 135 | assert(llvm::isPowerOf2_32(Align)); |
| 136 | FixedAllocaAlignBytes = Align; |
| 137 | PrologEmitsFixedAllocas = true; |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 138 | } |
| 139 | int32_t getFrameFixedAllocaOffset() const override { |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 140 | return FixedAllocaSizeBytes - (SpillAreaSizeBytes - MaxOutArgsSizeBytes); |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 141 | } |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 142 | uint32_t maxOutArgsSizeBytes() const override { return MaxOutArgsSizeBytes; } |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 143 | |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 144 | bool shouldSplitToVariable64On32(Type Ty) const override { |
| 145 | return Ty == IceType_i64; |
| 146 | } |
| 147 | |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 148 | // TODO(ascull): what size is best for ARM? |
| 149 | SizeT getMinJumpTableSize() const override { return 3; } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 150 | void emitJumpTable(const Cfg *Func, |
| 151 | const InstJumpTable *JumpTable) const override; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 152 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 153 | void emitVariable(const Variable *Var) const override; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 154 | |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 155 | void emit(const ConstantUndef *C) const final; |
| 156 | void emit(const ConstantInteger32 *C) const final; |
| 157 | void emit(const ConstantInteger64 *C) const final; |
| 158 | void emit(const ConstantFloat *C) const final; |
| 159 | void emit(const ConstantDouble *C) const final; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 160 | void emit(const ConstantRelocatable *C) const final; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 161 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 162 | void lowerArguments() override; |
| 163 | void addProlog(CfgNode *Node) override; |
| 164 | void addEpilog(CfgNode *Node) override; |
| 165 | |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 166 | Operand *loOperand(Operand *Operand); |
| 167 | Operand *hiOperand(Operand *Operand); |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 168 | void finishArgumentLowering(Variable *Arg, Variable *FramePtr, |
John Porto | 3f6b47d | 2015-11-19 05:42:59 -0800 | [diff] [blame] | 169 | size_t BasicFrameOffset, size_t *InArgsSizeBytes); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 170 | |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 171 | bool hasCPUFeature(TargetARM32Features::ARM32InstructionSet I) const { |
| 172 | return CPUFeatures.hasFeature(I); |
| 173 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 174 | |
| 175 | enum OperandLegalization { |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 176 | Legal_Reg = 1 << 0, /// physical register, not stack location |
| 177 | Legal_Flex = 1 << 1, /// A flexible operand2, which can hold rotated small |
| 178 | /// immediates, shifted registers, or modified fp imm. |
| 179 | Legal_Mem = 1 << 2, /// includes [r0, r1 lsl #2] as well as [sp, #12] |
John Porto | 866b6b1 | 2015-12-03 09:45:31 -0800 | [diff] [blame] | 180 | Legal_Rematerializable = 1 << 3, |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 181 | Legal_Default = ~Legal_Rematerializable, |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | using LegalMask = uint32_t; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 185 | Operand *legalizeUndef(Operand *From, RegNumT RegNum = RegNumT()); |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 186 | Operand *legalize(Operand *From, LegalMask Allowed = Legal_Default, |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 187 | RegNumT RegNum = RegNumT()); |
| 188 | Variable *legalizeToReg(Operand *From, RegNumT RegNum = RegNumT()); |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 189 | |
John Porto | 2758bb0 | 2015-11-17 14:31:25 -0800 | [diff] [blame] | 190 | OperandARM32ShAmtImm *shAmtImm(uint32_t ShAmtImm) const { |
| 191 | assert(ShAmtImm < 32); |
| 192 | return OperandARM32ShAmtImm::create( |
| 193 | Func, |
| 194 | llvm::cast<ConstantInteger32>(Ctx->getConstantInt32(ShAmtImm & 0x1F))); |
| 195 | } |
| 196 | |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 197 | GlobalContext *getCtx() const { return Ctx; } |
Jan Voung | d062f73 | 2015-06-15 17:17:31 -0700 | [diff] [blame] | 198 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 199 | protected: |
| 200 | explicit TargetARM32(Cfg *Func); |
| 201 | |
| 202 | void postLower() override; |
| 203 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 204 | enum SafeBoolChain { |
| 205 | SBC_No, |
| 206 | SBC_Yes, |
| 207 | }; |
| 208 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 209 | void lowerAlloca(const InstAlloca *Instr) override; |
| 210 | SafeBoolChain lowerInt1Arithmetic(const InstArithmetic *Instr); |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 211 | void lowerInt64Arithmetic(InstArithmetic::OpKind Op, Variable *Dest, |
| 212 | Operand *Src0, Operand *Src1); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 213 | void lowerArithmetic(const InstArithmetic *Instr) override; |
| 214 | void lowerAssign(const InstAssign *Instr) override; |
| 215 | void lowerBr(const InstBr *Instr) override; |
| 216 | void lowerCall(const InstCall *Instr) override; |
| 217 | void lowerCast(const InstCast *Instr) override; |
| 218 | void lowerExtractElement(const InstExtractElement *Instr) override; |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 219 | |
| 220 | /// CondWhenTrue is a helper type returned by every method in the lowering |
| 221 | /// that emits code to set the condition codes. |
| 222 | class CondWhenTrue { |
| 223 | public: |
| 224 | explicit CondWhenTrue(CondARM32::Cond T0, |
| 225 | CondARM32::Cond T1 = CondARM32::kNone) |
| 226 | : WhenTrue0(T0), WhenTrue1(T1) { |
| 227 | assert(T1 == CondARM32::kNone || T0 != CondARM32::kNone); |
| 228 | assert(T1 != T0 || T0 == CondARM32::kNone); |
| 229 | } |
| 230 | CondARM32::Cond WhenTrue0; |
| 231 | CondARM32::Cond WhenTrue1; |
| 232 | |
| 233 | /// invert returns a new object with WhenTrue0 and WhenTrue1 inverted. |
| 234 | CondWhenTrue invert() const { |
| 235 | switch (WhenTrue0) { |
| 236 | default: |
| 237 | if (WhenTrue1 == CondARM32::kNone) |
| 238 | return CondWhenTrue(InstARM32::getOppositeCondition(WhenTrue0)); |
| 239 | return CondWhenTrue(InstARM32::getOppositeCondition(WhenTrue0), |
| 240 | InstARM32::getOppositeCondition(WhenTrue1)); |
| 241 | case CondARM32::AL: |
| 242 | return CondWhenTrue(CondARM32::kNone); |
| 243 | case CondARM32::kNone: |
| 244 | return CondWhenTrue(CondARM32::AL); |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | CondWhenTrue lowerFcmpCond(const InstFcmp *Instr); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 250 | void lowerFcmp(const InstFcmp *Instr) override; |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 251 | CondWhenTrue lowerInt8AndInt16IcmpCond(InstIcmp::ICond Condition, |
| 252 | Operand *Src0, Operand *Src1); |
| 253 | CondWhenTrue lowerInt32IcmpCond(InstIcmp::ICond Condition, Operand *Src0, |
| 254 | Operand *Src1); |
| 255 | CondWhenTrue lowerInt64IcmpCond(InstIcmp::ICond Condition, Operand *Src0, |
| 256 | Operand *Src1); |
John Porto | 4b6e4b4 | 2016-02-17 05:00:59 -0800 | [diff] [blame] | 257 | CondWhenTrue lowerIcmpCond(InstIcmp::ICond Condition, Operand *Src0, |
| 258 | Operand *Src1); |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 259 | CondWhenTrue lowerIcmpCond(const InstIcmp *Instr); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 260 | void lowerIcmp(const InstIcmp *Instr) override; |
John Porto | 4b6e4b4 | 2016-02-17 05:00:59 -0800 | [diff] [blame] | 261 | /// Emits the basic sequence for lower-linked/store-exclusive loops: |
| 262 | /// |
| 263 | /// retry: |
| 264 | /// ldrex tmp, [Addr] |
| 265 | /// StoreValue = Operation(tmp) |
| 266 | /// strexCond success, StoreValue, [Addr] |
| 267 | /// cmpCond success, #0 |
| 268 | /// bne retry |
| 269 | /// |
| 270 | /// Operation needs to return which value to strex in Addr, it must not change |
| 271 | /// the flags if Cond is not AL, and must not emit any instructions that could |
| 272 | /// end up writing to memory. Operation also needs to handle fake-defing for |
| 273 | /// i64 handling. |
| 274 | void |
| 275 | lowerLoadLinkedStoreExclusive(Type Ty, Operand *Addr, |
| 276 | std::function<Variable *(Variable *)> Operation, |
| 277 | CondARM32::Cond Cond = CondARM32::AL); |
| 278 | void lowerInt64AtomicRMW(Variable *Dest, uint32_t Operation, Operand *Ptr, |
| 279 | Operand *Val); |
John Porto | 578f116 | 2015-10-06 06:54:42 -0700 | [diff] [blame] | 280 | void lowerAtomicRMW(Variable *Dest, uint32_t Operation, Operand *Ptr, |
| 281 | Operand *Val); |
Eric Holk | 67c7c41 | 2016-04-15 13:05:37 -0700 | [diff] [blame] | 282 | void lowerBreakpoint(const InstBreakpoint *Instr) override; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 283 | void lowerIntrinsic(const InstIntrinsic *Instr) override; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 284 | void lowerInsertElement(const InstInsertElement *Instr) override; |
| 285 | void lowerLoad(const InstLoad *Instr) override; |
| 286 | void lowerPhi(const InstPhi *Instr) override; |
| 287 | void lowerRet(const InstRet *Instr) override; |
| 288 | void lowerSelect(const InstSelect *Instr) override; |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 289 | void lowerShuffleVector(const InstShuffleVector *Instr) override; |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 290 | void lowerStore(const InstStore *Instr) override; |
| 291 | void lowerSwitch(const InstSwitch *Instr) override; |
| 292 | void lowerUnreachable(const InstUnreachable *Instr) override; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 293 | void prelowerPhis() override; |
John Porto | f419854 | 2015-11-20 14:17:23 -0800 | [diff] [blame] | 294 | uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) override; |
John Porto | c39ec10 | 2015-12-01 13:00:43 -0800 | [diff] [blame] | 295 | void genTargetHelperCallFor(Inst *Instr) override; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 296 | void doAddressOptLoad() override; |
| 297 | void doAddressOptStore() override; |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 298 | |
Jan Voung | befd03a | 2015-06-02 11:03:03 -0700 | [diff] [blame] | 299 | OperandARM32Mem *formMemoryOperand(Operand *Ptr, Type Ty); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 300 | |
John Porto | 578f116 | 2015-10-06 06:54:42 -0700 | [diff] [blame] | 301 | Variable64On32 *makeI64RegPair(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 302 | Variable *makeReg(Type Ty, RegNumT RegNum = RegNumT()); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 303 | static Type stackSlotType(); |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 304 | Variable *copyToReg(Operand *Src, RegNumT RegNum = RegNumT()); |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 305 | void alignRegisterPow2(Variable *Reg, uint32_t Align, |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 306 | RegNumT TmpRegNum = RegNumT()); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 307 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 308 | /// Returns a vector in a register with the given constant entries. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 309 | Variable *makeVectorOfZeros(Type Ty, RegNumT RegNum = RegNumT()); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 310 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 311 | // If a divide-by-zero check is needed, inserts a: test; branch .LSKIP; trap; |
| 312 | // .LSKIP: <continuation>. If no check is needed nothing is inserted. |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 313 | void div0Check(Type Ty, Operand *SrcLo, Operand *SrcHi); |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 314 | using ExtInstr = void (TargetARM32::*)(Variable *, Variable *, |
| 315 | CondARM32::Cond); |
| 316 | using DivInstr = void (TargetARM32::*)(Variable *, Variable *, Variable *, |
| 317 | CondARM32::Cond); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 318 | void lowerIDivRem(Variable *Dest, Variable *T, Variable *Src0R, Operand *Src1, |
John Porto | c39ec10 | 2015-12-01 13:00:43 -0800 | [diff] [blame] | 319 | ExtInstr ExtFunc, DivInstr DivFunc, bool IsRemainder); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 320 | |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 321 | void lowerCLZ(Variable *Dest, Variable *ValLo, Variable *ValHi); |
| 322 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 323 | // The following are helpers that insert lowered ARM32 instructions with |
| 324 | // minimal syntactic overhead, so that the lowering code can look as close to |
| 325 | // assembly as practical. |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 326 | void _add(Variable *Dest, Variable *Src0, Operand *Src1, |
| 327 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 328 | Context.insert<InstARM32Add>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 329 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 330 | void _adds(Variable *Dest, Variable *Src0, Operand *Src1, |
| 331 | CondARM32::Cond Pred = CondARM32::AL) { |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 332 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 333 | Context.insert<InstARM32Add>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 334 | if (SetFlags) { |
| 335 | Context.insert<InstFakeUse>(Dest); |
| 336 | } |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 337 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 338 | void _adc(Variable *Dest, Variable *Src0, Operand *Src1, |
| 339 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 340 | Context.insert<InstARM32Adc>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 341 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 342 | void _and(Variable *Dest, Variable *Src0, Operand *Src1, |
| 343 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 344 | Context.insert<InstARM32And>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 345 | } |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 346 | void _asr(Variable *Dest, Variable *Src0, Operand *Src1, |
| 347 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 348 | Context.insert<InstARM32Asr>(Dest, Src0, Src1, Pred); |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 349 | } |
Jan Voung | 55500db | 2015-05-26 14:25:40 -0700 | [diff] [blame] | 350 | void _bic(Variable *Dest, Variable *Src0, Operand *Src1, |
| 351 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 352 | Context.insert<InstARM32Bic>(Dest, Src0, Src1, Pred); |
Jan Voung | 55500db | 2015-05-26 14:25:40 -0700 | [diff] [blame] | 353 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 354 | void _br(CfgNode *TargetTrue, CfgNode *TargetFalse, |
| 355 | CondARM32::Cond Condition) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 356 | Context.insert<InstARM32Br>(TargetTrue, TargetFalse, Condition); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 357 | } |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 358 | void _br(CfgNode *Target) { Context.insert<InstARM32Br>(Target); } |
Andrew Scull | fdc54db | 2015-06-29 11:21:18 -0700 | [diff] [blame] | 359 | void _br(CfgNode *Target, CondARM32::Cond Condition) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 360 | Context.insert<InstARM32Br>(Target, Condition); |
Andrew Scull | fdc54db | 2015-06-29 11:21:18 -0700 | [diff] [blame] | 361 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 362 | void _br(InstARM32Label *Label, CondARM32::Cond Condition) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 363 | Context.insert<InstARM32Br>(Label, Condition); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 364 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 365 | void _cmn(Variable *Src0, Operand *Src1, |
| 366 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 367 | Context.insert<InstARM32Cmn>(Src0, Src1, Pred); |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 368 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 369 | void _cmp(Variable *Src0, Operand *Src1, |
| 370 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 371 | Context.insert<InstARM32Cmp>(Src0, Src1, Pred); |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 372 | } |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 373 | void _clz(Variable *Dest, Variable *Src0, |
| 374 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 375 | Context.insert<InstARM32Clz>(Dest, Src0, Pred); |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 376 | } |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 377 | void _dmb() { Context.insert<InstARM32Dmb>(); } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 378 | void _eor(Variable *Dest, Variable *Src0, Operand *Src1, |
| 379 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 380 | Context.insert<InstARM32Eor>(Dest, Src0, Src1, Pred); |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 381 | } |
John Porto | ba6a67c | 2015-09-25 15:19:45 -0700 | [diff] [blame] | 382 | /// _ldr, for all your memory to Variable data moves. It handles all types |
| 383 | /// (integer, floating point, and vectors.) Addr needs to be valid for Dest's |
| 384 | /// type (e.g., no immediates for vector loads, and no index registers for fp |
| 385 | /// loads.) |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 386 | void _ldr(Variable *Dest, OperandARM32Mem *Addr, |
| 387 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 388 | Context.insert<InstARM32Ldr>(Dest, Addr, Pred); |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 389 | } |
John Porto | 4b6e4b4 | 2016-02-17 05:00:59 -0800 | [diff] [blame] | 390 | InstARM32Ldrex *_ldrex(Variable *Dest, OperandARM32Mem *Addr, |
| 391 | CondARM32::Cond Pred = CondARM32::AL) { |
| 392 | auto *Ldrex = Context.insert<InstARM32Ldrex>(Dest, Addr, Pred); |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 393 | if (auto *Dest64 = llvm::dyn_cast<Variable64On32>(Dest)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 394 | Context.insert<InstFakeDef>(Dest64->getLo(), Dest); |
| 395 | Context.insert<InstFakeDef>(Dest64->getHi(), Dest); |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 396 | } |
John Porto | 4b6e4b4 | 2016-02-17 05:00:59 -0800 | [diff] [blame] | 397 | return Ldrex; |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 398 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 399 | void _lsl(Variable *Dest, Variable *Src0, Operand *Src1, |
| 400 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 401 | Context.insert<InstARM32Lsl>(Dest, Src0, Src1, Pred); |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 402 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 403 | void _lsls(Variable *Dest, Variable *Src0, Operand *Src1, |
| 404 | CondARM32::Cond Pred = CondARM32::AL) { |
| 405 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 406 | Context.insert<InstARM32Lsl>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 407 | if (SetFlags) { |
| 408 | Context.insert<InstFakeUse>(Dest); |
| 409 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 410 | } |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 411 | void _lsr(Variable *Dest, Variable *Src0, Operand *Src1, |
| 412 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 413 | Context.insert<InstARM32Lsr>(Dest, Src0, Src1, Pred); |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 414 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 415 | void _mla(Variable *Dest, Variable *Src0, Variable *Src1, Variable *Acc, |
| 416 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 417 | Context.insert<InstARM32Mla>(Dest, Src0, Src1, Acc, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 418 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 419 | void _mls(Variable *Dest, Variable *Src0, Variable *Src1, Variable *Acc, |
| 420 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 421 | Context.insert<InstARM32Mls>(Dest, Src0, Src1, Acc, Pred); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 422 | } |
John Porto | ba6a67c | 2015-09-25 15:19:45 -0700 | [diff] [blame] | 423 | /// _mov, for all your Variable to Variable data movement needs. It handles |
| 424 | /// all types (integer, floating point, and vectors), as well as moves between |
| 425 | /// Core and VFP registers. This is not a panacea: you must obey the (weird, |
| 426 | /// confusing, non-uniform) rules for data moves in ARM. |
| 427 | void _mov(Variable *Dest, Operand *Src0, |
| 428 | CondARM32::Cond Pred = CondARM32::AL) { |
| 429 | // _mov used to be unique in the sense that it would create a temporary |
| 430 | // automagically if Dest was nullptr. It won't do that anymore, so we keep |
| 431 | // an assert around just in case there is some untested code path where Dest |
| 432 | // is nullptr. |
| 433 | assert(Dest != nullptr); |
John Porto | 3f6b47d | 2015-11-19 05:42:59 -0800 | [diff] [blame] | 434 | assert(!llvm::isa<OperandARM32Mem>(Src0)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 435 | auto *Instr = Context.insert<InstARM32Mov>(Dest, Src0, Pred); |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 436 | |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 437 | if (Instr->isMultiDest()) { |
| 438 | // If Instr is multi-dest, then Dest must be a Variable64On32. We add a |
| 439 | // fake-def for Instr.DestHi here. |
| 440 | assert(llvm::isa<Variable64On32>(Dest)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 441 | Context.insert<InstFakeDef>(Instr->getDestHi()); |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 442 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 443 | } |
John Porto | 578f116 | 2015-10-06 06:54:42 -0700 | [diff] [blame] | 444 | |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 445 | void _mov_redefined(Variable *Dest, Operand *Src0, |
| 446 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 447 | auto *Instr = Context.insert<InstARM32Mov>(Dest, Src0, Pred); |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 448 | Instr->setDestRedefined(); |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 449 | if (Instr->isMultiDest()) { |
| 450 | // If Instr is multi-dest, then Dest must be a Variable64On32. We add a |
| 451 | // fake-def for Instr.DestHi here. |
| 452 | assert(llvm::isa<Variable64On32>(Dest)); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 453 | Context.insert<InstFakeDef>(Instr->getDestHi()); |
John Porto | e0b829f | 2015-09-28 09:50:48 -0700 | [diff] [blame] | 454 | } |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 455 | } |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 456 | |
Karl Schimpf | f084a57 | 2016-02-09 13:09:23 -0800 | [diff] [blame] | 457 | void _nop() { Context.insert<InstARM32Nop>(); } |
| 458 | |
Eric Holk | 658bae2 | 2016-02-08 15:22:18 -0800 | [diff] [blame] | 459 | // Generates a vmov instruction to extract the given index from a vector |
| 460 | // register. |
| 461 | void _extractelement(Variable *Dest, Variable *Src0, uint32_t Index, |
| 462 | CondARM32::Cond Pred = CondARM32::AL) { |
| 463 | Context.insert<InstARM32Extract>(Dest, Src0, Index, Pred); |
| 464 | } |
| 465 | |
| 466 | // Generates a vmov instruction to insert a value into the given index of a |
| 467 | // vector register. |
| 468 | void _insertelement(Variable *Dest, Variable *Src0, uint32_t Index, |
| 469 | CondARM32::Cond Pred = CondARM32::AL) { |
| 470 | Context.insert<InstARM32Insert>(Dest, Src0, Index, Pred); |
| 471 | } |
| 472 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 473 | // -------------------------------------------------------------------------- |
| 474 | // Begin bool folding machinery. |
| 475 | // |
| 476 | // There are three types of boolean lowerings handled by this target: |
| 477 | // |
| 478 | // 1) Boolean expressions leading to a boolean Variable definition |
| 479 | // --------------------------------------------------------------- |
| 480 | // |
| 481 | // Whenever a i1 Variable is live out (i.e., its live range extends beyond |
| 482 | // the defining basic block) we do not fold the operation. We instead |
| 483 | // materialize (i.e., compute) the variable normally, so that it can be used |
| 484 | // when needed. We also materialize i1 values that are not single use to |
| 485 | // avoid code duplication. These expressions are not short circuited. |
| 486 | // |
| 487 | // 2) Boolean expressions leading to a select |
| 488 | // ------------------------------------------ |
| 489 | // |
| 490 | // These include boolean chains leading to a select instruction, as well as |
| 491 | // i1 Sexts. These boolean expressions are lowered to: |
| 492 | // |
| 493 | // mov T, <false value> |
| 494 | // CC <- eval(Boolean Expression) |
| 495 | // movCC T, <true value> |
| 496 | // |
| 497 | // For Sexts, <false value> is 0, and <true value> is -1. |
| 498 | // |
| 499 | // 3) Boolean expressions leading to a br i1 |
| 500 | // ----------------------------------------- |
| 501 | // |
| 502 | // These are the boolean chains leading to a branch. These chains are |
| 503 | // short-circuited, i.e.: |
| 504 | // |
| 505 | // A = or i1 B, C |
| 506 | // br i1 A, label %T, label %F |
| 507 | // |
| 508 | // becomes |
| 509 | // |
| 510 | // tst B |
| 511 | // jne %T |
| 512 | // tst B |
| 513 | // jne %T |
| 514 | // j %F |
| 515 | // |
| 516 | // and |
| 517 | // |
| 518 | // A = and i1 B, C |
| 519 | // br i1 A, label %T, label %F |
| 520 | // |
| 521 | // becomes |
| 522 | // |
| 523 | // tst B |
| 524 | // jeq %F |
| 525 | // tst B |
| 526 | // jeq %F |
| 527 | // j %T |
| 528 | // |
| 529 | // Arbitrarily long chains are short circuited, e.g |
| 530 | // |
| 531 | // A = or i1 B, C |
| 532 | // D = and i1 A, E |
| 533 | // F = and i1 G, H |
| 534 | // I = or i1 D, F |
| 535 | // br i1 I, label %True, label %False |
| 536 | // |
| 537 | // becomes |
| 538 | // |
| 539 | // Label[A]: |
| 540 | // tst B, 1 |
| 541 | // bne Label[D] |
| 542 | // tst C, 1 |
| 543 | // beq Label[I] |
| 544 | // Label[D]: |
| 545 | // tst E, 1 |
| 546 | // bne %True |
| 547 | // Label[I] |
| 548 | // tst G, 1 |
| 549 | // beq %False |
| 550 | // tst H, 1 |
| 551 | // beq %False (bne %True) |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 552 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 553 | /// lowerInt1 materializes Boolean to a Variable. |
| 554 | SafeBoolChain lowerInt1(Variable *Dest, Operand *Boolean); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 555 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 556 | /// lowerInt1ForSelect generates the following instruction sequence: |
| 557 | /// |
| 558 | /// mov T, FalseValue |
| 559 | /// CC <- eval(Boolean) |
| 560 | /// movCC T, TrueValue |
| 561 | /// mov Dest, T |
| 562 | /// |
| 563 | /// It is used for lowering select i1, as well as i1 Sext. |
| 564 | void lowerInt1ForSelect(Variable *Dest, Operand *Boolean, Operand *TrueValue, |
| 565 | Operand *FalseValue); |
| 566 | |
| 567 | /// LowerInt1BranchTarget is used by lowerIntForBranch. It wraps a CfgNode, or |
| 568 | /// an InstARM32Label (but never both) so that, during br i1 lowering, we can |
| 569 | /// create auxiliary labels for short circuiting the condition evaluation. |
| 570 | class LowerInt1BranchTarget { |
| 571 | public: |
| 572 | explicit LowerInt1BranchTarget(CfgNode *const Target) |
| 573 | : NodeTarget(Target) {} |
| 574 | explicit LowerInt1BranchTarget(InstARM32Label *const Target) |
| 575 | : LabelTarget(Target) {} |
| 576 | |
| 577 | /// createForLabelOrDuplicate will return a new LowerInt1BranchTarget that |
| 578 | /// is the exact copy of this if Label is nullptr; otherwise, the returned |
| 579 | /// object will wrap Label instead. |
| 580 | LowerInt1BranchTarget |
| 581 | createForLabelOrDuplicate(InstARM32Label *Label) const { |
| 582 | if (Label != nullptr) |
| 583 | return LowerInt1BranchTarget(Label); |
| 584 | if (NodeTarget) |
| 585 | return LowerInt1BranchTarget(NodeTarget); |
| 586 | return LowerInt1BranchTarget(LabelTarget); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 587 | } |
| 588 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 589 | CfgNode *const NodeTarget = nullptr; |
| 590 | InstARM32Label *const LabelTarget = nullptr; |
| 591 | }; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 592 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 593 | /// LowerInt1AllowShortCircuit is a helper type used by lowerInt1ForBranch for |
| 594 | /// determining which type arithmetic is allowed to be short circuited. This |
| 595 | /// is useful for lowering |
| 596 | /// |
| 597 | /// t1 = and i1 A, B |
| 598 | /// t2 = and i1 t1, C |
| 599 | /// br i1 t2, label %False, label %True |
| 600 | /// |
| 601 | /// to |
| 602 | /// |
| 603 | /// tst A, 1 |
| 604 | /// beq %False |
| 605 | /// tst B, 1 |
| 606 | /// beq %False |
| 607 | /// tst C, 1 |
| 608 | /// bne %True |
| 609 | /// b %False |
| 610 | /// |
| 611 | /// Without this information, short circuiting would only allow to short |
| 612 | /// circuit a single high level instruction. For example: |
| 613 | /// |
| 614 | /// t1 = or i1 A, B |
| 615 | /// t2 = and i1 t1, C |
| 616 | /// br i1 t2, label %False, label %True |
| 617 | /// |
| 618 | /// cannot be lowered to |
| 619 | /// |
| 620 | /// tst A, 1 |
| 621 | /// bne %True |
| 622 | /// tst B, 1 |
| 623 | /// bne %True |
| 624 | /// tst C, 1 |
| 625 | /// beq %True |
| 626 | /// b %False |
| 627 | /// |
| 628 | /// It needs to be lowered to |
| 629 | /// |
| 630 | /// tst A, 1 |
| 631 | /// bne Aux |
| 632 | /// tst B, 1 |
| 633 | /// beq %False |
| 634 | /// Aux: |
| 635 | /// tst C, 1 |
| 636 | /// bne %True |
| 637 | /// b %False |
| 638 | /// |
| 639 | /// TODO(jpp): evaluate if this kind of short circuiting hurts performance (it |
| 640 | /// might.) |
| 641 | enum LowerInt1AllowShortCircuit { |
| 642 | SC_And = 1, |
| 643 | SC_Or = 2, |
| 644 | SC_All = SC_And | SC_Or, |
| 645 | }; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 646 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 647 | /// ShortCircuitCondAndLabel wraps the condition codes that should be used |
| 648 | /// after a lowerInt1ForBranch returns to branch to the |
| 649 | /// TrueTarget/FalseTarget. If ShortCircuitLabel is not nullptr, then the |
| 650 | /// called lowerInt1forBranch created an internal (i.e., short-circuit) label |
| 651 | /// used for short circuiting. |
| 652 | class ShortCircuitCondAndLabel { |
| 653 | public: |
| 654 | explicit ShortCircuitCondAndLabel(CondWhenTrue &&C, |
| 655 | InstARM32Label *L = nullptr) |
| 656 | : Cond(std::move(C)), ShortCircuitTarget(L) {} |
| 657 | const CondWhenTrue Cond; |
| 658 | InstARM32Label *const ShortCircuitTarget; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 659 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 660 | CondWhenTrue assertNoLabelAndReturnCond() const { |
| 661 | assert(ShortCircuitTarget == nullptr); |
| 662 | return Cond; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 663 | } |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 664 | }; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 665 | |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 666 | /// lowerInt1ForBranch expands Boolean, and returns the condition codes that |
| 667 | /// are to be used for branching to the branch's TrueTarget. It may return a |
| 668 | /// label that the expansion of Boolean used to short circuit the chain's |
| 669 | /// evaluation. |
| 670 | ShortCircuitCondAndLabel |
| 671 | lowerInt1ForBranch(Operand *Boolean, const LowerInt1BranchTarget &TargetTrue, |
| 672 | const LowerInt1BranchTarget &TargetFalse, |
| 673 | uint32_t ShortCircuitable); |
| 674 | |
| 675 | // _br is a convenience wrapper that emits br instructions to Target. |
| 676 | void _br(const LowerInt1BranchTarget &BrTarget, |
| 677 | CondARM32::Cond Cond = CondARM32::AL) { |
| 678 | assert((BrTarget.NodeTarget == nullptr) != |
| 679 | (BrTarget.LabelTarget == nullptr)); |
| 680 | if (BrTarget.NodeTarget != nullptr) |
| 681 | _br(BrTarget.NodeTarget, Cond); |
| 682 | else |
| 683 | _br(BrTarget.LabelTarget, Cond); |
| 684 | } |
| 685 | |
| 686 | // _br_short_circuit is used when lowering InstArithmetic::And and |
| 687 | // InstArithmetic::Or and a short circuit branch is needed. |
| 688 | void _br_short_circuit(const LowerInt1BranchTarget &Target, |
| 689 | const CondWhenTrue &Cond) { |
| 690 | if (Cond.WhenTrue1 != CondARM32::kNone) { |
| 691 | _br(Target, Cond.WhenTrue1); |
| 692 | } |
| 693 | if (Cond.WhenTrue0 != CondARM32::kNone) { |
| 694 | _br(Target, Cond.WhenTrue0); |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 697 | // End of bool folding machinery |
| 698 | // -------------------------------------------------------------------------- |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 699 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 700 | /// The Operand can only be a 16-bit immediate or a ConstantRelocatable (with |
| 701 | /// an upper16 relocation). |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 702 | void _movt(Variable *Dest, Operand *Src0, |
| 703 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 704 | Context.insert<InstARM32Movt>(Dest, Src0, Pred); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 705 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 706 | void _movw(Variable *Dest, Operand *Src0, |
| 707 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 708 | Context.insert<InstARM32Movw>(Dest, Src0, Pred); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 709 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 710 | void _mul(Variable *Dest, Variable *Src0, Variable *Src1, |
| 711 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 712 | Context.insert<InstARM32Mul>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 713 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 714 | void _mvn(Variable *Dest, Operand *Src0, |
| 715 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 716 | Context.insert<InstARM32Mvn>(Dest, Src0, Pred); |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 717 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 718 | void _orr(Variable *Dest, Variable *Src0, Operand *Src1, |
| 719 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 720 | Context.insert<InstARM32Orr>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 721 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 722 | void _orrs(Variable *Dest, Variable *Src0, Operand *Src1, |
| 723 | CondARM32::Cond Pred = CondARM32::AL) { |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 724 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 725 | Context.insert<InstARM32Orr>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 726 | if (SetFlags) { |
| 727 | Context.insert<InstFakeUse>(Dest); |
| 728 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 729 | } |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 730 | void _push(const VarList &Sources) { Context.insert<InstARM32Push>(Sources); } |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 731 | void _pop(const VarList &Dests) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 732 | Context.insert<InstARM32Pop>(Dests); |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 733 | // Mark dests as modified. |
| 734 | for (Variable *Dest : Dests) |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 735 | Context.insert<InstFakeDef>(Dest); |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 736 | } |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 737 | void _rbit(Variable *Dest, Variable *Src0, |
| 738 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 739 | Context.insert<InstARM32Rbit>(Dest, Src0, Pred); |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 740 | } |
| 741 | void _rev(Variable *Dest, Variable *Src0, |
| 742 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 743 | Context.insert<InstARM32Rev>(Dest, Src0, Pred); |
Jan Voung | f645d85 | 2015-07-09 10:35:09 -0700 | [diff] [blame] | 744 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 745 | void _ret(Variable *LR, Variable *Src0 = nullptr) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 746 | Context.insert<InstARM32Ret>(LR, Src0); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 747 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 748 | void _rscs(Variable *Dest, Variable *Src0, Operand *Src1, |
| 749 | CondARM32::Cond Pred = CondARM32::AL) { |
| 750 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 751 | Context.insert<InstARM32Rsc>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 752 | if (SetFlags) { |
| 753 | Context.insert<InstFakeUse>(Dest); |
| 754 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 755 | } |
| 756 | void _rsc(Variable *Dest, Variable *Src0, Operand *Src1, |
| 757 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 758 | Context.insert<InstARM32Rsc>(Dest, Src0, Src1, Pred); |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 759 | } |
| 760 | void _rsbs(Variable *Dest, Variable *Src0, Operand *Src1, |
| 761 | CondARM32::Cond Pred = CondARM32::AL) { |
| 762 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 763 | Context.insert<InstARM32Rsb>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 764 | if (SetFlags) { |
| 765 | Context.insert<InstFakeUse>(Dest); |
| 766 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 767 | } |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 768 | void _rsb(Variable *Dest, Variable *Src0, Operand *Src1, |
| 769 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 770 | Context.insert<InstARM32Rsb>(Dest, Src0, Src1, Pred); |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 771 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 772 | void _sbc(Variable *Dest, Variable *Src0, Operand *Src1, |
| 773 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 774 | Context.insert<InstARM32Sbc>(Dest, Src0, Src1, Pred); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 775 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 776 | void _sbcs(Variable *Dest, Variable *Src0, Operand *Src1, |
| 777 | CondARM32::Cond Pred = CondARM32::AL) { |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 778 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 779 | Context.insert<InstARM32Sbc>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 780 | if (SetFlags) { |
| 781 | Context.insert<InstFakeUse>(Dest); |
| 782 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 783 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 784 | void _sdiv(Variable *Dest, Variable *Src0, Variable *Src1, |
| 785 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 786 | Context.insert<InstARM32Sdiv>(Dest, Src0, Src1, Pred); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 787 | } |
John Porto | ba6a67c | 2015-09-25 15:19:45 -0700 | [diff] [blame] | 788 | /// _str, for all your Variable to memory transfers. Addr has the same |
| 789 | /// restrictions that it does in _ldr. |
Jan Voung | befd03a | 2015-06-02 11:03:03 -0700 | [diff] [blame] | 790 | void _str(Variable *Value, OperandARM32Mem *Addr, |
| 791 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 792 | Context.insert<InstARM32Str>(Value, Addr, Pred); |
Jan Voung | befd03a | 2015-06-02 11:03:03 -0700 | [diff] [blame] | 793 | } |
John Porto | 324334e | 2016-03-08 11:00:53 -0800 | [diff] [blame] | 794 | InstARM32Strex *_strex(Variable *Dest, Variable *Value, OperandARM32Mem *Addr, |
| 795 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1699184 | 2015-10-01 15:11:23 -0700 | [diff] [blame] | 796 | if (auto *Value64 = llvm::dyn_cast<Variable64On32>(Value)) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 797 | Context.insert<InstFakeUse>(Value64->getLo()); |
| 798 | Context.insert<InstFakeUse>(Value64->getHi()); |
John Porto | 1699184 | 2015-10-01 15:11:23 -0700 | [diff] [blame] | 799 | } |
John Porto | 324334e | 2016-03-08 11:00:53 -0800 | [diff] [blame] | 800 | return Context.insert<InstARM32Strex>(Dest, Value, Addr, Pred); |
John Porto | 1699184 | 2015-10-01 15:11:23 -0700 | [diff] [blame] | 801 | } |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 802 | void _sub(Variable *Dest, Variable *Src0, Operand *Src1, |
| 803 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 804 | Context.insert<InstARM32Sub>(Dest, Src0, Src1, Pred); |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 805 | } |
| 806 | void _subs(Variable *Dest, Variable *Src0, Operand *Src1, |
| 807 | CondARM32::Cond Pred = CondARM32::AL) { |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 808 | constexpr bool SetFlags = true; |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 809 | Context.insert<InstARM32Sub>(Dest, Src0, Src1, Pred, SetFlags); |
John Porto | 7e6aa5a | 2016-03-02 15:10:19 -0800 | [diff] [blame] | 810 | if (SetFlags) { |
| 811 | Context.insert<InstFakeUse>(Dest); |
| 812 | } |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 813 | } |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 814 | void _sxt(Variable *Dest, Variable *Src0, |
| 815 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 816 | Context.insert<InstARM32Sxt>(Dest, Src0, Pred); |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 817 | } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 818 | void _tst(Variable *Src0, Operand *Src1, |
| 819 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 820 | Context.insert<InstARM32Tst>(Src0, Src1, Pred); |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 821 | } |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 822 | void _trap() { Context.insert<InstARM32Trap>(); } |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 823 | void _udiv(Variable *Dest, Variable *Src0, Variable *Src1, |
| 824 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 825 | Context.insert<InstARM32Udiv>(Dest, Src0, Src1, Pred); |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 826 | } |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 827 | void _umull(Variable *DestLo, Variable *DestHi, Variable *Src0, |
Jan Voung | 3bfd99a | 2015-05-22 16:35:25 -0700 | [diff] [blame] | 828 | Variable *Src1, CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | a1cdd57 | 2016-03-01 15:19:29 -0800 | [diff] [blame] | 829 | // umull requires DestLo and DestHi to be assigned to different GPRs. The |
| 830 | // following lines create overlapping liveness ranges for both variables. If |
| 831 | // either one of them is live, then they are both going to be live, and thus |
| 832 | // assigned to different registers; if they are both dead, then DCE will |
| 833 | // kick in and delete the following three instructions. |
| 834 | Context.insert<InstFakeDef>(DestHi); |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 835 | Context.insert<InstARM32Umull>(DestLo, DestHi, Src0, Src1, Pred); |
John Porto | a1cdd57 | 2016-03-01 15:19:29 -0800 | [diff] [blame] | 836 | Context.insert<InstFakeDef>(DestHi, DestLo)->setDestRedefined(); |
Jim Stichnoth | 28df6ba | 2016-02-05 15:43:24 -0800 | [diff] [blame] | 837 | Context.insert<InstFakeUse>(DestHi); |
Jan Voung | 2971997 | 2015-05-19 11:24:51 -0700 | [diff] [blame] | 838 | } |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 839 | void _uxt(Variable *Dest, Variable *Src0, |
| 840 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 841 | Context.insert<InstARM32Uxt>(Dest, Src0, Pred); |
Jan Voung | 66c3d5e | 2015-06-04 17:02:31 -0700 | [diff] [blame] | 842 | } |
John Porto | ba6a67c | 2015-09-25 15:19:45 -0700 | [diff] [blame] | 843 | void _vabs(Variable *Dest, Variable *Src, |
| 844 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 845 | Context.insert<InstARM32Vabs>(Dest, Src, Pred); |
John Porto | ba6a67c | 2015-09-25 15:19:45 -0700 | [diff] [blame] | 846 | } |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 847 | void _vadd(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 848 | Context.insert<InstARM32Vadd>(Dest, Src0, Src1); |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 849 | } |
Eric Holk | b58170c | 2016-01-27 11:18:29 -0800 | [diff] [blame] | 850 | void _vand(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 851 | Context.insert<InstARM32Vand>(Dest, Src0, Src1); |
| 852 | } |
John Porto | 397f602 | 2016-04-15 06:26:58 -0700 | [diff] [blame] | 853 | InstARM32Vbsl *_vbsl(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 854 | return Context.insert<InstARM32Vbsl>(Dest, Src0, Src1); |
| 855 | } |
John Porto | a4d100a | 2016-04-18 15:32:27 -0700 | [diff] [blame] | 856 | void _vceq(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 857 | Context.insert<InstARM32Vceq>(Dest, Src0, Src1); |
| 858 | } |
| 859 | InstARM32Vcge *_vcge(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 860 | return Context.insert<InstARM32Vcge>(Dest, Src0, Src1); |
| 861 | } |
| 862 | InstARM32Vcgt *_vcgt(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 863 | return Context.insert<InstARM32Vcgt>(Dest, Src0, Src1); |
| 864 | } |
John Porto | c31e2ed | 2015-09-11 05:17:08 -0700 | [diff] [blame] | 865 | void _vcvt(Variable *Dest, Variable *Src, InstARM32Vcvt::VcvtVariant Variant, |
| 866 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 867 | Context.insert<InstARM32Vcvt>(Dest, Src, Variant, Pred); |
John Porto | c31e2ed | 2015-09-11 05:17:08 -0700 | [diff] [blame] | 868 | } |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 869 | void _vdiv(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 870 | Context.insert<InstARM32Vdiv>(Dest, Src0, Src1); |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 871 | } |
John Porto | 2f5534f | 2015-09-18 15:59:47 -0700 | [diff] [blame] | 872 | void _vcmp(Variable *Src0, Variable *Src1, |
| 873 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 874 | Context.insert<InstARM32Vcmp>(Src0, Src1, Pred); |
John Porto | 2f5534f | 2015-09-18 15:59:47 -0700 | [diff] [blame] | 875 | } |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 876 | void _vcmp(Variable *Src0, OperandARM32FlexFpZero *FpZero, |
| 877 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 878 | Context.insert<InstARM32Vcmp>(Src0, FpZero, Pred); |
John Porto | ccea793 | 2015-11-17 04:58:36 -0800 | [diff] [blame] | 879 | } |
Nicolas Capens | f6951fa | 2017-10-02 10:44:03 -0400 | [diff] [blame] | 880 | void _vdup(Variable *Dest, Variable *Src, int Idx) { |
| 881 | Context.insert<InstARM32Vdup>(Dest, Src, Idx); |
| 882 | } |
John Porto | 3f6b47d | 2015-11-19 05:42:59 -0800 | [diff] [blame] | 883 | void _veor(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 884 | Context.insert<InstARM32Veor>(Dest, Src0, Src1); |
John Porto | 3f6b47d | 2015-11-19 05:42:59 -0800 | [diff] [blame] | 885 | } |
Nicolas Capens | 675e15b | 2017-09-27 15:06:35 -0400 | [diff] [blame] | 886 | void _vldr1d(Variable *Dest, OperandARM32Mem *Addr, |
| 887 | CondARM32::Cond Pred = CondARM32::AL) { |
| 888 | Context.insert<InstARM32Vldr1d>(Dest, Addr, Pred); |
| 889 | } |
| 890 | void _vldr1q(Variable *Dest, OperandARM32Mem *Addr, |
| 891 | CondARM32::Cond Pred = CondARM32::AL) { |
| 892 | Context.insert<InstARM32Vldr1q>(Dest, Addr, Pred); |
| 893 | } |
John Porto | 2f5534f | 2015-09-18 15:59:47 -0700 | [diff] [blame] | 894 | void _vmrs(CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 895 | Context.insert<InstARM32Vmrs>(Pred); |
John Porto | 2f5534f | 2015-09-18 15:59:47 -0700 | [diff] [blame] | 896 | } |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 897 | void _vmla(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 898 | Context.insert<InstARM32Vmla>(Dest, Src0, Src1); |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 899 | } |
Nicolas Capens | 675e15b | 2017-09-27 15:06:35 -0400 | [diff] [blame] | 900 | void _vmlap(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 901 | Context.insert<InstARM32Vmlap>(Dest, Src0, Src1); |
| 902 | } |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 903 | void _vmls(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 904 | Context.insert<InstARM32Vmls>(Dest, Src0, Src1); |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 905 | } |
Nicolas Capens | f6951fa | 2017-10-02 10:44:03 -0400 | [diff] [blame] | 906 | void _vmovl(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 907 | Context.insert<InstARM32Vmovl>(Dest, Src0, Src1); |
| 908 | } |
| 909 | void _vmovh(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 910 | Context.insert<InstARM32Vmovh>(Dest, Src0, Src1); |
| 911 | } |
| 912 | void _vmovhl(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 913 | Context.insert<InstARM32Vmovhl>(Dest, Src0, Src1); |
| 914 | } |
| 915 | void _vmovlh(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 916 | Context.insert<InstARM32Vmovlh>(Dest, Src0, Src1); |
| 917 | } |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 918 | void _vmul(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 919 | Context.insert<InstARM32Vmul>(Dest, Src0, Src1); |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 920 | } |
Nicolas Capens | 675e15b | 2017-09-27 15:06:35 -0400 | [diff] [blame] | 921 | void _vmulh(Variable *Dest, Variable *Src0, Variable *Src1, bool Unsigned) { |
| 922 | Context.insert<InstARM32Vmulh>(Dest, Src0, Src1) |
| 923 | ->setSignType(Unsigned ? InstARM32::FS_Unsigned : InstARM32::FS_Signed); |
| 924 | } |
John Porto | a4d100a | 2016-04-18 15:32:27 -0700 | [diff] [blame] | 925 | void _vmvn(Variable *Dest, Variable *Src0) { |
| 926 | Context.insert<InstARM32Vmvn>(Dest, Src0, CondARM32::AL); |
| 927 | } |
John Porto | 15e77d4 | 2016-04-13 12:57:14 -0700 | [diff] [blame] | 928 | void _vneg(Variable *Dest, Variable *Src0) { |
| 929 | Context.insert<InstARM32Vneg>(Dest, Src0, CondARM32::AL) |
| 930 | ->setSignType(InstARM32::FS_Signed); |
| 931 | } |
Eric Holk | cad0b75 | 2016-01-27 14:56:22 -0800 | [diff] [blame] | 932 | void _vorr(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 933 | Context.insert<InstARM32Vorr>(Dest, Src0, Src1); |
| 934 | } |
Casey Dahlin | b40560b | 2017-06-28 13:58:58 -0700 | [diff] [blame] | 935 | void _vqadd(Variable *Dest, Variable *Src0, Variable *Src1, bool Unsigned) { |
| 936 | Context.insert<InstARM32Vqadd>(Dest, Src0, Src1) |
| 937 | ->setSignType(Unsigned ? InstARM32::FS_Unsigned : InstARM32::FS_Signed); |
| 938 | } |
Nicolas Capens | 675e15b | 2017-09-27 15:06:35 -0400 | [diff] [blame] | 939 | void _vqmovn2(Variable *Dest, Variable *Src0, Variable *Src1, bool Unsigned, |
| 940 | bool Saturating) { |
| 941 | Context.insert<InstARM32Vqmovn2>(Dest, Src0, Src1) |
| 942 | ->setSignType(Saturating ? (Unsigned ? InstARM32::FS_Unsigned |
| 943 | : InstARM32::FS_Signed) |
| 944 | : InstARM32::FS_None); |
| 945 | } |
Casey Dahlin | b40560b | 2017-06-28 13:58:58 -0700 | [diff] [blame] | 946 | void _vqsub(Variable *Dest, Variable *Src0, Variable *Src1, bool Unsigned) { |
| 947 | Context.insert<InstARM32Vqsub>(Dest, Src0, Src1) |
| 948 | ->setSignType(Unsigned ? InstARM32::FS_Unsigned : InstARM32::FS_Signed); |
| 949 | } |
John Porto | 15e77d4 | 2016-04-13 12:57:14 -0700 | [diff] [blame] | 950 | InstARM32Vshl *_vshl(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 951 | return Context.insert<InstARM32Vshl>(Dest, Src0, Src1); |
| 952 | } |
John Porto | e88c7de | 2016-04-14 11:51:38 -0700 | [diff] [blame] | 953 | void _vshl(Variable *Dest, Variable *Src0, ConstantInteger32 *Src1) { |
| 954 | Context.insert<InstARM32Vshl>(Dest, Src0, Src1) |
| 955 | ->setSignType(InstARM32::FS_Unsigned); |
| 956 | } |
| 957 | InstARM32Vshr *_vshr(Variable *Dest, Variable *Src0, |
| 958 | ConstantInteger32 *Src1) { |
| 959 | return Context.insert<InstARM32Vshr>(Dest, Src0, Src1); |
| 960 | } |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 961 | void _vsqrt(Variable *Dest, Variable *Src, |
| 962 | CondARM32::Cond Pred = CondARM32::AL) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 963 | Context.insert<InstARM32Vsqrt>(Dest, Src, Pred); |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 964 | } |
Nicolas Capens | 675e15b | 2017-09-27 15:06:35 -0400 | [diff] [blame] | 965 | void _vstr1d(Variable *Value, OperandARM32Mem *Addr, |
| 966 | CondARM32::Cond Pred = CondARM32::AL) { |
| 967 | Context.insert<InstARM32Vstr1>(Value, Addr, Pred, 32); |
| 968 | } |
| 969 | void _vstr1q(Variable *Value, OperandARM32Mem *Addr, |
| 970 | CondARM32::Cond Pred = CondARM32::AL) { |
| 971 | Context.insert<InstARM32Vstr1>(Value, Addr, Pred, 64); |
| 972 | } |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 973 | void _vsub(Variable *Dest, Variable *Src0, Variable *Src1) { |
John Porto | 1d937a8 | 2015-12-17 06:19:34 -0800 | [diff] [blame] | 974 | Context.insert<InstARM32Vsub>(Dest, Src0, Src1); |
Jan Voung | 86ebec1 | 2015-08-09 07:58:35 -0700 | [diff] [blame] | 975 | } |
Nicolas Capens | f6951fa | 2017-10-02 10:44:03 -0400 | [diff] [blame] | 976 | void _vzip(Variable *Dest, Variable *Src0, Variable *Src1) { |
| 977 | Context.insert<InstARM32Vzip>(Dest, Src0, Src1); |
| 978 | } |
Jan Voung | b2d5084 | 2015-05-12 09:53:50 -0700 | [diff] [blame] | 979 | |
John Porto | f419854 | 2015-11-20 14:17:23 -0800 | [diff] [blame] | 980 | // Iterates over the CFG and determines the maximum outgoing stack arguments |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 981 | // bytes. This information is later used during addProlog() to pre-allocate |
John Porto | f419854 | 2015-11-20 14:17:23 -0800 | [diff] [blame] | 982 | // the outargs area. |
| 983 | // TODO(jpp): This could live in the Parser, if we provided a Target-specific |
| 984 | // method that the Parser could call. |
| 985 | void findMaxStackOutArgsSize(); |
| 986 | |
John Porto | 866b6b1 | 2015-12-03 09:45:31 -0800 | [diff] [blame] | 987 | /// Returns true if the given Offset can be represented in a Load/Store Mem |
| 988 | /// Operand. |
John Porto | f5f02f7 | 2015-11-09 14:52:40 -0800 | [diff] [blame] | 989 | bool isLegalMemOffset(Type Ty, int32_t Offset) const; |
John Porto | 866b6b1 | 2015-12-03 09:45:31 -0800 | [diff] [blame] | 990 | |
| 991 | void postLowerLegalization(); |
| 992 | |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 993 | /// Manages the Gotoff relocations created during the function lowering. A |
| 994 | /// single Gotoff relocation is created for each global variable used by the |
| 995 | /// function being lowered. |
| 996 | /// @{ |
| 997 | // TODO(jpp): if the same global G is used in different functions, then this |
| 998 | // method will emit one G(gotoff) relocation per function. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 999 | GlobalString createGotoffRelocation(const ConstantRelocatable *CR); |
| 1000 | CfgUnorderedSet<GlobalString> KnownGotoffs; |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 1001 | /// @} |
| 1002 | |
John Porto | 866b6b1 | 2015-12-03 09:45:31 -0800 | [diff] [blame] | 1003 | class PostLoweringLegalizer { |
| 1004 | PostLoweringLegalizer() = delete; |
| 1005 | PostLoweringLegalizer(const PostLoweringLegalizer &) = delete; |
| 1006 | PostLoweringLegalizer &operator=(const PostLoweringLegalizer &) = delete; |
| 1007 | |
| 1008 | public: |
| 1009 | explicit PostLoweringLegalizer(TargetARM32 *Target) |
| 1010 | : Target(Target), StackOrFrameReg(Target->getPhysicalRegister( |
| 1011 | Target->getFrameOrStackReg())) {} |
| 1012 | |
| 1013 | void resetTempBaseIfClobberedBy(const Inst *Instr); |
| 1014 | |
| 1015 | // Ensures that the TempBase register held by the this legalizer (if any) is |
| 1016 | // assigned to IP. |
| 1017 | void assertNoTempOrAssignedToIP() const { |
| 1018 | assert(TempBaseReg == nullptr || |
| 1019 | TempBaseReg->getRegNum() == Target->getReservedTmpReg()); |
| 1020 | } |
| 1021 | |
| 1022 | // Legalizes Mem. if Mem.Base is a Reamaterializable variable, Mem.Offset is |
| 1023 | // fixed up. |
| 1024 | OperandARM32Mem *legalizeMemOperand(OperandARM32Mem *Mem, |
| 1025 | bool AllowOffsets = true); |
| 1026 | |
| 1027 | /// Legalizes Mov if its Source (or Destination) is a spilled Variable, or |
| 1028 | /// if its Source is a Rematerializable variable (this form is used in lieu |
| 1029 | /// of lea, which is not available in ARM.) |
| 1030 | /// |
| 1031 | /// Moves to memory become store instructions, and moves from memory, loads. |
| 1032 | void legalizeMov(InstARM32Mov *Mov); |
| 1033 | |
| 1034 | private: |
| 1035 | /// Creates a new Base register centered around [Base, +/- Offset]. |
| 1036 | Variable *newBaseRegister(Variable *Base, int32_t Offset, |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1037 | RegNumT ScratchRegNum); |
John Porto | 866b6b1 | 2015-12-03 09:45:31 -0800 | [diff] [blame] | 1038 | |
| 1039 | /// Creates a new, legal OperandARM32Mem for accessing Base + Offset. |
| 1040 | /// The returned mem operand is a legal operand for accessing memory that is |
| 1041 | /// of type Ty. |
| 1042 | /// |
| 1043 | /// If [Base, #Offset] is encodable, then the method returns a Mem operand |
| 1044 | /// expressing it. Otherwise, |
| 1045 | /// |
| 1046 | /// if [TempBaseReg, #Offset-TempBaseOffset] is a valid memory operand, the |
| 1047 | /// method will return that. Otherwise, |
| 1048 | /// |
| 1049 | /// a new base register ip=Base+Offset is created, and the method returns a |
| 1050 | /// memory operand expressing [ip, #0]. |
| 1051 | OperandARM32Mem *createMemOperand(Type Ty, Variable *Base, int32_t Offset, |
| 1052 | bool AllowOffsets = true); |
| 1053 | TargetARM32 *const Target; |
| 1054 | Variable *const StackOrFrameReg; |
| 1055 | Variable *TempBaseReg = nullptr; |
| 1056 | int32_t TempBaseOffset = 0; |
| 1057 | }; |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 1058 | |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 1059 | TargetARM32Features CPUFeatures; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1060 | bool UsesFramePointer = false; |
| 1061 | bool NeedsStackAlignment = false; |
| 1062 | bool MaybeLeafFunc = true; |
| 1063 | size_t SpillAreaSizeBytes = 0; |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 1064 | size_t FixedAllocaSizeBytes = 0; |
| 1065 | size_t FixedAllocaAlignBytes = 0; |
| 1066 | bool PrologEmitsFixedAllocas = false; |
John Porto | f419854 | 2015-11-20 14:17:23 -0800 | [diff] [blame] | 1067 | uint32_t MaxOutArgsSizeBytes = 0; |
John Porto | bb0a5fe | 2015-09-04 11:23:41 -0700 | [diff] [blame] | 1068 | // TODO(jpp): std::array instead of array. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1069 | static SmallBitVector TypeToRegisterSet[RegARM32::RCARM32_NUM]; |
| 1070 | static SmallBitVector TypeToRegisterSetUnfiltered[RegARM32::RCARM32_NUM]; |
| 1071 | static SmallBitVector RegisterAliases[RegARM32::Reg_NUM]; |
| 1072 | SmallBitVector RegsUsed; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1073 | VarList PhysicalRegisters[IceType_NUM]; |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1074 | VarList PreservedGPRs; |
| 1075 | VarList PreservedSRegs; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1076 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1077 | /// Helper class that understands the Calling Convention and register |
| 1078 | /// assignments. The first few integer type parameters can use r0-r3, |
| 1079 | /// regardless of their position relative to the floating-point/vector |
John Porto | 385351b | 2015-09-16 16:11:10 -0700 | [diff] [blame] | 1080 | /// arguments in the argument list. Floating-point and vector arguments |
| 1081 | /// can use q0-q3 (aka d0-d7, s0-s15). For more information on the topic, |
| 1082 | /// see the ARM Architecture Procedure Calling Standards (AAPCS). |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1083 | /// |
John Porto | 385351b | 2015-09-16 16:11:10 -0700 | [diff] [blame] | 1084 | /// Technically, arguments that can start with registers but extend beyond the |
| 1085 | /// available registers can be split between the registers and the stack. |
| 1086 | /// However, this is typically for passing GPR structs by value, and PNaCl |
| 1087 | /// transforms expand this out. |
| 1088 | /// |
| 1089 | /// At (public) function entry, the stack must be 8-byte aligned. |
Jan Voung | b0a8c24 | 2015-06-18 15:00:14 -0700 | [diff] [blame] | 1090 | class CallingConv { |
| 1091 | CallingConv(const CallingConv &) = delete; |
| 1092 | CallingConv &operator=(const CallingConv &) = delete; |
| 1093 | |
| 1094 | public: |
John Porto | 2187c84 | 2015-12-16 07:48:25 -0800 | [diff] [blame] | 1095 | CallingConv(); |
Jan Voung | b0a8c24 | 2015-06-18 15:00:14 -0700 | [diff] [blame] | 1096 | ~CallingConv() = default; |
| 1097 | |
John Porto | 2187c84 | 2015-12-16 07:48:25 -0800 | [diff] [blame] | 1098 | /// argInGPR returns true if there is a GPR available for the requested |
| 1099 | /// type, and false otherwise. If it returns true, Reg is set to the |
| 1100 | /// appropriate register number. Note that, when Ty == IceType_i64, Reg will |
| 1101 | /// be an I64 register pair. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1102 | bool argInGPR(Type Ty, RegNumT *Reg); |
Jan Voung | b0a8c24 | 2015-06-18 15:00:14 -0700 | [diff] [blame] | 1103 | |
John Porto | 2187c84 | 2015-12-16 07:48:25 -0800 | [diff] [blame] | 1104 | /// argInVFP is to floating-point/vector types what argInGPR is for integer |
| 1105 | /// types. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1106 | bool argInVFP(Type Ty, RegNumT *Reg); |
Jan Voung | b0a8c24 | 2015-06-18 15:00:14 -0700 | [diff] [blame] | 1107 | |
| 1108 | private: |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1109 | void discardUnavailableGPRsAndTheirAliases(CfgVector<RegNumT> *Regs); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1110 | SmallBitVector GPRegsUsed; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1111 | CfgVector<RegNumT> GPRArgs; |
| 1112 | CfgVector<RegNumT> I64Args; |
John Porto | 2187c84 | 2015-12-16 07:48:25 -0800 | [diff] [blame] | 1113 | |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1114 | void discardUnavailableVFPRegs(CfgVector<RegNumT> *Regs); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1115 | SmallBitVector VFPRegsUsed; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1116 | CfgVector<RegNumT> FP32Args; |
| 1117 | CfgVector<RegNumT> FP64Args; |
| 1118 | CfgVector<RegNumT> Vec128Args; |
Jan Voung | b0a8c24 | 2015-06-18 15:00:14 -0700 | [diff] [blame] | 1119 | }; |
| 1120 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1121 | private: |
John Porto | 53611e2 | 2015-12-30 07:30:10 -0800 | [diff] [blame] | 1122 | ENABLE_MAKE_UNIQUE; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1123 | |
John Porto | f5f02f7 | 2015-11-09 14:52:40 -0800 | [diff] [blame] | 1124 | OperandARM32Mem *formAddressingMode(Type Ty, Cfg *Func, const Inst *LdSt, |
| 1125 | Operand *Base); |
| 1126 | |
John Porto | c39ec10 | 2015-12-01 13:00:43 -0800 | [diff] [blame] | 1127 | void postambleCtpop64(const InstCall *Instr); |
| 1128 | void preambleDivRem(const InstCall *Instr); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1129 | CfgUnorderedMap<Operand *, void (TargetARM32::*)(const InstCall *Instr)> |
John Porto | c39ec10 | 2015-12-01 13:00:43 -0800 | [diff] [blame] | 1130 | ARM32HelpersPreamble; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1131 | CfgUnorderedMap<Operand *, void (TargetARM32::*)(const InstCall *Instr)> |
John Porto | c39ec10 | 2015-12-01 13:00:43 -0800 | [diff] [blame] | 1132 | ARM32HelpersPostamble; |
| 1133 | |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1134 | class ComputationTracker { |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1135 | public: |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1136 | ComputationTracker() = default; |
| 1137 | ~ComputationTracker() = default; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1138 | |
| 1139 | void forgetProducers() { KnownComputations.clear(); } |
| 1140 | void recordProducers(CfgNode *Node); |
| 1141 | |
| 1142 | const Inst *getProducerOf(const Operand *Opnd) const { |
| 1143 | auto *Var = llvm::dyn_cast<Variable>(Opnd); |
| 1144 | if (Var == nullptr) { |
| 1145 | return nullptr; |
| 1146 | } |
| 1147 | |
| 1148 | auto Iter = KnownComputations.find(Var->getIndex()); |
| 1149 | if (Iter == KnownComputations.end()) { |
| 1150 | return nullptr; |
| 1151 | } |
| 1152 | |
| 1153 | return Iter->second.Instr; |
| 1154 | } |
| 1155 | |
| 1156 | void dump(const Cfg *Func) const { |
| 1157 | if (!BuildDefs::dump() || !Func->isVerbose(IceV_Folding)) |
| 1158 | return; |
| 1159 | OstreamLocker L(Func->getContext()); |
| 1160 | Ostream &Str = Func->getContext()->getStrDump(); |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 1161 | Str << "foldable producer:\n"; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1162 | for (const auto &Computation : KnownComputations) { |
| 1163 | Str << " "; |
| 1164 | Computation.second.Instr->dump(Func); |
| 1165 | Str << "\n"; |
| 1166 | } |
| 1167 | Str << "\n"; |
| 1168 | } |
| 1169 | |
| 1170 | private: |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1171 | class ComputationEntry { |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1172 | public: |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1173 | ComputationEntry(Inst *I, Type Ty) : Instr(I), ComputationType(Ty) {} |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1174 | Inst *const Instr; |
| 1175 | // Boolean folding is disabled for variables whose live range is multi |
| 1176 | // block. We conservatively initialize IsLiveOut to true, and set it to |
| 1177 | // false once we find the end of the live range for the variable defined |
| 1178 | // by this instruction. If liveness analysis is not performed (e.g., in |
| 1179 | // Om1 mode) IsLiveOut will never be set to false, and folding will be |
| 1180 | // disabled. |
| 1181 | bool IsLiveOut = true; |
John Porto | 7b3d9cb | 2015-11-11 14:26:57 -0800 | [diff] [blame] | 1182 | int32_t NumUses = 0; |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1183 | Type ComputationType; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1184 | }; |
| 1185 | |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1186 | // ComputationMap maps a Variable number to a payload identifying which |
| 1187 | // instruction defined it. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1188 | using ComputationMap = CfgUnorderedMap<SizeT, ComputationEntry>; |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1189 | ComputationMap KnownComputations; |
John Porto | 4a5e6d0 | 2015-11-04 09:32:55 -0800 | [diff] [blame] | 1190 | }; |
| 1191 | |
John Porto | eb13acc | 2015-12-09 05:10:58 -0800 | [diff] [blame] | 1192 | ComputationTracker Computations; |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 1193 | |
| 1194 | // AllowTemporaryWithNoReg indicates if TargetARM32::makeReg() can be invoked |
| 1195 | // without specifying a physical register. This is needed for creating unbound |
| 1196 | // temporaries during Ice -> ARM lowering, but before register allocation. |
John Porto | 98cc08c | 2015-11-24 12:30:01 -0800 | [diff] [blame] | 1197 | // This a safe-guard that no unbound temporaries are created during the |
| 1198 | // legalization post-passes. |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 1199 | bool AllowTemporaryWithNoReg = true; |
| 1200 | // ForbidTemporaryWithoutReg is a RAII class that manages |
| 1201 | // AllowTemporaryWithNoReg. |
| 1202 | class ForbidTemporaryWithoutReg { |
| 1203 | ForbidTemporaryWithoutReg() = delete; |
John Porto | 98cc08c | 2015-11-24 12:30:01 -0800 | [diff] [blame] | 1204 | ForbidTemporaryWithoutReg(const ForbidTemporaryWithoutReg &) = delete; |
| 1205 | ForbidTemporaryWithoutReg & |
| 1206 | operator=(const ForbidTemporaryWithoutReg &) = delete; |
John Porto | 614140e | 2015-11-23 11:43:13 -0800 | [diff] [blame] | 1207 | |
| 1208 | public: |
| 1209 | explicit ForbidTemporaryWithoutReg(TargetARM32 *Target) : Target(Target) { |
| 1210 | Target->AllowTemporaryWithNoReg = false; |
| 1211 | } |
| 1212 | ~ForbidTemporaryWithoutReg() { Target->AllowTemporaryWithNoReg = true; } |
| 1213 | |
| 1214 | private: |
| 1215 | TargetARM32 *const Target; |
| 1216 | }; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1217 | }; |
| 1218 | |
John Porto | 0f86d03 | 2015-06-15 07:44:27 -0700 | [diff] [blame] | 1219 | class TargetDataARM32 final : public TargetDataLowering { |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1220 | TargetDataARM32() = delete; |
| 1221 | TargetDataARM32(const TargetDataARM32 &) = delete; |
| 1222 | TargetDataARM32 &operator=(const TargetDataARM32 &) = delete; |
| 1223 | |
| 1224 | public: |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 1225 | static std::unique_ptr<TargetDataLowering> create(GlobalContext *Ctx) { |
| 1226 | return std::unique_ptr<TargetDataLowering>(new TargetDataARM32(Ctx)); |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 1229 | void lowerGlobals(const VariableDeclarationList &Vars, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1230 | const std::string &SectionSuffix) override; |
John Porto | 0f86d03 | 2015-06-15 07:44:27 -0700 | [diff] [blame] | 1231 | void lowerConstants() override; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 1232 | void lowerJumpTables() override; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1233 | |
| 1234 | protected: |
| 1235 | explicit TargetDataARM32(GlobalContext *Ctx); |
| 1236 | |
| 1237 | private: |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 1238 | ~TargetDataARM32() override = default; |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1239 | }; |
| 1240 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 1241 | class TargetHeaderARM32 final : public TargetHeaderLowering { |
| 1242 | TargetHeaderARM32() = delete; |
| 1243 | TargetHeaderARM32(const TargetHeaderARM32 &) = delete; |
| 1244 | TargetHeaderARM32 &operator=(const TargetHeaderARM32 &) = delete; |
| 1245 | |
| 1246 | public: |
| 1247 | static std::unique_ptr<TargetHeaderLowering> create(GlobalContext *Ctx) { |
| 1248 | return std::unique_ptr<TargetHeaderLowering>(new TargetHeaderARM32(Ctx)); |
| 1249 | } |
| 1250 | |
John Porto | 0f86d03 | 2015-06-15 07:44:27 -0700 | [diff] [blame] | 1251 | void lower() override; |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 1252 | |
| 1253 | protected: |
| 1254 | explicit TargetHeaderARM32(GlobalContext *Ctx); |
| 1255 | |
| 1256 | private: |
| 1257 | ~TargetHeaderARM32() = default; |
Jan Voung | 6ec369e | 2015-06-30 11:03:15 -0700 | [diff] [blame] | 1258 | |
| 1259 | TargetARM32Features CPUFeatures; |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 1260 | }; |
| 1261 | |
John Porto | 4a56686 | 2016-01-04 09:33:41 -0800 | [diff] [blame] | 1262 | } // end of namespace ARM32 |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 1263 | } // end of namespace Ice |
| 1264 | |
| 1265 | #endif // SUBZERO_SRC_ICETARGETLOWERINGARM32_H |