blob: 32fdc442e4dc79eafe0806f59c83e1ff877f179a [file] [log] [blame]
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -07001//===- subzero/src/IceTargetLowering.h - Lowering interface -----*- 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 Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file declares the TargetLowering, LoweringContext, and
Andrew Scull57e12682015-09-16 11:30:19 -070012/// TargetDataLowering classes. TargetLowering is an abstract class used to
13/// drive the translation/lowering process. LoweringContext maintains a context
14/// for lowering each instruction, offering conveniences such as iterating over
15/// non-deleted instructions. TargetDataLowering is an abstract class used to
16/// drive the lowering/emission of global initializers, external global
Andrew Scull9612d322015-07-06 14:53:25 -070017/// declarations, and internal constant pools.
18///
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070019//===----------------------------------------------------------------------===//
20
21#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
22#define SUBZERO_SRC_ICETARGETLOWERING_H
23
24#include "IceDefs.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070025#include "IceInst.h" // for the names of the Inst subtypes
Jan Voung76bb0be2015-05-14 09:26:19 -070026#include "IceOperand.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070027#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070028
29namespace Ice {
30
Andrew Scull57e12682015-09-16 11:30:19 -070031/// LoweringContext makes it easy to iterate through non-deleted instructions in
32/// a node, and insert new (lowered) instructions at the current point. Along
33/// with the instruction list container and associated iterators, it holds the
34/// current node, which is needed when inserting new instructions in order to
35/// track whether variables are used as single-block or multi-block.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070036class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070037 LoweringContext(const LoweringContext &) = delete;
38 LoweringContext &operator=(const LoweringContext &) = delete;
39
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070040public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070041 LoweringContext() = default;
42 ~LoweringContext() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070043 void init(CfgNode *Node);
44 Inst *getNextInst() const {
45 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080046 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080047 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070048 }
Jan Voungc820ddf2014-07-29 14:38:51 -070049 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070050 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070051 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080052 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080053 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070054 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070055 CfgNode *getNode() const { return Node; }
56 bool atEnd() const { return Cur == End; }
57 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080058 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059 InstList::iterator getEnd() const { return End; }
60 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070061 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070062 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070063 void advanceNext() { advanceForward(Next); }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070064 void setCur(InstList::iterator C) { Cur = C; }
65 void setNext(InstList::iterator N) { Next = N; }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070066 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070067 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
Jim Stichnoth318f4cd2015-10-01 21:02:37 -070068 void availabilityReset();
69 void availabilityUpdate();
70 Variable *availabilityGet(Operand *Src) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070071
72private:
Andrew Scull9612d322015-07-06 14:53:25 -070073 /// Node is the argument to Inst::updateVars().
Jim Stichnotheafb56c2015-06-22 10:35:22 -070074 CfgNode *Node = nullptr;
75 Inst *LastInserted = nullptr;
Andrew Scull57e12682015-09-16 11:30:19 -070076 /// Cur points to the current instruction being considered. It is guaranteed
77 /// to point to a non-deleted instruction, or to be End.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070078 InstList::iterator Cur;
Andrew Scull57e12682015-09-16 11:30:19 -070079 /// Next doubles as a pointer to the next valid instruction (if any), and the
80 /// new-instruction insertion point. It is also updated for the caller in case
81 /// the lowering consumes more than one high-level instruction. It is
82 /// guaranteed to point to a non-deleted instruction after Cur, or to be End.
83 // TODO: Consider separating the notion of "next valid instruction" and "new
84 // instruction insertion point", to avoid confusion when previously-deleted
85 // instructions come between the two points.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070086 InstList::iterator Next;
Andrew Scull9612d322015-07-06 14:53:25 -070087 /// Begin is a copy of Insts.begin(), used if iterators are moved backward.
Jan Vounge6e497d2014-07-30 10:06:03 -070088 InstList::iterator Begin;
Andrew Scull9612d322015-07-06 14:53:25 -070089 /// End is a copy of Insts.end(), used if Next needs to be advanced.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070090 InstList::iterator End;
Jim Stichnoth318f4cd2015-10-01 21:02:37 -070091 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple
92 /// assignment inserted (provided Src is a variable). This is used for simple
93 /// availability analysis.
94 Variable *LastDest = nullptr;
95 Variable *LastSrc = nullptr;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070096
Jan Voungc820ddf2014-07-29 14:38:51 -070097 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070098 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070099};
100
Jan Voung28068ad2015-07-31 12:58:46 -0700101/// A helper class to advance the LoweringContext at each loop iteration.
102class PostIncrLoweringContext {
103 PostIncrLoweringContext() = delete;
104 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete;
105 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete;
106
107public:
108 explicit PostIncrLoweringContext(LoweringContext &Context)
109 : Context(Context) {}
110 ~PostIncrLoweringContext() {
111 Context.advanceCur();
112 Context.advanceNext();
113 }
114
115private:
116 LoweringContext &Context;
117};
118
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700119class TargetLowering {
Jim Stichnothc6ead202015-02-24 09:30:30 -0800120 TargetLowering() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700121 TargetLowering(const TargetLowering &) = delete;
122 TargetLowering &operator=(const TargetLowering &) = delete;
123
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700124public:
Jan Voungb36ad9b2015-04-21 17:01:49 -0700125 // TODO(jvoung): return a unique_ptr like the other factory functions.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700126 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voungec270732015-01-12 17:00:22 -0800127 static std::unique_ptr<Assembler> createAssembler(TargetArch Target,
128 Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700129 void translate() {
Jan Voung1f47ad02015-03-20 15:01:26 -0700130 switch (Ctx->getFlags().getOptLevel()) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700131 case Opt_m1:
132 translateOm1();
133 break;
134 case Opt_0:
135 translateO0();
136 break;
137 case Opt_1:
138 translateO1();
139 break;
140 case Opt_2:
141 translateO2();
142 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700143 }
144 }
145 virtual void translateOm1() {
146 Func->setError("Target doesn't specify Om1 lowering steps.");
147 }
148 virtual void translateO0() {
149 Func->setError("Target doesn't specify O0 lowering steps.");
150 }
151 virtual void translateO1() {
152 Func->setError("Target doesn't specify O1 lowering steps.");
153 }
154 virtual void translateO2() {
155 Func->setError("Target doesn't specify O2 lowering steps.");
156 }
157
Andrew Scull9612d322015-07-06 14:53:25 -0700158 /// Tries to do address mode optimization on a single instruction.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700159 void doAddressOpt();
Andrew Scull9612d322015-07-06 14:53:25 -0700160 /// Randomly insert NOPs.
Qining Luaee5fa82015-08-20 14:59:03 -0700161 void doNopInsertion(RandomNumberGenerator &RNG);
Andrew Scull9612d322015-07-06 14:53:25 -0700162 /// Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700163 void lower();
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700164 /// Inserts and lowers a single high-level instruction at a specific insertion
165 /// point.
166 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr);
Andrew Scull57e12682015-09-16 11:30:19 -0700167 /// Does preliminary lowering of the set of Phi instructions in the current
168 /// node. The main intention is to do what's needed to keep the unlowered Phi
169 /// instructions consistent with the lowered non-Phi instructions, e.g. to
170 /// lower 64-bit operands on a 32-bit target.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700171 virtual void prelowerPhis() {}
Andrew Scull57e12682015-09-16 11:30:19 -0700172 /// Tries to do branch optimization on a single instruction. Returns true if
173 /// some optimization was done.
Jim Stichnothff9c7062014-09-18 04:50:49 -0700174 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
175 return false;
176 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700177
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700178 virtual SizeT getNumRegisters() const = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700179 /// Returns a variable pre-colored to the specified physical register. This is
180 /// generally used to get very direct access to the register such as in the
181 /// prolog or epilog or for marking scratch registers as killed by a call. If
182 /// a Type is not provided, a target-specific default type is used.
Jim Stichnoth98712a32014-10-24 10:59:02 -0700183 virtual Variable *getPhysicalRegister(SizeT RegNum,
184 Type Ty = IceType_void) = 0;
Andrew Scull9612d322015-07-06 14:53:25 -0700185 /// Returns a printable name for the register.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700186 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
187
188 virtual bool hasFramePointer() const { return false; }
Jim Stichnothe7418712015-10-09 06:54:02 -0700189 virtual SizeT getStackReg() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700190 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700191 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700192
Andrew Scull6d47bcd2015-09-17 17:10:05 -0700193 /// Return whether a 64-bit Variable should be split into a Variable64On32.
194 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0;
195
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700196 bool hasComputedFrame() const { return HasComputedFrame; }
Andrew Scull57e12682015-09-16 11:30:19 -0700197 /// Returns true if this function calls a function that has the "returns
198 /// twice" attribute.
Jan Voung44d53e12014-09-11 19:18:03 -0700199 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800200 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700201 int32_t getStackAdjustment() const { return StackAdjustment; }
202 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
203 void resetStackAdjustment() { StackAdjustment = 0; }
Jan Voungb36ad9b2015-04-21 17:01:49 -0700204 SizeT makeNextLabelNumber() { return NextLabelNumber++; }
Andrew Scull86df4e92015-07-30 13:54:44 -0700205 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700206 LoweringContext &getContext() { return Context; }
207
208 enum RegSet {
209 RegSet_None = 0,
210 RegSet_CallerSave = 1 << 0,
211 RegSet_CalleeSave = 1 << 1,
212 RegSet_StackPointer = 1 << 2,
213 RegSet_FramePointer = 1 << 3,
214 RegSet_All = ~RegSet_None
215 };
Andrew Scull8072bae2015-09-14 16:01:26 -0700216 using RegSetMask = uint32_t;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700217
218 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
219 RegSetMask Exclude) const = 0;
220 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
John Portobb0a5fe2015-09-04 11:23:41 -0700221 virtual const llvm::SmallBitVector &getAliasesForRegister(SizeT) const = 0;
222
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800223 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700224
Qining Luaee5fa82015-08-20 14:59:03 -0700225 virtual void
226 makeRandomRegisterPermutation(llvm::SmallVectorImpl<int32_t> &Permutation,
227 const llvm::SmallBitVector &ExcludeRegisters,
228 uint64_t Salt) const = 0;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800229
Andrew Scull57e12682015-09-16 11:30:19 -0700230 /// Save/restore any mutable state for the situation where code emission needs
231 /// multiple passes, such as sandboxing or relaxation. Subclasses may provide
232 /// their own implementation, but should be sure to also call the parent
233 /// class's methods.
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800234 virtual void snapshotEmitState() {
235 SnapshotStackAdjustment = StackAdjustment;
236 }
237 virtual void rollbackEmitState() {
238 StackAdjustment = SnapshotStackAdjustment;
239 }
240
Andrew Scull87f80c12015-07-20 10:19:16 -0700241 /// Get the minimum number of clusters required for a jump table to be
242 /// considered.
243 virtual SizeT getMinJumpTableSize() const = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700244 virtual void emitJumpTable(const Cfg *Func,
245 const InstJumpTable *JumpTable) const = 0;
Andrew Scull87f80c12015-07-20 10:19:16 -0700246
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700247 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700248
Jan Voung76bb0be2015-05-14 09:26:19 -0700249 void emitWithoutPrefix(const ConstantRelocatable *CR) const;
250 void emit(const ConstantRelocatable *CR) const;
251 virtual const char *getConstantPrefix() const = 0;
252
253 virtual void emit(const ConstantUndef *C) const = 0;
254 virtual void emit(const ConstantInteger32 *C) const = 0;
255 virtual void emit(const ConstantInteger64 *C) const = 0;
256 virtual void emit(const ConstantFloat *C) const = 0;
257 virtual void emit(const ConstantDouble *C) const = 0;
258
Andrew Scull9612d322015-07-06 14:53:25 -0700259 /// Performs target-specific argument lowering.
Matt Wala45a06232014-07-09 16:33:22 -0700260 virtual void lowerArguments() = 0;
261
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700262 virtual void initNodeForLowering(CfgNode *) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700263 virtual void addProlog(CfgNode *Node) = 0;
264 virtual void addEpilog(CfgNode *Node) = 0;
265
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700266 virtual ~TargetLowering() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700267
268protected:
Jim Stichnothc6ead202015-02-24 09:30:30 -0800269 explicit TargetLowering(Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700270 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
271 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
272 virtual void lowerAssign(const InstAssign *Inst) = 0;
273 virtual void lowerBr(const InstBr *Inst) = 0;
274 virtual void lowerCall(const InstCall *Inst) = 0;
275 virtual void lowerCast(const InstCast *Inst) = 0;
276 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700277 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700278 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700279 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700280 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700281 virtual void lowerLoad(const InstLoad *Inst) = 0;
282 virtual void lowerPhi(const InstPhi *Inst) = 0;
283 virtual void lowerRet(const InstRet *Inst) = 0;
284 virtual void lowerSelect(const InstSelect *Inst) = 0;
285 virtual void lowerStore(const InstStore *Inst) = 0;
286 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
287 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
Jim Stichnothe4f65d82015-06-17 22:16:02 -0700288 virtual void lowerOther(const Inst *Instr);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700289
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700290 virtual void doAddressOptLoad() {}
291 virtual void doAddressOptStore() {}
Jim Stichnothad2989b2015-09-15 10:21:42 -0700292 virtual void doMockBoundsCheck(Operand *) {}
Qining Luaee5fa82015-08-20 14:59:03 -0700293 virtual void randomlyInsertNop(float Probability,
294 RandomNumberGenerator &RNG) = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700295 /// This gives the target an opportunity to post-process the lowered expansion
296 /// before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700297 virtual void postLower() {}
298
Jim Stichnoth230d4102015-09-25 17:40:32 -0700299 /// Find (non-SSA) instructions where the Dest variable appears in some source
300 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis
301 /// consistent.
302 void markRedefinitions();
Jan Voungb3401d22015-05-18 09:38:21 -0700303
Andrew Scull57e12682015-09-16 11:30:19 -0700304 /// Make a pass over the Cfg to determine which variables need stack slots and
305 /// place them in a sorted list (SortedSpilledVariables). Among those, vars,
306 /// classify the spill variables as local to the basic block vs global
307 /// (multi-block) in order to compute the parameters GlobalsSize and
308 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of
309 /// locals is disallowed) along with alignments required for variables in each
310 /// area. We rely on accurate VMetadata in order to classify a variable as
311 /// global vs local (otherwise the variable is conservatively global). The
312 /// in-args should be initialized to 0.
Andrew Scull9612d322015-07-06 14:53:25 -0700313 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700314 /// This is only a pre-pass and the actual stack slot assignment is handled
315 /// separately.
Andrew Scull9612d322015-07-06 14:53:25 -0700316 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700317 /// There may be target-specific Variable types, which will be handled by
318 /// TargetVarHook. If the TargetVarHook returns true, then the variable is
319 /// skipped and not considered with the rest of the spilled variables.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700320 void getVarStackSlotParams(VarList &SortedSpilledVariables,
321 llvm::SmallBitVector &RegsUsed,
322 size_t *GlobalsSize, size_t *SpillAreaSizeBytes,
323 uint32_t *SpillAreaAlignmentBytes,
324 uint32_t *LocalsSlotsAlignmentBytes,
325 std::function<bool(Variable *)> TargetVarHook);
326
Andrew Scull57e12682015-09-16 11:30:19 -0700327 /// Calculate the amount of padding needed to align the local and global areas
328 /// to the required alignment. This assumes the globals/locals layout used by
329 /// getVarStackSlotParams and assignVarStackSlots.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700330 void alignStackSpillAreas(uint32_t SpillAreaStartOffset,
331 uint32_t SpillAreaAlignmentBytes,
332 size_t GlobalsSize,
333 uint32_t LocalsSlotsAlignmentBytes,
334 uint32_t *SpillAreaPaddingBytes,
335 uint32_t *LocalsSlotsPaddingBytes);
336
Andrew Scull57e12682015-09-16 11:30:19 -0700337 /// Make a pass through the SortedSpilledVariables and actually assign stack
338 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding.
339 /// The SpillArea starts after that amount of padding. This matches the scheme
340 /// in getVarStackSlotParams, where there may be a separate multi-block global
341 /// var spill area and a local var spill area.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700342 void assignVarStackSlots(VarList &SortedSpilledVariables,
343 size_t SpillAreaPaddingBytes,
344 size_t SpillAreaSizeBytes,
345 size_t GlobalsAndSubsequentPaddingSize,
346 bool UsesFramePointer);
347
Andrew Scull57e12682015-09-16 11:30:19 -0700348 /// Sort the variables in Source based on required alignment. The variables
349 /// with the largest alignment need are placed in the front of the Dest list.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700350 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
351
Andrew Scull9612d322015-07-06 14:53:25 -0700352 /// Make a call to an external helper function.
Jan Voungb36ad9b2015-04-21 17:01:49 -0700353 InstCall *makeHelperCall(const IceString &Name, Variable *Dest,
354 SizeT MaxSrcs);
355
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700356 void
357 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) {
358 Context.insert(InstBundleLock::create(Func, BundleOption));
359 }
360 void _bundle_unlock() { Context.insert(InstBundleUnlock::create(Func)); }
Jim Stichnoth230d4102015-09-25 17:40:32 -0700361 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700362
Andrew Scullcfa628b2015-08-20 14:23:05 -0700363 bool shouldOptimizeMemIntrins();
364
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700365 Cfg *Func;
366 GlobalContext *Ctx;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700367 bool HasComputedFrame = false;
368 bool CallsReturnsTwice = false;
Andrew Scull57e12682015-09-16 11:30:19 -0700369 /// StackAdjustment keeps track of the current stack offset from its natural
Jim Stichnoth55f931f2015-09-23 16:33:08 -0700370 /// location, e.g. as arguments are pushed for a function call or as
371 /// fixed-size alloca instructions are executed in the entry block.
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700372 int32_t StackAdjustment = 0;
373 SizeT NextLabelNumber = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700374 SizeT NextJumpTableNumber = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700375 LoweringContext Context;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800376
Jim Stichnothc4508792015-03-01 23:12:55 -0800377 // Runtime helper function names
378 const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16";
379 const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8";
380 const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1";
381 const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1";
382 const static constexpr char *H_call_ctpop_i32 = "__popcountsi2";
383 const static constexpr char *H_call_ctpop_i64 = "__popcountdi2";
384 const static constexpr char *H_call_longjmp = "longjmp";
385 const static constexpr char *H_call_memcpy = "memcpy";
386 const static constexpr char *H_call_memmove = "memmove";
387 const static constexpr char *H_call_memset = "memset";
388 const static constexpr char *H_call_read_tp = "__nacl_read_tp";
389 const static constexpr char *H_call_setjmp = "setjmp";
390 const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64";
391 const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64";
392 const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32";
393 const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32";
394 const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64";
395 const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32";
396 const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64";
397 const static constexpr char *H_frem_f32 = "fmodf";
398 const static constexpr char *H_frem_f64 = "fmod";
Jan Voung6ec369e2015-06-30 11:03:15 -0700399 const static constexpr char *H_sdiv_i32 = "__divsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800400 const static constexpr char *H_sdiv_i64 = "__divdi3";
401 const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32";
402 const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700403 const static constexpr char *H_srem_i32 = "__modsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800404 const static constexpr char *H_srem_i64 = "__moddi3";
Jan Voung6ec369e2015-06-30 11:03:15 -0700405 const static constexpr char *H_udiv_i32 = "__udivsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800406 const static constexpr char *H_udiv_i64 = "__udivdi3";
407 const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32";
408 const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32";
409 const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64";
410 const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32";
411 const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700412 const static constexpr char *H_urem_i32 = "__umodsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800413 const static constexpr char *H_urem_i64 = "__umoddi3";
414
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800415private:
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700416 int32_t SnapshotStackAdjustment = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700417};
418
Andrew Scull57e12682015-09-16 11:30:19 -0700419/// TargetDataLowering is used for "lowering" data including initializers for
420/// global variables, and the internal constant pools. It is separated out from
421/// TargetLowering because it does not require a Cfg.
Jan Voung72984d82015-01-29 14:42:38 -0800422class TargetDataLowering {
423 TargetDataLowering() = delete;
424 TargetDataLowering(const TargetDataLowering &) = delete;
425 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700426
Jim Stichnothde4ca712014-06-29 08:13:48 -0700427public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800428 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800429 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700430
John Porto8b1a7052015-06-17 13:20:08 -0700431 virtual void lowerGlobals(const VariableDeclarationList &Vars,
432 const IceString &SectionSuffix) = 0;
John Porto0f86d032015-06-15 07:44:27 -0700433 virtual void lowerConstants() = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700434 virtual void lowerJumpTables() = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700435
436protected:
John Porto8b1a7052015-06-17 13:20:08 -0700437 void emitGlobal(const VariableDeclaration &Var,
438 const IceString &SectionSuffix);
Jan Voung58eea4d2015-06-15 15:11:56 -0700439
Andrew Scull57e12682015-09-16 11:30:19 -0700440 /// For now, we assume .long is the right directive for emitting 4 byte emit
441 /// global relocations. However, LLVM MIPS usually uses .4byte instead.
Andrew Scull9612d322015-07-06 14:53:25 -0700442 /// Perhaps there is some difference when the location is unaligned.
John Porto8b1a7052015-06-17 13:20:08 -0700443 static const char *getEmit32Directive() { return ".long"; }
Jan Voung58eea4d2015-06-15 15:11:56 -0700444
Jim Stichnothc6ead202015-02-24 09:30:30 -0800445 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700446 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700447};
448
Andrew Scull57e12682015-09-16 11:30:19 -0700449/// TargetHeaderLowering is used to "lower" the header of an output file. It
450/// writes out the target-specific header attributes. E.g., for ARM this writes
451/// out the build attributes (float ABI, etc.).
Jan Voungfb792842015-06-11 15:27:50 -0700452class TargetHeaderLowering {
453 TargetHeaderLowering() = delete;
454 TargetHeaderLowering(const TargetHeaderLowering &) = delete;
455 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete;
456
457public:
458 static std::unique_ptr<TargetHeaderLowering>
459 createLowering(GlobalContext *Ctx);
460 virtual ~TargetHeaderLowering();
461
462 virtual void lower() {}
463
464protected:
465 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
466 GlobalContext *Ctx;
467};
468
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700469} // end of namespace Ice
470
471#endif // SUBZERO_SRC_ICETARGETLOWERING_H