blob: c613d99d072b7d45b338c381cc34ea11a63681a8 [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
Karl Schimpfc5abdc12015-10-09 13:29:13 -070031// UnimplementedError is defined as a macro so that we can get actual line
32// numbers.
33#define UnimplementedError(Flags) \
34 do { \
35 if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \
36 /* Use llvm_unreachable instead of report_fatal_error, which gives \
37 better stack traces. */ \
38 llvm_unreachable("Not yet implemented"); \
39 abort(); \
40 } \
41 } while (0)
42
Andrew Scull57e12682015-09-16 11:30:19 -070043/// LoweringContext makes it easy to iterate through non-deleted instructions in
44/// a node, and insert new (lowered) instructions at the current point. Along
45/// with the instruction list container and associated iterators, it holds the
46/// current node, which is needed when inserting new instructions in order to
47/// track whether variables are used as single-block or multi-block.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070048class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070049 LoweringContext(const LoweringContext &) = delete;
50 LoweringContext &operator=(const LoweringContext &) = delete;
51
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070052public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070053 LoweringContext() = default;
54 ~LoweringContext() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070055 void init(CfgNode *Node);
56 Inst *getNextInst() const {
57 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080058 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080059 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070060 }
Jan Voungc820ddf2014-07-29 14:38:51 -070061 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070062 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070063 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080064 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080065 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070066 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070067 CfgNode *getNode() const { return Node; }
68 bool atEnd() const { return Cur == End; }
69 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080070 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070071 InstList::iterator getEnd() const { return End; }
72 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070073 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070074 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070075 void advanceNext() { advanceForward(Next); }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070076 void setCur(InstList::iterator C) { Cur = C; }
77 void setNext(InstList::iterator N) { Next = N; }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070078 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070079 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
Jim Stichnoth318f4cd2015-10-01 21:02:37 -070080 void availabilityReset();
81 void availabilityUpdate();
82 Variable *availabilityGet(Operand *Src) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070083
84private:
Andrew Scull9612d322015-07-06 14:53:25 -070085 /// Node is the argument to Inst::updateVars().
Jim Stichnotheafb56c2015-06-22 10:35:22 -070086 CfgNode *Node = nullptr;
87 Inst *LastInserted = nullptr;
Andrew Scull57e12682015-09-16 11:30:19 -070088 /// Cur points to the current instruction being considered. It is guaranteed
89 /// to point to a non-deleted instruction, or to be End.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070090 InstList::iterator Cur;
Andrew Scull57e12682015-09-16 11:30:19 -070091 /// Next doubles as a pointer to the next valid instruction (if any), and the
92 /// new-instruction insertion point. It is also updated for the caller in case
93 /// the lowering consumes more than one high-level instruction. It is
94 /// guaranteed to point to a non-deleted instruction after Cur, or to be End.
95 // TODO: Consider separating the notion of "next valid instruction" and "new
96 // instruction insertion point", to avoid confusion when previously-deleted
97 // instructions come between the two points.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070098 InstList::iterator Next;
Andrew Scull9612d322015-07-06 14:53:25 -070099 /// Begin is a copy of Insts.begin(), used if iterators are moved backward.
Jan Vounge6e497d2014-07-30 10:06:03 -0700100 InstList::iterator Begin;
Andrew Scull9612d322015-07-06 14:53:25 -0700101 /// End is a copy of Insts.end(), used if Next needs to be advanced.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700102 InstList::iterator End;
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700103 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple
104 /// assignment inserted (provided Src is a variable). This is used for simple
105 /// availability analysis.
106 Variable *LastDest = nullptr;
107 Variable *LastSrc = nullptr;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700108
Jan Voungc820ddf2014-07-29 14:38:51 -0700109 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -0700110 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700111};
112
Jan Voung28068ad2015-07-31 12:58:46 -0700113/// A helper class to advance the LoweringContext at each loop iteration.
114class PostIncrLoweringContext {
115 PostIncrLoweringContext() = delete;
116 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete;
117 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete;
118
119public:
120 explicit PostIncrLoweringContext(LoweringContext &Context)
121 : Context(Context) {}
122 ~PostIncrLoweringContext() {
123 Context.advanceCur();
124 Context.advanceNext();
125 }
126
127private:
128 LoweringContext &Context;
129};
130
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700131class TargetLowering {
Jim Stichnothc6ead202015-02-24 09:30:30 -0800132 TargetLowering() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700133 TargetLowering(const TargetLowering &) = delete;
134 TargetLowering &operator=(const TargetLowering &) = delete;
135
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700136public:
Jan Voungb36ad9b2015-04-21 17:01:49 -0700137 // TODO(jvoung): return a unique_ptr like the other factory functions.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700138 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jim Stichnoth94844f12015-11-04 16:06:16 -0800139 static void staticInit(TargetArch Target);
140 // Each target must define a public static method:
141 // static void staticInit();
Jan Voungec270732015-01-12 17:00:22 -0800142 static std::unique_ptr<Assembler> createAssembler(TargetArch Target,
143 Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700144 void translate() {
Jan Voung1f47ad02015-03-20 15:01:26 -0700145 switch (Ctx->getFlags().getOptLevel()) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700146 case Opt_m1:
147 translateOm1();
148 break;
149 case Opt_0:
150 translateO0();
151 break;
152 case Opt_1:
153 translateO1();
154 break;
155 case Opt_2:
156 translateO2();
157 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700158 }
159 }
160 virtual void translateOm1() {
161 Func->setError("Target doesn't specify Om1 lowering steps.");
162 }
163 virtual void translateO0() {
164 Func->setError("Target doesn't specify O0 lowering steps.");
165 }
166 virtual void translateO1() {
167 Func->setError("Target doesn't specify O1 lowering steps.");
168 }
169 virtual void translateO2() {
170 Func->setError("Target doesn't specify O2 lowering steps.");
171 }
172
Andrew Scull9612d322015-07-06 14:53:25 -0700173 /// Tries to do address mode optimization on a single instruction.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700174 void doAddressOpt();
Andrew Scull9612d322015-07-06 14:53:25 -0700175 /// Randomly insert NOPs.
Qining Luaee5fa82015-08-20 14:59:03 -0700176 void doNopInsertion(RandomNumberGenerator &RNG);
Andrew Scull9612d322015-07-06 14:53:25 -0700177 /// Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700178 void lower();
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700179 /// Inserts and lowers a single high-level instruction at a specific insertion
180 /// point.
181 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr);
Andrew Scull57e12682015-09-16 11:30:19 -0700182 /// Does preliminary lowering of the set of Phi instructions in the current
183 /// node. The main intention is to do what's needed to keep the unlowered Phi
184 /// instructions consistent with the lowered non-Phi instructions, e.g. to
185 /// lower 64-bit operands on a 32-bit target.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700186 virtual void prelowerPhis() {}
Andrew Scull57e12682015-09-16 11:30:19 -0700187 /// Tries to do branch optimization on a single instruction. Returns true if
188 /// some optimization was done.
Jim Stichnothff9c7062014-09-18 04:50:49 -0700189 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
190 return false;
191 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700192
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700193 virtual SizeT getNumRegisters() const = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700194 /// Returns a variable pre-colored to the specified physical register. This is
195 /// generally used to get very direct access to the register such as in the
196 /// prolog or epilog or for marking scratch registers as killed by a call. If
197 /// a Type is not provided, a target-specific default type is used.
Jim Stichnoth98712a32014-10-24 10:59:02 -0700198 virtual Variable *getPhysicalRegister(SizeT RegNum,
199 Type Ty = IceType_void) = 0;
Andrew Scull9612d322015-07-06 14:53:25 -0700200 /// Returns a printable name for the register.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700201 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
202
203 virtual bool hasFramePointer() const { return false; }
David Sehre39d0ca2015-11-06 11:25:41 -0800204 virtual void setHasFramePointer() = 0;
Jim Stichnothe7418712015-10-09 06:54:02 -0700205 virtual SizeT getStackReg() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800206 virtual SizeT getFrameReg() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700207 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700208 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
David Sehre39d0ca2015-11-06 11:25:41 -0800209 virtual uint32_t getStackAlignment() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800210 virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0;
211 virtual int32_t getFrameFixedAllocaOffset() const = 0;
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700212
Andrew Scull6d47bcd2015-09-17 17:10:05 -0700213 /// Return whether a 64-bit Variable should be split into a Variable64On32.
214 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0;
215
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700216 bool hasComputedFrame() const { return HasComputedFrame; }
Andrew Scull57e12682015-09-16 11:30:19 -0700217 /// Returns true if this function calls a function that has the "returns
218 /// twice" attribute.
Jan Voung44d53e12014-09-11 19:18:03 -0700219 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800220 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700221 int32_t getStackAdjustment() const { return StackAdjustment; }
222 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
223 void resetStackAdjustment() { StackAdjustment = 0; }
Jan Voungb36ad9b2015-04-21 17:01:49 -0700224 SizeT makeNextLabelNumber() { return NextLabelNumber++; }
Andrew Scull86df4e92015-07-30 13:54:44 -0700225 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700226 LoweringContext &getContext() { return Context; }
227
228 enum RegSet {
229 RegSet_None = 0,
230 RegSet_CallerSave = 1 << 0,
231 RegSet_CalleeSave = 1 << 1,
232 RegSet_StackPointer = 1 << 2,
233 RegSet_FramePointer = 1 << 3,
234 RegSet_All = ~RegSet_None
235 };
Andrew Scull8072bae2015-09-14 16:01:26 -0700236 using RegSetMask = uint32_t;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700237
238 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
239 RegSetMask Exclude) const = 0;
Jim Stichnothc59288b2015-11-09 11:38:40 -0800240 virtual const llvm::SmallBitVector &
241 getRegistersForVariable(const Variable *Var) const = 0;
John Portobb0a5fe2015-09-04 11:23:41 -0700242 virtual const llvm::SmallBitVector &getAliasesForRegister(SizeT) const = 0;
243
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800244 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700245
Qining Luaee5fa82015-08-20 14:59:03 -0700246 virtual void
247 makeRandomRegisterPermutation(llvm::SmallVectorImpl<int32_t> &Permutation,
248 const llvm::SmallBitVector &ExcludeRegisters,
249 uint64_t Salt) const = 0;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800250
Andrew Scull57e12682015-09-16 11:30:19 -0700251 /// Save/restore any mutable state for the situation where code emission needs
252 /// multiple passes, such as sandboxing or relaxation. Subclasses may provide
253 /// their own implementation, but should be sure to also call the parent
254 /// class's methods.
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800255 virtual void snapshotEmitState() {
256 SnapshotStackAdjustment = StackAdjustment;
257 }
258 virtual void rollbackEmitState() {
259 StackAdjustment = SnapshotStackAdjustment;
260 }
261
Andrew Scull87f80c12015-07-20 10:19:16 -0700262 /// Get the minimum number of clusters required for a jump table to be
263 /// considered.
264 virtual SizeT getMinJumpTableSize() const = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700265 virtual void emitJumpTable(const Cfg *Func,
266 const InstJumpTable *JumpTable) const = 0;
Andrew Scull87f80c12015-07-20 10:19:16 -0700267
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700268 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700269
Jan Voung76bb0be2015-05-14 09:26:19 -0700270 void emitWithoutPrefix(const ConstantRelocatable *CR) const;
271 void emit(const ConstantRelocatable *CR) const;
272 virtual const char *getConstantPrefix() const = 0;
273
274 virtual void emit(const ConstantUndef *C) const = 0;
275 virtual void emit(const ConstantInteger32 *C) const = 0;
276 virtual void emit(const ConstantInteger64 *C) const = 0;
277 virtual void emit(const ConstantFloat *C) const = 0;
278 virtual void emit(const ConstantDouble *C) const = 0;
279
Andrew Scull9612d322015-07-06 14:53:25 -0700280 /// Performs target-specific argument lowering.
Matt Wala45a06232014-07-09 16:33:22 -0700281 virtual void lowerArguments() = 0;
282
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700283 virtual void initNodeForLowering(CfgNode *) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700284 virtual void addProlog(CfgNode *Node) = 0;
285 virtual void addEpilog(CfgNode *Node) = 0;
286
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700287 virtual ~TargetLowering() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700288
289protected:
Jim Stichnothc6ead202015-02-24 09:30:30 -0800290 explicit TargetLowering(Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700291 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
292 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
293 virtual void lowerAssign(const InstAssign *Inst) = 0;
294 virtual void lowerBr(const InstBr *Inst) = 0;
295 virtual void lowerCall(const InstCall *Inst) = 0;
296 virtual void lowerCast(const InstCast *Inst) = 0;
297 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700298 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700299 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700300 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700301 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700302 virtual void lowerLoad(const InstLoad *Inst) = 0;
303 virtual void lowerPhi(const InstPhi *Inst) = 0;
304 virtual void lowerRet(const InstRet *Inst) = 0;
305 virtual void lowerSelect(const InstSelect *Inst) = 0;
306 virtual void lowerStore(const InstStore *Inst) = 0;
307 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
308 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
Jim Stichnothe4f65d82015-06-17 22:16:02 -0700309 virtual void lowerOther(const Inst *Instr);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700310
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700311 virtual void doAddressOptLoad() {}
312 virtual void doAddressOptStore() {}
Jim Stichnothad2989b2015-09-15 10:21:42 -0700313 virtual void doMockBoundsCheck(Operand *) {}
Qining Luaee5fa82015-08-20 14:59:03 -0700314 virtual void randomlyInsertNop(float Probability,
315 RandomNumberGenerator &RNG) = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700316 /// This gives the target an opportunity to post-process the lowered expansion
317 /// before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700318 virtual void postLower() {}
319
Jim Stichnoth230d4102015-09-25 17:40:32 -0700320 /// Find (non-SSA) instructions where the Dest variable appears in some source
321 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis
322 /// consistent.
323 void markRedefinitions();
Jan Voungb3401d22015-05-18 09:38:21 -0700324
Andrew Scull57e12682015-09-16 11:30:19 -0700325 /// Make a pass over the Cfg to determine which variables need stack slots and
326 /// place them in a sorted list (SortedSpilledVariables). Among those, vars,
327 /// classify the spill variables as local to the basic block vs global
328 /// (multi-block) in order to compute the parameters GlobalsSize and
329 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of
330 /// locals is disallowed) along with alignments required for variables in each
331 /// area. We rely on accurate VMetadata in order to classify a variable as
332 /// global vs local (otherwise the variable is conservatively global). The
333 /// in-args should be initialized to 0.
Andrew Scull9612d322015-07-06 14:53:25 -0700334 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700335 /// This is only a pre-pass and the actual stack slot assignment is handled
336 /// separately.
Andrew Scull9612d322015-07-06 14:53:25 -0700337 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700338 /// There may be target-specific Variable types, which will be handled by
339 /// TargetVarHook. If the TargetVarHook returns true, then the variable is
340 /// skipped and not considered with the rest of the spilled variables.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700341 void getVarStackSlotParams(VarList &SortedSpilledVariables,
342 llvm::SmallBitVector &RegsUsed,
343 size_t *GlobalsSize, size_t *SpillAreaSizeBytes,
344 uint32_t *SpillAreaAlignmentBytes,
345 uint32_t *LocalsSlotsAlignmentBytes,
346 std::function<bool(Variable *)> TargetVarHook);
347
Andrew Scull57e12682015-09-16 11:30:19 -0700348 /// Calculate the amount of padding needed to align the local and global areas
349 /// to the required alignment. This assumes the globals/locals layout used by
350 /// getVarStackSlotParams and assignVarStackSlots.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700351 void alignStackSpillAreas(uint32_t SpillAreaStartOffset,
352 uint32_t SpillAreaAlignmentBytes,
353 size_t GlobalsSize,
354 uint32_t LocalsSlotsAlignmentBytes,
355 uint32_t *SpillAreaPaddingBytes,
356 uint32_t *LocalsSlotsPaddingBytes);
357
Andrew Scull57e12682015-09-16 11:30:19 -0700358 /// Make a pass through the SortedSpilledVariables and actually assign stack
359 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding.
360 /// The SpillArea starts after that amount of padding. This matches the scheme
361 /// in getVarStackSlotParams, where there may be a separate multi-block global
362 /// var spill area and a local var spill area.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700363 void assignVarStackSlots(VarList &SortedSpilledVariables,
364 size_t SpillAreaPaddingBytes,
365 size_t SpillAreaSizeBytes,
366 size_t GlobalsAndSubsequentPaddingSize,
367 bool UsesFramePointer);
368
Andrew Scull57e12682015-09-16 11:30:19 -0700369 /// Sort the variables in Source based on required alignment. The variables
370 /// with the largest alignment need are placed in the front of the Dest list.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700371 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
372
Andrew Scull9612d322015-07-06 14:53:25 -0700373 /// Make a call to an external helper function.
Jan Voungb36ad9b2015-04-21 17:01:49 -0700374 InstCall *makeHelperCall(const IceString &Name, Variable *Dest,
375 SizeT MaxSrcs);
376
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700377 void
378 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) {
379 Context.insert(InstBundleLock::create(Func, BundleOption));
380 }
381 void _bundle_unlock() { Context.insert(InstBundleUnlock::create(Func)); }
Jim Stichnoth230d4102015-09-25 17:40:32 -0700382 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700383
Andrew Scullcfa628b2015-08-20 14:23:05 -0700384 bool shouldOptimizeMemIntrins();
385
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700386 Cfg *Func;
387 GlobalContext *Ctx;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700388 bool HasComputedFrame = false;
389 bool CallsReturnsTwice = false;
Andrew Scull57e12682015-09-16 11:30:19 -0700390 /// StackAdjustment keeps track of the current stack offset from its natural
Jim Stichnoth55f931f2015-09-23 16:33:08 -0700391 /// location, e.g. as arguments are pushed for a function call or as
392 /// fixed-size alloca instructions are executed in the entry block.
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700393 int32_t StackAdjustment = 0;
394 SizeT NextLabelNumber = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700395 SizeT NextJumpTableNumber = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700396 LoweringContext Context;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800397
Jim Stichnothc4508792015-03-01 23:12:55 -0800398 // Runtime helper function names
399 const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16";
400 const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8";
401 const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1";
402 const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1";
403 const static constexpr char *H_call_ctpop_i32 = "__popcountsi2";
404 const static constexpr char *H_call_ctpop_i64 = "__popcountdi2";
405 const static constexpr char *H_call_longjmp = "longjmp";
406 const static constexpr char *H_call_memcpy = "memcpy";
407 const static constexpr char *H_call_memmove = "memmove";
408 const static constexpr char *H_call_memset = "memset";
409 const static constexpr char *H_call_read_tp = "__nacl_read_tp";
410 const static constexpr char *H_call_setjmp = "setjmp";
411 const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64";
412 const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64";
413 const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32";
414 const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32";
415 const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64";
416 const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32";
417 const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64";
418 const static constexpr char *H_frem_f32 = "fmodf";
419 const static constexpr char *H_frem_f64 = "fmod";
Jan Voung6ec369e2015-06-30 11:03:15 -0700420 const static constexpr char *H_sdiv_i32 = "__divsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800421 const static constexpr char *H_sdiv_i64 = "__divdi3";
422 const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32";
423 const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700424 const static constexpr char *H_srem_i32 = "__modsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800425 const static constexpr char *H_srem_i64 = "__moddi3";
Jan Voung6ec369e2015-06-30 11:03:15 -0700426 const static constexpr char *H_udiv_i32 = "__udivsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800427 const static constexpr char *H_udiv_i64 = "__udivdi3";
428 const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32";
429 const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32";
430 const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64";
431 const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32";
432 const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700433 const static constexpr char *H_urem_i32 = "__umodsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800434 const static constexpr char *H_urem_i64 = "__umoddi3";
435
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800436private:
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700437 int32_t SnapshotStackAdjustment = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700438};
439
Andrew Scull57e12682015-09-16 11:30:19 -0700440/// TargetDataLowering is used for "lowering" data including initializers for
441/// global variables, and the internal constant pools. It is separated out from
442/// TargetLowering because it does not require a Cfg.
Jan Voung72984d82015-01-29 14:42:38 -0800443class TargetDataLowering {
444 TargetDataLowering() = delete;
445 TargetDataLowering(const TargetDataLowering &) = delete;
446 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700447
Jim Stichnothde4ca712014-06-29 08:13:48 -0700448public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800449 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800450 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700451
John Porto8b1a7052015-06-17 13:20:08 -0700452 virtual void lowerGlobals(const VariableDeclarationList &Vars,
453 const IceString &SectionSuffix) = 0;
John Porto0f86d032015-06-15 07:44:27 -0700454 virtual void lowerConstants() = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700455 virtual void lowerJumpTables() = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700456
457protected:
John Porto8b1a7052015-06-17 13:20:08 -0700458 void emitGlobal(const VariableDeclaration &Var,
459 const IceString &SectionSuffix);
Jan Voung58eea4d2015-06-15 15:11:56 -0700460
Andrew Scull57e12682015-09-16 11:30:19 -0700461 /// For now, we assume .long is the right directive for emitting 4 byte emit
462 /// global relocations. However, LLVM MIPS usually uses .4byte instead.
Andrew Scull9612d322015-07-06 14:53:25 -0700463 /// Perhaps there is some difference when the location is unaligned.
John Porto8b1a7052015-06-17 13:20:08 -0700464 static const char *getEmit32Directive() { return ".long"; }
Jan Voung58eea4d2015-06-15 15:11:56 -0700465
Jim Stichnothc6ead202015-02-24 09:30:30 -0800466 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700467 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700468};
469
Andrew Scull57e12682015-09-16 11:30:19 -0700470/// TargetHeaderLowering is used to "lower" the header of an output file. It
471/// writes out the target-specific header attributes. E.g., for ARM this writes
472/// out the build attributes (float ABI, etc.).
Jan Voungfb792842015-06-11 15:27:50 -0700473class TargetHeaderLowering {
474 TargetHeaderLowering() = delete;
475 TargetHeaderLowering(const TargetHeaderLowering &) = delete;
476 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete;
477
478public:
479 static std::unique_ptr<TargetHeaderLowering>
480 createLowering(GlobalContext *Ctx);
481 virtual ~TargetHeaderLowering();
482
483 virtual void lower() {}
484
485protected:
486 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
487 GlobalContext *Ctx;
488};
489
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700490} // end of namespace Ice
491
492#endif // SUBZERO_SRC_ICETARGETLOWERING_H