summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTim Northover <tnorthover@apple.com>2016-10-31 18:30:59 +0000
committerTim Northover <tnorthover@apple.com>2016-10-31 18:30:59 +0000
commitfa8311d1c91c38c75bc48da1ef8cba179642db21 (patch)
treeb314402cd3fe097e6895ad866b0b0ea3187d8c3b /lib
parent9b108814a3d26246c3ab6b1d8687195d98ee893f (diff)
GlobalISel: translate stack protector intrinsics
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285614 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/CodeGen/GlobalISel/IRTranslator.cpp74
-rw-r--r--lib/CodeGen/GlobalISel/LegalizerInfo.cpp2
-rw-r--r--lib/Target/AArch64/AArch64InstructionSelector.cpp15
3 files changed, 75 insertions, 16 deletions
diff --git a/lib/CodeGen/GlobalISel/IRTranslator.cpp b/lib/CodeGen/GlobalISel/IRTranslator.cpp
index 2a66251190c..dcf69f5e060 100644
--- a/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -74,6 +74,27 @@ unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
return ValReg;
}
+int IRTranslator::getOrCreateFrameIndex(const AllocaInst &AI) {
+ if (FrameIndices.find(&AI) != FrameIndices.end())
+ return FrameIndices[&AI];
+
+ MachineFunction &MF = MIRBuilder.getMF();
+ unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
+ unsigned Size =
+ ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
+
+ // Always allocate at least one byte.
+ Size = std::max(Size, 1u);
+
+ unsigned Alignment = AI.getAlignment();
+ if (!Alignment)
+ Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
+
+ int &FI = FrameIndices[&AI];
+ FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
+ return FI;
+}
+
unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
unsigned Alignment = 0;
Type *ValTy = nullptr;
@@ -382,6 +403,26 @@ bool IRTranslator::translateMemcpy(const CallInst &CI) {
CallLowering::ArgInfo(0, CI.getType()), Args);
}
+void IRTranslator::getStackGuard(unsigned DstReg) {
+ auto MIB = MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD);
+ MIB.addDef(DstReg);
+
+ auto &MF = MIRBuilder.getMF();
+ auto &TLI = *MF.getSubtarget().getTargetLowering();
+ Value *Global = TLI.getSDagStackGuard(*MF.getFunction()->getParent());
+ if (!Global)
+ return;
+
+ MachinePointerInfo MPInfo(Global);
+ MachineInstr::mmo_iterator MemRefs = MF.allocateMemRefsArray(1);
+ auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
+ MachineMemOperand::MODereferenceable;
+ *MemRefs =
+ MF.getMachineMemOperand(MPInfo, Flags, DL->getPointerSizeInBits() / 8,
+ DL->getPointerABIAlignment());
+ MIB.setMemRefs(MemRefs, MemRefs + 1);
+}
+
bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
Intrinsic::ID ID) {
unsigned Op = 0;
@@ -402,6 +443,24 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
MIRBuilder.buildConstant(getOrCreateVReg(CI), Min->isZero() ? -1ULL : 0);
return true;
}
+ case Intrinsic::stackguard:
+ getStackGuard(getOrCreateVReg(CI));
+ return true;
+ case Intrinsic::stackprotector: {
+ MachineFunction &MF = MIRBuilder.getMF();
+ LLT PtrTy{*CI.getArgOperand(0)->getType(), *DL};
+ unsigned GuardVal = MRI->createGenericVirtualRegister(PtrTy);
+ getStackGuard(GuardVal);
+
+ AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
+ MIRBuilder.buildStore(
+ GuardVal, getOrCreateVReg(*Slot),
+ *MF.getMachineMemOperand(
+ MachinePointerInfo::getFixedStack(MF, getOrCreateFrameIndex(*Slot)),
+ MachineMemOperand::MOStore | MachineMemOperand::MOVolatile,
+ PtrTy.getSizeInBits() / 8, 8));
+ return true;
+ }
}
LLT Ty{*CI.getOperand(0)->getType(), *DL};
@@ -468,20 +527,8 @@ bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
return false;
assert(AI.isStaticAlloca() && "only handle static allocas now");
- MachineFunction &MF = MIRBuilder.getMF();
- unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
- unsigned Size =
- ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
-
- // Always allocate at least one byte.
- Size = std::max(Size, 1u);
-
- unsigned Alignment = AI.getAlignment();
- if (!Alignment)
- Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
-
unsigned Res = getOrCreateVReg(AI);
- int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
+ int FI = getOrCreateFrameIndex(AI);
MIRBuilder.buildFrameIndex(Res, FI);
return true;
}
@@ -566,6 +613,7 @@ void IRTranslator::finalizeFunction() {
// Release the memory used by the different maps we
// needed during the translation.
ValToVReg.clear();
+ FrameIndices.clear();
Constants.clear();
}
diff --git a/lib/CodeGen/GlobalISel/LegalizerInfo.cpp b/lib/CodeGen/GlobalISel/LegalizerInfo.cpp
index da7428df543..ebbed12943b 100644
--- a/lib/CodeGen/GlobalISel/LegalizerInfo.cpp
+++ b/lib/CodeGen/GlobalISel/LegalizerInfo.cpp
@@ -37,6 +37,8 @@ LegalizerInfo::LegalizerInfo() : TablesInitialized(false) {
DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
+ DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar;
+ DefaultActions[TargetOpcode::G_STORE] = NarrowScalar;
DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
}
diff --git a/lib/Target/AArch64/AArch64InstructionSelector.cpp b/lib/Target/AArch64/AArch64InstructionSelector.cpp
index eb2614d482c..c5777598b65 100644
--- a/lib/Target/AArch64/AArch64InstructionSelector.cpp
+++ b/lib/Target/AArch64/AArch64InstructionSelector.cpp
@@ -478,15 +478,24 @@ bool AArch64InstructionSelector::select(MachineInstr &I) const {
MachineFunction &MF = *MBB.getParent();
MachineRegisterInfo &MRI = MF.getRegInfo();
- if (!isPreISelGenericOpcode(I.getOpcode()))
- return !I.isCopy() || selectCopy(I, TII, MRI, TRI, RBI);
+ unsigned Opcode = I.getOpcode();
+ if (!isPreISelGenericOpcode(I.getOpcode())) {
+ // Certain non-generic instructions also need some special handling.
+
+ if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
+ return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
+ else if (I.isCopy())
+ return selectCopy(I, TII, MRI, TRI, RBI);
+ else
+ return true;
+ }
+
if (I.getNumOperands() != I.getNumExplicitOperands()) {
DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
return false;
}
- unsigned Opcode = I.getOpcode();
LLT Ty =
I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};