Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceTargetLowering.cpp - Basic lowering implementation --===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 11 | /// This file implements the skeleton of the TargetLowering class, specifically |
| 12 | /// invoking the appropriate lowering method for a given instruction kind and |
| 13 | /// driving global register allocation. It also implements the non-deleted |
| 14 | /// instruction iteration in LoweringContext. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 15 | /// |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 18 | #include "IceTargetLowering.h" |
| 19 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 20 | #include "IceAssemblerARM32.h" |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 21 | #include "IceAssemblerMIPS32.h" |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 22 | #include "IceAssemblerX8632.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 23 | #include "IceAssemblerX8664.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 24 | #include "IceCfg.h" // setError() |
| 25 | #include "IceCfgNode.h" |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 26 | #include "IceGlobalInits.h" |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 27 | #include "IceInstVarIter.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 28 | #include "IceOperand.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 29 | #include "IceRegAlloc.h" |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 30 | #include "IceTargetLoweringARM32.h" |
Jim Stichnoth | 6da4cef | 2015-06-11 13:26:33 -0700 | [diff] [blame] | 31 | #include "IceTargetLoweringMIPS32.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 32 | #include "IceTargetLoweringX8632.h" |
John Porto | 7e93c62 | 2015-06-23 10:58:57 -0700 | [diff] [blame] | 33 | #include "IceTargetLoweringX8664.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 34 | |
| 35 | namespace Ice { |
| 36 | |
| 37 | void LoweringContext::init(CfgNode *N) { |
| 38 | Node = N; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 39 | End = getNode()->getInsts().end(); |
| 40 | rewind(); |
| 41 | advanceForward(Next); |
| 42 | } |
| 43 | |
| 44 | void LoweringContext::rewind() { |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 45 | Begin = getNode()->getInsts().begin(); |
| 46 | Cur = Begin; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 47 | skipDeleted(Cur); |
| 48 | Next = Cur; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void LoweringContext::insert(Inst *Inst) { |
| 52 | getNode()->getInsts().insert(Next, Inst); |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 53 | LastInserted = Inst; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 56 | void LoweringContext::skipDeleted(InstList::iterator &I) const { |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 57 | while (I != End && I->isDeleted()) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 58 | ++I; |
| 59 | } |
| 60 | |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 61 | void LoweringContext::advanceForward(InstList::iterator &I) const { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 62 | if (I != End) { |
| 63 | ++I; |
| 64 | skipDeleted(I); |
| 65 | } |
| 66 | } |
| 67 | |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 68 | Inst *LoweringContext::getLastInserted() const { |
Jim Stichnoth | 98712a3 | 2014-10-24 10:59:02 -0700 | [diff] [blame] | 69 | assert(LastInserted); |
| 70 | return LastInserted; |
Jan Voung | e6e497d | 2014-07-30 10:06:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 73 | TargetLowering *TargetLowering::createLowering(TargetArch Target, Cfg *Func) { |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 74 | #define SUBZERO_TARGET(X) \ |
| 75 | if (Target == Target_##X) \ |
| 76 | return Target##X::create(Func); |
| 77 | #include "llvm/Config/SZTargets.def" |
| 78 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 79 | Func->setError("Unsupported target"); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 80 | return nullptr; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 83 | TargetLowering::TargetLowering(Cfg *Func) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 84 | : Func(Func), Ctx(Func->getContext()), Context() {} |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 85 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 86 | std::unique_ptr<Assembler> TargetLowering::createAssembler(TargetArch Target, |
| 87 | Cfg *Func) { |
Jan Voung | 90ccc3f | 2015-04-30 14:15:10 -0700 | [diff] [blame] | 88 | #define SUBZERO_TARGET(X) \ |
| 89 | if (Target == Target_##X) \ |
| 90 | return std::unique_ptr<Assembler>(new X::Assembler##X()); |
| 91 | #include "llvm/Config/SZTargets.def" |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 92 | |
| 93 | Func->setError("Unsupported target assembler"); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 94 | return nullptr; |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 97 | void TargetLowering::doAddressOpt() { |
| 98 | if (llvm::isa<InstLoad>(*Context.getCur())) |
| 99 | doAddressOptLoad(); |
| 100 | else if (llvm::isa<InstStore>(*Context.getCur())) |
| 101 | doAddressOptStore(); |
| 102 | Context.advanceCur(); |
| 103 | Context.advanceNext(); |
| 104 | } |
| 105 | |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 106 | void TargetLowering::doNopInsertion(RandomNumberGenerator &RNG) { |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 107 | Inst *I = Context.getCur(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 108 | bool ShouldSkip = llvm::isa<InstFakeUse>(I) || llvm::isa<InstFakeDef>(I) || |
| 109 | llvm::isa<InstFakeKill>(I) || I->isRedundantAssign() || |
| 110 | I->isDeleted(); |
| 111 | if (!ShouldSkip) { |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 112 | int Probability = Ctx->getFlags().getNopProbabilityAsPercentage(); |
| 113 | for (int I = 0; I < Ctx->getFlags().getMaxNopsPerInstruction(); ++I) { |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 114 | randomlyInsertNop(Probability / 100.0, RNG); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 119 | // Lowers a single instruction according to the information in Context, by |
| 120 | // checking the Context.Cur instruction kind and calling the appropriate |
| 121 | // lowering method. The lowering method should insert target instructions at |
| 122 | // the Cur.Next insertion point, and should not delete the Context.Cur |
| 123 | // instruction or advance Context.Cur. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 124 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 125 | // The lowering method may look ahead in the instruction stream as desired, and |
| 126 | // lower additional instructions in conjunction with the current one, for |
| 127 | // example fusing a compare and branch. If it does, it should advance |
| 128 | // Context.Cur to point to the next non-deleted instruction to process, and it |
| 129 | // should delete any additional instructions it consumes. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 130 | void TargetLowering::lower() { |
| 131 | assert(!Context.atEnd()); |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 132 | Inst *Inst = Context.getCur(); |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 133 | Inst->deleteIfDead(); |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 134 | if (!Inst->isDeleted() && !llvm::isa<InstFakeDef>(Inst) && |
| 135 | !llvm::isa<InstFakeUse>(Inst)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 136 | // Mark the current instruction as deleted before lowering, otherwise the |
| 137 | // Dest variable will likely get marked as non-SSA. See |
| 138 | // Variable::setDefinition(). However, just pass-through FakeDef and |
| 139 | // FakeUse instructions that might have been inserted prior to lowering. |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 140 | Inst->setDeleted(); |
| 141 | switch (Inst->getKind()) { |
| 142 | case Inst::Alloca: |
| 143 | lowerAlloca(llvm::cast<InstAlloca>(Inst)); |
| 144 | break; |
| 145 | case Inst::Arithmetic: |
| 146 | lowerArithmetic(llvm::cast<InstArithmetic>(Inst)); |
| 147 | break; |
| 148 | case Inst::Assign: |
| 149 | lowerAssign(llvm::cast<InstAssign>(Inst)); |
| 150 | break; |
| 151 | case Inst::Br: |
| 152 | lowerBr(llvm::cast<InstBr>(Inst)); |
| 153 | break; |
| 154 | case Inst::Call: |
| 155 | lowerCall(llvm::cast<InstCall>(Inst)); |
| 156 | break; |
| 157 | case Inst::Cast: |
| 158 | lowerCast(llvm::cast<InstCast>(Inst)); |
| 159 | break; |
| 160 | case Inst::ExtractElement: |
| 161 | lowerExtractElement(llvm::cast<InstExtractElement>(Inst)); |
| 162 | break; |
| 163 | case Inst::Fcmp: |
| 164 | lowerFcmp(llvm::cast<InstFcmp>(Inst)); |
| 165 | break; |
| 166 | case Inst::Icmp: |
| 167 | lowerIcmp(llvm::cast<InstIcmp>(Inst)); |
| 168 | break; |
| 169 | case Inst::InsertElement: |
| 170 | lowerInsertElement(llvm::cast<InstInsertElement>(Inst)); |
| 171 | break; |
| 172 | case Inst::IntrinsicCall: { |
| 173 | InstIntrinsicCall *Call = llvm::cast<InstIntrinsicCall>(Inst); |
| 174 | if (Call->getIntrinsicInfo().ReturnsTwice) |
| 175 | setCallsReturnsTwice(true); |
| 176 | lowerIntrinsicCall(Call); |
| 177 | break; |
| 178 | } |
| 179 | case Inst::Load: |
| 180 | lowerLoad(llvm::cast<InstLoad>(Inst)); |
| 181 | break; |
| 182 | case Inst::Phi: |
| 183 | lowerPhi(llvm::cast<InstPhi>(Inst)); |
| 184 | break; |
| 185 | case Inst::Ret: |
| 186 | lowerRet(llvm::cast<InstRet>(Inst)); |
| 187 | break; |
| 188 | case Inst::Select: |
| 189 | lowerSelect(llvm::cast<InstSelect>(Inst)); |
| 190 | break; |
| 191 | case Inst::Store: |
| 192 | lowerStore(llvm::cast<InstStore>(Inst)); |
| 193 | break; |
| 194 | case Inst::Switch: |
| 195 | lowerSwitch(llvm::cast<InstSwitch>(Inst)); |
| 196 | break; |
| 197 | case Inst::Unreachable: |
| 198 | lowerUnreachable(llvm::cast<InstUnreachable>(Inst)); |
| 199 | break; |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 200 | default: |
| 201 | lowerOther(Inst); |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 202 | break; |
| 203 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 204 | |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 205 | postLower(); |
| 206 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 207 | |
| 208 | Context.advanceCur(); |
| 209 | Context.advanceNext(); |
| 210 | } |
| 211 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 212 | void TargetLowering::lowerInst(CfgNode *Node, InstList::iterator Next, |
| 213 | InstHighLevel *Instr) { |
| 214 | // TODO(stichnot): Consider modifying the design/implementation to avoid |
| 215 | // multiple init() calls when using lowerInst() to lower several instructions |
| 216 | // in the same node. |
| 217 | Context.init(Node); |
| 218 | Context.setNext(Next); |
| 219 | Context.insert(Instr); |
| 220 | --Next; |
| 221 | assert(&*Next == Instr); |
| 222 | Context.setCur(Next); |
| 223 | lower(); |
| 224 | } |
| 225 | |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 226 | void TargetLowering::lowerOther(const Inst *Instr) { |
| 227 | (void)Instr; |
| 228 | Func->setError("Can't lower unsupported instruction type"); |
| 229 | } |
| 230 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 231 | // Drives register allocation, allowing all physical registers (except perhaps |
| 232 | // for the frame pointer) to be allocated. This set of registers could |
| 233 | // potentially be parameterized if we want to restrict registers e.g. for |
| 234 | // performance testing. |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 235 | void TargetLowering::regAlloc(RegAllocKind Kind) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 236 | TimerMarker T(TimerStack::TT_regAlloc, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 237 | LinearScan LinearScan(Func); |
| 238 | RegSetMask RegInclude = RegSet_None; |
| 239 | RegSetMask RegExclude = RegSet_None; |
| 240 | RegInclude |= RegSet_CallerSave; |
| 241 | RegInclude |= RegSet_CalleeSave; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 242 | if (hasFramePointer()) |
| 243 | RegExclude |= RegSet_FramePointer; |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 244 | LinearScan.init(Kind); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 245 | llvm::SmallBitVector RegMask = getRegisterSet(RegInclude, RegExclude); |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 246 | LinearScan.scan(RegMask, Ctx->getFlags().shouldRandomizeRegAlloc()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 249 | void TargetLowering::inferTwoAddress() { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 250 | // Find two-address non-SSA instructions where Dest==Src0, and set the |
| 251 | // DestNonKillable flag to keep liveness analysis consistent. |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 252 | for (auto Inst = Context.getCur(), E = Context.getNext(); Inst != E; ++Inst) { |
| 253 | if (Inst->isDeleted()) |
| 254 | continue; |
| 255 | if (Variable *Dest = Inst->getDest()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 256 | // TODO(stichnot): We may need to consider all source operands, not just |
| 257 | // the first one, if using 3-address instructions. |
Jan Voung | b3401d2 | 2015-05-18 09:38:21 -0700 | [diff] [blame] | 258 | if (Inst->getSrcSize() > 0 && Inst->getSrc(0) == Dest) |
| 259 | Inst->setDestNonKillable(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 264 | void TargetLowering::sortVarsByAlignment(VarList &Dest, |
| 265 | const VarList &Source) const { |
| 266 | Dest = Source; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 267 | // Instead of std::sort, we could do a bucket sort with log2(alignment) as |
| 268 | // the buckets, if performance is an issue. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 269 | std::sort(Dest.begin(), Dest.end(), |
| 270 | [this](const Variable *V1, const Variable *V2) { |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 271 | return typeWidthInBytesOnStack(V1->getType()) > |
| 272 | typeWidthInBytesOnStack(V2->getType()); |
| 273 | }); |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void TargetLowering::getVarStackSlotParams( |
| 277 | VarList &SortedSpilledVariables, llvm::SmallBitVector &RegsUsed, |
| 278 | size_t *GlobalsSize, size_t *SpillAreaSizeBytes, |
| 279 | uint32_t *SpillAreaAlignmentBytes, uint32_t *LocalsSlotsAlignmentBytes, |
| 280 | std::function<bool(Variable *)> TargetVarHook) { |
| 281 | const VariablesMetadata *VMetadata = Func->getVMetadata(); |
| 282 | llvm::BitVector IsVarReferenced(Func->getNumVariables()); |
| 283 | for (CfgNode *Node : Func->getNodes()) { |
| 284 | for (Inst &Inst : Node->getInsts()) { |
| 285 | if (Inst.isDeleted()) |
| 286 | continue; |
| 287 | if (const Variable *Var = Inst.getDest()) |
| 288 | IsVarReferenced[Var->getIndex()] = true; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 289 | FOREACH_VAR_IN_INST(Var, Inst) { |
| 290 | IsVarReferenced[Var->getIndex()] = true; |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 295 | // If SimpleCoalescing is false, each variable without a register gets its |
| 296 | // own unique stack slot, which leads to large stack frames. If |
| 297 | // SimpleCoalescing is true, then each "global" variable without a register |
| 298 | // gets its own slot, but "local" variable slots are reused across basic |
| 299 | // blocks. E.g., if A and B are local to block 1 and C is local to block 2, |
| 300 | // then C may share a slot with A or B. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 301 | // |
| 302 | // We cannot coalesce stack slots if this function calls a "returns twice" |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 303 | // function. In that case, basic blocks may be revisited, and variables local |
| 304 | // to those basic blocks are actually live until after the called function |
| 305 | // returns a second time. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 306 | const bool SimpleCoalescing = !callsReturnsTwice(); |
| 307 | |
| 308 | std::vector<size_t> LocalsSize(Func->getNumNodes()); |
| 309 | const VarList &Variables = Func->getVariables(); |
| 310 | VarList SpilledVariables; |
| 311 | for (Variable *Var : Variables) { |
| 312 | if (Var->hasReg()) { |
| 313 | RegsUsed[Var->getRegNum()] = true; |
| 314 | continue; |
| 315 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 316 | // An argument either does not need a stack slot (if passed in a register) |
| 317 | // or already has one (if passed on the stack). |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 318 | if (Var->getIsArg()) |
| 319 | continue; |
| 320 | // An unreferenced variable doesn't need a stack slot. |
| 321 | if (!IsVarReferenced[Var->getIndex()]) |
| 322 | continue; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 323 | // Check a target-specific variable (it may end up sharing stack slots) and |
| 324 | // not need accounting here. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 325 | if (TargetVarHook(Var)) |
| 326 | continue; |
| 327 | SpilledVariables.push_back(Var); |
| 328 | } |
| 329 | |
| 330 | SortedSpilledVariables.reserve(SpilledVariables.size()); |
| 331 | sortVarsByAlignment(SortedSpilledVariables, SpilledVariables); |
| 332 | |
| 333 | for (Variable *Var : SortedSpilledVariables) { |
| 334 | size_t Increment = typeWidthInBytesOnStack(Var->getType()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 335 | // We have sorted by alignment, so the first variable we encounter that is |
| 336 | // located in each area determines the max alignment for the area. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 337 | if (!*SpillAreaAlignmentBytes) |
| 338 | *SpillAreaAlignmentBytes = Increment; |
| 339 | if (SimpleCoalescing && VMetadata->isTracked(Var)) { |
| 340 | if (VMetadata->isMultiBlock(Var)) { |
| 341 | *GlobalsSize += Increment; |
| 342 | } else { |
| 343 | SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); |
| 344 | LocalsSize[NodeIndex] += Increment; |
| 345 | if (LocalsSize[NodeIndex] > *SpillAreaSizeBytes) |
| 346 | *SpillAreaSizeBytes = LocalsSize[NodeIndex]; |
| 347 | if (!*LocalsSlotsAlignmentBytes) |
| 348 | *LocalsSlotsAlignmentBytes = Increment; |
| 349 | } |
| 350 | } else { |
| 351 | *SpillAreaSizeBytes += Increment; |
| 352 | } |
| 353 | } |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 354 | // For testing legalization of large stack offsets on targets with limited |
| 355 | // offset bits in instruction encodings, add some padding. |
| 356 | *SpillAreaSizeBytes += Ctx->getFlags().getTestStackExtra(); |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void TargetLowering::alignStackSpillAreas(uint32_t SpillAreaStartOffset, |
| 360 | uint32_t SpillAreaAlignmentBytes, |
| 361 | size_t GlobalsSize, |
| 362 | uint32_t LocalsSlotsAlignmentBytes, |
| 363 | uint32_t *SpillAreaPaddingBytes, |
| 364 | uint32_t *LocalsSlotsPaddingBytes) { |
| 365 | if (SpillAreaAlignmentBytes) { |
| 366 | uint32_t PaddingStart = SpillAreaStartOffset; |
| 367 | uint32_t SpillAreaStart = |
| 368 | Utils::applyAlignment(PaddingStart, SpillAreaAlignmentBytes); |
| 369 | *SpillAreaPaddingBytes = SpillAreaStart - PaddingStart; |
| 370 | } |
| 371 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 372 | // If there are separate globals and locals areas, make sure the locals area |
| 373 | // is aligned by padding the end of the globals area. |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 374 | if (LocalsSlotsAlignmentBytes) { |
| 375 | uint32_t GlobalsAndSubsequentPaddingSize = GlobalsSize; |
| 376 | GlobalsAndSubsequentPaddingSize = |
| 377 | Utils::applyAlignment(GlobalsSize, LocalsSlotsAlignmentBytes); |
| 378 | *LocalsSlotsPaddingBytes = GlobalsAndSubsequentPaddingSize - GlobalsSize; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void TargetLowering::assignVarStackSlots(VarList &SortedSpilledVariables, |
| 383 | size_t SpillAreaPaddingBytes, |
| 384 | size_t SpillAreaSizeBytes, |
| 385 | size_t GlobalsAndSubsequentPaddingSize, |
| 386 | bool UsesFramePointer) { |
| 387 | const VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 388 | // For testing legalization of large stack offsets on targets with limited |
| 389 | // offset bits in instruction encodings, add some padding. This assumes that |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 390 | // SpillAreaSizeBytes has accounted for the extra test padding. When |
| 391 | // UseFramePointer is true, the offset depends on the padding, not just the |
| 392 | // SpillAreaSizeBytes. On the other hand, when UseFramePointer is false, the |
| 393 | // offsets depend on the gap between SpillAreaSizeBytes and |
| 394 | // SpillAreaPaddingBytes, so we don't increment that. |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 395 | size_t TestPadding = Ctx->getFlags().getTestStackExtra(); |
| 396 | if (UsesFramePointer) |
| 397 | SpillAreaPaddingBytes += TestPadding; |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 398 | size_t GlobalsSpaceUsed = SpillAreaPaddingBytes; |
| 399 | size_t NextStackOffset = SpillAreaPaddingBytes; |
| 400 | std::vector<size_t> LocalsSize(Func->getNumNodes()); |
| 401 | const bool SimpleCoalescing = !callsReturnsTwice(); |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 402 | |
Jan Voung | 0fa6c5a | 2015-06-01 11:04:04 -0700 | [diff] [blame] | 403 | for (Variable *Var : SortedSpilledVariables) { |
| 404 | size_t Increment = typeWidthInBytesOnStack(Var->getType()); |
| 405 | if (SimpleCoalescing && VMetadata->isTracked(Var)) { |
| 406 | if (VMetadata->isMultiBlock(Var)) { |
| 407 | GlobalsSpaceUsed += Increment; |
| 408 | NextStackOffset = GlobalsSpaceUsed; |
| 409 | } else { |
| 410 | SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); |
| 411 | LocalsSize[NodeIndex] += Increment; |
| 412 | NextStackOffset = SpillAreaPaddingBytes + |
| 413 | GlobalsAndSubsequentPaddingSize + |
| 414 | LocalsSize[NodeIndex]; |
| 415 | } |
| 416 | } else { |
| 417 | NextStackOffset += Increment; |
| 418 | } |
| 419 | if (UsesFramePointer) |
| 420 | Var->setStackOffset(-NextStackOffset); |
| 421 | else |
| 422 | Var->setStackOffset(SpillAreaSizeBytes - NextStackOffset); |
| 423 | } |
| 424 | } |
| 425 | |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 426 | InstCall *TargetLowering::makeHelperCall(const IceString &Name, Variable *Dest, |
| 427 | SizeT MaxSrcs) { |
| 428 | const bool HasTailCall = false; |
| 429 | Constant *CallTarget = Ctx->getConstantExternSym(Name); |
| 430 | InstCall *Call = |
| 431 | InstCall::create(Func, MaxSrcs, Dest, CallTarget, HasTailCall); |
| 432 | return Call; |
| 433 | } |
| 434 | |
Andrew Scull | cfa628b | 2015-08-20 14:23:05 -0700 | [diff] [blame] | 435 | bool TargetLowering::shouldOptimizeMemIntrins() { |
| 436 | return Ctx->getFlags().getOptLevel() >= Opt_1 || |
| 437 | Ctx->getFlags().getForceMemIntrinOpt(); |
| 438 | } |
| 439 | |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 440 | void TargetLowering::emitWithoutPrefix(const ConstantRelocatable *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 441 | if (!BuildDefs::dump()) |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 442 | return; |
| 443 | Ostream &Str = Ctx->getStrEmit(); |
| 444 | if (C->getSuppressMangling()) |
| 445 | Str << C->getName(); |
| 446 | else |
| 447 | Str << Ctx->mangleName(C->getName()); |
| 448 | RelocOffsetT Offset = C->getOffset(); |
| 449 | if (Offset) { |
| 450 | if (Offset > 0) |
| 451 | Str << "+"; |
| 452 | Str << Offset; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void TargetLowering::emit(const ConstantRelocatable *C) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 457 | if (!BuildDefs::dump()) |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 458 | return; |
| 459 | Ostream &Str = Ctx->getStrEmit(); |
| 460 | Str << getConstantPrefix(); |
| 461 | emitWithoutPrefix(C); |
| 462 | } |
| 463 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 464 | std::unique_ptr<TargetDataLowering> |
| 465 | TargetDataLowering::createLowering(GlobalContext *Ctx) { |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 466 | TargetArch Target = Ctx->getFlags().getTargetArch(); |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 467 | #define SUBZERO_TARGET(X) \ |
| 468 | if (Target == Target_##X) \ |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 469 | return TargetData##X::create(Ctx); |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 470 | #include "llvm/Config/SZTargets.def" |
| 471 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 472 | llvm::report_fatal_error("Unsupported target data lowering"); |
Jim Stichnoth | de4ca71 | 2014-06-29 08:13:48 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 475 | TargetDataLowering::~TargetDataLowering() = default; |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 476 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 477 | namespace { |
| 478 | |
| 479 | // dataSectionSuffix decides whether to use SectionSuffix or MangledVarName as |
| 480 | // data section suffix. Essentially, when using separate data sections for |
| 481 | // globals SectionSuffix is not necessary. |
| 482 | IceString dataSectionSuffix(const IceString &SectionSuffix, |
| 483 | const IceString &MangledVarName, |
| 484 | const bool DataSections) { |
| 485 | if (SectionSuffix.empty() && !DataSections) { |
| 486 | return ""; |
| 487 | } |
| 488 | |
| 489 | if (DataSections) { |
| 490 | // With data sections we don't need to use the SectionSuffix. |
| 491 | return "." + MangledVarName; |
| 492 | } |
| 493 | |
| 494 | assert(!SectionSuffix.empty()); |
| 495 | return "." + SectionSuffix; |
| 496 | } |
| 497 | |
| 498 | } // end of anonymous namespace |
| 499 | |
| 500 | void TargetDataLowering::emitGlobal(const VariableDeclaration &Var, |
| 501 | const IceString &SectionSuffix) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 502 | if (!BuildDefs::dump()) |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 503 | return; |
| 504 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 505 | // If external and not initialized, this must be a cross test. Don't generate |
| 506 | // a declaration for such cases. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 507 | const bool IsExternal = |
| 508 | Var.isExternal() || Ctx->getFlags().getDisableInternal(); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 509 | if (IsExternal && !Var.hasInitializer()) |
| 510 | return; |
| 511 | |
| 512 | Ostream &Str = Ctx->getStrEmit(); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 513 | const bool HasNonzeroInitializer = Var.hasNonzeroInitializer(); |
| 514 | const bool IsConstant = Var.getIsConstant(); |
| 515 | const SizeT Size = Var.getNumBytes(); |
| 516 | const IceString MangledName = Var.mangleName(Ctx); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 517 | |
| 518 | Str << "\t.type\t" << MangledName << ",%object\n"; |
| 519 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 520 | const bool UseDataSections = Ctx->getFlags().getDataSections(); |
| 521 | const IceString Suffix = |
| 522 | dataSectionSuffix(SectionSuffix, MangledName, UseDataSections); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 523 | if (IsConstant) |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 524 | Str << "\t.section\t.rodata" << Suffix << ",\"a\",%progbits\n"; |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 525 | else if (HasNonzeroInitializer) |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 526 | Str << "\t.section\t.data" << Suffix << ",\"aw\",%progbits\n"; |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 527 | else |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 528 | Str << "\t.section\t.bss" << Suffix << ",\"aw\",%nobits\n"; |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 529 | |
| 530 | if (IsExternal) |
| 531 | Str << "\t.globl\t" << MangledName << "\n"; |
| 532 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 533 | const uint32_t Align = Var.getAlignment(); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 534 | if (Align > 1) { |
| 535 | assert(llvm::isPowerOf2_32(Align)); |
| 536 | // Use the .p2align directive, since the .align N directive can either |
| 537 | // interpret N as bytes, or power of 2 bytes, depending on the target. |
| 538 | Str << "\t.p2align\t" << llvm::Log2_32(Align) << "\n"; |
| 539 | } |
| 540 | |
| 541 | Str << MangledName << ":\n"; |
| 542 | |
| 543 | if (HasNonzeroInitializer) { |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 544 | for (const std::unique_ptr<VariableDeclaration::Initializer> &Init : |
| 545 | Var.getInitializers()) { |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 546 | switch (Init->getKind()) { |
| 547 | case VariableDeclaration::Initializer::DataInitializerKind: { |
Jan Voung | e0df91f | 2015-06-30 08:47:06 -0700 | [diff] [blame] | 548 | const auto &Data = |
| 549 | llvm::cast<VariableDeclaration::DataInitializer>(Init.get()) |
| 550 | ->getContents(); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 551 | for (SizeT i = 0; i < Init->getNumBytes(); ++i) { |
| 552 | Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | case VariableDeclaration::Initializer::ZeroInitializerKind: |
| 557 | Str << "\t.zero\t" << Init->getNumBytes() << "\n"; |
| 558 | break; |
| 559 | case VariableDeclaration::Initializer::RelocInitializerKind: { |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 560 | const auto *Reloc = |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 561 | llvm::cast<VariableDeclaration::RelocInitializer>(Init.get()); |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 562 | Str << "\t" << getEmit32Directive() << "\t"; |
| 563 | Str << Reloc->getDeclaration()->mangleName(Ctx); |
| 564 | if (RelocOffsetT Offset = Reloc->getOffset()) { |
| 565 | if (Offset >= 0 || (Offset == INT32_MIN)) |
| 566 | Str << " + " << Offset; |
| 567 | else |
| 568 | Str << " - " << -Offset; |
| 569 | } |
| 570 | Str << "\n"; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 575 | } else { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 576 | // NOTE: for non-constant zero initializers, this is BSS (no bits), so an |
| 577 | // ELF writer would not write to the file, and only track virtual offsets, |
| 578 | // but the .s writer still needs this .zero and cannot simply use the .size |
| 579 | // to advance offsets. |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 580 | Str << "\t.zero\t" << Size << "\n"; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 581 | } |
Jan Voung | 58eea4d | 2015-06-15 15:11:56 -0700 | [diff] [blame] | 582 | |
| 583 | Str << "\t.size\t" << MangledName << ", " << Size << "\n"; |
| 584 | } |
| 585 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 586 | std::unique_ptr<TargetHeaderLowering> |
| 587 | TargetHeaderLowering::createLowering(GlobalContext *Ctx) { |
| 588 | TargetArch Target = Ctx->getFlags().getTargetArch(); |
| 589 | #define SUBZERO_TARGET(X) \ |
| 590 | if (Target == Target_##X) \ |
| 591 | return TargetHeader##X::create(Ctx); |
| 592 | #include "llvm/Config/SZTargets.def" |
| 593 | |
| 594 | llvm::report_fatal_error("Unsupported target header lowering"); |
| 595 | } |
| 596 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 597 | TargetHeaderLowering::~TargetHeaderLowering() = default; |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 598 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 599 | } // end of namespace Ice |