blob: f45956c771e7078baef680f77f65ae2a1be0c056 [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
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Declares the TargetLowering, LoweringContext, and TargetDataLowering
12/// classes.
13///
14/// TargetLowering is an abstract class used to drive the translation/lowering
15/// process. LoweringContext maintains a context for lowering each instruction,
16/// offering conveniences such as iterating over non-deleted instructions.
17/// TargetDataLowering is an abstract class used to drive the lowering/emission
18/// of global initializers, external global declarations, and internal constant
19/// pools.
Andrew Scull9612d322015-07-06 14:53:25 -070020///
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070021//===----------------------------------------------------------------------===//
22
23#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
24#define SUBZERO_SRC_ICETARGETLOWERING_H
25
John Porto1d937a82015-12-17 06:19:34 -080026#include "IceCfgNode.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070027#include "IceDefs.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070028#include "IceInst.h" // for the names of the Inst subtypes
Jan Voung76bb0be2015-05-14 09:26:19 -070029#include "IceOperand.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070030#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070031
John Porto1d937a82015-12-17 06:19:34 -080032#include <utility>
33
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070034namespace Ice {
35
Karl Schimpfc5abdc12015-10-09 13:29:13 -070036// UnimplementedError is defined as a macro so that we can get actual line
37// numbers.
38#define UnimplementedError(Flags) \
39 do { \
40 if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \
41 /* Use llvm_unreachable instead of report_fatal_error, which gives \
42 better stack traces. */ \
43 llvm_unreachable("Not yet implemented"); \
44 abort(); \
45 } \
46 } while (0)
47
Andrew Scull57e12682015-09-16 11:30:19 -070048/// LoweringContext makes it easy to iterate through non-deleted instructions in
49/// a node, and insert new (lowered) instructions at the current point. Along
50/// with the instruction list container and associated iterators, it holds the
51/// current node, which is needed when inserting new instructions in order to
52/// track whether variables are used as single-block or multi-block.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070053class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070054 LoweringContext(const LoweringContext &) = delete;
55 LoweringContext &operator=(const LoweringContext &) = delete;
56
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070057public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070058 LoweringContext() = default;
59 ~LoweringContext() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070060 void init(CfgNode *Node);
61 Inst *getNextInst() const {
62 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080063 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080064 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070065 }
Jan Voungc820ddf2014-07-29 14:38:51 -070066 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070067 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070068 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080069 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080070 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070071 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070072 CfgNode *getNode() const { return Node; }
73 bool atEnd() const { return Cur == End; }
74 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080075 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070076 InstList::iterator getEnd() const { return End; }
77 void insert(Inst *Inst);
John Porto1d937a82015-12-17 06:19:34 -080078 template <typename Inst, typename... Args> Inst *insert(Args &&... A) {
79 auto *New = Inst::create(Node->getCfg(), std::forward<Args>(A)...);
80 insert(New);
81 return New;
82 }
Jan Vounge6e497d2014-07-30 10:06:03 -070083 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070084 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070085 void advanceNext() { advanceForward(Next); }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070086 void setCur(InstList::iterator C) { Cur = C; }
87 void setNext(InstList::iterator N) { Next = N; }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070088 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070089 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
Jim Stichnoth318f4cd2015-10-01 21:02:37 -070090 void availabilityReset();
91 void availabilityUpdate();
92 Variable *availabilityGet(Operand *Src) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070093
94private:
Andrew Scull9612d322015-07-06 14:53:25 -070095 /// Node is the argument to Inst::updateVars().
Jim Stichnotheafb56c2015-06-22 10:35:22 -070096 CfgNode *Node = nullptr;
97 Inst *LastInserted = nullptr;
Andrew Scull57e12682015-09-16 11:30:19 -070098 /// Cur points to the current instruction being considered. It is guaranteed
99 /// to point to a non-deleted instruction, or to be End.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700100 InstList::iterator Cur;
Andrew Scull57e12682015-09-16 11:30:19 -0700101 /// Next doubles as a pointer to the next valid instruction (if any), and the
102 /// new-instruction insertion point. It is also updated for the caller in case
103 /// the lowering consumes more than one high-level instruction. It is
104 /// guaranteed to point to a non-deleted instruction after Cur, or to be End.
105 // TODO: Consider separating the notion of "next valid instruction" and "new
106 // instruction insertion point", to avoid confusion when previously-deleted
107 // instructions come between the two points.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700108 InstList::iterator Next;
Andrew Scull9612d322015-07-06 14:53:25 -0700109 /// Begin is a copy of Insts.begin(), used if iterators are moved backward.
Jan Vounge6e497d2014-07-30 10:06:03 -0700110 InstList::iterator Begin;
Andrew Scull9612d322015-07-06 14:53:25 -0700111 /// End is a copy of Insts.end(), used if Next needs to be advanced.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700112 InstList::iterator End;
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700113 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple
114 /// assignment inserted (provided Src is a variable). This is used for simple
115 /// availability analysis.
116 Variable *LastDest = nullptr;
117 Variable *LastSrc = nullptr;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700118
Jan Voungc820ddf2014-07-29 14:38:51 -0700119 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -0700120 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700121};
122
Jan Voung28068ad2015-07-31 12:58:46 -0700123/// A helper class to advance the LoweringContext at each loop iteration.
124class PostIncrLoweringContext {
125 PostIncrLoweringContext() = delete;
126 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete;
127 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete;
128
129public:
130 explicit PostIncrLoweringContext(LoweringContext &Context)
131 : Context(Context) {}
132 ~PostIncrLoweringContext() {
133 Context.advanceCur();
134 Context.advanceNext();
135 }
136
137private:
138 LoweringContext &Context;
139};
140
John Porto53611e22015-12-30 07:30:10 -0800141/// TargetLowering is the base class for all backends in Subzero. In addition to
142/// implementing the abstract methods in this class, each concrete target must
143/// also implement a named constructor in its own namespace. For instance, for
144/// X8632 we have:
145///
146/// namespace X8632 {
147/// void createTargetLowering(Cfg *Func);
148/// }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700149class TargetLowering {
Jim Stichnothc6ead202015-02-24 09:30:30 -0800150 TargetLowering() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700151 TargetLowering(const TargetLowering &) = delete;
152 TargetLowering &operator=(const TargetLowering &) = delete;
153
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700154public:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800155 static void staticInit(GlobalContext *Ctx);
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800156 // Each target must define a public static method:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800157 // static void staticInit(GlobalContext *Ctx);
John Porto53611e22015-12-30 07:30:10 -0800158
159 static std::unique_ptr<TargetLowering> createLowering(TargetArch Target,
160 Cfg *Func);
161
162 virtual std::unique_ptr<Assembler> createAssembler() const = 0;
163
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700164 void translate() {
Jan Voung1f47ad02015-03-20 15:01:26 -0700165 switch (Ctx->getFlags().getOptLevel()) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700166 case Opt_m1:
167 translateOm1();
168 break;
169 case Opt_0:
170 translateO0();
171 break;
172 case Opt_1:
173 translateO1();
174 break;
175 case Opt_2:
176 translateO2();
177 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700178 }
179 }
180 virtual void translateOm1() {
181 Func->setError("Target doesn't specify Om1 lowering steps.");
182 }
183 virtual void translateO0() {
184 Func->setError("Target doesn't specify O0 lowering steps.");
185 }
186 virtual void translateO1() {
187 Func->setError("Target doesn't specify O1 lowering steps.");
188 }
189 virtual void translateO2() {
190 Func->setError("Target doesn't specify O2 lowering steps.");
191 }
192
John Porto5e0a8a72015-11-20 13:50:36 -0800193 /// Generates calls to intrinsics for operations the Target can't handle.
194 void genTargetHelperCalls();
Andrew Scull9612d322015-07-06 14:53:25 -0700195 /// Tries to do address mode optimization on a single instruction.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700196 void doAddressOpt();
Andrew Scull9612d322015-07-06 14:53:25 -0700197 /// Randomly insert NOPs.
Qining Luaee5fa82015-08-20 14:59:03 -0700198 void doNopInsertion(RandomNumberGenerator &RNG);
Andrew Scull9612d322015-07-06 14:53:25 -0700199 /// Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700200 void lower();
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700201 /// Inserts and lowers a single high-level instruction at a specific insertion
202 /// point.
203 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr);
Andrew Scull57e12682015-09-16 11:30:19 -0700204 /// Does preliminary lowering of the set of Phi instructions in the current
205 /// node. The main intention is to do what's needed to keep the unlowered Phi
206 /// instructions consistent with the lowered non-Phi instructions, e.g. to
207 /// lower 64-bit operands on a 32-bit target.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700208 virtual void prelowerPhis() {}
Andrew Scull57e12682015-09-16 11:30:19 -0700209 /// Tries to do branch optimization on a single instruction. Returns true if
210 /// some optimization was done.
Jim Stichnothff9c7062014-09-18 04:50:49 -0700211 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
212 return false;
213 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700214
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700215 virtual SizeT getNumRegisters() const = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700216 /// Returns a variable pre-colored to the specified physical register. This is
217 /// generally used to get very direct access to the register such as in the
218 /// prolog or epilog or for marking scratch registers as killed by a call. If
219 /// a Type is not provided, a target-specific default type is used.
Jim Stichnoth98712a32014-10-24 10:59:02 -0700220 virtual Variable *getPhysicalRegister(SizeT RegNum,
221 Type Ty = IceType_void) = 0;
Andrew Scull9612d322015-07-06 14:53:25 -0700222 /// Returns a printable name for the register.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700223 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
224
225 virtual bool hasFramePointer() const { return false; }
David Sehre39d0ca2015-11-06 11:25:41 -0800226 virtual void setHasFramePointer() = 0;
Jim Stichnothe7418712015-10-09 06:54:02 -0700227 virtual SizeT getStackReg() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800228 virtual SizeT getFrameReg() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700229 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700230 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
David Sehre39d0ca2015-11-06 11:25:41 -0800231 virtual uint32_t getStackAlignment() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800232 virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0;
233 virtual int32_t getFrameFixedAllocaOffset() const = 0;
John Porto614140e2015-11-23 11:43:13 -0800234 virtual uint32_t maxOutArgsSizeBytes() const { return 0; }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700235
Andrew Scull6d47bcd2015-09-17 17:10:05 -0700236 /// Return whether a 64-bit Variable should be split into a Variable64On32.
237 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0;
238
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700239 bool hasComputedFrame() const { return HasComputedFrame; }
Andrew Scull57e12682015-09-16 11:30:19 -0700240 /// Returns true if this function calls a function that has the "returns
241 /// twice" attribute.
Jan Voung44d53e12014-09-11 19:18:03 -0700242 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800243 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jan Voungb36ad9b2015-04-21 17:01:49 -0700244 SizeT makeNextLabelNumber() { return NextLabelNumber++; }
Andrew Scull86df4e92015-07-30 13:54:44 -0700245 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700246 LoweringContext &getContext() { return Context; }
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800247 Cfg *getFunc() const { return Func; }
248 GlobalContext *getGlobalContext() const { return Ctx; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700249
250 enum RegSet {
251 RegSet_None = 0,
252 RegSet_CallerSave = 1 << 0,
253 RegSet_CalleeSave = 1 << 1,
254 RegSet_StackPointer = 1 << 2,
255 RegSet_FramePointer = 1 << 3,
256 RegSet_All = ~RegSet_None
257 };
Andrew Scull8072bae2015-09-14 16:01:26 -0700258 using RegSetMask = uint32_t;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700259
260 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
261 RegSetMask Exclude) const = 0;
Jim Stichnothc59288b2015-11-09 11:38:40 -0800262 virtual const llvm::SmallBitVector &
263 getRegistersForVariable(const Variable *Var) const = 0;
John Portobb0a5fe2015-09-04 11:23:41 -0700264 virtual const llvm::SmallBitVector &getAliasesForRegister(SizeT) const = 0;
265
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800266 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700267
Qining Luaee5fa82015-08-20 14:59:03 -0700268 virtual void
269 makeRandomRegisterPermutation(llvm::SmallVectorImpl<int32_t> &Permutation,
270 const llvm::SmallBitVector &ExcludeRegisters,
271 uint64_t Salt) const = 0;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800272
Andrew Scull87f80c12015-07-20 10:19:16 -0700273 /// Get the minimum number of clusters required for a jump table to be
274 /// considered.
275 virtual SizeT getMinJumpTableSize() const = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700276 virtual void emitJumpTable(const Cfg *Func,
277 const InstJumpTable *JumpTable) const = 0;
Andrew Scull87f80c12015-07-20 10:19:16 -0700278
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700279 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700280
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800281 void emitWithoutPrefix(const ConstantRelocatable *CR,
282 const char *Suffix = "") const;
Jan Voung76bb0be2015-05-14 09:26:19 -0700283
Jan Voung76bb0be2015-05-14 09:26:19 -0700284 virtual void emit(const ConstantInteger32 *C) const = 0;
285 virtual void emit(const ConstantInteger64 *C) const = 0;
286 virtual void emit(const ConstantFloat *C) const = 0;
287 virtual void emit(const ConstantDouble *C) const = 0;
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800288 virtual void emit(const ConstantUndef *C) const = 0;
289 virtual void emit(const ConstantRelocatable *CR) const = 0;
Jan Voung76bb0be2015-05-14 09:26:19 -0700290
Andrew Scull9612d322015-07-06 14:53:25 -0700291 /// Performs target-specific argument lowering.
Matt Wala45a06232014-07-09 16:33:22 -0700292 virtual void lowerArguments() = 0;
293
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700294 virtual void initNodeForLowering(CfgNode *) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700295 virtual void addProlog(CfgNode *Node) = 0;
296 virtual void addEpilog(CfgNode *Node) = 0;
297
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700298 virtual ~TargetLowering() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700299
John Porto3bf335f2016-01-15 11:17:55 -0800300private:
301 // This control variable is used by AutoBundle (RAII-style bundle
302 // locking/unlocking) to prevent nested bundles.
303 bool AutoBundling = false;
304
305 // _bundle_lock(), and _bundle_unlock(), were made private to force subtargets
306 // to use the AutoBundle helper.
307 void
308 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) {
309 Context.insert<InstBundleLock>(BundleOption);
310 }
311 void _bundle_unlock() { Context.insert<InstBundleUnlock>(); }
312
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700313protected:
John Porto3bf335f2016-01-15 11:17:55 -0800314 /// AutoBundle provides RIAA-style bundling. Sub-targets are expected to use
315 /// it when emitting NaCl Bundles to ensure proper bundle_unlocking, and
316 /// prevent nested bundles.
317 ///
318 /// AutoBundle objects will emit a _bundle_lock during construction (but only
319 /// if sandboxed code generation was requested), and a bundle_unlock() during
320 /// destruction. By carefully scoping objects of this type, Subtargets can
321 /// ensure proper bundle emission.
322 class AutoBundle {
323 AutoBundle() = delete;
324 AutoBundle(const AutoBundle &) = delete;
325 AutoBundle &operator=(const AutoBundle &) = delete;
326
327 public:
328 explicit AutoBundle(TargetLowering *Target, InstBundleLock::Option Option =
329 InstBundleLock::Opt_None);
330 ~AutoBundle();
331
332 private:
333 TargetLowering *const Target;
334 const bool NeedSandboxing;
335 };
336
Jim Stichnothc6ead202015-02-24 09:30:30 -0800337 explicit TargetLowering(Cfg *Func);
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800338 // Applies command line filters to TypeToRegisterSet array.
339 static void
340 filterTypeToRegisterSet(GlobalContext *Ctx, int32_t NumRegs,
341 llvm::SmallBitVector TypeToRegisterSet[],
342 size_t TypeToRegisterSetSize,
343 std::function<IceString(int32_t)> getRegName);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700344 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
345 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
346 virtual void lowerAssign(const InstAssign *Inst) = 0;
347 virtual void lowerBr(const InstBr *Inst) = 0;
348 virtual void lowerCall(const InstCall *Inst) = 0;
349 virtual void lowerCast(const InstCast *Inst) = 0;
350 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700351 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700352 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700353 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700354 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700355 virtual void lowerLoad(const InstLoad *Inst) = 0;
356 virtual void lowerPhi(const InstPhi *Inst) = 0;
357 virtual void lowerRet(const InstRet *Inst) = 0;
358 virtual void lowerSelect(const InstSelect *Inst) = 0;
359 virtual void lowerStore(const InstStore *Inst) = 0;
360 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
361 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
Jim Stichnothe4f65d82015-06-17 22:16:02 -0700362 virtual void lowerOther(const Inst *Instr);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700363
John Porto5e0a8a72015-11-20 13:50:36 -0800364 virtual void genTargetHelperCallFor(Inst *Instr) = 0;
John Portof4198542015-11-20 14:17:23 -0800365 virtual uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) = 0;
John Porto5e0a8a72015-11-20 13:50:36 -0800366
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700367 virtual void doAddressOptLoad() {}
368 virtual void doAddressOptStore() {}
Jim Stichnothad2989b2015-09-15 10:21:42 -0700369 virtual void doMockBoundsCheck(Operand *) {}
Qining Luaee5fa82015-08-20 14:59:03 -0700370 virtual void randomlyInsertNop(float Probability,
371 RandomNumberGenerator &RNG) = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700372 /// This gives the target an opportunity to post-process the lowered expansion
373 /// before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700374 virtual void postLower() {}
375
Jim Stichnoth230d4102015-09-25 17:40:32 -0700376 /// Find (non-SSA) instructions where the Dest variable appears in some source
377 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis
378 /// consistent.
379 void markRedefinitions();
Jan Voungb3401d22015-05-18 09:38:21 -0700380
Andrew Scull57e12682015-09-16 11:30:19 -0700381 /// Make a pass over the Cfg to determine which variables need stack slots and
382 /// place them in a sorted list (SortedSpilledVariables). Among those, vars,
383 /// classify the spill variables as local to the basic block vs global
384 /// (multi-block) in order to compute the parameters GlobalsSize and
385 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of
386 /// locals is disallowed) along with alignments required for variables in each
387 /// area. We rely on accurate VMetadata in order to classify a variable as
388 /// global vs local (otherwise the variable is conservatively global). The
389 /// in-args should be initialized to 0.
Andrew Scull9612d322015-07-06 14:53:25 -0700390 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700391 /// This is only a pre-pass and the actual stack slot assignment is handled
392 /// separately.
Andrew Scull9612d322015-07-06 14:53:25 -0700393 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700394 /// There may be target-specific Variable types, which will be handled by
395 /// TargetVarHook. If the TargetVarHook returns true, then the variable is
396 /// skipped and not considered with the rest of the spilled variables.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700397 void getVarStackSlotParams(VarList &SortedSpilledVariables,
398 llvm::SmallBitVector &RegsUsed,
399 size_t *GlobalsSize, size_t *SpillAreaSizeBytes,
400 uint32_t *SpillAreaAlignmentBytes,
401 uint32_t *LocalsSlotsAlignmentBytes,
402 std::function<bool(Variable *)> TargetVarHook);
403
Andrew Scull57e12682015-09-16 11:30:19 -0700404 /// Calculate the amount of padding needed to align the local and global areas
405 /// to the required alignment. This assumes the globals/locals layout used by
406 /// getVarStackSlotParams and assignVarStackSlots.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700407 void alignStackSpillAreas(uint32_t SpillAreaStartOffset,
408 uint32_t SpillAreaAlignmentBytes,
409 size_t GlobalsSize,
410 uint32_t LocalsSlotsAlignmentBytes,
411 uint32_t *SpillAreaPaddingBytes,
412 uint32_t *LocalsSlotsPaddingBytes);
413
Andrew Scull57e12682015-09-16 11:30:19 -0700414 /// Make a pass through the SortedSpilledVariables and actually assign stack
415 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding.
416 /// The SpillArea starts after that amount of padding. This matches the scheme
417 /// in getVarStackSlotParams, where there may be a separate multi-block global
418 /// var spill area and a local var spill area.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700419 void assignVarStackSlots(VarList &SortedSpilledVariables,
420 size_t SpillAreaPaddingBytes,
421 size_t SpillAreaSizeBytes,
422 size_t GlobalsAndSubsequentPaddingSize,
423 bool UsesFramePointer);
424
Andrew Scull57e12682015-09-16 11:30:19 -0700425 /// Sort the variables in Source based on required alignment. The variables
426 /// with the largest alignment need are placed in the front of the Dest list.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700427 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
428
Andrew Scull9612d322015-07-06 14:53:25 -0700429 /// Make a call to an external helper function.
Jan Voungb36ad9b2015-04-21 17:01:49 -0700430 InstCall *makeHelperCall(const IceString &Name, Variable *Dest,
431 SizeT MaxSrcs);
432
Jim Stichnoth230d4102015-09-25 17:40:32 -0700433 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700434
Andrew Scullcfa628b2015-08-20 14:23:05 -0700435 bool shouldOptimizeMemIntrins();
436
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700437 Cfg *Func;
438 GlobalContext *Ctx;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700439 bool HasComputedFrame = false;
440 bool CallsReturnsTwice = false;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700441 SizeT NextLabelNumber = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700442 SizeT NextJumpTableNumber = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700443 LoweringContext Context;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800444
Jim Stichnothc4508792015-03-01 23:12:55 -0800445 // Runtime helper function names
446 const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16";
447 const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8";
448 const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1";
449 const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1";
450 const static constexpr char *H_call_ctpop_i32 = "__popcountsi2";
451 const static constexpr char *H_call_ctpop_i64 = "__popcountdi2";
452 const static constexpr char *H_call_longjmp = "longjmp";
453 const static constexpr char *H_call_memcpy = "memcpy";
454 const static constexpr char *H_call_memmove = "memmove";
455 const static constexpr char *H_call_memset = "memset";
456 const static constexpr char *H_call_read_tp = "__nacl_read_tp";
457 const static constexpr char *H_call_setjmp = "setjmp";
458 const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64";
459 const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64";
460 const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32";
461 const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32";
462 const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64";
463 const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32";
464 const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64";
465 const static constexpr char *H_frem_f32 = "fmodf";
466 const static constexpr char *H_frem_f64 = "fmod";
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800467 const static constexpr char *H_getIP_prefix = "__Sz_getIP_";
Jan Voung6ec369e2015-06-30 11:03:15 -0700468 const static constexpr char *H_sdiv_i32 = "__divsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800469 const static constexpr char *H_sdiv_i64 = "__divdi3";
470 const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32";
471 const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700472 const static constexpr char *H_srem_i32 = "__modsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800473 const static constexpr char *H_srem_i64 = "__moddi3";
Jan Voung6ec369e2015-06-30 11:03:15 -0700474 const static constexpr char *H_udiv_i32 = "__udivsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800475 const static constexpr char *H_udiv_i64 = "__udivdi3";
476 const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32";
477 const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32";
478 const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64";
479 const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32";
480 const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64";
Jan Voung6ec369e2015-06-30 11:03:15 -0700481 const static constexpr char *H_urem_i32 = "__umodsi3";
Jim Stichnothc4508792015-03-01 23:12:55 -0800482 const static constexpr char *H_urem_i64 = "__umoddi3";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700483};
484
Andrew Scull57e12682015-09-16 11:30:19 -0700485/// TargetDataLowering is used for "lowering" data including initializers for
486/// global variables, and the internal constant pools. It is separated out from
487/// TargetLowering because it does not require a Cfg.
Jan Voung72984d82015-01-29 14:42:38 -0800488class TargetDataLowering {
489 TargetDataLowering() = delete;
490 TargetDataLowering(const TargetDataLowering &) = delete;
491 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700492
Jim Stichnothde4ca712014-06-29 08:13:48 -0700493public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800494 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800495 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700496
John Porto8b1a7052015-06-17 13:20:08 -0700497 virtual void lowerGlobals(const VariableDeclarationList &Vars,
498 const IceString &SectionSuffix) = 0;
John Porto0f86d032015-06-15 07:44:27 -0700499 virtual void lowerConstants() = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700500 virtual void lowerJumpTables() = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700501
502protected:
John Porto8b1a7052015-06-17 13:20:08 -0700503 void emitGlobal(const VariableDeclaration &Var,
504 const IceString &SectionSuffix);
Jan Voung58eea4d2015-06-15 15:11:56 -0700505
Andrew Scull57e12682015-09-16 11:30:19 -0700506 /// For now, we assume .long is the right directive for emitting 4 byte emit
507 /// global relocations. However, LLVM MIPS usually uses .4byte instead.
Andrew Scull9612d322015-07-06 14:53:25 -0700508 /// Perhaps there is some difference when the location is unaligned.
John Porto8b1a7052015-06-17 13:20:08 -0700509 static const char *getEmit32Directive() { return ".long"; }
Jan Voung58eea4d2015-06-15 15:11:56 -0700510
Jim Stichnothc6ead202015-02-24 09:30:30 -0800511 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700512 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700513};
514
Andrew Scull57e12682015-09-16 11:30:19 -0700515/// TargetHeaderLowering is used to "lower" the header of an output file. It
516/// writes out the target-specific header attributes. E.g., for ARM this writes
517/// out the build attributes (float ABI, etc.).
Jan Voungfb792842015-06-11 15:27:50 -0700518class TargetHeaderLowering {
519 TargetHeaderLowering() = delete;
520 TargetHeaderLowering(const TargetHeaderLowering &) = delete;
521 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete;
522
523public:
524 static std::unique_ptr<TargetHeaderLowering>
525 createLowering(GlobalContext *Ctx);
526 virtual ~TargetHeaderLowering();
527
528 virtual void lower() {}
529
530protected:
531 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
532 GlobalContext *Ctx;
533};
534
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700535} // end of namespace Ice
536
537#endif // SUBZERO_SRC_ICETARGETLOWERING_H