Commit 20070e84 by Karl Schimpf

Cache common constants before lowering.

This code moves some constants used by the target lowering, so that they are defined (and cached) during static initialization of the target lowering, rather than looking up everytime they are used. This CL does this for the constant zero, and predefined helper functions. BUG= https://bugs.chromium.org/p/nativeclient/issues/detail?id=4076 R=jpp@chromium.org, stichnot@chromium.org Review URL: https://codereview.chromium.org/1775253003 .
parent 70e3f1cd
......@@ -25,6 +25,7 @@
#include "IceOperand.h"
#include "IceTargetLowering.h"
#include "IceTimerTree.h"
#include "IceTypes.def"
#include "IceTypes.h"
#ifdef __clang__
......@@ -291,6 +292,18 @@ GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit, Ostream *OsError,
case FT_Iasm:
break;
}
// Cache up front common constants.
#define X(tag, sizeLog2, align, elts, elty, str, rcstr) \
ConstZeroForType[IceType_##tag] = getConstantZeroInternal(IceType_##tag);
ICETYPE_TABLE;
#undef X
ConstantTrue = getConstantInt1Internal(1);
// Define runtime helper functions.
#define X(Tag, Name) \
RuntimeHelperFunc[static_cast<size_t>(RuntimeHelper::H_##Tag)] = \
getConstantExternSym(Name);
RUNTIME_HELPER_FUNCTIONS_TABLE
#undef X
TargetLowering::staticInit(this);
}
......@@ -657,24 +670,24 @@ Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) {
return nullptr;
}
Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) {
Constant *GlobalContext::getConstantInt1Internal(int8_t ConstantInt1) {
ConstantInt1 &= INT8_C(1);
return getConstPool()->Integers1.getOrAdd(this, ConstantInt1);
}
Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) {
Constant *GlobalContext::getConstantInt8Internal(int8_t ConstantInt8) {
return getConstPool()->Integers8.getOrAdd(this, ConstantInt8);
}
Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) {
Constant *GlobalContext::getConstantInt16Internal(int16_t ConstantInt16) {
return getConstPool()->Integers16.getOrAdd(this, ConstantInt16);
}
Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) {
Constant *GlobalContext::getConstantInt32Internal(int32_t ConstantInt32) {
return getConstPool()->Integers32.getOrAdd(this, ConstantInt32);
}
Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) {
Constant *GlobalContext::getConstantInt64Internal(int64_t ConstantInt64) {
return getConstPool()->Integers64.getOrAdd(this, ConstantInt64);
}
......@@ -710,40 +723,33 @@ Constant *GlobalContext::getConstantUndef(Type Ty) {
return getConstPool()->Undefs.getOrAdd(this, Ty);
}
// All locking is done by the getConstant*() target function.
Constant *GlobalContext::getConstantZero(Type Ty) {
Constant *Zero = ConstZeroForType[Ty];
if (Zero == nullptr)
llvm::report_fatal_error("Unsupported constant type: " + typeIceString(Ty));
return Zero;
}
// All locking is done by the getConstant*() target function.
Constant *GlobalContext::getConstantZeroInternal(Type Ty) {
switch (Ty) {
case IceType_i1:
return getConstantInt1(0);
return getConstantInt1Internal(0);
case IceType_i8:
return getConstantInt8(0);
return getConstantInt8Internal(0);
case IceType_i16:
return getConstantInt16(0);
return getConstantInt16Internal(0);
case IceType_i32:
return getConstantInt32(0);
return getConstantInt32Internal(0);
case IceType_i64:
return getConstantInt64(0);
return getConstantInt64Internal(0);
case IceType_f32:
return getConstantFloat(0);
case IceType_f64:
return getConstantDouble(0);
case IceType_v4i1:
case IceType_v8i1:
case IceType_v16i1:
case IceType_v16i8:
case IceType_v8i16:
case IceType_v4i32:
case IceType_v4f32: {
IceString Str;
llvm::raw_string_ostream BaseOS(Str);
BaseOS << "Unsupported constant type: " << Ty;
llvm_unreachable(BaseOS.str().c_str());
} break;
case IceType_void:
case IceType_NUM:
break;
default:
return nullptr;
}
llvm_unreachable("Unknown type");
}
ConstantList GlobalContext::getConstantPool(Type Ty) {
......
......@@ -21,12 +21,14 @@
#include "IceIntrinsics.h"
#include "IceRNG.h"
#include "IceSwitchLowering.h"
#include "IceTargetLowering.def"
#include "IceThreading.h"
#include "IceTimerTree.h"
#include "IceTypes.h"
#include "IceUtils.h"
#include <array>
#include <cassert>
#include <functional>
#include <memory>
#include <mutex>
......@@ -42,6 +44,15 @@ class ConstantPool;
class EmitterWorkItem;
class FuncSigType;
// Runtime helper function IDs
enum class RuntimeHelper {
#define X(Tag, Name) H_##Tag,
RUNTIME_HELPER_FUNCTIONS_TABLE
#undef X
H_Num
};
/// LockedPtr is a way to provide automatically locked access to some object.
template <typename T> class LockedPtr {
LockedPtr() = delete;
......@@ -189,11 +200,50 @@ public:
// getConstant*() functions are not const because they might add something to
// the constant pool.
Constant *getConstantInt(Type Ty, int64_t Value);
Constant *getConstantInt1(int8_t ConstantInt1);
Constant *getConstantInt8(int8_t ConstantInt8);
Constant *getConstantInt16(int16_t ConstantInt16);
Constant *getConstantInt32(int32_t ConstantInt32);
Constant *getConstantInt64(int64_t ConstantInt64);
Constant *getConstantInt1(int8_t ConstantInt1) {
ConstantInt1 &= INT8_C(1);
switch (ConstantInt1) {
case 0:
return getConstantZero(IceType_i1);
case 1:
return ConstantTrue;
default:
assert(false && "getConstantInt1 not on true/false");
return getConstantInt1Internal(ConstantInt1);
}
}
Constant *getConstantInt8(int8_t ConstantInt8) {
switch (ConstantInt8) {
case 0:
return getConstantZero(IceType_i8);
default:
return getConstantInt8Internal(ConstantInt8);
}
}
Constant *getConstantInt16(int16_t ConstantInt16) {
switch (ConstantInt16) {
case 0:
return getConstantZero(IceType_i16);
default:
return getConstantInt16Internal(ConstantInt16);
}
}
Constant *getConstantInt32(int32_t ConstantInt32) {
switch (ConstantInt32) {
case 0:
return getConstantZero(IceType_i32);
default:
return getConstantInt32Internal(ConstantInt32);
}
}
Constant *getConstantInt64(int64_t ConstantInt64) {
switch (ConstantInt64) {
case 0:
return getConstantZero(IceType_i64);
default:
return getConstantInt64Internal(ConstantInt64);
}
}
Constant *getConstantFloat(float Value);
Constant *getConstantDouble(double Value);
/// Returns a symbolic constant.
......@@ -212,6 +262,12 @@ public:
/// Returns a copy of the list of external symbols.
ConstantList getConstantExternSyms();
/// @}
Constant *getRuntimeHelperFunc(RuntimeHelper FuncID) const {
assert(FuncID < RuntimeHelper::H_Num);
Constant *Result = RuntimeHelperFunc[static_cast<size_t>(FuncID)];
assert(Result != nullptr && "No such runtime helper function");
return Result;
}
/// Return a locked pointer to the registered jump tables.
JumpTableDataList getJumpTables();
......@@ -524,7 +580,18 @@ private:
/// Indicates if global variable declarations can be disposed of right after
/// lowering.
bool DisposeGlobalVariablesAfterLowering = true;
Constant *ConstZeroForType[IceType_NUM];
Constant *ConstantTrue;
// Holds the constants representing each runtime helper function.
Constant *RuntimeHelperFunc[static_cast<size_t>(RuntimeHelper::H_Num)];
Constant *getConstantZeroInternal(Type Ty);
Constant *getConstantIntInternal(Type Ty, int64_t Value);
Constant *getConstantInt1Internal(int8_t ConstantInt1);
Constant *getConstantInt8Internal(int8_t ConstantInt8);
Constant *getConstantInt16Internal(int16_t ConstantInt16);
Constant *getConstantInt32Internal(int32_t ConstantInt32);
Constant *getConstantInt64Internal(int64_t ConstantInt64);
LockedPtr<ArenaAllocator> getAllocator() {
return LockedPtr<ArenaAllocator>(&Allocator, &AllocLock);
}
......
......@@ -697,10 +697,10 @@ void TargetLowering::assignVarStackSlots(VarList &SortedSpilledVariables,
}
}
InstCall *TargetLowering::makeHelperCall(const IceString &Name, Variable *Dest,
InstCall *TargetLowering::makeHelperCall(RuntimeHelper FuncID, Variable *Dest,
SizeT MaxSrcs) {
constexpr bool HasTailCall = false;
Constant *CallTarget = Ctx->getConstantExternSym(Name);
Constant *CallTarget = Ctx->getRuntimeHelperFunc(FuncID);
InstCall *Call =
InstCall::create(Func, MaxSrcs, Dest, CallTarget, HasTailCall);
return Call;
......
//===- subzero/src/IceTargetLowering.def - Target X-macros ------*- C++ -*-===//
//
// The Subzero Code Generator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines certain platform independent patterns for lowering target
// instructions, in the form of x-macros.
//
//===----------------------------------------------------------------------===//
#ifndef SUBZERO_SRC_ICETARGETLOWERING_DEF
#define SUBZERO_SRC_ICETARGETLOWERING_DEF
// Pattern for runtime helper functions
#define RUNTIME_HELPER_FUNCTIONS_TABLE \
/* tag , name */ \
X(bitcast_16xi1_i16, "__Sz_bitcast_16xi1_i16") \
X(bitcast_8xi1_i8, "__Sz_bitcast_8xi1_i8") \
X(bitcast_i16_16xi1, "__Sz_bitcast_i16_16xi1") \
X(bitcast_i8_8xi1, "__Sz_bitcast_i8_8xi1") \
X(call_ctpop_i32, "__popcountsi2") \
X(call_ctpop_i64, "__popcountdi2") \
X(call_longjmp, "longjmp") \
X(call_memcpy, "memcpy") \
X(call_memmove, "memmove") \
X(call_memset, "memset") \
X(call_read_tp, "__nacl_read_tp") \
X(call_setjmp, "setjmp") \
X(fptosi_f32_i64, "__Sz_fptosi_f32_i64") \
X(fptosi_f64_i64, "__Sz_fptosi_f64_i64") \
X(fptoui_4xi32_f32, "__Sz_fptoui_4xi32_f32") \
X(fptoui_f32_i32, "__Sz_fptoui_f32_i32") \
X(fptoui_f32_i64, "__Sz_fptoui_f32_i64") \
X(fptoui_f64_i32, "__Sz_fptoui_f64_i32") \
X(fptoui_f64_i64, "__Sz_fptoui_f64_i64") \
X(frem_f32, "fmodf") \
X(frem_f64, "fmod") \
X(sdiv_i32, "__divsi3") \
X(sdiv_i64, "__divdi3") \
X(sitofp_i64_f32, "__Sz_sitofp_i64_f32") \
X(sitofp_i64_f64, "__Sz_sitofp_i64_f64") \
X(srem_i32, "__modsi3") \
X(srem_i64, "__moddi3") \
X(udiv_i32, "__udivsi3") \
X(udiv_i64, "__udivdi3") \
X(uitofp_4xi32_4xf32, "__Sz_uitofp_4xi32_4xf32") \
X(uitofp_i32_f32, "__Sz_uitofp_i32_f32") \
X(uitofp_i32_f64, "__Sz_uitofp_i32_f64") \
X(uitofp_i64_f32, "__Sz_uitofp_i64_f32") \
X(uitofp_i64_f64, "__Sz_uitofp_i64_f64") \
X(urem_i32, "__umodsi3") \
X(urem_i64, "__umoddi3")
//#define X(Tag, Name)
#endif // SUBZERO_SRC_ICETARGETLOWERING_DEF
......@@ -461,9 +461,7 @@ protected:
/// with the largest alignment need are placed in the front of the Dest list.
void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
/// Make a call to an external helper function.
InstCall *makeHelperCall(const IceString &Name, Variable *Dest,
SizeT MaxSrcs);
InstCall *makeHelperCall(RuntimeHelper FuncID, Variable *Dest, SizeT MaxSrcs);
void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
......@@ -569,44 +567,7 @@ protected:
LoweringContext Context;
const SandboxType SandboxingType = ST_None;
// Runtime helper function names
const static constexpr char *H_bitcast_16xi1_i16 = "__Sz_bitcast_16xi1_i16";
const static constexpr char *H_bitcast_8xi1_i8 = "__Sz_bitcast_8xi1_i8";
const static constexpr char *H_bitcast_i16_16xi1 = "__Sz_bitcast_i16_16xi1";
const static constexpr char *H_bitcast_i8_8xi1 = "__Sz_bitcast_i8_8xi1";
const static constexpr char *H_call_ctpop_i32 = "__popcountsi2";
const static constexpr char *H_call_ctpop_i64 = "__popcountdi2";
const static constexpr char *H_call_longjmp = "longjmp";
const static constexpr char *H_call_memcpy = "memcpy";
const static constexpr char *H_call_memmove = "memmove";
const static constexpr char *H_call_memset = "memset";
const static constexpr char *H_call_read_tp = "__nacl_read_tp";
const static constexpr char *H_call_setjmp = "setjmp";
const static constexpr char *H_fptosi_f32_i64 = "__Sz_fptosi_f32_i64";
const static constexpr char *H_fptosi_f64_i64 = "__Sz_fptosi_f64_i64";
const static constexpr char *H_fptoui_4xi32_f32 = "__Sz_fptoui_4xi32_f32";
const static constexpr char *H_fptoui_f32_i32 = "__Sz_fptoui_f32_i32";
const static constexpr char *H_fptoui_f32_i64 = "__Sz_fptoui_f32_i64";
const static constexpr char *H_fptoui_f64_i32 = "__Sz_fptoui_f64_i32";
const static constexpr char *H_fptoui_f64_i64 = "__Sz_fptoui_f64_i64";
const static constexpr char *H_frem_f32 = "fmodf";
const static constexpr char *H_frem_f64 = "fmod";
const static constexpr char *H_getIP_prefix = "__Sz_getIP_";
const static constexpr char *H_sdiv_i32 = "__divsi3";
const static constexpr char *H_sdiv_i64 = "__divdi3";
const static constexpr char *H_sitofp_i64_f32 = "__Sz_sitofp_i64_f32";
const static constexpr char *H_sitofp_i64_f64 = "__Sz_sitofp_i64_f64";
const static constexpr char *H_srem_i32 = "__modsi3";
const static constexpr char *H_srem_i64 = "__moddi3";
const static constexpr char *H_udiv_i32 = "__udivsi3";
const static constexpr char *H_udiv_i64 = "__udivdi3";
const static constexpr char *H_uitofp_4xi32_4xf32 = "__Sz_uitofp_4xi32_4xf32";
const static constexpr char *H_uitofp_i32_f32 = "__Sz_uitofp_i32_f32";
const static constexpr char *H_uitofp_i32_f64 = "__Sz_uitofp_i32_f64";
const static constexpr char *H_uitofp_i64_f32 = "__Sz_uitofp_i64_f32";
const static constexpr char *H_uitofp_i64_f64 = "__Sz_uitofp_i64_f64";
const static constexpr char *H_urem_i32 = "__umodsi3";
const static constexpr char *H_urem_i64 = "__umoddi3";
};
/// TargetDataLowering is used for "lowering" data including initializers for
......
......@@ -1010,7 +1010,7 @@ void TargetMIPS32::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
return;
}
case Intrinsics::Longjmp: {
InstCall *Call = makeHelperCall(H_call_longjmp, nullptr, 2);
InstCall *Call = makeHelperCall(RuntimeHelper::H_call_longjmp, nullptr, 2);
Call->addArg(Instr->getArg(0));
Call->addArg(Instr->getArg(1));
lowerCall(Call);
......@@ -1019,7 +1019,7 @@ void TargetMIPS32::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
case Intrinsics::Memcpy: {
// In the future, we could potentially emit an inline memcpy/memset, etc.
// for intrinsic calls w/ a known length.
InstCall *Call = makeHelperCall(H_call_memcpy, nullptr, 3);
InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memcpy, nullptr, 3);
Call->addArg(Instr->getArg(0));
Call->addArg(Instr->getArg(1));
Call->addArg(Instr->getArg(2));
......@@ -1027,7 +1027,7 @@ void TargetMIPS32::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
return;
}
case Intrinsics::Memmove: {
InstCall *Call = makeHelperCall(H_call_memmove, nullptr, 3);
InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memmove, nullptr, 3);
Call->addArg(Instr->getArg(0));
Call->addArg(Instr->getArg(1));
Call->addArg(Instr->getArg(2));
......@@ -1041,7 +1041,7 @@ void TargetMIPS32::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
assert(ValOp->getType() == IceType_i8);
Variable *ValExt = Func->makeVariable(stackSlotType());
lowerCast(InstCast::create(Func, InstCast::Zext, ValExt, ValOp));
InstCall *Call = makeHelperCall(H_call_memset, nullptr, 3);
InstCall *Call = makeHelperCall(RuntimeHelper::H_call_memset, nullptr, 3);
Call->addArg(Instr->getArg(0));
Call->addArg(ValExt);
Call->addArg(Instr->getArg(2));
......@@ -1052,13 +1052,15 @@ void TargetMIPS32::lowerIntrinsicCall(const InstIntrinsicCall *Instr) {
if (Ctx->getFlags().getUseSandboxing()) {
UnimplementedLoweringError(this, Instr);
} else {
InstCall *Call = makeHelperCall(H_call_read_tp, Instr->getDest(), 0);
InstCall *Call =
makeHelperCall(RuntimeHelper::H_call_read_tp, Instr->getDest(), 0);
lowerCall(Call);
}
return;
}
case Intrinsics::Setjmp: {
InstCall *Call = makeHelperCall(H_call_setjmp, Instr->getDest(), 1);
InstCall *Call =
makeHelperCall(RuntimeHelper::H_call_setjmp, Instr->getDest(), 1);
Call->addArg(Instr->getArg(0));
lowerCall(Call);
return;
......
......@@ -74,7 +74,7 @@ private:
Operand *createNaClReadTPSrcOperand() {
Variable *TDB = makeReg(IceType_i32);
InstCall *Call = makeHelperCall(H_call_read_tp, TDB, 0);
InstCall *Call = makeHelperCall(RuntimeHelper::H_call_read_tp, TDB, 0);
lowerCall(Call);
return TDB;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment